def test_config_is_empty(self): settings.CONFIG_FILE = 'tests/fixtures/empty_config.yaml' with self.assertRaises(RuntimeError) as context: config.load_context_section('section') self.assertTrue( 'Config file "tests/fixtures/empty_config.yaml" is empty' in str( context.exception))
def test_env_var_in_include_2_levels_dont_set(self): settings.CONFIG_FILE = 'tests/fixtures/config_with_include_and_env_vars.yaml' settings.GET_ENVIRON_STRICT = True with self.assertRaises(RuntimeError): config.load_context_section('section-1') settings.GET_ENVIRON_STRICT = False
def test_env_var_in_section2_dont_set(self): settings.CONFIG_FILE = 'tests/fixtures/config_with_env_vars.yaml' settings.GET_ENVIRON_STRICT = True with self.assertRaises(RuntimeError) as context: config.load_context_section('section-2') settings.GET_ENVIRON_STRICT = False self.assertTrue('Environment variable "SECTION2" is not set' in str(context.exception))
def test_check_empty_var(self): settings.CONFIG_FILE = 'tests/fixtures/config.yaml' settings.GET_ENVIRON_STRICT = True with self.assertRaises(RuntimeError) as context: config.load_context_section('deployment') settings.GET_ENVIRON_STRICT = False self.assertTrue('Environment variable "EMPTY_ENV" is not set' in str(context.exception))
def test_dashes_in_var_names(self): settings.TEMPLATES_DIR = 'templates_tests' settings.CONFIG_FILE = 'tests/fixtures/dashes_config.yaml' with self.assertRaises(RuntimeError) as context: config.load_context_section('section') self.assertTrue('Variable names should never include dashes, ' 'check your vars, please: my-nested-var, my-var, your-var' in str(context.exception), context.exception)
def test_dashes_in_var_names(self): settings.TEMPLATES_DIR = 'templates_tests' settings.CONFIG_FILE = 'tests/fixtures/dashes_config.yaml' with self.assertRaises(RuntimeError) as context: config.load_context_section('not_allowed') self.assertTrue('Root variable names should never include dashes, ' 'check your vars please: my-var, my-var-with-dashes' in str(context.exception), context.exception) c = config.load_context_section('allowed') self.assertEqual(c.get('var').get('router').get('your'), 2)
def test_filters(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) context = config.load_context_section('test_filters') r.generate_by_context(context) result = '{}/filters.yaml'.format(settings.TEMP_DIR) with open(result, 'r') as f: actual = yaml.safe_load(f) self.assertEqual('aGVsbG8gd29ybGQ=', actual.get('b64encode')) self.assertEqual('k8s-handle', actual.get('b64decode')) self.assertEqual( '8fae6dd899aace000fd494fd6d795e26e2c85bf8e59d4262ef56b03dc91e924c', actual.get('sha256')) affinity = [{ 'effect': 'NoSchedule', 'key': 'dedicated', 'operator': 'Equal', 'value': 'monitoring' }, { 'effect': 'NoSchedule', 'key': 'dedicated', 'operator': 'Equal', 'value': { 'hello': 'world' } }] self.assertEqual(affinity, actual.get('affinity'))
def test_merge_section_options(self): settings.TEMPLATES_DIR = 'templates_tests' c = config.load_context_section('test_dirs') self.assertEqual(c['my_var'], 'my_value') self.assertEqual(c['my_env_var'], 'My value') self.assertEqual(c['my_file'], {'ha_ha': 'included_var'}) self.assertTrue(c['dirs'])
def test_no_templates_in_kubectl(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) with self.assertRaises(RuntimeError) as context: r.generate_by_context(config.load_context_section('no_templates')) self.assertTrue('Templates section doesn\'t have any template items' in str(context.exception))
def test_generate_templates(self): r = templating.Renderer(os.path.join(os.path.dirname(__file__), 'templates_tests')) context = config.load_context_section('test_dirs') r.generate_by_context(context) file_path_1 = '{}/template1.yaml'.format(settings.TEMP_DIR) file_path_2 = '{}/template2.yaml'.format(settings.TEMP_DIR) file_path_3 = '{}/template3.yaml'.format(settings.TEMP_DIR) file_path_4 = '{}/innerdir/template1.yaml'.format(settings.TEMP_DIR) file_path_5 = '{}/template_include_file.yaml'.format(settings.TEMP_DIR) self.assertTrue(os.path.exists(file_path_1)) self.assertTrue(os.path.exists(file_path_2)) self.assertTrue(os.path.exists(file_path_3)) with open(file_path_1, 'r') as f: content = f.read() self.assertEqual(content, "{'ha_ha': 'included_var'}") with open(file_path_2, 'r') as f: content = f.read() self.assertEqual(content, 'TXkgdmFsdWU=') with open(file_path_3, 'r') as f: content = f.read() self.assertEqual(content, 'My value') with open(file_path_4, 'r') as f: content = f.read() self.assertEqual(content, "{'ha_ha': 'included_var'}") with open(file_path_5, 'r') as f: content = f.read() self.assertEqual(content, "test: |\n {{ hello world1 }}\n\n {{ hello world }}\n new\n line")
def handler_render(args): context = config.load_context_section(args.get('section')) templating.Renderer( settings.TEMPLATES_DIR, args.get('tags'), args.get('skip_tags') ).generate_by_context(context)
def test_recursive_vars(self): settings.TEMPLATES_DIR = 'templates_tests' c = config.load_context_section('test_recursive_vars') self.assertEqual({'router': { 'my': 'var', 'my1': 'var1', 'your': 2 }}, c['var'])
def test_render_not_existent_template(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) with self.assertRaises(TemplateRenderingError) as context: r.generate_by_context( config.load_context_section('not_existent_template')) self.assertTrue('doesnotexist.yaml.j2' in str(context.exception), context.exception)
def test_io_2709(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) with self.assertRaises(TemplateRenderingError) as context: c = config.load_context_section('io_2709') r.generate_by_context(c) self.assertTrue('due to: \'undefined_variable\' is undefined' in str( context.exception))
def test_templates_regex_parse_failed(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) c = config.load_context_section('templates_regex_invalid') with self.assertRaises(TemplateRenderingError) as context: r.generate_by_context(c) self.assertTrue('Processing [: template [ hasn\'t been found' in str( context.exception))
def test_concatination_with_env(self): settings.TEMPLATES_DIR = 'templates_tests' c = config.load_context_section('test_dirs') self.assertEqual(c['my_conc_env_var1'], 'prefix-My value-postfix') self.assertEqual(c['my_conc_env_var2'], 'prefix-My value') self.assertEqual(c['my_conc_env_var3'], 'My value-postfix')
def test_dashes(self): r = templating.Renderer( os.path.join(os.path.dirname(__file__), 'templates_tests')) context = config.load_context_section('test_dashes') r.generate_by_context(context) result = '{}/template-dashes.yaml'.format(settings.TEMP_DIR) with open(result, 'r') as f: actual = yaml.safe_load(f) self.assertEqual('do this', actual)
def test_generate_group_templates(self): r = templating.Renderer(os.path.join(os.path.dirname(__file__), 'templates_tests')) context = config.load_context_section('test_groups') r.generate_by_context(context) file_path_1 = '{}/template1.yaml'.format(settings.TEMP_DIR) file_path_2 = '{}/template2.yaml'.format(settings.TEMP_DIR) file_path_3 = '{}/template3.yaml'.format(settings.TEMP_DIR) self.assertTrue(os.path.exists(file_path_1)) self.assertTrue(os.path.exists(file_path_2)) self.assertTrue(os.path.exists(file_path_3))
def _handler_deploy_destroy(args, command): context = config.load_context_section(args.get('section')) resources = templating.Renderer( settings.TEMPLATES_DIR, args.get('tags'), args.get('skip_tags')).generate_by_context(context) if args.get('dry_run'): return _handler_provision(command, resources, config.PriorityEvaluator(args, context, os.environ), args.get('use_kubeconfig'), args.get('sync_mode'), args.get('show_logs'))
def test_templates_regex(self): r = templating.Renderer(os.path.join(os.path.dirname(__file__), 'templates_tests')) context = config.load_context_section('templates_regex') file_path_1 = '{}/template1.yaml'.format(settings.TEMP_DIR) file_path_2 = '{}/template2.yaml'.format(settings.TEMP_DIR) file_path_3 = '{}/template3.yaml'.format(settings.TEMP_DIR) file_path_4 = '{}/template4.yaml'.format(settings.TEMP_DIR) file_path_5 = '{}/innerdir/template1.yaml'.format(settings.TEMP_DIR) file_path_6 = '{}/template_include_file.yaml'.format(settings.TEMP_DIR) r.generate_by_context(context) self.assertTrue(os.path.exists(file_path_1)) self.assertFalse(os.path.exists(file_path_2)) self.assertFalse(os.path.exists(file_path_3)) self.assertFalse(os.path.exists(file_path_4)) self.assertTrue(os.path.exists(file_path_5)) self.assertFalse(os.path.exists(file_path_6))
def test_infinite_recursion_loop(self): settings.CONFIG_FILE = 'tests/fixtures/config_with_include_and_env_vars.yaml' with self.assertRaises(RuntimeError): config.load_context_section('section-3')
def test_config_not_exist(self): settings.CONFIG_FILE = 'tests/config.yaml' with self.assertRaises(Exception) as context: config.load_context_section('section') self.assertTrue('No such file or directory: \'tests/config.yaml\'' in str(context.exception))
def test_config_incorrect(self): settings.CONFIG_FILE = 'tests/fixtures/incorrect_config.yaml' with self.assertRaises(InvalidYamlError): config.load_context_section('section')
def test_not_existed_section(self): with self.assertRaises(RuntimeError) as context: config.load_context_section('absent_section') self.assertTrue('Section "absent_section" not found' in str(context.exception))
def test_no_templates(self): with self.assertRaises(RuntimeError) as context: config.load_context_section('no_templates_section') self.assertTrue('Section "templates" or "kubectl" not found in config file' in str(context.exception))
def test_common_section(self): with self.assertRaises(RuntimeError) as context: config.load_context_section(settings.COMMON_SECTION_NAME) self.assertTrue('Section "{}" is not intended to deploy'.format( settings.COMMON_SECTION_NAME) in str(context.exception))
def test_empty_section(self): with self.assertRaises(RuntimeError) as context: config.load_context_section('') self.assertEqual('Empty section specification is not allowed', str(context.exception))