def __init__(self, command_executor, **kwargs): """ :type command_executor: BaseCommandExecutor :param command_executor: Command Line Executor :type parameters: dict :param parameters: CLI arguments """ self.terraform = Terraform(command_executor, **kwargs)
def test_destroy(self): expected_args = [ 'terraform', 'destroy', '-no-color', '-auto-approve', '-var', "'test=[\"Test\", \"Test2\"]'" ] tf = Terraform(self.command_executor_mock, variables={'test': ['Test', 'Test2']}) tf.destroy() self.command_executor_mock.run.assert_called() args, kwargs = self.command_executor_mock.run.call_args for i in expected_args: self.assertIn(i, args[0])
def test_apply(self): expected_args = [ 'terraform', 'apply', '-no-color', '-auto-approve', '-var', "'test=Test'" ] tf = Terraform(self.command_executor_mock, variables={ 'test': 'Test', 'no_value': None }) tf.apply() self.command_executor_mock.run.assert_called() args, kwargs = self.command_executor_mock.run.call_args for i in expected_args: self.assertIn(i, args[0])
class TerraformServiceProvider(BaseIaCServiceProvider): def __init__(self, command_executor, **kwargs): """ :type command_executor: BaseCommandExecutor :param command_executor: Command Line Executor :type parameters: dict :param parameters: CLI arguments """ self.terraform = Terraform(command_executor, **kwargs) def provision(self): """Provision terraform""" self.terraform.initialize() self.terraform.validate() self.terraform.apply() def destroy(self): """Deploy terraform""" self.terraform.initialize() self.terraform.validate() self.terraform.destroy() def output(self): """Extract terraform output :rtype dict """ return self.terraform.output()
def test_wrong_command_executor(self): types = [str, int, list, dict, set, tuple] for executor_type in types: with self.assertRaises(DLabException): Terraform(MagicMock(spec=executor_type))
def test_wrong_parameters_type(self): with self.assertRaises(DLabException): Terraform(self.command_executor_mock, no_color='')
def test_output(self): Terraform(self.command_executor_mock).output() self.command_executor_mock.run.assert_called_with( 'terraform output -json')
def test_validate(self): self.command_executor_mock.run.return_value = TF_VALIDATE_SUCCESS_MSG Terraform(self.command_executor_mock).validate() self.command_executor_mock.run.assert_called_with('terraform validate')
def test_failed_initialize(self): self.command_executor_mock.run.return_value = "error" with self.assertRaises(TerraformException): Terraform(self.command_executor_mock).initialize()
def test_initialize(self): self.command_executor_mock.run.return_value = TF_INIT_SUCCESS_MSG Terraform(self.command_executor_mock).initialize() self.command_executor_mock.run.assert_called_with('terraform init')