From f99b229f5a6a20915ea0cf4e0e114109ce8cd21a Mon Sep 17 00:00:00 2001 From: Matt Richardson Date: Thu, 25 Aug 2022 06:26:22 +1000 Subject: [PATCH] Show correct deployment name to key vault deployment (#226) * Show correct deployment name to key vault deployment * Update tests --- builder/azure/arm/builder.go | 6 ++-- builder/azure/arm/step_validate_template.go | 9 ++--- .../azure/arm/step_validate_template_test.go | 36 ++++++++++++++----- 3 files changed, 36 insertions(+), 15 deletions(-) diff --git a/builder/azure/arm/builder.go b/builder/azure/arm/builder.go index 62134577..1dfa42eb 100644 --- a/builder/azure/arm/builder.go +++ b/builder/azure/arm/builder.go @@ -216,7 +216,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) error: func(e error) { ui.Error(e.Error()) }, }, NewStepCreateResourceGroup(azureClient, ui), - NewStepValidateTemplate(azureClient, ui, &b.config, GetVirtualMachineDeployment), + NewStepValidateTemplate(azureClient, ui, &b.config, deploymentName, GetVirtualMachineDeployment), NewStepDeployTemplate(azureClient, ui, &b.config, deploymentName, GetVirtualMachineDeployment), NewStepGetIPAddress(azureClient, ui, endpointConnectType), &communicator.StepConnectSSH{ @@ -248,7 +248,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) if b.config.BuildKeyVaultName == "" { keyVaultDeploymentName := b.stateBag.Get(constants.ArmKeyVaultDeploymentName).(string) steps = append(steps, - NewStepValidateTemplate(azureClient, ui, &b.config, GetKeyVaultDeployment), + NewStepValidateTemplate(azureClient, ui, &b.config, keyVaultDeploymentName, GetKeyVaultDeployment), NewStepDeployTemplate(azureClient, ui, &b.config, keyVaultDeploymentName, GetKeyVaultDeployment), ) } else { @@ -257,7 +257,7 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook) steps = append(steps, NewStepGetCertificate(azureClient, ui), NewStepSetCertificate(&b.config, ui), - NewStepValidateTemplate(azureClient, ui, &b.config, GetVirtualMachineDeployment), + NewStepValidateTemplate(azureClient, ui, &b.config, deploymentName, GetVirtualMachineDeployment), NewStepDeployTemplate(azureClient, ui, &b.config, deploymentName, GetVirtualMachineDeployment), NewStepGetIPAddress(azureClient, ui, endpointConnectType), &communicator.StepConnectWinRM{ diff --git a/builder/azure/arm/step_validate_template.go b/builder/azure/arm/step_validate_template.go index bb706717..5f0818d8 100644 --- a/builder/azure/arm/step_validate_template.go +++ b/builder/azure/arm/step_validate_template.go @@ -16,15 +16,17 @@ type StepValidateTemplate struct { error func(e error) config *Config factory templateFactoryFunc + name string } -func NewStepValidateTemplate(client *AzureClient, ui packersdk.Ui, config *Config, factory templateFactoryFunc) *StepValidateTemplate { +func NewStepValidateTemplate(client *AzureClient, ui packersdk.Ui, config *Config, deploymentName string, factory templateFactoryFunc) *StepValidateTemplate { var step = &StepValidateTemplate{ client: client, say: func(message string) { ui.Say(message) }, error: func(e error) { ui.Error(e.Error()) }, config: config, factory: factory, + name: deploymentName, } step.validate = step.validateTemplate @@ -48,12 +50,11 @@ func (s *StepValidateTemplate) Run(ctx context.Context, state multistep.StateBag s.say("Validating deployment template ...") var resourceGroupName = state.Get(constants.ArmResourceGroupName).(string) - var deploymentName = state.Get(constants.ArmDeploymentName).(string) s.say(fmt.Sprintf(" -> ResourceGroupName : '%s'", resourceGroupName)) - s.say(fmt.Sprintf(" -> DeploymentName : '%s'", deploymentName)) + s.say(fmt.Sprintf(" -> DeploymentName : '%s'", s.name)) - err := s.validate(ctx, resourceGroupName, deploymentName) + err := s.validate(ctx, resourceGroupName, s.name) return processStepResult(err, s.error, state) } diff --git a/builder/azure/arm/step_validate_template_test.go b/builder/azure/arm/step_validate_template_test.go index 321c3a49..8840cd81 100644 --- a/builder/azure/arm/step_validate_template_test.go +++ b/builder/azure/arm/step_validate_template_test.go @@ -47,14 +47,12 @@ func TestStepValidateTemplateShouldPassIfValidatePasses(t *testing.T) { } } -func TestStepValidateTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) { +func TestStepValidateTemplateShouldTakeResourceGroupNameArgumentFromStateBag(t *testing.T) { var actualResourceGroupName string - var actualDeploymentName string var testSubject = &StepValidateTemplate{ validate: func(ctx context.Context, resourceGroupName string, deploymentName string) error { actualResourceGroupName = resourceGroupName - actualDeploymentName = deploymentName return nil }, @@ -69,18 +67,40 @@ func TestStepValidateTemplateShouldTakeStepArgumentsFromStateBag(t *testing.T) { t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) } - var expectedDeploymentName = stateBag.Get(constants.ArmDeploymentName).(string) var expectedResourceGroupName = stateBag.Get(constants.ArmResourceGroupName).(string) - if actualDeploymentName != expectedDeploymentName { - t.Fatal("Expected the step to source 'constants.ArmDeploymentName' from the state bag, but it did not.") - } - if actualResourceGroupName != expectedResourceGroupName { t.Fatal("Expected the step to source 'constants.ArmResourceGroupName' from the state bag, but it did not.") } } +func TestStepValidateTemplateShouldTakeDeploymentNameArgumentFromParam(t *testing.T) { + var actualDeploymentName string + var expectedDeploymentName = "Unit Test: DeploymentName" + + stateBag := createTestStateBagStepValidateTemplate() + var testSubject = &StepValidateTemplate{ + validate: func(ctx context.Context, resourceGroupName string, deploymentName string) error { + actualDeploymentName = deploymentName + + return nil + }, + say: func(message string) {}, + error: func(e error) {}, + name: expectedDeploymentName, + } + + var result = testSubject.Run(context.Background(), stateBag) + + if result != multistep.ActionContinue { + t.Fatalf("Expected the step to return 'ActionContinue', but got '%d'.", result) + } + + if actualDeploymentName != expectedDeploymentName { + t.Fatal("Expected the step to source 'deploymentName' from parameter, but it did not.") + } +} + func createTestStateBagStepValidateTemplate() multistep.StateBag { stateBag := new(multistep.BasicStateBag)