# Copyright 2019 Red Hat, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.

import os.path
import pytest

testinfra_hosts = ['backup01.region.provider.opendev.org',
                   'backup-test01.opendev.org',
                   'backup-test02.opendev.org']


def test_bup_installed(host):
    package = host.package("bup")
    assert package.is_installed

def test_server_users(host):
    hostname = host.backend.get_hostname()
    if hostname.startswith('backup-test'):
        pytest.skip()

    for username in 'bup-backup01', 'bup-backup-test02':
        homedir = os.path.join('/opt/backups/', username)
        bup_config = os.path.join(homedir, '.bup', 'config')
        authorized_keys = os.path.join(homedir, '.ssh', 'authorized_keys')

        user = host.user(username)
        assert user.exists
        assert user.home == homedir

        f = host.file(authorized_keys)
        assert f.exists
        assert f.contains("ssh-ed25519")

        f = host.file(bup_config)
        assert f.exists

def test_backup_host_config(host):
    hostname = host.backend.get_hostname()
    if hostname == 'backup01.region.provider.opendev.org':
        pytest.skip()

    f = host.file('/root/.ssh/id_backup_ed25519')
    assert f.exists

    f = host.file('/root/.ssh/config')
    assert f.exists
    assert f.contains('Host backup01.region.provider.opendev.org')

    f = host.file('/root/.bup/config')
    assert f.exists