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