def test_apply_exports_state_files(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True ) # create a new temp dir as the state output dir with common.create_test_working_dir() as state_output_dir: # apply without plan lib.terraform_dir.apply_terraform_dir( terraform_dir, state_file_path=common.TEST_STATE_FILE_WITHOUT_KEY, state_output_dir=state_output_dir, debug=True) # check for exported state files exported_state_file_path = \ os.path.join( state_output_dir, lib.terraform_dir.TERRAFORM_STATE_FILE_NAME) exported_backup_state_file_path = \ os.path.join( state_output_dir, lib.terraform_dir.TERRAFORM_BACKUP_STATE_FILE_NAME) self.assertTrue( os.path.isfile(exported_state_file_path)) self.assertTrue( os.path.isfile(exported_backup_state_file_path))
def test_allows_specifying_output_targets(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: with common.create_test_working_dir() as test_output_dir: # init terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( terraform_work_dir=test_working_dir, debug=True) # create targets output_targets = {"test": "example"} # output lib.terraform_dir.output_terraform_dir( terraform_dir, test_output_dir, output_targets=output_targets, terraform_work_dir=test_working_dir, state_file_path=common.TEST_STATE_FILE_WITH_OUTPUT, debug=True) # read output file output_file_path = \ os.path.join( test_output_dir, "test.json") with open(output_file_path, 'r') as output_file: output_file_contents = json.load(output_file) self.assertTrue('value' in output_file_contents) self.assertEqual(output_file_contents['value'], 'hello world')
def test_apply_exports_state_file_on_failure(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_INVALID_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True ) # create a new temp dir as the state output dir with common.create_test_working_dir() as state_output_dir: with self.assertRaises(CalledProcessError): # apply without plan lib.terraform_dir.apply_terraform_dir( terraform_dir, state_file_path=common.TEST_STATE_FILE_WITHOUT_KEY, state_output_dir=state_output_dir, debug=True) # check for exported state file exported_state_file_path = \ os.path.join( state_output_dir, lib.terraform_dir.TERRAFORM_STATE_FILE_NAME) self.assertTrue( os.path.isfile(exported_state_file_path))
def test_raises_when_no_archives_are_present(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # create a new temp dir as the archive output dir with common.create_test_working_dir() as archive_output_dir: # restore the archive with self.assertRaises(FileNotFoundError): lib.terraform_dir.restore_terraform_dir( archive_output_dir, terraform_work_dir=test_working_dir, debug=True)
def test_requires_archive_input_dir(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: with self.assertRaises(ValueError): # archive with empty string as the archive input dir lib.terraform_dir.restore_terraform_dir( '', terraform_work_dir=test_working_dir, debug=True)
def test_creates_backend_file_for_backend_type_in_terraform_dir_path(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # get the terraform dir path terraform_dir = os.path.join(test_working_dir, lib.terraform_dir.TERRAFORM_DIR_NAME) # get the terraform dir path terraform_dir_path = 'foo' # get the expected backend file path backend_file_path = \ os.path.join(terraform_dir, terraform_dir_path, lib.terraform_dir.BACKEND_FILE_NAME) # set the backend type in the environment with common.mocked_env_vars( {lib.terraform_dir.BACKEND_TYPE_VAR: 'local'}): # test init lib.terraform_dir.init_terraform_dir( terraform_source_dir=common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, terraform_dir_path=terraform_dir_path, debug=True) # assert the file was created self.assertTrue(os.path.exists(backend_file_path)) # read the backend file with open(backend_file_path, 'r') as backend_file: backend_file_contents = backend_file.read() # assert the file contents contain the expected backend self.assertTrue('backend "local"' in backend_file_contents)
def test_imports_plugin_cache(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # get the terraform dir path terraform_dir = os.path.join(test_working_dir, lib.terraform_dir.TERRAFORM_DIR_NAME) # get the expected cached aux file path aux_file_path = \ os.path.join( terraform_dir, lib.terraform_dir.TERRAFORM_PLUGIN_CACHE_DIR_NAME, common.TEST_TERRAFORM_AUX_FILE_NAME) # set the cache path in the environment with common.mocked_env_vars({ lib.terraform_dir.TERRAFORM_PLUGIN_CACHE_VAR_NAME: common.TEST_TERRAFORM_AUX_DIR }): # test init lib.terraform_dir.init_terraform_dir( terraform_source_dir=common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # assert the file was copied self.assertTrue(os.path.exists(aux_file_path)) # read the aux file with open(aux_file_path, 'r') as aux_file: aux_file_contents = aux_file.read() # assert the file contains the expected content self.assertEqual(aux_file_contents, "hello world")
def test_apply_imports_output_var_file_with_multi_values(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_VAR_DIR, terraform_work_dir=test_working_dir, debug=True ) output_var_files = { 'multi': common.TEST_TERRAFORM_MULTI_OUTPUT_VAR_FILE_PATH } expected_var_file_path = \ os.path.join(terraform_dir, 'multi.tfvars.json') # apply the terraform dir lib.terraform_dir.apply_terraform_dir( terraform_dir, output_var_files=output_var_files, debug=True) self.assertTrue(os.path.exists(expected_var_file_path)) with open( common.TEST_TERRAFORM_CONVERTED_MULTI_OUTPUT_VAR_FILE_PATH, 'r') as expected_var_file: expected_var_file_contents = expected_var_file.read() with open(expected_var_file_path, 'r') as var_file: actual_var_file_contents = var_file.read() self.assertEqual( expected_var_file_contents, actual_var_file_contents)
def test_imports_aux_input_with_name(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # get the terraform dir path terraform_dir = os.path.join(test_working_dir, lib.terraform_dir.TERRAFORM_DIR_NAME) # get the expected aux file path aux_file_path = \ os.path.join( terraform_dir, TEST_AUX_INPUT_NAME, common.TEST_TERRAFORM_AUX_FILE_NAME) # set the aux path and name in the environment with common.mocked_env_vars({ lib.terraform_dir.AUX_INPUT_PATH_PREFIX + "0": common.TEST_TERRAFORM_AUX_DIR, lib.terraform_dir.AUX_INPUT_NAME_PREFIX + "0": TEST_AUX_INPUT_NAME }): # test init lib.terraform_dir.init_terraform_dir( terraform_source_dir=common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # assert the file was copied self.assertTrue(os.path.exists(aux_file_path)) # read the aux file with open(aux_file_path, 'r') as aux_file: aux_file_contents = aux_file.read() # assert the file contains the expected content self.assertEqual(aux_file_contents, "hello world")
def test_requires_terraform_dir(self): # create a new temp dir as the archive output dir with common.create_test_working_dir() as archive_output_dir: with self.assertRaises(ValueError): # archive with empty string as the terraform dir lib.terraform_dir.archive_terraform_dir('', archive_output_dir, debug=True)
def test_apply_exports_state_files_on_failure(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # create a new temp dir as the state output dir with common.create_test_working_dir() as state_output_dir: # run plan to import existing state # but don't create a plan file lib.terraform_dir.plan_terraform_dir( terraform_dir, state_file_path=common.TEST_STATE_FILE_WITHOUT_KEY, debug=True) # manually create a backup state file since # apply won't succeed mock_backup_state_file_path = \ os.path.join( terraform_dir, lib.terraform_dir.TERRAFORM_BACKUP_STATE_FILE_NAME) with open(mock_backup_state_file_path, 'w') \ as mock_backup_state_file: mock_backup_state_file.write('{}') # apply a missing plan file with self.assertRaises(CalledProcessError): lib.terraform_dir.apply_terraform_plan( terraform_dir, state_output_dir=state_output_dir, debug=True) # check for exported state files exported_state_file_path = \ os.path.join( state_output_dir, lib.terraform_dir.TERRAFORM_STATE_FILE_NAME) exported_backup_state_file_path = \ os.path.join( state_output_dir, lib.terraform_dir.TERRAFORM_BACKUP_STATE_FILE_NAME) self.assertTrue(os.path.isfile(exported_state_file_path)) self.assertTrue( os.path.isfile(exported_backup_state_file_path))
def test_plan_with_no_output(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # plan the terraform dir lib.terraform_dir.plan_terraform_dir(terraform_dir, debug=True)
def test_creates_output_file(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: with common.create_test_working_dir() as test_output_dir: # init terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( terraform_work_dir=test_working_dir, debug=True) # output lib.terraform_dir.output_terraform_dir( terraform_dir, test_output_dir, terraform_work_dir=test_working_dir, state_file_path=common.TEST_STATE_FILE_WITH_OUTPUT, debug=True) # check for expected output file expected_output_file_path = \ os.path.join( test_output_dir, lib.terraform_dir.TERRAFORM_OUTPUT_FILE_NAME) self.assertTrue(os.path.isfile(expected_output_file_path))
def test_raises_when_multiple_archives_are_present(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # create a new temp dir as the archive output dir with common.create_test_working_dir() as archive_output_dir: # file paths for mock archives archive_file_path_a = \ os.path.join(archive_output_dir, 'terraform-a.tar.gz') archive_file_path_b = \ os.path.join(archive_output_dir, 'terraform-b.tar.gz') # write the mock archive files archive_file_a = tarfile.open(archive_file_path_a, 'w') archive_file_a.close() archive_file_b = tarfile.open(archive_file_path_b, 'w') archive_file_b.close() # restore the archive with self.assertRaises(FileExistsError): lib.terraform_dir.restore_terraform_dir( archive_output_dir, terraform_work_dir=test_working_dir, debug=True)
def test_apply(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True ) # apply without plan lib.terraform_dir.apply_terraform_dir( terraform_dir, debug=True)
def test_plan_with_destroy(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # plan the terraform dir lib.terraform_dir.plan_terraform_dir(terraform_dir, error_on_no_changes=False, destroy=True, debug=True)
def test_apply_from_default_plan_file(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # create plan file lib.terraform_dir.plan_terraform_dir(terraform_dir, create_plan_file=True, debug=True) # apply plan file lib.terraform_dir.apply_terraform_plan(terraform_dir, debug=True)
def test_exports_plugin_cache(self): # create a new temp dir as the plugin cache with common.create_test_working_dir() as test_plugin_cache: # set the cache path in the environment with common.mocked_env_vars({ lib.terraform_dir.TERRAFORM_PLUGIN_CACHE_VAR_NAME: test_plugin_cache }): # get an expected cached plugin dir path cached_plugin_arch_dir = \ os.path.join( test_plugin_cache, 'linux_amd64') # assert the dir does not yet exist self.assertFalse(os.path.exists(cached_plugin_arch_dir)) # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # test init lib.terraform_dir.init_terraform_dir( terraform_source_dir=common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # assert the dir was created self.assertTrue(os.path.exists(cached_plugin_arch_dir))
def test_restores_archive_of_terraform_dir(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # get the test terraform file contents with open(common.TEST_TERRAFORM_FILE_PATH, 'r') as terraform_file: terraform_file_contents = terraform_file.read() # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # create a new temp dir as the archive output dir with common.create_test_working_dir() as archive_output_dir: # archive lib.terraform_dir.archive_terraform_dir(terraform_dir, archive_output_dir, debug=True) # restore the archive restored_terraform_dir = \ lib.terraform_dir.restore_terraform_dir( archive_output_dir, terraform_work_dir=test_working_dir, debug=True) # assert that it returned a directory path self.assertTrue(os.path.isdir(restored_terraform_dir)) # get the extracted terraform file contents extracted_terraform_file_path = \ os.path.join( terraform_dir, common.TEST_TERRAFORM_FILE_NAME) with open(extracted_terraform_file_path, 'r') \ as terraform_file: extracted_terraform_file_contents = terraform_file.read() # assert that the file contents match self.assertEqual(terraform_file_contents, extracted_terraform_file_contents)
def test_creates_archive_of_terraform_dir(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # create a new temp dir as the archive output dir with common.create_test_working_dir() as archive_output_dir: # archive archive_file_path = lib.terraform_dir.archive_terraform_dir( terraform_dir, archive_output_dir, debug=True) # assert the file was created self.assertTrue(os.path.exists(archive_file_path)) # assert that the terraform file exists in the archive expected_terraform_archive_file = \ os.path.join( lib.terraform_dir.TERRAFORM_DIR_NAME, common.TEST_TERRAFORM_FILE_NAME) with tarfile.open(archive_file_path, 'r:gz') as archive_file: archive_file.debug = 3 self.assertIn(expected_terraform_archive_file, archive_file.getnames())
def test_plan_creates_plan_file(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # plan the terraform dir terraform_plan_file = lib.terraform_dir.plan_terraform_dir( terraform_dir, create_plan_file=True, debug=True) # check for expected file expected_plan_file_path = \ os.path.join( terraform_dir, terraform_plan_file) self.assertTrue(os.path.isfile(expected_plan_file_path))
def test_show_plan(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True ) # create plan file terraform_plan_file = lib.terraform_dir.plan_terraform_dir( terraform_dir, create_plan_file=True, debug=True) # show plan file lib.terraform_dir.show_terraform_plan( terraform_dir, terraform_plan_file, debug=True)
def test_plan_imports_state_file(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # init the terraform dir terraform_dir = lib.terraform_dir.init_terraform_dir( common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # plan the terraform dir lib.terraform_dir.plan_terraform_dir( terraform_dir, state_file_path=common.TEST_STATE_FILE_WITHOUT_KEY, debug=True) # check for expected state file expected_state_file_path = \ os.path.join( terraform_dir, lib.terraform_dir.TERRAFORM_STATE_FILE_NAME) self.assertTrue(os.path.isfile(expected_state_file_path))
def test_destroys_existing_terraform_dir(self): # create a new temp dir as the working dir with common.create_test_working_dir() as test_working_dir: # create the terraform dir terraform_dir = os.path.join(test_working_dir, lib.terraform_dir.TERRAFORM_DIR_NAME) os.mkdir(terraform_dir) # create an existing file in it terraform_file_path = \ os.path.join(terraform_dir, 'vars.tf') with open(terraform_file_path, 'w') as terraform_file: terraform_file.write('variable "test" {}') # test init lib.terraform_dir.init_terraform_dir( terraform_source_dir=common.TEST_TERRAFORM_DIR, terraform_work_dir=test_working_dir, debug=True) # assert the file was destroyed self.assertFalse(os.path.exists(terraform_file_path))