#!/usr/bin/env python
# -*- coding: UTF-8 -*-

# Isomer - The distributed application framework
# ==============================================
# Copyright (C) 2011-2018 Heiko 'riot' Weinen <riot@c-base.org> and others.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

__author__ = "Heiko 'riot' Weinen"
__license__ = "AGPLv3"

"""
Hackerfleet Operating System - Backend

Test Isomer Auth
==============



"""

import os

import pytest
from isomer.tool.etc import load_instance
from isomer.tool.tool import isotool

from .test_instance_mgmt import reset_base, run_cli


def test_instance_clear():
    """Creates a new default instances and clears it without archiving"""
    reset_base()

    _ = run_cli(isotool, ['instance', 'create'], full_log=True)
    # pytest.exit('LOL')

    assert os.path.exists('/tmp/isomer-test/etc/isomer/instances/default.conf')
    assert not os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green')

    result = run_cli(isotool, ['environment', 'clear', '--no-archive'])
    print(result.output)

    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green')
    assert os.path.exists('/tmp/isomer-test/var/cache/isomer/default/green')
    assert os.path.exists('/tmp/isomer-test/var/local/isomer/default/green')
    assert result.exit_code == 0


def test_install():
    """Creates a new default instances and clears it without archiving"""
    reset_base()
    import os
    import pwd

    def get_username():
        """Return current username"""
        return pwd.getpwuid(os.getuid())[0]

    _ = run_cli(isotool, ['instance', 'create'])
    _ = run_cli(isotool, ['instance', 'set', 'user', get_username()])
    _ = run_cli(isotool, ['environment', 'clear', '--no-archive'])

    assert os.path.exists('/tmp/isomer-test/etc/isomer/instances/default.conf')
    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green')

    repo_path = os.path.abspath(os.path.join(os.path.dirname(__file__), '../'))

    result = run_cli(
        isotool,
        ['environment', 'install', '--no-sudo', '--source', 'copy', '--url', repo_path],
        full_log=True
    )

    assert result.exit_code == 0

    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green')
    assert os.path.exists('/tmp/isomer-test/var/cache/isomer/default/green')
    assert os.path.exists('/tmp/isomer-test/var/local/isomer/default/green')
    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green/venv/bin/python3')
    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green/venv/bin/iso')
    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green/repository')
    assert os.path.exists('/tmp/isomer-test/var/lib/isomer/default/green/repository/frontend')

    instance_config = load_instance('default')
    environment = instance_config['environments']['green']

    assert environment['installed'] is True
    # TODO: IMPORTANT|Investigation pending for 1.0.1 ff.
    # Rest of the test deactivated due to strange problems on travis.ci.
    return True
    assert environment['provisioned'] is True
    assert environment['migrated'] is True
    assert environment['frontend'] is True
    assert environment['tested'] is True
    assert environment['database'] == 'default_green'

    if result.exit_code != 0:
        print(result.output)
