# -*- encoding: utf-8 -*-

"""
Unit test for roger_promote.py

"""

import tests.helper

import unittest
import os
import os.path
import pytest
import requests

from mockito import mock, Mock, when


from cli.roger_promote import RogerPromote
from cli.appconfig import AppConfig
from cli.settings import Settings
from cli.framework import Framework
from cli.frameworkUtils import FrameworkUtils
from cli.marathon import Marathon
from cli.chronos import Chronos


class TestRogerPromote(unittest.TestCase):
    def setUp(self):
        self.marathon = mock(Marathon)
        self.settings = mock(Settings)
        self.app_config = mock(AppConfig)
        self.framework = self.marathon
        self.framework_utils = mock(FrameworkUtils)
        self.config_file = "test.yml"
        self.roger_env = {}
        os.environ['ROGER_CONFIG_DIR'] = '/vagrant/config'

    @property
    def config_dir(self):
        return os.environ['ROGER_CONFIG_DIR']

    def test_config_dir(self):

        rp = RogerPromote()
        assert rp.config_dir == '/vagrant/config'

    def test_roger_env(self):

        fake_config = tests.helper.fake_config()
        settings = mock(Settings)

        when(self.app_config).getRogerEnv(
            self.config_dir
        ).thenReturn(fake_config)

        rp = RogerPromote(app_config=self.app_config)
        assert rp.roger_env == fake_config

    def test_set_framework(self):
        app_data = {'test_app': {'name': 'test_app'}}

        when(self.app_config).getAppData(
            self.config_dir, self.config_file, 'test_app'
        ).thenReturn(app_data)

        rp = RogerPromote(app_config=self.app_config)

        rp._set_framework(self.config_file, 'test_app')

        assert rp._framework.getName() == 'Marathon'

    def test_image_name(self):
        os.environ['ROGER_USER'] = "first.last"
        os.environ['ROGER_USER_PASS_DEV'] = "password"
        os.environ['ROGER_USER_PASS_STAGE'] = "password"
        os.environ['ROGER_USER_PASS_PROD'] = "password"

        framework = mock(Marathon)

        when(framework).getName().thenReturn("Marathon")
        when(framework).get_app_id(
            "test_path/test_app.json",
            "Marathon"
        ).thenReturn("app_id")
        when(framework).get_image_name(
            'first.last',
            "password",
            "dev",
            "app_id",
            self.config_dir,
            self.config_file
        ).thenReturn("test_image")

        rp = RogerPromote(framework=framework)
        assert rp._image_name(
            'dev',
            self.config_file,
            "test_path/test_app.json") == 'test_image'

    def test_config_resolver(self):
        framework = mock(Framework)
        settings = mock(Settings)
        app_config = mock(AppConfig)
        config_dir = '/vagrant/config'
        fake_team_config = tests.helper.fake_team_config()

        when(settings).getConfigDir().thenReturn(config_dir)
        when(app_config).getConfig(
            config_dir, 'roger.json'
        ).thenReturn(fake_team_config)

        rp = RogerPromote(settings=settings, app_config=app_config)
        val = rp._config_resolver('template_path', 'test_app', 'roger.json')
        assert val == 'framework_template_path'

    def test_roger_push_script(self):
        path = RogerPromote()._roger_push_script()
        assert 'roger-mesos-tools/cli/roger_push.py' in path

[((551, 565), 'mockito.mock', 'mock', (['Marathon'], {}), '(Marathon)\n', (555, 565), False, 'from mockito import mock, Mock, when\n'), ((590, 604), 'mockito.mock', 'mock', (['Settings'], {}), '(Settings)\n', (594, 604), False, 'from mockito import mock, Mock, when\n'), ((631, 646), 'mockito.mock', 'mock', (['AppConfig'], {}), '(AppConfig)\n', (635, 646), False, 'from mockito import mock, Mock, when\n'), ((717, 737), 'mockito.mock', 'mock', (['FrameworkUtils'], {}), '(FrameworkUtils)\n', (721, 737), False, 'from mockito import mock, Mock, when\n'), ((996, 1010), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {}), '()\n', (1008, 1010), False, 'from cli.roger_promote import RogerPromote\n'), ((1161, 1175), 'mockito.mock', 'mock', (['Settings'], {}), '(Settings)\n', (1165, 1175), False, 'from mockito import mock, Mock, when\n'), ((1296, 1336), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {'app_config': 'self.app_config'}), '(app_config=self.app_config)\n', (1308, 1336), False, 'from cli.roger_promote import RogerPromote\n'), ((1615, 1655), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {'app_config': 'self.app_config'}), '(app_config=self.app_config)\n', (1627, 1655), False, 'from cli.roger_promote import RogerPromote\n'), ((2036, 2050), 'mockito.mock', 'mock', (['Marathon'], {}), '(Marathon)\n', (2040, 2050), False, 'from mockito import mock, Mock, when\n'), ((2476, 2509), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {'framework': 'framework'}), '(framework=framework)\n', (2488, 2509), False, 'from cli.roger_promote import RogerPromote\n'), ((2702, 2717), 'mockito.mock', 'mock', (['Framework'], {}), '(Framework)\n', (2706, 2717), False, 'from mockito import mock, Mock, when\n'), ((2737, 2751), 'mockito.mock', 'mock', (['Settings'], {}), '(Settings)\n', (2741, 2751), False, 'from mockito import mock, Mock, when\n'), ((2773, 2788), 'mockito.mock', 'mock', (['AppConfig'], {}), '(AppConfig)\n', (2777, 2788), False, 'from mockito import mock, Mock, when\n'), ((3075, 3129), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {'settings': 'settings', 'app_config': 'app_config'}), '(settings=settings, app_config=app_config)\n', (3087, 3129), False, 'from cli.roger_promote import RogerPromote\n'), ((3309, 3323), 'cli.roger_promote.RogerPromote', 'RogerPromote', ([], {}), '()\n', (3321, 3323), False, 'from cli.roger_promote import RogerPromote\n'), ((1185, 1206), 'mockito.when', 'when', (['self.app_config'], {}), '(self.app_config)\n', (1189, 1206), False, 'from mockito import mock, Mock, when\n'), ((1478, 1499), 'mockito.when', 'when', (['self.app_config'], {}), '(self.app_config)\n', (1482, 1499), False, 'from mockito import mock, Mock, when\n'), ((2060, 2075), 'mockito.when', 'when', (['framework'], {}), '(framework)\n', (2064, 2075), False, 'from mockito import mock, Mock, when\n'), ((2117, 2132), 'mockito.when', 'when', (['framework'], {}), '(framework)\n', (2121, 2132), False, 'from mockito import mock, Mock, when\n'), ((2246, 2261), 'mockito.when', 'when', (['framework'], {}), '(framework)\n', (2250, 2261), False, 'from mockito import mock, Mock, when\n'), ((2896, 2910), 'mockito.when', 'when', (['settings'], {}), '(settings)\n', (2900, 2910), False, 'from mockito import mock, Mock, when\n'), ((2957, 2973), 'mockito.when', 'when', (['app_config'], {}), '(app_config)\n', (2961, 2973), False, 'from mockito import mock, Mock, when\n')]