def test_get_services_filter_empty_services(self): config = TFVarsConfig(None, [], load_config=False) config.data = { '/path/repo/1': { 'path': '/path/repo/1', 'git': None, 'group': 'aaa', 'data': { 'my_service_1_image': 'aaa', 'my_service_1_desired_count': 'aaa', 'my_service_1_random_variable': 'aaa', }, }, '/path/repo/2': { 'path': '/path/repo/2', 'git': None, 'group': 'bbb', 'data': { 'my_service_2_image': 'bbb', 'my_service_2_desired_count': 'bbb', 'my_service_2_random_variable': 'bbb', }, }, } result = config.get_services('aaa') expected_output = { 'my_service_1': { 'image': 'aaa', 'random_variable': 'aaa', 'desired_count': 'aaa' } } assert result == expected_output
def test_get_service_names_should_give_all_services(self): config = TFVarsConfig(None, [], load_config=False) config.data = { '/path/repo/1': { 'path': '/path/repo/1', 'git': None, 'group': 'aaa', 'data': { 'my_service_1_image': 'aaa', 'my_service_1_desired_count': 'aaa', 'my_service_1_random_variable': 'aaa', 'my_service_2_image': 'aaa', 'my_service_2_desired_count': 'aaa', 'my_service_2_random_variable': 'aaa', 'my_service_3_image': 'aaa', 'my_service_3_desired_count': 'aaa', 'my_service_3_random_variable': 'aaa', 'my_service_4_image': 'aaa', 'my_service_4_desired_count': 'aaa', 'my_service_4_random_variable': 'aaa', 'my_service_5_image': 'aaa', 'my_service_5_desired_count': 'aaa', 'my_service_5_random_variable': 'aaa', }, } } result = sorted(config.get_service_names()) expected_output = sorted([ 'my_service_1', 'my_service_2', 'my_service_3', 'my_service_4', 'my_service_5' ]) assert result == expected_output
def test_get_services_empty_config(self): config = TFVarsConfig(None, [], load_config=False) config.data = {} result = config.get_services(None) expected_output = {} assert result == expected_output
def test_get_service_names_should_give_zero_serices(self): config = TFVarsConfig(None, [], load_config=False) config.data = { '/path/repo/1': { 'path': '/path/repo/1', 'git': None, 'group': 'aaa', 'data': {}, } } result = config.get_service_names() expected_output = [] assert result == expected_output
class TestTerraform(object): def setup_method(self): self.config = TFVarsConfig(None) self.config.has_loaded_config = True def test_normalize_service_name_normal_case(self): result = self.config.normalize_service_name('my-custom-service123') expected_output = 'my_custom_service123' assert result == expected_output def test_get_artifact_key_as_non_ami(self): result = self.config.get_artifact_key('my_custom_service', is_ami=False) expected_output = 'my_custom_service_image' assert result == expected_output def test_get_services_valid_config(self): self.config._config = { 'my_service_1_image': 'xxx', 'my_service_1_desired_count': 'yyy', 'my_service_1_random_variable': 'zzz', 'my_service_2_image': 'xxx', 'my_service_2_desired_count': 'yyy', 'my_service_2_random_variable': 'zzz', } result = self.config.get_services() expected_output = { 'my_service_2': { 'image': 'xxx', 'random_variable': 'zzz', 'desired_count': 'yyy' }, 'my_service_1': { 'image': 'xxx', 'random_variable': 'zzz', 'desired_count': 'yyy' } } assert result == expected_output def test_get_services_empty_config(self): self.config._config = {} result = self.config.get_services() expected_output = {} assert result == expected_output
def _read_tfvars(self, repo): """Reads all tfvars (json file) in the given `repo['path']`/terraform/tfvars directory. Terraform deployment projects are expected to follow this structure: ├── README.md ├── scripts │ └── ... └── terraform ... ├── main.tf ├── tfvars │ ├── variables.production.json │ └── variables.staging.json └── variables.tf `tfvars/` follow a flat structure. Variables are free to be split between many JSON files and can be grouped based on `<group>` (<name>[.<group>].json). NOTE: tfvar files don't need a grouping. If no group is found, the tfvar is assumed to be applied on all groups. """ repo_path = os.path.join(repo['path'], 'terraform/tfvars') if not os.path.isdir(repo_path): raise NoTFVarsFilesFound(repo_path) tfvars_paths = [] for root, dirs, files in os.walk(repo_path): for f in files: if not f.endswith('.json'): # skip non-json files. continue tfvars_path = os.path.join(os.path.abspath(root), f) tfvars_paths.append(tfvars_path) if not tfvars_paths: raise NoTFVarsFilesFound(repo_path) return TFVarsConfig(repo, tfvars_paths) # aggregates all tfvars files.
def setup_method(self): self.config = TFVarsConfig(None) self.config.has_loaded_config = True