Skip to content

Commit

Permalink
Show correct deployment name to key vault deployment (#226)
Browse files Browse the repository at this point in the history
* Show correct deployment name to key vault deployment

* Update tests
  • Loading branch information
matt-richardson authored Aug 24, 2022
1 parent a1b8fc8 commit f99b229
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 15 deletions.
6 changes: 3 additions & 3 deletions builder/azure/arm/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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 {
Expand All @@ -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{
Expand Down
9 changes: 5 additions & 4 deletions builder/azure/arm/step_validate_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand Down
36 changes: 28 additions & 8 deletions builder/azure/arm/step_validate_template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
},
Expand All @@ -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)

Expand Down

0 comments on commit f99b229

Please sign in to comment.