# -*- 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

[((30, 24, 30, 38), 'mockito.mock', 'mock', ({(30, 29, 30, 37): 'Marathon'}, {}), '(Marathon)', False, 'from mockito import mock, Mock, when\n'), ((31, 24, 31, 38), 'mockito.mock', 'mock', ({(31, 29, 31, 37): 'Settings'}, {}), '(Settings)', False, 'from mockito import mock, Mock, when\n'), ((32, 26, 32, 41), 'mockito.mock', 'mock', ({(32, 31, 32, 40): 'AppConfig'}, {}), '(AppConfig)', False, 'from mockito import mock, Mock, when\n'), ((34, 31, 34, 51), 'mockito.mock', 'mock', ({(34, 36, 34, 50): 'FrameworkUtils'}, {}), '(FrameworkUtils)', False, 'from mockito import mock, Mock, when\n'), ((45, 13, 45, 27), 'cli.roger_promote.RogerPromote', 'RogerPromote', ({}, {}), '()', False, 'from cli.roger_promote import RogerPromote\n'), ((51, 19, 51, 33), 'mockito.mock', 'mock', ({(51, 24, 51, 32): 'Settings'}, {}), '(Settings)', False, 'from mockito import mock, Mock, when\n'), ((57, 13, 57, 53), 'cli.roger_promote.RogerPromote', 'RogerPromote', (), '', False, 'from cli.roger_promote import RogerPromote\n'), ((67, 13, 67, 53), 'cli.roger_promote.RogerPromote', 'RogerPromote', (), '', False, 'from cli.roger_promote import RogerPromote\n'), ((79, 20, 79, 34), 'mockito.mock', 'mock', ({(79, 25, 79, 33): 'Marathon'}, {}), '(Marathon)', False, 'from mockito import mock, Mock, when\n'), ((95, 13, 95, 46), 'cli.roger_promote.RogerPromote', 'RogerPromote', (), '', False, 'from cli.roger_promote import RogerPromote\n'), ((102, 20, 102, 35), 'mockito.mock', 'mock', ({(102, 25, 102, 34): 'Framework'}, {}), '(Framework)', False, 'from mockito import mock, Mock, when\n'), ((103, 19, 103, 33), 'mockito.mock', 'mock', ({(103, 24, 103, 32): 'Settings'}, {}), '(Settings)', False, 'from mockito import mock, Mock, when\n'), ((104, 21, 104, 36), 'mockito.mock', 'mock', ({(104, 26, 104, 35): 'AppConfig'}, {}), '(AppConfig)', False, 'from mockito import mock, Mock, when\n'), ((113, 13, 113, 67), 'cli.roger_promote.RogerPromote', 'RogerPromote', (), '', False, 'from cli.roger_promote import RogerPromote\n'), ((118, 15, 118, 29), 'cli.roger_promote.RogerPromote', 'RogerPromote', ({}, {}), '()', False, 'from cli.roger_promote import RogerPromote\n'), ((53, 8, 53, 29), 'mockito.when', 'when', ({(53, 13, 53, 28): 'self.app_config'}, {}), '(self.app_config)', False, 'from mockito import mock, Mock, when\n'), ((63, 8, 63, 29), 'mockito.when', 'when', ({(63, 13, 63, 28): 'self.app_config'}, {}), '(self.app_config)', False, 'from mockito import mock, Mock, when\n'), ((81, 8, 81, 23), 'mockito.when', 'when', ({(81, 13, 81, 22): 'framework'}, {}), '(framework)', False, 'from mockito import mock, Mock, when\n'), ((82, 8, 82, 23), 'mockito.when', 'when', ({(82, 13, 82, 22): 'framework'}, {}), '(framework)', False, 'from mockito import mock, Mock, when\n'), ((86, 8, 86, 23), 'mockito.when', 'when', ({(86, 13, 86, 22): 'framework'}, {}), '(framework)', False, 'from mockito import mock, Mock, when\n'), ((108, 8, 108, 22), 'mockito.when', 'when', ({(108, 13, 108, 21): 'settings'}, {}), '(settings)', False, 'from mockito import mock, Mock, when\n'), ((109, 8, 109, 24), 'mockito.when', 'when', ({(109, 13, 109, 23): 'app_config'}, {}), '(app_config)', False, 'from mockito import mock, Mock, when\n')]