예제 #1
0
 def setUp(self):
     self.gitObj = GitUtils()
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
     self.work_dir = self.base_dir + "/tests/work_dir"
     self.branch = "master"
예제 #2
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument(
            '-e',
            '--env',
            metavar='env',
            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push',
            '-s',
            help="Don't push. Only generate components. Defaults to false.",
            action="store_true")
        parser.add_argument(
            '--secrets-file',
            '-S',
            help=
            "Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        with open(self.configs_dir +
                  '/roger_single_container_var_tests.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['container-vars']
        self.config = config
        self.roger_env = roger_env
        self.data = data
예제 #3
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('app_name', metavar='app_name',
                            help="application to build. Example: 'agora'.")
        parser.add_argument('env', metavar='env',
                            help="environment. Example: 'test'.")
        parser.add_argument('directory', metavar='directory',
                            help="working directory. Example: '/home/vagrant/work_dir'.")
        parser.add_argument('tag_name', metavar='tag_name',
                            help="tag for the built image. Example: 'roger-collectd:0.20'.")
        parser.add_argument('config_file', metavar='config_file',
                            help="configuration file to use. Example: 'content.json'.")
        parser.add_argument(
            '--push', '-p', help="Also push to registry. Defaults to false.", action="store_true")

        self.parser = parser
        self.args = self.parser
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
예제 #4
0
 def setUp(self):
     self.webhook = WebHook()
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
     self.components_dir = self.base_dir + '/tests/components/dev'
     with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
         roger_env = yaml.load(roger)
     self.roger_env = roger_env
예제 #5
0
 def setUp(self):
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
     self.components_dir = self.base_dir + '/tests/components/dev'
     self.configs_dir = self.base_dir + "/tests/configs"
     with open(self.configs_dir + '/app.json') as config:
         config = json.load(config)
     self.config = config
예제 #6
0
 def getStatsClient(self):
     settingObj = Settings()
     appObj = AppConfig()
     config_dir = settingObj.getConfigDir()
     roger_env = appObj.getRogerEnv(config_dir)
     statsd_url = ""
     statsd_port = ""
     if 'statsd_endpoint' in roger_env.keys():
         statsd_url = roger_env['statsd_endpoint']
     if 'statsd_port' in roger_env.keys():
         statsd_port = int(roger_env['statsd_port'])
     return statsd.StatsClient(statsd_url, statsd_port)
예제 #7
0
 def get_haproxy_config(self, environment):
     haproxy_config = ""
     settingObj = Settings()
     appObj = AppConfig()
     config_dir = settingObj.getConfigDir()
     roger_env = appObj.getRogerEnv(config_dir)
     host = roger_env['environments'][environment]['host']
     haproxy_config_path = roger_env['environments'][
         environment]['haproxy_config_path']
     url = "{}{}".format(host, haproxy_config_path)
     haproxy_config = requests.get(url, stream=True)
     return haproxy_config.text
예제 #8
0
 def get_proxy_config(self, environment):
     proxy_config = ""
     settingObj = Settings()
     appObj = AppConfig()
     config_dir = settingObj.getConfigDir()
     roger_env = appObj.getRogerEnv(config_dir)
     host = roger_env['environments'][environment]['host']
     proxy_config_path = roger_env['environments'][
         environment]['proxy_config_path']
     url = "{}{}".format(host, proxy_config_path)
     proxy_config = requests.get(url).json()
     return proxy_config
예제 #9
0
 def getStatsClient(self):
     settingObj = Settings()
     appObj = AppConfig()
     config_dir = settingObj.getConfigDir()
     roger_env = appObj.getRogerEnv(config_dir)
     statsd_url = ""
     statsd_port = ""
     if 'statsd_endpoint' in roger_env.keys():
         statsd_url = roger_env['statsd_endpoint']
     if 'statsd_port' in roger_env.keys():
         statsd_port = int(roger_env['statsd_port'])
     return statsd.StatsClient(statsd_url, statsd_port)
예제 #10
0
 def get_proxy_config(self, environment):
     proxy_config = ""
     settingObj = Settings()
     appObj = AppConfig()
     config_dir = settingObj.getConfigDir()
     roger_env = appObj.getRogerEnv(config_dir)
     host = roger_env['environments'][environment]['host']
     proxy_config_path = roger_env['environments'][environment][
         'proxy_config_path']
     url = "{}{}".format(host, proxy_config_path)
     proxy_config = requests.get(url).json()
     return proxy_config
예제 #11
0
 def __init__(self):
     self.disabled = True
     self.emoji = ':rocket:'
     self.defChannel = ''
     self.config_dir = ''
     self.username = '******'
     self.settingObj = Settings()
     self.appconfigObj = AppConfig()
     self.configLoadFlag = False
     self.config = ''
     self.config_channels = []
     self.config_envs = []
     self.config_commands = []
예제 #12
0
 def getRepoUrl(self, repo):
     if repo.startswith('*****@*****.**'):
         return repo
     else:
         settingObj = Settings()
         config_dir = settingObj.getConfigDir()
         roger_env = self.getRogerEnv(config_dir)
         if 'default_github_repo_prefix' in roger_env.keys():
             prefix = roger_env['default_github_repo_prefix']
         else:
             raise ValueError(
                 "Could not determine github repo.Please provide default \"github repo prefix\" or ensure repo startswith [email protected]")
         return str(prefix + '{}.git'.format(repo))
예제 #13
0
 def getRepoUrl(self, repo):
     if repo.startswith('*****@*****.**'):
         return repo
     else:
         settingObj = Settings()
         config_dir = settingObj.getConfigDir()
         roger_env = self.getRogerEnv(config_dir)
         if 'default_github_repo_prefix' in roger_env.keys():
             prefix = roger_env['default_github_repo_prefix']
         else:
             raise ValueError(
                 "Could not determine github repo.Please provide default \"github repo prefix\" or ensure repo startswith [email protected]"
             )
         return str(prefix + '{}.git'.format(repo))
예제 #14
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('app_name', metavar='app_name',
                            help="application for which code is to be pulled. Example: 'agora' or 'grafana'")
        parser.add_argument('directory', metavar='directory',
                            help="working directory. The repo has its own directory it this. Example: '/home/vagrant/work_dir'")
        parser.add_argument('-b', '--branch', metavar='branch',
                            help="git branch to pull code from. Example: 'production' or 'master'. Defaults to master.")
        parser.add_argument('config_file', metavar='config_file',
                            help="configuration file to use. Example: 'content.json' or 'kwe.json'")

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.work_dir = self.base_dir + "/tests/work_dir"
        self.components_dir = self.base_dir + '/tests/components/dev'
        self.args = parser

        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)

        data = config['apps']['grafana_test_app']
        self.roger_env = roger_env
        self.data = data
        self.config = config

        pass
예제 #15
0
 def setUp(self):
     self.gitObj = GitUtils()
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
     self.work_dir = self.base_dir + "/tests/work_dir"
     self.branch = "master"
예제 #16
0
    def _get_template_path(self,
                           container_name,
                           config_dir,
                           args,
                           app_name,
                           app_object=AppConfig(),
                           settings_object=Settings()):
        """
        Returns the template path for a given container_name

        Each framework requires an template_path for the app_id method

        :Params:
        :config_dir [str]: path to the config directory
        :args [argparse.NameSpace]: how to get acceses to the values passed
        :app_name [str]: name of app
        :app_object [cli.appconfig.AppConfig]: instance of AppConfig
        :settings_object [cli.settings.Settings]: instance of Settings

        """

        data = app_object.getConfig(config_dir, args.config)
        repo = self._config_resolver('repo', app_name, args.config)
        template_path = self._config_resolver('template_path', app_name,
                                              args.config)

        # this path is always relative to the root repo dir, so join
        if template_path and not os.path.isabs(template_path):
            app_path = os.path.join(self._temp_dir, repo, template_path)
        else:
            app_path = settings_object.getTemplatesDir()

        file_name = "{0}-{1}.json".format(data['name'], container_name)

        return os.path.join(app_path, file_name)
예제 #17
0
class TestBuild(unittest.TestCase):
    def setUp(self):
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        self.configs_dir = self.base_dir + "/tests/configs"
        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        self.config = config

    def test_docker_build(self):
        raised_exception = False
        try:
            appObj = mock(AppConfig)
            dockerUtilsObj = mock(DockerUtils)
            dockerObj = Docker()
            when(appObj).getRepoName(any()).thenReturn('roger')
            when(dockerUtilsObj).docker_build(any(), any()).thenReturn(True)
            directory = self.base_dir + '/tests/testrepo'
            config = self.config
            repo = config['repo']
            projects = 'none'
            path = ''
            image_tag = 'test_image_tag'
            build_filename = ''
            dockerObj.docker_build(dockerUtilsObj, appObj, directory, repo,
                                   projects, path, image_tag, build_filename)
        except:
            raised_exception = True
        self.assertFalse(raised_exception)

    def tearDown(self):
        pass
예제 #18
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument(
            'app_name',
            metavar='app_name',
            help=
            "application for which code is to be pulled. Example: 'agora' or 'grafana'"
        )
        parser.add_argument(
            'directory',
            metavar='directory',
            help=
            "working directory. The repo has its own directory it this. Example: '/home/vagrant/work_dir'"
        )
        parser.add_argument(
            '-b',
            '--branch',
            metavar='branch',
            help=
            "git branch to pull code from. Example: 'production' or 'master'. Defaults to master."
        )
        parser.add_argument(
            'config_file',
            metavar='config_file',
            help=
            "configuration file to use. Example: 'content.json' or 'kwe.json'")

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.work_dir = self.base_dir + "/tests/work_dir"
        self.components_dir = self.base_dir + '/tests/components/dev'
        self.args = parser

        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)

        data = config['apps']['grafana_test_app']
        self.roger_env = roger_env
        self.data = data
        self.config = config

        pass
예제 #19
0
 def setUp(self):
     self.webhook = WebHook()
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
     self.components_dir = self.base_dir + '/tests/components/dev'
     with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
         roger_env = yaml.load(roger)
     self.roger_env = roger_env
예제 #20
0
파일: cli.py 프로젝트: Tohmua/engima
def main():
    args = getCliArguments()
    settings = Settings(args.settings)
    machine = machineFactory(settings)

    output = []

    for letter in list(args.message):
        output.append(machine.encode(letter))

    print(''.join(output))
예제 #21
0
 def __init__(self,
              app_config=AppConfig(),
              settings=Settings(),
              framework_utils=FrameworkUtils(),
              framework=Marathon()):
     self._app_config = app_config
     self._settings = settings
     self._framework_utils = framework_utils
     self._framework = framework
     self._config_dir = None
     self._roger_env = None
     self._temp_dir = None
예제 #22
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--environment', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument('-s', '--skip-build', action="store_true",
                            help="Flag that skips roger-build when set to true. Defaults to false.'")
        parser.add_argument('-M', '--incr-major', action="store_true",
                            help="Increment major in version. Defaults to false.'")
        parser.add_argument('-p', '--incr-patch', action="store_true",
                            help="Increment patch in version. Defaults to false.'")
        parser.add_argument('-sp', '--skip-push', action="store_true",
                            help="Flag that skips roger push when set to true. Defaults to false.'")
        parser.add_argument(
            '-S', '--secrets-file', help="Specify an optional secrets file for deployment runtime variables.")
        parser.add_argument(
            '-d', '--directory', help="Specify an optional directory to pull out the repo. This is the working directory.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        config = {u'repo': u'roger', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}},
                                                                                                                                                                     u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}},
                                                                                                                                                                                                                                               u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app', u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'1'},
                                                                                                                                                                                                                                                                                                                                                    u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE'}}}

        roger_env = {u'default_environment': u'dev', u'registry': u'example.com:5000', u'environments': {u'prod': {u'chronos_endpoint': u'http://prod.example.com:4400',
                                                                                                         u'marathon_endpoint': u'http://prod.example.com:8080'}, u'dev': {u'chronos_endpoint': u'http://dev.example.com:4400', u'marathon_endpoint': u'http://dev.example.com:8080'},
                                                                                                         u'stage': {u'chronos_endpoint': u'http://stage.example.com:4400', u'marathon_endpoint': u'http://stage.example.com:8080'}}}

        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
예제 #23
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--env', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push', '-s', help="Don't push. Only generate components. Defaults to false.", action="store_true")
        parser.add_argument(
            '--secrets-file', '-S', help="Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'

        config = {u'repo': u'roger', u'act-as': u'test', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'vars': {u'global': {u'env_value1': u'12', u'env_value2': u'16'}, u'environment': {u'test': {u'env_value1': u'20', u'env_value2': u'24'}}}, u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}, u'test': {u'env_value1': u'64', u'env_value2': u'128'}}, u'global': {u'mem': u'128', u'cpus': u'0.1', u'env_value1': u'30', u'env_value2': u'54'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app',
                  u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'test': {u'env_value1': u'4', u'env_value2': u'8'}, u'dev': {u'mem': u'512', u'cpus': u'1'}, u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE', u'env_value1': u'3', u'env_value2': u'3'}}}

        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
        test_config = config
        test_data = test_config['apps']['grafana_test_app']
        self.test_config = test_config
        self.test_data = test_data
        template = Template(
            '{ "env": { "ENV_VAR1": "{{ env_value1 }}", "ENV_VAR2": "{{ env_value2 }}" }}')
        self.template = template
        self.additional_vars = {}
예제 #24
0
class TestInit(unittest.TestCase):
    def setUp(self):
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.config_dir = self.base_dir + "/tests/configs"
        self.template_dir = self.base_dir + "/tests/templates"
        pass

    def test_roger_init(self):
        set_config_dir = ''
        if "ROGER_CONFIG_DIR" in os.environ:
            set_config_dir = os.environ.get('ROGER_CONFIG_DIR')
        os.environ["ROGER_CONFIG_DIR"] = self.config_dir

        set_templ_dir = ''
        if "ROGER_TEMPLATES_DIR" in os.environ:
            set_templ_dir = os.environ.get('ROGER_TEMPLATES_DIR')
        os.environ["ROGER_TEMPLATES_DIR"] = self.template_dir

        os.system("roger init test_app roger")
        config_file = self.config_dir + "/app.json"
        template_file = self.template_dir + "/test-app-grafana.json"
        assert os.path.exists(config_file) is True
        assert os.path.exists(template_file) is True
        with open('{0}'.format(config_file)) as config:
            config = json.load(config)
        with open('{0}'.format(template_file)) as template:
            template = json.load(template)

        assert config['name'] == "test-app"
        assert len(config['apps']) == 3
        for app in config['apps']:
            assert config['apps'][app]['name'].startswith("test_app")
            if len(config['apps'][app]['containers']) > 0 and type(
                    config['apps'][app]) != dict:
                assert len(config['apps'][app]['containers']) == 2
                for container in config['apps'][app]['containers']:
                    assert container.startswith("container_name")
            assert config['apps'][app]['name'].startswith("test_app")

        assert template['id'] == "test-grafana"
        assert template['container']['type'] == "DOCKER"

    def tearDown(self):
        pass
예제 #25
0
class TestWebhook(unittest.TestCase):

    def setUp(self):
        self.webhook = WebHook()
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        self.roger_env = roger_env

# check for value error / exception assert
# check for one positive case - May be introduce exit code or something

    @pytest.mark.skip
    def test_invoke_webhook_when_hook_input_metrics_is_invalid(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        appdata = 'valid-app-data'
        hook_input_metrics = 'invalid-hook-input-metrics'
        conf_file = 'roger-mesos-tools.config'
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(conf_file)
        with self.assertRaises(ValueError):
            self.webhook.invoke_webhook(appdata, hook_input_metrics, conf_file)

    @pytest.mark.skip
    def test_invoke_webhook_when_appdata_is_invalid(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        appdata = 'invalid-app-data'
        hook_input_metrics = 'hook-input-metrics'
        conf_file = 'roger-mesos-tools.config'
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(conf_file)
        with self.assertRaises(ValueError):
            self.webhook.invoke_webhook(appdata, hook_input_metrics, conf_file)

    def tearDown(self):
        pass
예제 #26
0
class TestWebhook(unittest.TestCase):
    def setUp(self):
        self.webhook = WebHook()
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        self.roger_env = roger_env


# check for value error / exception assert
# check for one positive case - May be introduce exit code or something

    @pytest.mark.skip
    def test_invoke_webhook_when_hook_input_metrics_is_invalid(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        appdata = 'valid-app-data'
        conf_file = 'roger-mesos-tools.config'
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(conf_file)
        with self.assertRaises(ValueError):
            self.webhook.invoke_webhook(appdata, conf_file, any(), any(),
                                        any())

    @pytest.mark.skip
    def test_invoke_webhook_when_appdata_is_invalid(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        appdata = 'invalid-app-data'
        conf_file = 'roger-mesos-tools.config'
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(self.roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(conf_file)
        with self.assertRaises(ValueError):
            self.webhook.invoke_webhook(appdata, conf_file, any(), any(),
                                        any())

    def tearDown(self):
        pass
예제 #27
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--environment', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument('-s', '--skip-build', action="store_true",
                            help="Flag that skips roger-build when set to true. Defaults to false.'")
        parser.add_argument('-M', '--incr-major', action="store_true",
                            help="Increment major in version. Defaults to false.'")
        parser.add_argument('-p', '--incr-patch', action="store_true",
                            help="Increment patch in version. Defaults to false.'")
        parser.add_argument('-sp', '--skip-push', action="store_true",
                            help="Flag that skips roger push when set to true. Defaults to false.'")
        parser.add_argument(
            '-S', '--secrets-file', help="Specify an optional secrets file for deployment runtime variables.")
        parser.add_argument(
            '-d', '--directory', help="Specify an optional directory to pull out the repo. This is the working directory.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        config = {u'repo': u'roger', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}},
                                                                                                                                                                     u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}},
                                                                                                                                                                                                                                               u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app', u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'1'},
                                                                                                                                                                                                                                                                                                                                                    u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE'}}}

        roger_env = {u'default_environment': u'dev', u'registry': u'example.com:5000', u'environments': {u'prod': {u'chronos_endpoint': u'http://prod.example.com:4400',
                                                                                                         u'marathon_endpoint': u'http://prod.example.com:8080'}, u'dev': {u'chronos_endpoint': u'http://dev.example.com:4400', u'marathon_endpoint': u'http://dev.example.com:8080'},
                                                                                                         u'stage': {u'chronos_endpoint': u'http://stage.example.com:4400', u'marathon_endpoint': u'http://stage.example.com:8080'}}}

        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
예제 #28
0
class TestBuild(unittest.TestCase):

    def setUp(self):
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        self.configs_dir = self.base_dir + "/tests/configs"
        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        self.config = config

    def test_docker_build(self):
        raised_exception = False
        try:
            appObj = mock(AppConfig)
            dockerUtilsObj = mock(DockerUtils)
            dockerObj = Docker()
            when(appObj).getRepoName(any()).thenReturn('roger')
            when(dockerUtilsObj).docker_build(any(), any()).thenReturn(True)
            directory = self.base_dir + '/tests/testrepo'
            config = self.config
            repo = config['repo']
            projects = 'none'
            path = ''
            image_tag = 'test_image_tag'
            build_filename = ''
            verbose_mode = False
            dockerObj.docker_build(dockerUtilsObj, appObj,
                                   directory, repo, projects, path, image_tag, verbose_mode, build_filename)
        except (Exception) as e:
            print("Build error is: ")
            print(e)
            raised_exception = True
        self.assertFalse(raised_exception)

    def tearDown(self):
        pass
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--env', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push', '-s', help="Don't push. Only generate components. Defaults to false.", action="store_true")
        parser.add_argument('--secrets-file', '-S',
                            help="Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        with open(self.configs_dir + '/roger_single_container_var_tests.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['container-vars']
        self.config = config
        self.roger_env = roger_env
        self.data = data
예제 #30
0
class TestAppConfig(unittest.TestCase):
    def setUp(self):
        self.appObj = AppConfig()
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

    def test_getRogerEnv(self):
        roger_env = self.appObj.getRogerEnv(self.configs_dir)
        assert roger_env['registry'] == "example.com:5000"
        assert roger_env['default_environment'] == "dev"
        assert roger_env['environments']['dev'][
            'marathon_endpoint'] == "http://dev.example.com:8080"
        assert roger_env['environments']['prod'][
            'chronos_endpoint'] == "http://prod.example.com:4400"

    def test_getConfig(self):
        config = self.appObj.getConfig(self.configs_dir, "app.json")
        assert config['name'] == "test-app"
        assert config['repo'] == "roger"
        assert config['vars']['environment']['prod']['mem'] == "2048"
        assert len(config['apps'].keys()) == 3
        for app in config['apps']:
            assert "test_app" in app
            assert config['apps'][app]['imageBase'] == "test_app_base"

    def test_getAppData(self):
        app_data = self.appObj.getAppData(self.configs_dir, "app.json",
                                          "app_name")
        assert app_data == ''
        app_data = self.appObj.getAppData(self.configs_dir, "app.json",
                                          "test_app")
        assert app_data['imageBase'] == "test_app_base"
        assert len(app_data['containers']) == 2

    def tearDown(self):
        pass
예제 #31
0
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--env', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push', '-s', help="Don't push. Only generate components. Defaults to false.", action="store_true")
        parser.add_argument(
            '--secrets-file', '-S', help="Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'

        config = {u'repo': u'roger', u'act-as': u'test', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'vars': {u'global': {u'env_value1': u'12', u'env_value2': u'16'}, u'environment': {u'test': {u'env_value1': u'20', u'env_value2': u'24'}}}, u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}, u'test': {u'env_value1': u'64', u'env_value2': u'128'}}, u'global': {u'mem': u'128', u'cpus': u'0.1', u'env_value1': u'30', u'env_value2': u'54'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app',
                  u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'test': {u'env_value1': u'4', u'env_value2': u'8'}, u'dev': {u'mem': u'512', u'cpus': u'1'}, u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE', u'env_value1': u'3', u'env_value2': u'3'}}}

        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
        test_config = config
        test_data = test_config['apps']['grafana_test_app']
        self.test_config = test_config
        self.test_data = test_data
        template = Template(
            '{ "env": { "ENV_VAR1": "{{ env_value1 }}", "ENV_VAR2": "{{ env_value2 }}" }}')
        self.template = template
        self.additional_vars = {}
예제 #32
0
class TestGitPull(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('app_name', metavar='app_name',
                            help="application for which code is to be pulled. Example: 'agora' or 'grafana'")
        parser.add_argument('directory', metavar='directory',
                            help="working directory. The repo has its own directory it this. Example: '/home/vagrant/work_dir'")
        parser.add_argument('-b', '--branch', metavar='branch',
                            help="git branch to pull code from. Example: 'production' or 'master'. Defaults to master.")
        parser.add_argument('config_file', metavar='config_file',
                            help="configuration file to use. Example: 'content.json' or 'kwe.json'")

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.work_dir = self.base_dir + "/tests/work_dir"
        self.components_dir = self.base_dir + '/tests/components/dev'
        self.args = parser

        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)

        data = config['apps']['grafana_test_app']
        self.roger_env = roger_env
        self.data = data
        self.config = config

        pass

    def test_rogerGitPull(self):
        set_config_dir = ''
        if "ROGER_CONFIG_DIR" in os.environ:
            set_config_dir = os.environ.get('ROGER_CONFIG_DIR')
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        config_file = "app.json"
        work_dir = self.work_dir
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_gitpull = RogerGitPull()
        roger_gitpull.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        gitObj = mock(GitUtils)
        data = self.data
        config = self.config
        roger_env = self.roger_env
        repo_name = 'roger'

        when(roger_gitpull.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_gitpull.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).put(self.components_dir + '/test-roger-grafana.json',
                           roger_env['environments']['dev'], 'grafana_test_app').thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(
            self.configs_dir, "app.json").thenReturn(config)
        when(appConfig).getAppData(self.configs_dir,
                                   "app.json", "grafana_test_app").thenReturn(data)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        args = self.args
        args.app_name = "grafana_test_app"
        args.config_file = config_file
        args.branch = "master"
        args.environment = 'test'
        args.directory = self.work_dir
        args.verbose = False
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        raised_exception = False
        try:
            return_code = roger_gitpull.main(settings, appConfig, gitObj, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertTrue(raised_exception)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        exists = os.path.exists(work_dir)
        assert exists is True
        shutil.rmtree(work_dir)
        if set_config_dir.strip() != '':
            os.environ["ROGER_CONFIG_DIR"] = "{}".format(set_config_dir)

    def test_roger_gitpull_calls_pregitpull_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_gitpull = RogerGitPull()
        mockedHooks = mock(Hooks)
        gitObj = mock(GitUtils)
        roger_env = {}
        roger_env["registry"] = "any registry"
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        appdata = {}
        appdata["hooks"] = dict([("pre_gitpull", "some command")])
        repo_name = 'roger'
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        config = self.config

        when(roger_gitpull.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_gitpull.utils).extract_app_name(any()).thenReturn("test")
        when(settings).getUser().thenReturn('test_user')

        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.environment = 'test'
        args.branch = "some_branch"
        args.directory = '/tmp'
        args.verbose = False
        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        raised_exception = False
        try:
            return_code = roger_gitpull.main(settings, appConfig, gitObj, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertTrue(raised_exception)
        verify(mockedHooks).run_hook("pre_gitpull", any(), any(), any())

    def test_roger_gitpull_calls_postgitpull_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_gitpull = RogerGitPull()
        roger_gitpull.utils = mock(Utils)
        mockedHooks = mock(Hooks)
        gitObj = mock(GitUtils)
        roger_env = {}
        repo_name = 'roger'
        repo_url = 'test_url'
        roger_env["registry"] = "any registry"
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        appdata = {}
        appdata["hooks"] = dict([("post_gitpull", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        config = self.config

        when(roger_gitpull.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_gitpull.utils).extract_app_name(any()).thenReturn("test")

        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.branch = "some_branch"
        args.directory = '/tmp'
        args.environment = 'test'
        args.verbose = False
        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        raised_exception = False
        try:
            return_code = roger_gitpull.main(settings, appConfig, gitObj, mockedHooks, args)
            verify(mockedHooks).run_hook("post_gitpull", any(), any(), any())
        except (Exception) as e:
            raised_exception = True
        self.assertTrue(raised_exception)

    def tearDown(self):
        pass
예제 #33
0
import json
import yaml
import re
from jinja2 import Environment, FileSystemLoader
from termcolor import colored
from cli.framework import Framework
from cli.utils import Utils
from cli.settings import Settings
from cli.marathonvalidator import MarathonValidator
from cli.proxyparser import ProxyParser
from cli.appconfig import AppConfig
from distutils.version import LooseVersion
requests.packages.urllib3.disable_warnings()

utils = Utils()
settings = Settings()


class Marathon(Framework):
    def __init__(self):
        self.user = None
        self.passw = None
        self.marathonvalidator = MarathonValidator()
        self.proxyparser = ProxyParser()

    def getName(self):
        return "Marathon"

    def get(self, roger_env, environment):
        url = roger_env['environments'][environment][
            'marathon_endpoint'] + "/v2/apps"
예제 #34
0
class TestInit(unittest.TestCase):
    def setUp(self):
        self.gitObj = GitUtils()
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.work_dir = self.base_dir + "/tests/work_dir"
        self.branch = "master"

    @pytest.mark.skip
    def test_gitPull(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        return_code = self.gitObj.gitClone(repo, branch)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        os.chdir("{}/{}".format(work_dir, repo))
        return_code = self.gitObj.gitPull(self.branch)
        assert return_code == 0
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)
        pass

    @pytest.mark.skip
    def test_gitShallowClone(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        return_code = self.gitObj.gitShallowClone(repo, branch)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)

    @pytest.mark.skip
    def test_gitClone(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        return_code = self.gitObj.gitClone(repo, branch)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)

    def tearDown(self):
        pass
예제 #35
0
class WebHook:

    def __init__(self):
        self.disabled = True
        self.emoji = ':rocket:'
        self.defChannel = ''
        self.config_dir = ''
        self.username = '******'
        self.settingObj = Settings()
        self.appconfigObj = AppConfig()
        self.configLoadFlag = False
        self.config = ''
        self.config_channels = []
        self.config_envs = []
        self.config_commands = []

    def webhookSetting(self):
        """
        Prepares webhook setting from roger-mesos-tools.config file
        File should have all the required variables otherwise it exits
        with a warning message
        """
        try:
            self.config_dir = self.settingObj.getConfigDir()
            roger_env = self.appconfigObj.getRogerEnv(self.config_dir)
            if 'slack_webhook_url' in roger_env.keys():
                self.webhookURL = roger_env['slack_webhook_url']
            if 'slack_api_token' in roger_env.keys():
                self.token = roger_env['slack_api_token']
            if 'slack_default_channel' in roger_env.keys():
                self.defChannel = roger_env['slack_default_channel']
            if 'slack_deploy_botid' in roger_env.keys():
                self.botid = roger_env['slack_deploy_botid']
        except (Exception) as e:
            print("Warning: slackweb basic initialization failed (error: %s).\
            Not using slack." % e)
            return
        try:
            self.sc = SlackClient(self.token)
            self.client = slackweb.Slack(url=self.webhookURL)
            self.disabled = False
        except (Exception) as e:
            print("Warning: slackweb basic initialization failed (error: %s).\
            Not using slack." % e)
            return  # disabled flag remains False

    def custom_api_call(self, text, channel):
        """ Makes the webhook call
        Keyword arguments:
        text -- message to be posted
        channel -- to which channel

        posts a message if rogeros - bot is present
        """
        try:
            self.webhookSetting()
            if len(channel) == 0:
                channel = self.defChannel
            var = self.sc.api_call("channels.list")
            length = len(var['channels'])
            for iterator in range(0, length):
                # getting rid of # for comparison
                if channel[1:] == var['channels'][iterator]['name']:
                    if self.botid in var['channels'][iterator]['members']:
                        if not self.disabled:
                            self.client.notify(channel=channel, username=self.username,
                                               icon_emoji=self.emoji, text=text)
        except (Exception) as e:
            # notify to channel and log it as well
            printException(e)
            raise

    def areBasicKeysAvailableInConfig(self, config):
        if 'notifications' not in config:
            return False
        tempList = config['notifications'].keys()
        if 'channels' not in tempList:
            return False  # if no channel is available nothing can be done
        if 'envs' not in tempList:
            self.config_envs = ['dev', 'production', 'staging', 'local']
        if 'commands' not in tempList:
            self.config_commands = ['pull', 'build', 'push']
        return True

    def areBasicKeysAvailableInAppdata(self, appdata):
        tempList = appdata.keys()
        if 'notifications' not in tempList:
            return False
        appKeys = appdata['notifications'].keys()
        if 'channels' not in appKeys:
            return False
        return True

    def configLevelSettings(self, config_file):
        """ Prepares all the config_level settings as variables

        Keyword arguments:
        config_file -- This is the file name passed as argument
        return three sets of channels, envs and commands.
        self variable as config level variable not expected to change for the run
        """
        if (not self.configLoadFlag):
            self.config = self.appconfigObj.getConfig(self.config_dir, config_file)
            self.configLoadFlag = True
            if(not self.areBasicKeysAvailableInConfig(self.config)):
                return
            try:
                self.config_channels = Set(self.config['notifications']['channels'])
                if 'envs' in self.config['notifications'].keys():
                    self.config_envs = Set(self.config['notifications']['envs'])
                if 'commands' in self.config['notifications'].keys():
                    self.config_commands = Set(self.config['notifications']['commands'])
                if (len(self.config_channels) == 0 or len(self.config_envs) == 0 or len(self.config_commands) == 0):
                    return
            except (Exception, KeyError, ValueError) as e:
                # notify to channel and log it as well
                printException(e)
                raise

    def invoke_webhook(self, appdata, config_file, action, env, user):
        """ Pepares set and posts to slack channel

        Keyword arguments:
        appdata -- this is value related to an app
        hook_input_metric -- value it  gets from hook class per app
        config_file -- the file name under for the app deployment
        """
        envSet = []
        commandsSet = []
        self.function_execution_start_time = datetime.now()
        self.webhookSetting()
        self.configLevelSettings(config_file)
        self.action = action
        self.envr = env
        self.user = user
        self.app_name = appdata['name'] 
        try:

            if(not self.areBasicKeysAvailableInAppdata(appdata)):
                return
            if 'notifications' in appdata:
                channelsSet = Set(appdata['notifications']['channels'])
                # to handle if tag is there but no data is present
                if (len(self.config_channels) != 0):
                    channelsSet = channelsSet.union(self.config_channels)
                # to handle if tag is there
                if 'envs' in appdata['notifications'].keys():
                    envSet = Set(appdata['notifications']['envs'])
                # to handle if tag is there but data is not there
                if (len(self.config_envs) != 0):
                    envSet = envSet.union(self.config_envs)
                if 'commands' in appdata['notifications'].keys():
                    commandsSet = Set(appdata['notifications']['commands'])
                if (len(self.config_commands) != 0):
                    commandsSet = commandsSet.union(self.config_commands)
                if (len(channelsSet) == 0 or len(envSet) == 0 or len(commandsSet) == 0):
                    return
            else:
                if list(self.config_envs)[0] == 'all':
                    self.config_envs = ['dev', 'production', 'staging', 'local']
                if list(self.config_commands)[0] == 'all':
                    self.config_commands = ['pull', 'build', 'push']
                self.postToSlack(self.action, self.config_envs, self.config_commands, self.config_channels)
                return
            if list(envSet)[0] == 'all':
                envSet = ['dev', 'production', 'staging', 'local']
            if list(commandsSet)[0] == 'all':
                commandsSet = ['pull', 'build', 'push']
        except (Exception, KeyError, ValueError) as e:
            printException(e)
            raise
        try:
            self.postToSlack(self.action, envSet, commandsSet, channelsSet)
        except (Exception) as e:
            self.custom_api_call("Error : %s" % e, self.defChannel)
            printException(e)
            raise

    def postToSlack(self, action, envSet, commandsSet, channelsSet):
        """ Prepares post to slack channel
        Keyword arguments:
        action -- action extracted from hookname_input_metric
        envSet -- set of accepted environment
        commandsSet -- set of accepted commandssets
        channelsSet -- set of accepted channelsets
        """
        try:
            if ('post' in action and self.envr in envSet and action.split('_')[1] in commandsSet):
                for channel in channelsSet:
                    timeElapsed = '{0:.2f}'.format((datetime.now() - self.function_execution_start_time).total_seconds() * 1000)
                    timeasInt = float(timeElapsed)
                    h, m, s, ms = self.makeTimeReadable(timeasInt)
                    readableTime = self.createMesage(h, m, s, ms)
                    slackMessage = ("Completed *" + action.split('_')[1] + "* of *" + self.app_name + "* on *" + self.envr + "* in *" + readableTime + "*  (triggered by *" + self.user + "*)")
                    self.custom_api_call(slackMessage, '#' + channel)
        except (Exception) as e:
            self.custom_api_call("Error : %s" % e, self.defChannel)
            printException(e)
            raise

    def makeTimeReadable(self, ms):
        """ Make time readable

        Keyword arguments:
        ms -- time in miliseconds
        """
        s = ms / 1000
        m, s = divmod(s, 60)
        h, m = divmod(m, 60)
        return h, m, s, ms

    def createMesage(self, h, m, s, ms):
        message = ''
        if int(h) > 0:
            message += str(int(h)) + "h "
        if int(m) > 0:
            message += str(int(m)) + "m "
        if int(s) > 0:
            message += str(int(s)) + "s "
        message += str(ms) + "ms "
        return message
예제 #36
0
class TestPush(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--env', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push', '-s', help="Don't push. Only generate components. Defaults to false.", action="store_true")
        parser.add_argument(
            '--secrets-file', '-S', help="Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'

        config = {u'repo': u'roger', u'act-as': u'test', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'vars': {u'global': {u'env_value1': u'12', u'env_value2': u'16'}, u'environment': {u'test': {u'env_value1': u'20', u'env_value2': u'24'}}}, u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}, u'test': {u'env_value1': u'64', u'env_value2': u'128'}}, u'global': {u'mem': u'128', u'cpus': u'0.1', u'env_value1': u'30', u'env_value2': u'54'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app',
                  u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'test': {u'env_value1': u'4', u'env_value2': u'8'}, u'dev': {u'mem': u'512', u'cpus': u'1'}, u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE', u'env_value1': u'3', u'env_value2': u'3'}}}

        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
        test_config = config
        test_data = test_config['apps']['grafana_test_app']
        self.test_config = test_config
        self.test_data = test_data
        template = Template(
            '{ "env": { "ENV_VAR1": "{{ env_value1 }}", "ENV_VAR2": "{{ env_value2 }}" }}')
        self.template = template
        self.additional_vars = {}

    def test_template_render_for_extra_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['grafana_test_app']
        container = app_data['containers'][1]['grafana1']
        additional_vars = self.additional_vars
        additional_vars['env_value1'] = "100"
        additional_vars['env_value2'] = "200"
        output = roger_push.renderTemplate(
            self.template, "test", "test_image", app_data, self.config, container, "grafana1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '100'
        assert result['env']['ENV_VAR2'] == '200'

    def test_template_render_for_config_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['test_app']
        container = "container_name1"
        output = roger_push.renderTemplate(
            self.template, "test", "test_image", app_data, self.config, container, "container_name1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '4'
        assert result['env']['ENV_VAR2'] == '8'

    def test_template_render_for_app_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['test_app1']
        container = "container_name1"
        # Passing environment that doesn't exist
        output = roger_push.renderTemplate(self.template, "non_existing_env", "test_image",
                                           app_data, self.config, container, "container_name1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '12'
        assert result['env']['ENV_VAR2'] == '16'
        # Existing environment
        output = roger_push.renderTemplate(
            self.template, "test", "test_image", app_data, self.config, container, "container_name1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '20'
        assert result['env']['ENV_VAR2'] == '24'

    def test_template_render_for_container_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['grafana_test_app']
        container = "grafana"
        # Passing environment that doesn't exist
        output = roger_push.renderTemplate(self.template, "non_existing_env", "test_image",
                                           app_data, self.config, container, "grafana", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '3'
        assert result['env']['ENV_VAR2'] == '3'
        # Existing environment
        output = roger_push.renderTemplate(
            self.template, "test", "test_image", app_data, self.config, container, "grafana", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '4'
        assert result['env']['ENV_VAR2'] == '8'
        container = app_data['containers'][1]['grafana1']
        output = roger_push.renderTemplate(self.template, "non_existing_env", "test_image",
                                           app_data, self.config, container, "grafana1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '30'
        assert result['env']['ENV_VAR2'] == '54'
        output = roger_push.renderTemplate(
            self.template, "test", "test_image", app_data, self.config, container, "grafana1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '64'
        assert result['env']['ENV_VAR2'] == '128'

    def test_roger_push_grafana_test_app(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]", any())
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        with open(self.base_dir + '/tests/templates/test-app-grafana.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        verify(settings).getConfigDir()
        verify(settings).getComponentsDir()
        verify(settings).getTemplatesDir()
        verify(settings).getSecretsDir()
        verify(frameworkUtils).getFramework(data)

    def test_container_resolution(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.test_config
        data = self.test_data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.directory = self.base_dir + '/tests/testrepo'
        args.config_file = 'test.json'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        with open(self.base_dir + '/tests/templates/test-app-grafana.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 2
        assert output['mem'] == 1024
        assert output['uris'] == ["abc", "xyz", "$ENV_VAR"]
        with open(self.base_dir + '/tests/templates/test-app-grafana1.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 0.5
        assert output['mem'] == 512
        with open(self.base_dir + '/tests/templates/test-app-grafana2.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 1
        assert output['mem'] == 1024

    def test_roger_push_with_no_app_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.app_name = ''
        args.config_file = 'test.json'
        args.env = 'some_test_env'
        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_roger_push_with_no_registry_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'

        config_dir = settings.getConfigDir()
        roger_env = appConfig.getRogerEnv(config_dir)
        # Remove registry key from dictionary
        del roger_env['registry']
        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_roger_push_with_no_environment_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        # Set environment variable as None
        args.env = ''
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_unresolved_jinja_variable_fails(self):

        with open(self.configs_dir + '/roger_push_unresolved_jinja.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['container-vars']
        self.config = config
        self.roger_env = roger_env
        self.data = data

        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).get_version().thenReturn(any())

        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(self.configs_dir,
                                  "roger_push_unresolved_jinja.json").thenReturn(config)
        when(appConfig).getAppData(self.configs_dir,
                                   "roger_push_unresolved_jinja.json", "container-vars").thenReturn(data)

        args = self.args
        args.env = "dev"
        args.skip_push = False
        args.force_push = False
        args.secrets_dir = ""
        args.secrets_file = ""
        args.app_name = 'container-vars'
        args.config_file = 'roger_push_unresolved_jinja.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'tests/v0.1.0'
        args.verbose = False
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        verify(frameworkUtils, times=0).put(any(), any(), any(), any())

    def test_roger_push_calls_prepush_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("pre_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.env = 'dev'
        args.image_name = 'tests/v0.1.0'
        args.directory = '/tmp'
        args.secrets_file = ""
        args.skip_push = True
        args.verbose = False
        raised_exception = False
        try:
            return_code = roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertFalse(raised_exception) 
        verify(mockedHooks).run_hook("pre_push", any(), any(), any())

    def test_roger_push_calls_postpush_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("post_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.env = 'dev'
        args.image_name = 'tests/v0.1.0'
        args.directory = '/tmp'
        args.secrets_file = ""
        args.skip_push = True
        args.verbose = False
        raised_exception = False
        try:
            return_code = roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertFalse(raised_exception) 
        verify(mockedHooks).run_hook("post_push", any(), any(), any())

    def test_roger_push_verify_default_env_use(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        frameworkUtils = mock(FrameworkUtils)
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = None
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        roger_env["default_environment"] = "test_env"

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_roger_push_with_incorrect_container_name(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        appdata = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        frameworkUtils = mock(FrameworkUtils)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app:test'
        args.config_file = 'test.json'

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_roger_push_with_correct_container_name(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config
        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("post_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app:grafana'
        args.config_file = 'test.json'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        raised_exception = False
        try:
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertFalse(raised_exception)
        verify(frameworkUtils).getFramework(any())

    def test_roger_push_env_from_ROGER_ENV_VAR(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        frameworkUtils = mock(FrameworkUtils)
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = None
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        roger_env["default_environment"] = None
        # Setting ROGER_ENV to specific value
        os.environ["ROGER_ENV"] = "test_env"

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig,
                            frameworkUtils, mockedHooks, args)

    def test_push_happens_with_validation_error_when_force_push_set(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any(),
                           any(), any()).thenReturn(["Response [200]", any()])
        when(marathon).runDeploymentChecks(any(), any()).thenReturn(True)
        frameworkUtils = mock(FrameworkUtils)
        frameworkUtils = mock(FrameworkUtils)

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).modify_task_id(any()).thenReturn([any()])
        when(roger_push.utils).get_version().thenReturn(any())

        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = False
        args.force_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        raised_exception = False
        try:
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertFalse(raised_exception)

    def test_roger_push_skip_push_set(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).put(any(), any(), any()).thenReturn(["Response [200]", any()])
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        return_code = roger_push.main(
            settings, appConfig, frameworkUtils, mockedHooks, args)
        verify(frameworkUtils, times=0).runDeploymentChecks(any(), any())

    def test_push_fails_with_validation_error(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).modify_task_id(any()).thenReturn([any()])
        when(roger_push.utils).get_version().thenReturn(any())

        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any(),
                           any(), any()).thenReturn(["Response [200]", any()])
        when(marathon).runDeploymentChecks(any(), any()).thenReturn(True)
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = False
        args.force_push = False
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        raised_exception = False
        try:
            return_code = roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        except (Exception) as e:
            raised_exception = True
        self.assertFalse(raised_exception)
        verify(frameworkUtils, times=0).put(any(), any(), any(), any())

    def test_roger_push_secrets_replaced(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        args.verbose = False
        exit_code = roger_push.main(
            settings, appConfig, frameworkUtils, mockedHooks, args)
        file_path = ("{0}/{1}/{2}".format(self.components_dir,
                                          args.env, args.config_file))
        assert (os.path.isfile(file_path) is not True)

    def test_secret_vars_replacement_for_no_secrets_file(self):
        args = self.args
        args.env = "test"
        args.secrets_file = ""
        args.app_name = 'test_app'
        args.image_name = 'test_image'
        args.verbose = False
        secrets_dir = self.base_dir + "/tests/secrets"
        roger_push = RogerPush()
        app_data = self.config['apps'][args.app_name]
        container = "container1"
        additional_vars = self.additional_vars
        extra_vars = {}
        extra_vars['env_value1'] = "100"
        extra_vars['env_value2'] = "200"
        additional_vars.update(extra_vars)
        secret_vars = roger_push.loadSecrets(
            secrets_dir, args.secrets_file, args, args.env)
        additional_vars.update(secret_vars)
        output = roger_push.renderTemplate(
            self.template, args.env, args.image_name, app_data, self.config, container, "container1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '100'
        assert result['env']['ENV_VAR2'] == '200'

    def test_secret_vars_replacement_with_secrets_file(self):
        args = self.args
        args.env = "test"
        args.secrets_file = "test_app-container1.json"
        args.app_name = 'test_app'
        args.image_name = 'test_image'
        args.verbose = False
        secrets_dir = self.base_dir + "/tests/secrets"
        roger_push = RogerPush()
        app_data = self.config['apps'][args.app_name]
        container = "container1"
        additional_vars = self.additional_vars
        extra_vars = {}
        extra_vars['env_value1'] = "100"
        extra_vars['env_value2'] = "200"
        additional_vars.update(extra_vars)
        secret_vars = roger_push.loadSecrets(
            secrets_dir, args.secrets_file, args, args.env)
        print(secret_vars)
        additional_vars.update(secret_vars)
        output = roger_push.renderTemplate(
            self.template, args.env, args.image_name, app_data, self.config, container, "container1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == 'test_value1'
        assert result['env']['ENV_VAR2'] == 'test_value2'

    def tearDown(self):
        pass
예제 #37
0
    def main(self, settingObject, appObject, frameworkUtilsObject, gitObj, hooksObj, args):
        try:
            function_execution_start_time = datetime.now()
            execution_result = 'SUCCESS'
            settingObj = settingObject
            appObj = appObject
            config_dir = settingObj.getConfigDir()
            root = settingObj.getCliDir()
            roger_env = appObj.getRogerEnv(config_dir)
            config = appObj.getConfig(config_dir, args.config_file)
            config_name = ""
            if 'name' in config:
                config_name = config['name']
            if 'registry' not in roger_env:
                raise ValueError('Registry not found in roger-mesos-tools.config file.')
            else:
                self.registry = roger_env['registry']

            # Setup for Slack-Client, token, and git user
            # (vmahedia) todo: ExtractClass Notifications, it should know who all to notify on what event
            # Event should be registered and SlackNotification should be one of the members. it can have
            # N notifications on a particular "event", Notifications.Notify will broadcast notification to
            # all the interested parties.
            if 'notifications' in config:
                self.slack = Slack(config['notifications'],
                                   '/home/vagrant/.roger_cli.conf.d/slack_token')

            self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.application)

            apps = []
            apps_container_dict = {}
            if args.application == 'all':
                apps = config['apps'].keys()
            else:
                if ":" not in args.application and "[" not in args.application:
                    apps.append(args.application)
                else:
                    for item in args.application.split(":"):
                        if '[' in item:
                            matchObj = re.match(r'(.*)\[(.*)\]', item)
                            apps.append(matchObj.group(1))
                            apps_container_dict[matchObj.group(1)] = matchObj.group(2)
                        else:
                            apps.append(item)

            common_repo = config.get('repo', '')
            environment = roger_env.get('default_environment', '')

            work_dir = ''
            if args.directory:
                work_dir = args.directory
                temp_dir_created = False
                if args.verbose:
                    print("Using {0} as the working directory".format(work_dir))
            else:
                work_dir = mkdtemp()
                temp_dir_created = True
                if args.verbose:
                    print("Created a temporary dir: {0}".format(work_dir))

            if args.environment is None:
                if "ROGER_ENV" in os.environ:
                    env_var = os.environ.get('ROGER_ENV')
                    if env_var.strip() == '':
                        print(
                            "Environment variable $ROGER_ENV is not set. Using the default set from roger-mesos-tools.config file")
                    else:
                        print(
                            "Using value {} from environment variable $ROGER_ENV".format(env_var))
                        environment = env_var
            else:
                environment = args.environment

            if environment not in roger_env['environments']:
                self.removeDirTree(work_dir, args, temp_dir_created)
                raise ValueError('Environment not found in roger-mesos-tools.config file.')

            branch = "master"  # master by default
            if args.branch is not None:
                branch = args.branch

            try:
                for app in apps:
                    if app not in config['apps']:
                        raise ValueError('Application {} specified not found.'.format(app))
                    else:
                        try:
                            if args.verbose:
                                print("Deploying {} ...".format(app))
                            self.deployApp(settingObject, appObject, frameworkUtilsObject, gitObj, hooksObj,
                                           root, args, config, roger_env, work_dir, config_dir, environment, app, branch, self.slack, args.config_file, common_repo, temp_dir_created, apps_container_dict)
                        except (IOError, ValueError) as e:
                            error_msg = "Error when deploying {}: {}".format(app, repr(e))
                            printErrorMsg(error_msg)
                            pass    # try deploying the next app
            except (Exception) as e:
                printException(e)
                raise
        except (Exception) as e:
            execution_result = 'FAILURE'
            printException(e)
            raise
        finally:
            # Check if the initializition of variables carried out
            if 'function_execution_start_time' not in globals() and 'function_execution_start_time' not in locals():
                function_execution_start_time = datetime.now()

            if 'execution_result' not in globals() and 'execution_result' not in locals():
                execution_result = 'FAILURE'

            if 'config_name' not in globals() and 'config_name' not in locals():
                config_name = ""

            if 'environment' not in globals() and 'environment' not in locals():
                environment = "dev"

            if not hasattr(args, "application"):
                args.application = ""

            if 'settingObj' not in globals() and 'settingObj' not in locals():
                settingObj = Settings()

            if 'work_dir' not in globals() and 'work_dir' not in locals():
                work_dir = ''
                temp_dir_created = False

            if not (self.rogerGitPullObject.outcome is 1 and self.rogerBuildObject.outcome is 1 and self.rogerPushObject.outcome is 1):
                execution_result = 'FAILURE'

            try:
                # If the deploy fails before going through any steps
                sc = self.utils.getStatsClient()
                if not hasattr(self, "identifier"):
                    self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.application)
                args.application = self.utils.extract_app_name(args.application)
                time_take_milliseonds = ((datetime.now() - function_execution_start_time).total_seconds() * 1000)
                input_metric = "roger-tools.rogeros_tools_exec_time," + "app_name=" + str(args.application) + ",event=deploy" + ",outcome=" + str(execution_result) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******",identifier=" + str(self.identifier)
                tup = (input_metric, time_take_milliseonds)
                self.statsd_message_list.append(tup)
                self.removeDirTree(work_dir, args, temp_dir_created)
            except (Exception) as e:
                error_msg = "Error when deploying {}: {}".format(app, repr(e))
                printErrorMsg(error_msg)
                raise
예제 #38
0
                        self.task_id.extend(container_task_id)
                    except (Exception) as e:
                        print("ERROR - : %s" %e, file=sys.stderr)
                        execution_result = 'FAILURE'
                        raise
                    finally:
                        # todo: maybe send datadog event from here?
                        pass

            hookname = "post_push"
            exit_code = hooksObj.run_hook(hookname, data, app_path, args.env, settingObj.getUser())
            if exit_code != 0:
                raise ValueError("{} hook failed.".format(hookname))
            print(colored("******Done with the PUSH step******", "green"))

        except (Exception) as e:
            raise ValueError("ERROR - {}".format(e))

if __name__ == "__main__":
    settingObj = Settings()
    appObj = AppConfig()
    frameworkUtils = FrameworkUtils()
    hooksObj = Hooks()
    roger_push = RogerPush()
    try:
        roger_push.parser = roger_push.parse_args()
        roger_push.args = roger_push.parser.parse_args()
        roger_push.main(settingObj, appObj, frameworkUtils, hooksObj, roger_push.args)
    except (Exception) as e:
        printException(e)
예제 #39
0
class TestInit(unittest.TestCase):

    def setUp(self):
        self.gitObj = GitUtils()
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.work_dir = self.base_dir + "/tests/work_dir"
        self.branch = "master"

    @pytest.mark.skip
    def test_gitPull(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        return_code = self.gitObj.gitClone(repo, branch)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        os.chdir("{}/{}".format(work_dir, repo))
        return_code = self.gitObj.gitPull(self.branch)
        assert return_code == 0
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)
        pass

    @pytest.mark.skip
    def test_gitShallowClone(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        verbose_mode = False
        return_code = self.gitObj.gitShallowClone(repo, branch, verbose_mode)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)

    @pytest.mark.skip
    def test_gitClone(self):
        config_file = "app.json"
        work_dir = self.work_dir
        branch = self.branch

        if not os.path.exists(self.work_dir):
            try:
                os.makedirs(self.work_dir)
            except OSError as exception:
                if exception.errno != errno.EEXIST:
                    raise
        os.chdir(self.work_dir)
        with open(self.configs_dir + '/{}'.format(config_file)) as config:
            config = json.load(config)
        repo = config['repo']
        return_code = self.gitObj.gitClone(repo, branch)
        assert return_code == 0
        exists = os.path.exists(work_dir)
        assert exists is True
        exists = os.path.exists("{}/{}".format(work_dir, repo))
        assert exists is True
        exists = os.path.exists("{}/{}/ansible".format(work_dir, repo))
        assert exists is True
        shutil.rmtree(work_dir)

    def tearDown(self):
        pass
예제 #40
0
    def main(self, settings, appConfig, gitObject, hooksObj, args):
        try:
            function_execution_start_time = datetime.now()
            execution_result = 'SUCCESS'  # Assume the execution_result to be SUCCESS unless exception occurs
            environment = "dev"
            if hasattr(args, "environment"):
                environment = args.environment
            settingObj = settings
            appObj = appConfig
            gitObj = gitObject
            config_dir = settingObj.getConfigDir()
            hooksObj.config_file = args.config_file
            config = appObj.getConfig(config_dir, args.config_file)
            config_name = ""
            if 'name' in config:
                config_name = config['name']
            common_repo = config.get('repo', '')
            data = appObj.getAppData(config_dir, args.config_file, args.app_name)
            if not data:
                raise ValueError('Application with name [{}] or data for it not found at {}/{}.'.format(
                    args.app_name, config_dir, args.config_file))
            repo = ''
            if common_repo != '':
                repo = data.get('repo', common_repo)
            else:
                repo = data.get('repo', args.app_name)

            branch = "master"  # master by default
            if args.branch is not None:
                branch = args.branch

            if not os.path.exists(args.directory):
                try:
                    os.makedirs(args.directory)
                except OSError as exception:
                    if exception.errno != errno.EEXIST:
                        raise

            if not hasattr(args, "app_name"):
                args.app_name = ""

            if not hasattr(self, "identifier"):
                self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)

            args.app_name = self.utils.extract_app_name(args.app_name)

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "pre_gitpull"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"{0}/{1}".format(args.directory, repo_name)
            if os.path.isdir(path):
                with chdir(path):
                    exit_code = gitObj.gitPull(branch)
            else:
                with chdir('{0}'.format(args.directory)):
                    exit_code = gitObj.gitShallowClone(repo, branch)

            if exit_code != 0:
                raise ValueError('gitpull failed.')

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "post_gitpull"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"The following error occurred: %s" %
                  e, file=sys.stderr)
            execution_result = 'FAILURE'
            raise
        finally:
            try:
                # If the gitpull fails before going through any steps
                if 'function_execution_start_time' not in globals() and 'function_execution_start_time' not in locals():
                    function_execution_start_time = datetime.now()

                if 'execution_result' not in globals() and 'execution_result' not in locals():
                    execution_result = 'FAILURE'

                if 'config_name' not in globals() and 'config_name' not in locals():
                    config_name = ""

                if 'environment' not in globals() and 'environment' not in locals():
                    environment = "dev"

                if not hasattr(args, "app_name"):
                    args.app_name = ""

                if 'settingObj' not in globals() and 'settingObj' not in locals():
                    settingObj = Settings()

                if 'execution_result' is 'FAILURE':
                    self.outcome = 0

                sc = self.utils.getStatsClient()
                if not hasattr(self, "identifier"):
                    self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)
                time_take_milliseonds = ((datetime.now() - function_execution_start_time).total_seconds() * 1000)
                input_metric = "roger-tools.rogeros_tools_exec_time," + "app_name=" + str(args.app_name) + ",event=gitpull" + ",identifier=" + str(self.identifier) + ",outcome=" + str(execution_result) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"The following error occurred: %s" %
                      e, file=sys.stderr)
                raise
예제 #41
0
class TestDeploy(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--environment', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument('-s', '--skip-build', action="store_true",
                            help="Flag that skips roger-build when set to true. Defaults to false.'")
        parser.add_argument('-M', '--incr-major', action="store_true",
                            help="Increment major in version. Defaults to false.'")
        parser.add_argument('-p', '--incr-patch', action="store_true",
                            help="Increment patch in version. Defaults to false.'")
        parser.add_argument('-sp', '--skip-push', action="store_true",
                            help="Flag that skips roger push when set to true. Defaults to false.'")
        parser.add_argument(
            '-S', '--secrets-file', help="Specify an optional secrets file for deployment runtime variables.")
        parser.add_argument(
            '-d', '--directory', help="Specify an optional directory to pull out the repo. This is the working directory.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        config = {u'repo': u'roger', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}},
                                                                                                                                                                     u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}},
                                                                                                                                                                                                                                               u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app', u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'1'},
                                                                                                                                                                                                                                                                                                                                                    u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE'}}}

        roger_env = {u'default_environment': u'dev', u'registry': u'example.com:5000', u'environments': {u'prod': {u'chronos_endpoint': u'http://prod.example.com:4400',
                                                                                                         u'marathon_endpoint': u'http://prod.example.com:8080'}, u'dev': {u'chronos_endpoint': u'http://dev.example.com:4400', u'marathon_endpoint': u'http://dev.example.com:8080'},
                                                                                                         u'stage': {u'chronos_endpoint': u'http://stage.example.com:4400', u'marathon_endpoint': u'http://stage.example.com:8080'}}}

        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data

    def test_splitVersion(self):
        roger_deploy = RogerDeploy()
        assert roger_deploy.splitVersion("0.1.0") == (0, 1, 0)
        assert roger_deploy.splitVersion("2.0013") == (2, 13, 0)

    def test_incrementVersion(self):
        git_sha = "dwqjdqgwd7y12edq21"
        image_version_list = ['0.001', '0.2.034', '1.1.2', '1.002.1']
        roger_deploy = RogerDeploy()
        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.incr_patch = True
        args.incr_major = True

        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'
        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'
        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'

    def test_tempDirCheck(self):
        work_dir = "./test_dir"
        roger_deploy = RogerDeploy()
        args = self.args
        args.directory = ""
        args.skip_push = False
        args.secrets_file = ""
        temp_dir_created = True
        roger_deploy.removeDirTree(work_dir, args, temp_dir_created)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is False
        os.makedirs(work_dir)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is True
        roger_deploy.removeDirTree(work_dir, args, temp_dir_created)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is False

    def test_roger_deploy_with_no_app(self):
        try:
            raised_exception = False
            settings = mock(Settings)
            appConfig = mock(AppConfig)
            roger_deploy = RogerDeploy()
            marathon = mock(Marathon)
            gitObj = mock(GitUtils)
            mockedHooks = mock(Hooks)
            roger_deploy.rogerGitPullObject = mock(RogerGitPull)
            roger_deploy.rogerPushObject = mock(RogerPush)
            roger_deploy.rogerBuildObject = mock(RogerBuild)
            roger_deploy.dockerUtilsObject = mock(DockerUtils)
            roger_deploy.dockerObject = mock(Docker)
            roger_deploy.utils = mock(Utils)
            roger_env = self.roger_env
            config = self.config
            data = self.data

            sc = mock(StatsClient)
            when(sc).timing(any(), any()).thenReturn(any())
            when(roger_deploy.utils).getStatsClient().thenReturn(sc)
            when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
            when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

            when(marathon).getCurrentImageVersion(
                any(), any(), any()).thenReturn("testversion/v0.1.0")
            frameworkUtils = mock(FrameworkUtils)
            when(frameworkUtils).getFramework(data).thenReturn(marathon)
            when(settings).getConfigDir().thenReturn(any())
            when(settings).getCliDir().thenReturn(any())
            when(settings).getUser().thenReturn('test_user')
            when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
            when(appConfig).getConfig(any(), any()).thenReturn(config)
            when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

            args = self.args
            args.directory = ""
            args.secrets_file = ""
            args.environment = "dev"
            args.skip_push = False
            args.application = ''
            args.config_file = 'test.json'
            args.skip_build = True
            args.branch = None
            os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        except:
            raised_exception = True
        self.assertFalse(raised_exception)

    def test_roger_deploy_with_no_registry_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).getCurrentImageVersion(
            any(), any(), any(), any()).thenReturn("testversion/v0.1.0")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        del roger_env['registry']
        with self.assertRaises(ValueError):
            roger_deploy.main(settings, appConfig,
                              frameworkUtils, gitObj, mockedHooks, args)

    def test_roger_deploy_with_no_environment_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        gitObj = mock(GitUtils)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = ""
        args.skip_push = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        with self.assertRaises(ValueError):
            roger_deploy.main(settings, appConfig,
                              frameworkUtils, gitObj, mockedHooks, args)

    def test_rogerDeploy(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = False
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(settings, times=1).getConfigDir()
        verify(settings).getCliDir()
        verify(appConfig).getRogerEnv(any())
        verify(appConfig, times=1).getConfig(any(), any())
        verify(frameworkUtils).getFramework(data)
        verify(marathon).getCurrentImageVersion(any(), any(), any())

    def test_rogerDeploy_with_skip_push(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data
        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(settings, times=1).getConfigDir()
        verify(settings).getCliDir()
        verify(appConfig).getRogerEnv(any())
        verify(appConfig, times=1).getConfig(any(), any())
        verify(frameworkUtils).getFramework(data)
        verify(marathon).getCurrentImageVersion(any(), any(), any())

    def test_rogerDeploy_with_skip_gitpull_false(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(roger_deploy.rogerGitPullObject, times=1).main(any(), any(), any(), any(), any())

    def test_rogerDeploy_with_skip_gitpull_true(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data
        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_deploy.utils).getStatsClient().thenReturn(sc)
        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = True
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(roger_deploy.rogerGitPullObject, times=0).main(any(), any(), any(), any(), any())

    def tearDown(self):
        pass
예제 #42
0
 def setUp(self):
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
예제 #43
0
class TestSettings(unittest.TestCase):

    def setUp(self):
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()

    def test_getConfigDir(self):
        set_config_dir = ''
        if "ROGER_CONFIG_DIR" in os.environ:
            set_config_dir = os.environ.get('ROGER_CONFIG_DIR')
        os.environ["ROGER_CONFIG_DIR"] = self.base_dir + "/testconfigdir"
        config_dir = self.settingObj.getConfigDir()
        assert config_dir == self.base_dir + "/testconfigdir"
        del os.environ['ROGER_CONFIG_DIR']
        try:
            config_dir = self.settingObj.getConfigDir()
        except (ValueError) as e:
            assert (
                "Environment variable $ROGER_CONFIG_DIR is not set." in e)
        if set_config_dir.strip() != '':
            os.environ["ROGER_CONFIG_DIR"] = "{}".format(set_config_dir)

    def test_getComponentsDir(self):
        set_comp_dir = ''
        if "ROGER_COMPONENTS_DIR" in os.environ:
            set_comp_dir = os.environ.get('ROGER_COMPONENTS_DIR')
        os.environ["ROGER_COMPONENTS_DIR"] = self.base_dir + "/testcompdir"
        comp_dir = self.settingObj.getComponentsDir()
        assert comp_dir == self.base_dir + "/testcompdir"
        del os.environ['ROGER_COMPONENTS_DIR']
        try:
            comp_dir = self.settingObj.getComponentsDir()
        except (ValueError) as e:
            assert (
                "Environment variable $ROGER_COMPONENTS_DIR is not set." in e)
        if set_comp_dir.strip() != '':
            os.environ["ROGER_COMPONENTS_DIR"] = "{}".format(set_comp_dir)

    def test_getTemplatesDir(self):
        set_temp_dir = ''
        if "ROGER_TEMPLATES_DIR" in os.environ:
            set_temp_dir = os.environ.get('ROGER_TEMPLATES_DIR')
        os.environ["ROGER_TEMPLATES_DIR"] = self.base_dir + "/testtempldir"
        temp_dir = self.settingObj.getTemplatesDir()
        assert temp_dir == self.base_dir + "/testtempldir"
        del os.environ['ROGER_TEMPLATES_DIR']
        try:
            temp_dir = self.settingObj.getTemplatesDir()
        except (ValueError) as e:
            assert (
                "Environment variable $ROGER_TEMPLATES_DIR is not set." in e)
        if set_temp_dir.strip() != '':
            os.environ["ROGER_TEMPLATES_DIR"] = "{}".format(set_temp_dir)

    def test_getSecretsDir(self):
        set_sect_dir = ''
        if "ROGER_SECRETS_DIR" in os.environ:
            set_sect_dir = os.environ.get('ROGER_SECRETS_DIR')
        os.environ["ROGER_SECRETS_DIR"] = self.base_dir + "/testsectdir"
        sect_dir = self.settingObj.getSecretsDir()
        assert sect_dir == self.base_dir + "/testsectdir"
        del os.environ['ROGER_SECRETS_DIR']
        try:
            sect_dir = self.settingObj.getSecretsDir()
        except (ValueError) as e:
            assert (
                "Environment variable $ROGER_SECRETS_DIR is not set." in e)
        if set_sect_dir.strip() != '':
            os.environ["ROGER_SECRETS_DIR"] = "{}".format(set_sect_dir)

    def test_getCliDir(self):
        set_cli_dir = ''
        cli_dir = self.settingObj.getCliDir()
        assert cli_dir is not None

    def tearDown(self):
        pass
예제 #44
0
    def main(self, settingObj, appObj, hooksObj, dockerUtilsObj, dockerObj, args):
        print(colored("******Building the Docker image now******", "grey"))
        try:
            function_execution_start_time = datetime.now()
            execution_result = 'SUCCESS'  # Assume the execution_result to be SUCCESS unless exception occurs
            config_dir = settingObj.getConfigDir()
            root = settingObj.getCliDir()
            config = appObj.getConfig(config_dir, args.config_file)
            hooksObj.config_file = args.config_file
            roger_env = appObj.getRogerEnv(config_dir)
            config_name = ""
            if 'name' in config:
                config_name = config['name']
            common_repo = config.get('repo', '')
            if not hasattr(args, "env"):
                args.env = "dev"
            data = appObj.getAppData(config_dir, args.config_file, args.app_name)
            if not data:
                raise ValueError("Application with name [{}] or data for it not found at {}/{}.".format(
                    args.app_name, config_dir, args.config_file))
            repo = ''
            if common_repo != '':
                repo = data.get('repo', common_repo)
            else:
                repo = data.get('repo', args.app_name)

            build_args = {}
            if 'build-args' in data:
                if 'environment' in data['build-args']:
                    if args.env in data['build-args']['environment']:
                        build_args = data['build-args']['environment'][args.env]

            projects = data.get('privateProjects', [])


            # get/update target source(s)
            file_exists = True
            file_path = ''
            cur_dir = ''
            if "PWD" in os.environ:
                cur_dir = os.environ.get('PWD')


            # This is bad code, assuming current directory and then trying to again guess, this is not rocket science
            # it's a f*****g file path, as simple as that. https://seomoz.atlassian.net/browse/ROGER-2405
            # dockerfile location possibilities
            #    1. Path relative to the repo, we know repo path for cli is <checkout_dir>/<repo>
            #    2. Absolute path
            # This path comes from config file and not passed on commandline so we should not try to prefix current
            # working directory if the relative path is passed, don't try to guess too much.
            # changelog : relative path from current directory won't work for working_directory or checkout_dir
            # changelog : working_directory or checkout_dir should be absolute path, not backward-compatible
            checkout_dir = os.path.abspath(args.directory)
            repo_name = appObj.getRepoName(repo)
            # (vmahedia) todo : this should be called docker_file_dir 
            dockerfile_rel_repo_path = data.get('path', '')
            file_path = os.path.join(checkout_dir, repo_name, dockerfile_rel_repo_path)


            if not hasattr(args, "app_name"):
                args.app_name = ""

            if not hasattr(self, "identifier"):
                self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)

            args.app_name = self.utils.extract_app_name(args.app_name)
            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "pre_build"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(args.env) + ",user="******"{} hook failed.".format(hookname))

            build_filename = 'Dockerfile'

            if 'build_filename' in data:
                build_filename = ("{0}/{1}".format(file_path, data['build_filename']))
                file_exists = os.path.exists(build_filename)
                if not file_exists:
                    raise ValueError("Specified build file: {} does not exist. Exiting build.".format(build_filename))
            else:
                file_exists = os.path.exists("{0}/Dockerfile".format(file_path))

            if file_exists:
                # (vmahedia) todo: We know what parameters are required for build command so we should not wait until
                # now to bailout. Config parser should have a validator for every command to see if all the Required
                # parameters are passed or not. Why do all this up to this point if we know we will fail on this.
                # RequiredParameter, below, "registry"
                if 'registry' not in roger_env:
                    raise ValueError("Registry not found in roger-mesos-tools.config file.")
                else:
                    self.registry = roger_env['registry']
                self.tag_name = args.tag_name
                image = "{0}/{1}".format(roger_env['registry'], args.tag_name)
                try:
                    if checkout_dir == args.directory:
                        try:
                            dockerObj.docker_build(
                                dockerUtilsObj, appObj, args.directory, repo, projects, dockerfile_rel_repo_path, image, build_args, args.verbose, build_filename)
                        except ValueError:
                            raise ValueError("Docker build failed")
                    else:
                        directory = '{0}/{1}'.format(cur_dir, args.directory)
                        try:
                            dockerObj.docker_build(
                                dockerUtilsObj, appObj, directory, repo, projects, dockerfile_rel_repo_path, image, build_args, args.verbose, build_filename)
                        except ValueError:
                            print('Docker build failed.')
                            raise
                    print(colored("******Successfully built Docker image******", "green"))
                    build_message = "Image [{}]".format(image)
                    if(args.push):
                        print(colored("******Pushing Docker image to registry******", "grey"))
                        exit_code = dockerUtilsObj.docker_push(image, args.verbose)
                        if exit_code != 0:
                            raise ValueError(
                                'Docker push failed.')
                        build_message += " successfully pushed to registry [{}]*******".format(roger_env[
                                                                             'registry'])
                    print(colored(build_message, "green"))
                except (IOError) as e:
                    printException(e)
                    raise
            else:
                print(colored("Dockerfile does not exist in dir: {}".format(file_path), "red"))

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "post_build"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(args.env) + ",user="******""

                if not hasattr(args, "env"):
                    args.env = "dev"

                if not hasattr(args, "app_name"):
                    args.app_name = ""

                if 'settingObj' not in globals() and 'settingObj' not in locals():
                    settingObj = Settings()

                if 'execution_result' is 'FAILURE':
                    self.outcome = 0

                sc = self.utils.getStatsClient()
                if not hasattr(self, "identifier"):
                    self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)
                time_take_milliseonds = ((datetime.now() - function_execution_start_time).total_seconds() * 1000)
                input_metric = "roger-tools.rogeros_tools_exec_time," + "app_name=" + str(args.app_name) + ",event=build" + ",identifier=" + str(self.identifier) + ",outcome=" + str(execution_result) + ",config_name=" + str(config_name) + ",env=" + str(args.env) + ",user=" + str(settingObj.getUser())
                tup = (input_metric, time_take_milliseonds)
                self.statsd_message_list.append(tup)
            except (Exception) as e:
                printException(e)
                raise
예제 #45
0
class Testcontainer(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--env', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push', '-s', help="Don't push. Only generate components. Defaults to false.", action="store_true")
        parser.add_argument('--secrets-file', '-S',
                            help="Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        with open(self.configs_dir + '/roger_single_container_var_tests.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['container-vars']
        self.config = config
        self.roger_env = roger_env
        self.data = data

    def test_vars_single_container(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(
            self.base_dir + "/tests/components")
        when(settings).getSecretsDir().thenReturn(
            self.base_dir + "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(
            self.base_dir + "/tests/templates")

        when(roger_push.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(self.configs_dir,
                                  "roger_single_container_var_tests.json").thenReturn(config)

        when(appConfig).getAppData(self.configs_dir,
                                   "roger_single_container_var_tests.json", "container-vars").thenReturn(data)

        args = self.args
        args.env = "dev"
        args.skip_push = True
        args.secrets_dir = ""

        args.app_name = 'container-vars'
        args.config_file = 'roger_single_container_var_tests.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'tests/v0.1.0'
        args.secrets_file = 'test'
        args.verbose = False

        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)

        with open(self.base_dir + '/tests/components/dev/roger-single-container-var-tests.json') as output:
            output = json.load(output)

        var1 = output["env"]["VAR_1"]
        var3 = output["env"]["VAR_3"]
        var4 = output["env"]["VAR_4"]

        print ("Expected Value -> Var1 : environment_value_1")
        print ("Actual Value   : {}".format(var1))

        print ("Expected Value -> Var3 : value_3")
        print ("Actual Value   : {}".format(var3))

        assert var3 == "value_3"
        assert var1 == "environment_value_1"

    def tearDown(self):
        pass
예제 #46
0
class TestPush(unittest.TestCase):
    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument(
            '-e',
            '--env',
            metavar='env',
            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument(
            '--skip-push',
            '-s',
            help="Don't push. Only generate components. Defaults to false.",
            action="store_true")
        parser.add_argument(
            '--secrets-file',
            '-S',
            help=
            "Specify an optional secrets file for deploy runtime variables.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'

        config = {
            u'repo': u'roger',
            u'act-as': u'test',
            u'notifications': {
                u'username': u'Roger Deploy',
                u'method': u'chat.postMessage',
                u'channel': u'Channel ID',
                u'emoji': u':rocket:'
            },
            u'apps': {
                u'test_app': {
                    u'imageBase': u'test_app_base',
                    u'name': u'test_app',
                    u'containers': [u'container_name1', u'container_name2']
                },
                u'test_app1': {
                    u'vars': {
                        u'global': {
                            u'env_value1': u'12',
                            u'env_value2': u'16'
                        },
                        u'environment': {
                            u'test': {
                                u'env_value1': u'20',
                                u'env_value2': u'24'
                            }
                        }
                    },
                    u'framework': u'chronos',
                    u'name': u'test_app1',
                    u'containers': [u'container_name1', u'container_name2'],
                    u'imageBase': u'test_app_base'
                },
                u'grafana_test_app': {
                    u'imageBase':
                    u'test_app_base',
                    u'name':
                    u'test_app_grafana',
                    u'containers': [
                        u'grafana', {
                            u'grafana1': {
                                u'vars': {
                                    u'environment': {
                                        u'prod': {
                                            u'mem': u'2048',
                                            u'cpus': u'2'
                                        },
                                        u'dev': {
                                            u'mem': u'512',
                                            u'cpus': u'0.5'
                                        },
                                        u'test': {
                                            u'env_value1': u'64',
                                            u'env_value2': u'128'
                                        }
                                    },
                                    u'global': {
                                        u'mem': u'128',
                                        u'cpus': u'0.1',
                                        u'env_value1': u'30',
                                        u'env_value2': u'54'
                                    }
                                }
                            }
                        }, {
                            u'grafana2': {
                                u'vars': {
                                    u'environment': {
                                        u'prod': {
                                            u'mem': u'2048',
                                            u'cpus': u'2'
                                        },
                                        u'dev': {
                                            u'mem': u'1024',
                                            u'cpus': u'1'
                                        }
                                    },
                                    u'global': {
                                        u'mem': u'128',
                                        u'cpus': u'0.1'
                                    }
                                }
                            }
                        }
                    ]
                }
            },
            u'name': u'test-app',
            u'vars': {
                u'environment': {
                    u'prod': {
                        u'mem': u'2048',
                        u'cpus': u'2'
                    },
                    u'test': {
                        u'env_value1': u'4',
                        u'env_value2': u'8'
                    },
                    u'dev': {
                        u'mem': u'512',
                        u'cpus': u'1'
                    },
                    u'stage': {
                        u'mem': u'1024',
                        u'cpus': u'1'
                    }
                },
                u'global': {
                    u'instances': u'1',
                    u'network': u'BRIDGE',
                    u'env_value1': u'3',
                    u'env_value2': u'3'
                }
            }
        }

        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data
        test_config = config
        test_data = test_config['apps']['grafana_test_app']
        self.test_config = test_config
        self.test_data = test_data
        template = Template(
            '{ "env": { "ENV_VAR1": "{{ env_value1 }}", "ENV_VAR2": "{{ env_value2 }}" }}'
        )
        self.template = template
        self.additional_vars = {}

    def test_template_render_for_extra_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['grafana_test_app']
        container = app_data['containers'][1]['grafana1']
        additional_vars = self.additional_vars
        additional_vars['env_value1'] = "100"
        additional_vars['env_value2'] = "200"
        output = roger_push.renderTemplate(self.template, "test", "test_image",
                                           app_data, self.config, container,
                                           "grafana1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '100'
        assert result['env']['ENV_VAR2'] == '200'

    def test_template_render_for_config_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['test_app']
        container = "container_name1"
        output = roger_push.renderTemplate(self.template, "test", "test_image",
                                           app_data, self.config, container,
                                           "container_name1",
                                           self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '4'
        assert result['env']['ENV_VAR2'] == '8'

    def test_template_render_for_app_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['test_app1']
        container = "container_name1"
        # Passing environment that doesn't exist
        output = roger_push.renderTemplate(self.template, "non_existing_env",
                                           "test_image", app_data, self.config,
                                           container, "container_name1",
                                           self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '12'
        assert result['env']['ENV_VAR2'] == '16'
        # Existing environment
        output = roger_push.renderTemplate(self.template, "test", "test_image",
                                           app_data, self.config, container,
                                           "container_name1",
                                           self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '20'
        assert result['env']['ENV_VAR2'] == '24'

    def test_template_render_for_container_level_variables(self):
        roger_push = RogerPush()
        app_data = self.config['apps']['grafana_test_app']
        container = "grafana"
        # Passing environment that doesn't exist
        output = roger_push.renderTemplate(self.template, "non_existing_env",
                                           "test_image", app_data, self.config,
                                           container, "grafana",
                                           self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '3'
        assert result['env']['ENV_VAR2'] == '3'
        # Existing environment
        output = roger_push.renderTemplate(self.template, "test", "test_image",
                                           app_data, self.config, container,
                                           "grafana", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '4'
        assert result['env']['ENV_VAR2'] == '8'
        container = app_data['containers'][1]['grafana1']
        output = roger_push.renderTemplate(self.template, "non_existing_env",
                                           "test_image", app_data, self.config,
                                           container, "grafana1",
                                           self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '30'
        assert result['env']['ENV_VAR2'] == '54'
        output = roger_push.renderTemplate(self.template, "test", "test_image",
                                           app_data, self.config, container,
                                           "grafana1", self.additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '64'
        assert result['env']['ENV_VAR2'] == '128'

    def test_roger_push_grafana_test_app(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).put(any(), any(),
                           any()).thenReturn("Response [200]", any())
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        with open(self.base_dir +
                  '/tests/templates/test-app-grafana.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        verify(settings).getConfigDir()
        verify(settings).getComponentsDir()
        verify(settings).getTemplatesDir()
        verify(settings).getSecretsDir()
        verify(frameworkUtils).getFramework(data)

    def test_container_resolution(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.test_config
        data = self.test_data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.directory = self.base_dir + '/tests/testrepo'
        args.config_file = 'test.json'
        args.image_name = 'grafana/grafana:2.1.3'
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        with open(self.base_dir +
                  '/tests/templates/test-app-grafana.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 2
        assert output['mem'] == 1024
        assert output['uris'] == ["abc", "xyz", "$ENV_VAR"]
        with open(self.base_dir +
                  '/tests/templates/test-app-grafana1.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 0.5
        assert output['mem'] == 512
        with open(self.base_dir +
                  '/tests/templates/test-app-grafana2.json') as output:
            output = json.load(output)
        assert output['container']['docker'][
            'image'] == "grafana/grafana:2.1.3"
        assert output['cpus'] == 1
        assert output['mem'] == 1024

    def test_roger_push_with_no_app_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        sc = mock(StatsClient)

        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.app_name = ''
        args.config_file = 'test.json'
        args.env = 'some_test_env'
        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_roger_push_with_no_registry_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'

        config_dir = settings.getConfigDir()
        roger_env = appConfig.getRogerEnv(config_dir)
        # Remove registry key from dictionary
        del roger_env['registry']
        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_roger_push_with_no_environment_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        # Set environment variable as None
        args.env = ''
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_unresolved_jinja_variable_fails(self):

        with open(self.configs_dir +
                  '/roger_push_unresolved_jinja.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['container-vars']
        self.config = config
        self.roger_env = roger_env
        self.data = data

        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).get_version().thenReturn(any())

        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(
            self.configs_dir,
            "roger_push_unresolved_jinja.json").thenReturn(config)
        when(appConfig).getAppData(self.configs_dir,
                                   "roger_push_unresolved_jinja.json",
                                   "container-vars").thenReturn(data)

        args = self.args
        args.env = "dev"
        args.skip_push = False
        args.force_push = False
        args.secrets_dir = ""
        args.secrets_file = ""
        args.app_name = 'container-vars'
        args.config_file = 'roger_push_unresolved_jinja.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'tests/v0.1.0'

        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        verify(frameworkUtils, times=0).put(any(), any(), any(), any())

    def test_roger_push_calls_prepush_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("pre_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.env = 'dev'
        args.image_name = 'tests/v0.1.0'
        args.directory = '/tmp'
        args.secrets_file = ""
        args.skip_push = True
        return_code = roger_push.main(settings, appConfig, frameworkUtils,
                                      mockedHooks, args)
        verify(mockedHooks).run_hook("pre_push", any(), any(), any())

    def test_roger_push_calls_postpush_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("post_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.env = 'dev'
        args.image_name = 'tests/v0.1.0'
        args.directory = '/tmp'
        args.secrets_file = ""
        args.skip_push = True
        return_code = roger_push.main(settings, appConfig, frameworkUtils,
                                      mockedHooks, args)
        verify(mockedHooks).run_hook("post_push", any(), any(), any())

    def test_roger_push_verify_default_env_use(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        frameworkUtils = mock(FrameworkUtils)
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = None
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        roger_env["default_environment"] = "test_env"

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_roger_push_with_incorrect_container_name(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        appdata = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        frameworkUtils = mock(FrameworkUtils)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app:test'
        args.config_file = 'test.json'

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_roger_push_with_correct_container_name(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        frameworkUtils = mock(FrameworkUtils)
        roger_env = self.roger_env
        appdata = self.data
        config = self.config
        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(frameworkUtils).getFramework(any()).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)

        appdata["hooks"] = dict([("post_push", "some command")])
        when(appConfig).getAppData(any(), any(), any()).thenReturn(appdata)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app:grafana'
        args.config_file = 'test.json'
        args.image_name = 'grafana/grafana:2.1.3'

        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)
        verify(frameworkUtils).getFramework(any())

    def test_roger_push_env_from_ROGER_ENV_VAR(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        frameworkUtils = mock(FrameworkUtils)
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(mockedHooks).run_hook(any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)

        args = self.args
        args.env = None
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        roger_env["default_environment"] = None
        # Setting ROGER_ENV to specific value
        os.environ["ROGER_ENV"] = "test_env"

        with self.assertRaises(ValueError):
            roger_push.main(settings, appConfig, frameworkUtils, mockedHooks,
                            args)

    def test_push_happens_with_validation_error_when_force_push_set(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any(), any(),
                           any()).thenReturn(["Response [200]",
                                              any()])
        when(marathon).runDeploymentChecks(any(), any()).thenReturn(True)
        frameworkUtils = mock(FrameworkUtils)
        frameworkUtils = mock(FrameworkUtils)

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).modify_task_id(any()).thenReturn([any()])
        when(roger_push.utils).get_version().thenReturn(any())

        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = False
        args.force_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        roger_push.main(settings, appConfig, frameworkUtils, mockedHooks, args)

    def test_roger_push_skip_push_set(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).put(any(), any(),
                           any()).thenReturn(["Response [200]",
                                              any()])
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'

        return_code = roger_push.main(settings, appConfig, frameworkUtils,
                                      mockedHooks, args)
        verify(frameworkUtils, times=0).runDeploymentChecks(any(), any())

    def test_push_fails_with_validation_error(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")
        when(roger_push.utils).modify_task_id(any()).thenReturn([any()])
        when(roger_push.utils).get_version().thenReturn(any())

        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(marathon).put(any(), any(), any(), any(),
                           any()).thenReturn(["Response [200]",
                                              any()])
        when(marathon).runDeploymentChecks(any(), any()).thenReturn(True)
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)
        when(appConfig).getRogerEnv(self.configs_dir).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = False
        args.force_push = False
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        return_code = roger_push.main(settings, appConfig, frameworkUtils,
                                      mockedHooks, args)
        verify(frameworkUtils, times=0).put(any(), any(), any(), any())

    def test_roger_push_secrets_replaced(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_push = RogerPush()
        roger_push.utils = mock(Utils)
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        sc = mock(StatsClient)
        when(sc).timing(any(), any()).thenReturn(any())
        when(roger_push.utils).getStatsClient().thenReturn(sc)
        when(roger_push.utils).get_identifier(any(), any(),
                                              any()).thenReturn(any())
        when(roger_push.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).put(any(), any(), any()).thenReturn("Response [200]")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(marathon).getName().thenReturn('Marathon')
        when(settings).getComponentsDir().thenReturn(self.base_dir +
                                                     "/tests/components")
        when(settings).getSecretsDir().thenReturn(self.base_dir +
                                                  "/tests/secrets")
        when(settings).getTemplatesDir().thenReturn(self.base_dir +
                                                    "/tests/templates")
        when(settings).getConfigDir().thenReturn(self.configs_dir)
        when(settings).getCliDir().thenReturn(self.base_dir)
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)
        when(mockedHooks).run_hook(any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.env = "dev"
        args.secrets_file = ""
        args.skip_push = True
        args.app_name = 'grafana_test_app'
        args.config_file = 'test.json'
        args.directory = self.base_dir + '/tests/testrepo'
        args.image_name = 'grafana/grafana:2.1.3'
        exit_code = roger_push.main(settings, appConfig, frameworkUtils,
                                    mockedHooks, args)
        file_path = ("{0}/{1}/{2}".format(self.components_dir, args.env,
                                          args.config_file))
        assert (os.path.isfile(file_path) is not True)

    def test_secret_vars_replacement_for_no_secrets_file(self):
        args = self.args
        args.env = "test"
        args.secrets_file = ""
        args.app_name = 'test_app'
        args.image_name = 'test_image'
        secrets_dir = self.base_dir + "/tests/secrets"
        roger_push = RogerPush()
        app_data = self.config['apps'][args.app_name]
        container = "container1"
        additional_vars = self.additional_vars
        extra_vars = {}
        extra_vars['env_value1'] = "100"
        extra_vars['env_value2'] = "200"
        additional_vars.update(extra_vars)
        secret_vars = roger_push.loadSecrets(secrets_dir, args.secrets_file,
                                             args, args.env)
        additional_vars.update(secret_vars)
        output = roger_push.renderTemplate(self.template, args.env,
                                           args.image_name, app_data,
                                           self.config, container,
                                           "container1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == '100'
        assert result['env']['ENV_VAR2'] == '200'

    def test_secret_vars_replacement_with_secrets_file(self):
        args = self.args
        args.env = "test"
        args.secrets_file = "test_app-container1.json"
        args.app_name = 'test_app'
        args.image_name = 'test_image'
        secrets_dir = self.base_dir + "/tests/secrets"
        roger_push = RogerPush()
        app_data = self.config['apps'][args.app_name]
        container = "container1"
        additional_vars = self.additional_vars
        extra_vars = {}
        extra_vars['env_value1'] = "100"
        extra_vars['env_value2'] = "200"
        additional_vars.update(extra_vars)
        secret_vars = roger_push.loadSecrets(secrets_dir, args.secrets_file,
                                             args, args.env)
        print(secret_vars)
        additional_vars.update(secret_vars)
        output = roger_push.renderTemplate(self.template, args.env,
                                           args.image_name, app_data,
                                           self.config, container,
                                           "container1", additional_vars)
        result = json.loads(output)
        assert result['env']['ENV_VAR1'] == 'test_value1'
        assert result['env']['ENV_VAR2'] == 'test_value2'

    def tearDown(self):
        pass
예제 #47
0
class TestDeploy(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('-e', '--environment', metavar='env',
                            help="Environment to deploy to. example: 'dev' or 'stage'")
        parser.add_argument('-s', '--skip-build', action="store_true",
                            help="Flag that skips roger-build when set to true. Defaults to false.'")
        parser.add_argument('-M', '--incr-major', action="store_true",
                            help="Increment major in version. Defaults to false.'")
        parser.add_argument('-p', '--incr-patch', action="store_true",
                            help="Increment patch in version. Defaults to false.'")
        parser.add_argument('-sp', '--skip-push', action="store_true",
                            help="Flag that skips roger push when set to true. Defaults to false.'")
        parser.add_argument(
            '-S', '--secrets-file', help="Specify an optional secrets file for deployment runtime variables.")
        parser.add_argument(
            '-d', '--directory', help="Specify an optional directory to pull out the repo. This is the working directory.")
        self.parser = parser
        self.args = parser

        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"

        config = {u'repo': u'roger', u'notifications': {u'username': u'Roger Deploy', u'method': u'chat.postMessage', u'channel': u'Channel ID', u'emoji': u':rocket:'},
                  u'apps': {u'test_app': {u'imageBase': u'test_app_base', u'name': u'test_app', u'containers': [u'container_name1', u'container_name2']},
                            u'test_app1': {u'framework': u'chronos', u'name': u'test_app1', u'containers': [u'container_name1', u'container_name2'], u'imageBase': u'test_app_base'},
                            u'grafana_test_app': {u'imageBase': u'test_app_base', u'name': u'test_app_grafana', u'containers': [u'grafana', {u'grafana1': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'0.5'}},
                                                                                                                                                                     u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}, {u'grafana2': {u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'1024', u'cpus': u'1'}},
                                                                                                                                                                                                                                               u'global': {u'mem': u'128', u'cpus': u'0.1'}}}}]}}, u'name': u'test-app', u'vars': {u'environment': {u'prod': {u'mem': u'2048', u'cpus': u'2'}, u'dev': {u'mem': u'512', u'cpus': u'1'},
                                                                                                                                                                                                                                                                                                                                                    u'stage': {u'mem': u'1024', u'cpus': u'1'}}, u'global': {u'instances': u'1', u'network': u'BRIDGE'}}}

        roger_env = {u'default_environment': u'dev', u'registry': u'example.com:5000', u'environments': {u'prod': {u'chronos_endpoint': u'http://prod.example.com:4400',
                                                                                                         u'marathon_endpoint': u'http://prod.example.com:8080'}, u'dev': {u'chronos_endpoint': u'http://dev.example.com:4400', u'marathon_endpoint': u'http://dev.example.com:8080'},
                                                                                                         u'stage': {u'chronos_endpoint': u'http://stage.example.com:4400', u'marathon_endpoint': u'http://stage.example.com:8080'}}}

        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data

    def test_splitVersion(self):
        roger_deploy = RogerDeploy()
        assert roger_deploy.splitVersion("0.1.0") == (0, 1, 0)
        assert roger_deploy.splitVersion("2.0013") == (2, 13, 0)

    def test_incrementVersion(self):
        git_sha = "dwqjdqgwd7y12edq21"
        image_version_list = ['0.001', '0.2.034', '1.1.2', '1.002.1']
        roger_deploy = RogerDeploy()
        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.incr_patch = True
        args.incr_major = True

        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'
        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'
        assert roger_deploy.incrementVersion(
            git_sha, image_version_list, args) == 'dwqjdqgwd7y12edq21/v2.0.0'

    def test_tempDirCheck(self):
        work_dir = "./test_dir"
        roger_deploy = RogerDeploy()
        args = self.args
        args.directory = ""
        args.skip_push = False
        args.secrets_file = ""
        temp_dir_created = True
        roger_deploy.removeDirTree(work_dir, args, temp_dir_created)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is False
        os.makedirs(work_dir)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is True
        roger_deploy.removeDirTree(work_dir, args, temp_dir_created)
        exists = os.path.exists(os.path.abspath(work_dir))
        assert exists is False

    def test_roger_deploy_with_no_app(self):
        try:
            raised_exception = False
            settings = mock(Settings)
            appConfig = mock(AppConfig)
            roger_deploy = RogerDeploy()
            marathon = mock(Marathon)
            gitObj = mock(GitUtils)
            mockedHooks = mock(Hooks)
            roger_deploy.rogerGitPullObject = mock(RogerGitPull)
            roger_deploy.rogerPushObject = mock(RogerPush)
            roger_deploy.rogerBuildObject = mock(RogerBuild)
            roger_deploy.dockerUtilsObject = mock(DockerUtils)
            roger_deploy.dockerObject = mock(Docker)
            roger_deploy.utils = mock(Utils)
            roger_env = self.roger_env
            config = self.config
            data = self.data

            when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
            when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

            when(marathon).getCurrentImageVersion(
                any(), any(), any()).thenReturn("testversion/v0.1.0")
            frameworkUtils = mock(FrameworkUtils)
            when(frameworkUtils).getFramework(data).thenReturn(marathon)
            when(settings).getConfigDir().thenReturn(any())
            when(settings).getCliDir().thenReturn(any())
            when(settings).getUser().thenReturn('test_user')
            when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
            when(appConfig).getConfig(any(), any()).thenReturn(config)
            when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

            args = self.args
            args.directory = ""
            args.secrets_file = ""
            args.environment = "dev"
            args.skip_push = False
            args.application = ''
            args.config_file = 'test.json'
            args.skip_build = True
            args.branch = None
            os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        except:
            raised_exception = True
        self.assertFalse(raised_exception)

    def test_roger_deploy_with_no_registry_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        mockedHooks = mock(Hooks)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")
        when(marathon).getCurrentImageVersion(
            any(), any(), any(), any()).thenReturn("testversion/v0.1.0")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir

        roger_env = appConfig.getRogerEnv(self.configs_dir)
        del roger_env['registry']
        with self.assertRaises(ValueError):
            roger_deploy.main(settings, appConfig,
                              frameworkUtils, gitObj, mockedHooks, args)

    def test_roger_deploy_with_no_environment_fails(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        mockedHooks = mock(Hooks)
        gitObj = mock(GitUtils)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = ""
        args.skip_push = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        args.verbose = False
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        with self.assertRaises(ValueError):
            roger_deploy.main(settings, appConfig,
                              frameworkUtils, gitObj, mockedHooks, args)

    def test_rogerDeploy(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)
        roger_env = self.roger_env
        config = self.config
        data = self.data

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = False
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.verbose = False
        args.branch = None
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(settings, times=1).getConfigDir()
        verify(settings).getCliDir()
        verify(appConfig).getRogerEnv(any())
        verify(appConfig, times=1).getConfig(any(), any())
        verify(frameworkUtils).getFramework(data)
        verify(marathon).getCurrentImageVersion(any(), any(), any())

    def test_rogerDeploy_with_skip_push(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data
        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        args.verbose = False
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(settings, times=1).getConfigDir()
        verify(settings).getCliDir()
        verify(appConfig).getRogerEnv(any())
        verify(appConfig, times=1).getConfig(any(), any())
        verify(frameworkUtils).getFramework(data)
        verify(marathon).getCurrentImageVersion(any(), any(), any())

    def test_rogerDeploy_with_skip_gitpull_false(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)
        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = False
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        args.verbose = False
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(roger_deploy.rogerGitPullObject, times=1).main(any(), any(), any(), any(), any())

    def test_rogerDeploy_with_skip_gitpull_true(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        roger_deploy = RogerDeploy()
        marathon = mock(Marathon)
        gitObj = mock(GitUtils)
        mockedHooks = mock(Hooks)
        roger_deploy.rogerGitPullObject = mock(RogerGitPull)
        roger_deploy.rogerPushObject = mock(RogerPush)
        roger_deploy.rogerBuildObject = mock(RogerBuild)
        roger_deploy.dockerUtilsObject = mock(DockerUtils)
        roger_deploy.dockerObject = mock(Docker)
        roger_deploy.utils = mock(Utils)

        roger_env = self.roger_env

        repo_name = 'roger'
        repo_url = 'test_url'
        random = 'test'

        config = self.config
        data = self.data
        when(marathon).getCurrentImageVersion(
            any(), any(), any()).thenReturn("testversion/v0.1.0")
        when(marathon).getName().thenReturn('Marathon')
        frameworkUtils = mock(FrameworkUtils)

        when(roger_deploy.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_deploy.utils).extract_app_name(any()).thenReturn("test")

        when(frameworkUtils).getFramework(data).thenReturn(marathon)
        when(settings).getConfigDir().thenReturn(any())
        when(settings).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn('test_user')
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)

        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)

        when(gitObj).gitPull(any()).thenReturn(0)
        when(gitObj).gitShallowClone(any(), any()).thenReturn(0)
        when(gitObj).gitClone(any(), any()).thenReturn(0)
        when(gitObj).getGitSha(any(), any(), any()).thenReturn(random)

        when(roger_deploy.rogerGitPullObject).main(any(), any(), any(), any(), any()).thenReturn(0)
        when(roger_deploy.rogerPushObject).main(any(), any(), any(), any(), any()).thenReturn(0)

        args = self.args
        args.directory = ""
        args.secrets_file = ""
        args.environment = "dev"
        args.skip_push = True
        args.skip_gitpull = True
        args.application = 'grafana_test_app'
        args.config_file = 'test.json'
        args.skip_build = True
        args.branch = None
        args.verbose = False
        os.environ["ROGER_CONFIG_DIR"] = self.configs_dir
        roger_deploy.main(settings, appConfig, frameworkUtils, gitObj, mockedHooks, args)
        verify(roger_deploy.rogerGitPullObject, times=0).main(any(), any(), any(), any(), any())

    def tearDown(self):
        pass
예제 #48
0
    def main(self, settings, appConfig, frameworkObject, hooksObj, args):
        print(colored("******Deploying application to framework******", "grey"))
        try:
            validation_failed = False
            settingObj = settings
            appObj = appConfig
            frameworkUtils = frameworkObject
            config_dir = settingObj.getConfigDir()
            hooksObj.config_file = args.config_file
            cur_file_path = os.path.dirname(os.path.realpath(__file__))
            config = appObj.getConfig(config_dir, args.config_file)
            config_name = ""
            act_as_user = ""
            if 'name' in config:
                config_name = config['name']
            if 'act-as' in config:
                act_as_user = config['act-as']
            roger_env = appObj.getRogerEnv(config_dir)

            if not hasattr(args, "app_name"):
                args.app_name = ""

            if 'registry' not in roger_env.keys():
                raise ValueError("Registry not found in roger-mesos-tools.config file.")
            else:
                self.registry = roger_env['registry']

            if hasattr(args, "image_name"):
                self.image_name = args.image_name

            environment = roger_env.get('default_environment', '')
            if args.env is None:
                if "ROGER_ENV" in os.environ:
                    env_var = os.environ.get('ROGER_ENV')
                    if env_var.strip() == '':
                        print(colored("WARNING - Environment variable $ROGER_ENV is not set. Using the default set from roger-mesos-tools.config file", "yellow"))
                    else:
                        if args.verbose:
                            print(colored("Using value {} from environment variable $ROGER_ENV".format(env_var), "grey"))
                        environment = env_var
            else:
                environment = args.env
            # ----------------------------------------------

            if environment not in roger_env['environments']:
                raise ValueError("Environment not found in roger-mesos-tools.config file.")

            # ----------------------------------------------
            # GetEnvironmentConfig(environment)
            # ----------------------------------------------
            environmentObj = roger_env['environments'][environment]
            common_repo = config.get('repo', '')

            # ----------------------------------------------
            # GetContainersForApp(app)
            # ----------------------------------------------
            app_name = args.app_name
            container_list = []
            # todo (vmahedia): What does ':' signify? Put explanation.
            if ':' in app_name:
                tokens = app_name.split(':')
                app_name = tokens[0]
                # todo (vmahedia): it's container list - need to explain syntax
                if ',' in tokens[1]:
                    container_list = tokens[1].split(',')
                else:
                    container_list.append(tokens[1])
            # ----------------------------------------------
            data = appObj.getAppData(config_dir, args.config_file, app_name)
            if not data:
                raise ValueError("Application with name [{}] or data for it not found at {}/{}.".format(
                    app_name, config_dir, args.config_file))

            configured_container_list = []
            for task in data['containers']:
                if type(task) == dict:
                    configured_container_list.append(task.keys()[0])
                else:
                    configured_container_list.append(task)
            if not set(container_list) <= set(configured_container_list):
                raise ValueError("List of containers [{}] passed do not match list of acceptable containers: [{}]".format(
                    container_list, configured_container_list))

            frameworkObj = frameworkUtils.getFramework(data)
            framework = frameworkObj.getName()

            repo = ''
            if common_repo != '':
                repo = data.get('repo', common_repo)
            else:
                repo = data.get('repo', app_name)

            comp_dir = settingObj.getComponentsDir()
            templ_dir = settingObj.getTemplatesDir()
            secrets_dir = settingObj.getSecretsDir()

            # Create comp_dir if it doesn't exist
            if not os.path.isdir(comp_dir):
                os.makedirs(comp_dir)

            if not container_list:
                data_containers = data['containers']
            else:
                data_containers = container_list

            failed_container_dict = {}

            # (vmahedia) upto this point it's all getting and checking the
            # configuration parameters
            template = ''
            # Required for when work_dir,component_dir,template_dir or
            # secret_env_dir is something like '.' or './temp"
            os.chdir(cur_file_path)
            app_path = ''
            if 'template_path' in data:
                app_path = self.repo_relative_path(appObj, args, repo, data['template_path'])
            else:
                app_path = templ_dir

            extra_vars = {}
            if 'extra_variables_path' in data:
                ev_path = self.repo_relative_path(appObj, args, repo, data['extra_variables_path'])
                with open(ev_path) as f:
                    extra_vars = yaml.load(f) if ev_path.lower(
                    ).endswith('.yml') else json.load(f)

            if not app_path.endswith('/'):
                app_path = app_path + '/'

            if not hasattr(self, "identifier"):
                self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)

            args.app_name = self.utils.extract_app_name(args.app_name)
            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "pre_push"
            hook_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"{} hook failed.".format(hookname))

            # ----------------------------------------------
            # (vmahedia) Figure out what the hell this loop does
            # and name it appropriately
            # it seems first part is just finding a template and Rendering
            # it against the given config, checking to see if there are errors
            # ----------------------------------------------
            # (vmahedia) Meat starts from here, probably.
            for container in data_containers:
                container_name = self.getContainerName(container)
                containerConfig = "{0}-{1}.json".format(config['name'], container_name)

                env = Environment(loader = FileSystemLoader("{}".format(app_path)), undefined = StrictUndefined)
                template_with_path = "[{}{}]".format(app_path, containerConfig)
                try:
                    template = env.get_template(containerConfig)
                except exceptions.TemplateNotFound as e:
                    raise ValueError("The template file {} does not exist".format(template_with_path))
                except Exception as e:
                    raise ValueError("Error while reading template from {} - {}".format(template_with_path, e))

                additional_vars = {}
                # (vmahedia)variables likes this should be at least visible within one
                # scroll up or down, move this code to near to context
                # Why are we getting the secrets everytime, this requires the file to be
                # present
                additional_vars.update(extra_vars)
                secret_vars = self.loadSecrets(secrets_dir, containerConfig, args, environment)
                additional_vars.update(secret_vars)

                image_path = "{0}/{1}".format(
                    roger_env['registry'], args.image_name)
                print("Rendering content from template {} for environment [{}]".format(
                    template_with_path, environment))
                try:
                    output = self.renderTemplate(template, environment, image_path, data, config, container, container_name, additional_vars)
                except exceptions.UndefinedError as e:
                    error_str = "The following Undefined Jinja variable error occurred. %s.\n" % e
                    print(colored(error_str, "red"), file=sys.stderr)
                    failed_container_dict[container_name] = error_str

                    # we are going to fail even if one of the container config is not valid but we will
                    # still go through the loop and collect all the errors before we bail out
                    validation_failed = True
                    pass
                # ----------------------------------------------
                # it seems the checks above can finish independent of the
                # following code, decouple this two parts, later when  the code
                # is well understood
                # ----------------------------------------------
                # Adding check to see if all jinja variables git resolved fot
                # the container
                if container_name not in failed_container_dict:
                    # Adding check so that not all apps try to mergeSecrets
                    try:
                        outputObj = json.loads(output)
                    except Exception as e:
                        raise ValueError("Error while loading json from {} - {}".format(template_with_path, e))

                    if '\"SECRET\"' in output:
                        output = self.mergeSecrets(output, self.loadSecrets(
                            secrets_dir, containerConfig, args, environment))
                    if output != "StandardError":
                        try:
                            comp_exists = os.path.exists("{0}".format(comp_dir))
                            if comp_exists is False:
                                os.makedirs("{0}".format(comp_dir))
                            comp_env_exists = os.path.exists(
                                "{0}/{1}".format(comp_dir, environment))
                            if comp_env_exists is False:
                                os.makedirs(
                                    "{0}/{1}".format(comp_dir, environment))
                        except Exception as e:
                            logging.error(traceback.format_exc())
                        # (vmahedia) Should we write out the files even though there is an error with one of the
                        # containers. Although maybe users would want to see some output
                        with open("{0}/{1}/{2}".format(comp_dir, environment, containerConfig), 'wb') as fh:
                            fh.write(output)
                    else:
                        raise ValueError("Error while loading secrets to render template file variables")

            # Notify container error messages
            # let failed_container_dict just be for now, but report all the errors
            if validation_failed:
                raise Exception("Unable to render Jinja template")

            deployment_check_failed = False
            # fail if the deployment check fails

            for container in data_containers:
                container_name = self.getContainerName(container)
                containerConfig = "{0}-{1}.json".format(config['name'], container_name)
                config_file_path = "{0}/{1}/{2}".format(comp_dir, environment, containerConfig)
                result = frameworkObj.runDeploymentChecks(config_file_path, environment)
                if not result:
                    # need to give more indication about what can they do to fix this and what exactly failed
                    # in the deployment check function, we should print an error in that function as well
                    print(colored("Deployment checks failed for container - {}".format(framework, container)), "red")
                    deployment_check_failed = True

            if deployment_check_failed:
                raise Exception("Deployment Check failed for one or more containers, check logs for more info!")

            if args.skip_push:
                print(colored("Skipping push to {} framework. The rendered config file(s) are under {}/{}/".format(
                    framework, colored(comp_dir, "cyan"), colored(environment, "cyan")), "yellow"))
            else:
                # push to roger framework
                if 'owner' in config:
                    frameworkObj.act_as_user = config['owner']

                tools_version_value = self.utils.get_version()
                image_name = self.registry + "/" + args.image_name
                image_tag_value = urllib.quote("'" + image_name + "'")

                for container in data_containers:
                    try:
                        function_execution_start_time = datetime.now()
                        # Assume SUCCESS unless exception
                        execution_result = 'SUCCESS'
                        sc = self.utils.getStatsClient()
                    except (Exception) as e:
                        raise ValueError("{} Error : {}".format(getDebugInfo(), e))
                    try:
                        # this is where actual push is happening
                        # we only push if forced, in case of failures
                        # in deployment checks
                        #
                        # (vmahedia) todo:
                        # list down scenarios in which this features
                        # will be useful
                        resp, task_id = frameworkObj.put(config_file_path, environmentObj,
                                                         container_name, environment, act_as_user)
                        container_task_id = self.utils.modify_task_id(task_id)
                        self.task_id.extend(container_task_id)
                        if hasattr(resp, "status_code"):
                            status_code = resp.status_code
                    except (Exception) as e:
                        print("ERROR - : %s" %
                              e, file=sys.stderr)
                        execution_result = 'FAILURE'
                        raise
                    finally:
                        try:
                            if 'function_execution_start_time' not in globals() and \
                               'function_execution_start_time' not in locals():
                                function_execution_start_time = datetime.now()

                            if 'execution_result' not in globals() and \
                               'execution_result' not in locals():
                                execution_result = 'FAILURE'

                            if 'config_name' not in globals() and \
                               'config_name' not in locals():
                                config_name = ""

                            if 'environment' not in globals() and \
                               'environment' not in locals():
                                environment = "dev"

                            if 'container_name' not in globals() and \
                               'container_name' not in locals():
                                container_name = ""

                            if 'status_code' not in globals() and \
                               'status_code' not in locals():
                                status_code = "500"

                            if not hasattr(args, "app_name"):
                                args.app_name = ""

                            if 'settingObj' not in globals() and \
                               'settingObj' not in locals():
                                settingObj = Settings()

                            if 'container_task_id' not in globals() and 'container_task_id' not in locals():
                                container_task_id = []

                            if not hasattr(self, "identifier"):
                                self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(),
                                                                            args.app_name)

                            if not str(status_code).startswith("20"):
                                execution_result = 'FAILURE'
                                self.outcome = 0

                            time_taken = (datetime.now() - function_execution_start_time).total_seconds()
                            for task_id in container_task_id:
                                input_metric = "roger-tools.rogeros_tools_exec_time" + \
                                               ",app_name=" + str(args.app_name) + \
                                               ",event=push" + \
                                               ",container_name=" + str(container_name) + \
                                               ",identifier=" + str(self.identifier) + \
                                               ",outcome=" + str(execution_result) + \
                                               ",response_code=" + str(status_code) + \
                                               ",config_name=" + str(config_name) + \
                                               ",env=" + str(environment) + \
                                               ",user="******"20"):
                                    metric = input_metric.replace("rogeros_tools_exec_time", "rogeros_events")
                                    metric = metric + ",source=tools" + ",task_id=" + task_id
                                    self.statsd_counter_logging(metric)

                        except (Exception) as e:
                            printException(e)
                            raise

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "post_push"
            hook_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + \
                                                                         ",app_name=" + str(args.app_name) + \
                                                                         ",identifier=" + str(self.identifier) + \
                                                                         ",config_name=" + str(config_name) + \
                                                                         ",env=" + str(environment) + \
                                                                         ",user="******"{} hook failed.".format(hookname))
            print(colored("******Done with the PUSH step******", "green"))

        except (Exception) as e:
            raise ValueError("ERROR - {}".format(e))
예제 #49
0
 def setUp(self):
     self.appObj = AppConfig()
     self.settingObj = Settings()
     self.base_dir = self.settingObj.getCliDir()
     self.configs_dir = self.base_dir + "/tests/configs"
예제 #50
0
class TestBuild(unittest.TestCase):

    def setUp(self):
        parser = argparse.ArgumentParser(description='Args for test')
        parser.add_argument('app_name', metavar='app_name',
                            help="application to build. Example: 'agora'.")
        parser.add_argument('env', metavar='env',
                            help="environment. Example: 'test'.")
        parser.add_argument('directory', metavar='directory',
                            help="working directory. Example: '/home/vagrant/work_dir'.")
        parser.add_argument('tag_name', metavar='tag_name',
                            help="tag for the built image. Example: 'roger-collectd:0.20'.")
        parser.add_argument('config_file', metavar='config_file',
                            help="configuration file to use. Example: 'content.json'.")
        parser.add_argument(
            '--push', '-p', help="Also push to registry. Defaults to false.", action="store_true")

        self.parser = parser
        self.args = self.parser
        self.settingObj = Settings()
        self.base_dir = self.settingObj.getCliDir()
        self.configs_dir = self.base_dir + "/tests/configs"
        self.components_dir = self.base_dir + '/tests/components/dev'
        with open(self.configs_dir + '/app.json') as config:
            config = json.load(config)
        with open(self.configs_dir + '/roger-mesos-tools.config') as roger:
            roger_env = yaml.load(roger)
        data = config['apps']['grafana_test_app']
        self.config = config
        self.roger_env = roger_env
        self.data = data

    def test_roger_build(self):
        try:
            settings = mock(Settings)
            appConfig = mock(AppConfig)
            dockerUtilsObj = mock(DockerUtils)
            dockerObj = mock(Docker)
            roger_build = RogerBuild()
            roger_build.utils = mock(Utils)
            mockedHooks = mock(Hooks)
            roger_env = self.roger_env
            config = self.config
            data = self.data
            repo_name = 'test'
            repo_url = 'test.com'
            raised_exception = False
            when(roger_build.utils).get_identifier(any(), any(), any()).thenReturn(any())
            when(roger_build.utils).extract_app_name(any()).thenReturn(any())
            when(settings, strict=False).getConfigDir().thenReturn(any())
            when(settings, strict=False).getCliDir().thenReturn(any())
            when(settings).getUser().thenReturn(any())
            when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
            when(appConfig).getConfig(any(), any()).thenReturn(config)
            when(appConfig).getAppData(any(), any(), any()).thenReturn(data)

            when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
            when(appConfig).getRepoName(any()).thenReturn(repo_name)

            when(mockedHooks, strict=False).run_hook(any(), any(), any(), any()).thenReturn(0)

            args = self.args
            # Setting app_name as empty
            args.app_name = ''
            args.env = 'test'
            args.config_file = 'app.json'
            args.directory = self.base_dir

            # with self.assertRaises(ValueError):
            roger_build.main(settings, appConfig, mockedHooks,
                             dockerUtilsObj, dockerObj, args)
        except:
            raised_exception = True
        self.assertFalse(raised_exception)

    def test_roger_build_calls_prebuild_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        dockerUtilsObj = mock(DockerUtils)
        dockerObj = mock(Docker)
        roger_build = RogerBuild()
        roger_build.utils = mock(Utils)
        mockedHooks = mock(Hooks)
        roger_env = {}
        roger_env["registry"] = "any registry"
        appdata = {}
        appdata["hooks"] = dict([("pre_build", "some command")])
        config = self.config
        args = self.args
        args.config_file = 'any.json'
        args.app_name = 'any app'
        args.env = 'test'
        args.directory = '/tmp'
        data = self.data
        repo_name = 'test'
        repo_url = 'test.com'
        when(roger_build.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_build.utils).extract_app_name(any()).thenReturn("any app")
        when(settings, strict=False).getConfigDir().thenReturn(any())
        when(settings, strict=False).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)
        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        return_code = roger_build.main(
            settings, appConfig, mockedHooks, dockerUtilsObj, dockerObj, args)
        verify(mockedHooks).run_hook("pre_build", any(), any(), any())

    def test_roger_build_calls_postbuild_hook_when_present(self):
        settings = mock(Settings)
        appConfig = mock(AppConfig)
        dockerUtilsObj = mock(DockerUtils)
        dockerObj = mock(Docker)
        roger_build = RogerBuild()
        roger_build.utils = mock(Utils)
        mockedHooks = mock(Hooks)
        roger_env = {}
        roger_env["registry"] = "any registry"
        appdata = {}
        appdata["hooks"] = dict([("post_build", "some command")])
        config = self.config
        args = self.args
        args.app_name = 'any app'
        args.env = 'test'
        args.directory = '/tmp'
        args.config_file = 'any.json'
        data = self.data
        repo_name = 'test'
        repo_url = 'test.com'

        when(roger_build.utils).get_identifier(any(), any(), any()).thenReturn(any())
        when(roger_build.utils).extract_app_name(any()).thenReturn("any app")
        when(settings, strict=False).getConfigDir().thenReturn(any())
        when(settings, strict=False).getCliDir().thenReturn(any())
        when(settings).getUser().thenReturn(any())
        when(appConfig).getRogerEnv(any()).thenReturn(roger_env)
        when(appConfig).getConfig(any(), any()).thenReturn(config)
        when(appConfig).getAppData(any(), any(), any()).thenReturn(data)
        when(appConfig).getRepoUrl(any()).thenReturn(repo_name)
        when(appConfig).getRepoName(any()).thenReturn(repo_name)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)
        when(mockedHooks).run_hook(any(), any(), any(), any(), any(), any()).thenReturn(0)

        return_code = roger_build.main(
            settings, appConfig, mockedHooks, dockerUtilsObj, dockerObj, args)
        verify(mockedHooks).run_hook("post_build", any(), any(), any())

    def tearDown(self):
        pass
예제 #51
0
    def main(self, settings, appConfig, gitObject, hooksObj, args):
        print(colored("******Executing GIT PULL of application repo******", "grey"))
        try:
            function_execution_start_time = datetime.now()
            execution_result = 'SUCCESS'  # Assume the execution_result to be SUCCESS unless exception occurs
            environment = "dev"
            if hasattr(args, "environment"):
                environment = args.environment
            settingObj = settings
            appObj = appConfig
            gitObj = gitObject
            config_dir = settingObj.getConfigDir()
            hooksObj.config_file = args.config_file
            config = appObj.getConfig(config_dir, args.config_file)
            config_name = ""
            if 'name' in config:
                config_name = config['name']
            common_repo = config.get('repo', '')
            data = appObj.getAppData(config_dir, args.config_file, args.app_name)
            if not data:
                raise ValueError("Application with name [{}] or data for it not found at {}/{}.".format(
                    args.app_name, config_dir, args.config_file))
            repo = ''
            if common_repo != '':
                repo = data.get('repo', common_repo)
            else:
                repo = data.get('repo', args.app_name)

            branch = "master"  # master by default
            if args.branch is not None:
                branch = args.branch

            if not os.path.exists(args.directory):
                try:
                    os.makedirs(args.directory)
                except OSError as exception:
                    if exception.errno != errno.EEXIST:
                        raise

            if not hasattr(args, "app_name"):
                args.app_name = ""

            if not hasattr(self, "identifier"):
                self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)

            args.app_name = self.utils.extract_app_name(args.app_name)

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "pre_gitpull"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"{} hook failed.".format(hookname))

            # get/update target source(s)
            repo_name = appObj.getRepoName(repo)
            path = "{0}/{1}".format(args.directory, repo_name)
            if os.path.isdir(path):
                with chdir(path):
                    exit_code = gitObj.gitPull(branch, args.verbose)
            else:
                with chdir('{0}'.format(args.directory)):
                    exit_code = gitObj.gitShallowClone(repo, branch, args.verbose)

            if exit_code != 0:
                raise ValueError("Gitpull failed.")

            hooksObj.statsd_message_list = self.statsd_message_list
            hookname = "post_gitpull"
            hookname_input_metric = "roger-tools.rogeros_tools_exec_time," + "event=" + hookname + ",app_name=" + str(args.app_name) + ",identifier=" + str(self.identifier) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"{} hook failed.".format(hookname))
        except (Exception) as e:
            printException(e)
            execution_result = 'FAILURE'
            raise
        finally:
            try:
                # If the gitpull fails before going through any steps
                if 'function_execution_start_time' not in globals() and 'function_execution_start_time' not in locals():
                    function_execution_start_time = datetime.now()

                if 'execution_result' not in globals() and 'execution_result' not in locals():
                    execution_result = 'FAILURE'

                if 'config_name' not in globals() and 'config_name' not in locals():
                    config_name = ""

                if 'environment' not in globals() and 'environment' not in locals():
                    environment = "dev"

                if not hasattr(args, "app_name"):
                    args.app_name = ""

                if 'settingObj' not in globals() and 'settingObj' not in locals():
                    settingObj = Settings()

                if 'execution_result' is 'FAILURE':
                    self.outcome = 0

                sc = self.utils.getStatsClient()
                if not hasattr(self, "identifier"):
                    self.identifier = self.utils.get_identifier(config_name, settingObj.getUser(), args.app_name)
                time_take_milliseonds = ((datetime.now() - function_execution_start_time).total_seconds() * 1000)
                input_metric = "roger-tools.rogeros_tools_exec_time," + "app_name=" + str(args.app_name) + ",event=gitpull" + ",identifier=" + str(self.identifier) + ",outcome=" + str(execution_result) + ",config_name=" + str(config_name) + ",env=" + str(environment) + ",user="******"ERROR - %s" %
                      e, file=sys.stderr)
                raise
            print(colored("******Completed the GIT PULL step successfully******", "green"))