Skip to content

Commit

Permalink
Use Full Template Path in Tag Validation (#6437)
Browse files Browse the repository at this point in the history
* Use Full Template Path in Tag Validation

* vsphere full template unit test
  • Loading branch information
tatlat authored Nov 22, 2023
1 parent 9f866a2 commit 7f767dc
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
13 changes: 7 additions & 6 deletions pkg/providers/vsphere/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,29 +209,30 @@ func (v *Validator) validateTemplates(ctx context.Context, spec *Spec) error {
for template, requiredTags := range tagsForTemplates {
datacenter := spec.VSphereDatacenter.Spec.Datacenter

if err := v.validateTemplatePresence(ctx, datacenter, template); err != nil {
templatePath, err := v.getTemplatePath(ctx, datacenter, template)
if err != nil {
return err
}

if err := v.validateTemplateTags(ctx, template, requiredTags); err != nil {
if err := v.validateTemplateTags(ctx, templatePath, requiredTags); err != nil {
return err
}
}

return nil
}

func (v *Validator) validateTemplatePresence(ctx context.Context, datacenter, templatePath string) error {
func (v *Validator) getTemplatePath(ctx context.Context, datacenter, templatePath string) (string, error) {
templateFullPath, err := v.govc.SearchTemplate(ctx, datacenter, templatePath)
if err != nil {
return fmt.Errorf("validating template: %v", err)
return "", fmt.Errorf("validating template: %v", err)
}

if len(templateFullPath) <= 0 {
return fmt.Errorf("template <%s> not found. Has the template been imported?", templatePath)
return "", fmt.Errorf("template <%s> not found. Has the template been imported?", templatePath)
}

return nil
return templateFullPath, nil
}

func (v *Validator) validateTemplateTags(ctx context.Context, templatePath string, requiredTags []string) error {
Expand Down
20 changes: 20 additions & 0 deletions pkg/providers/vsphere/validator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,26 @@ func TestValidatorValidateMachineConfigTagsExistTagDoesNotExist(t *testing.T) {
g.Expect(err).To(Not(BeNil()))
}

func TestValidatorValidateMachineConfigTemplateDoesNotExist(t *testing.T) {
ctrl := gomock.NewController(t)
govc := govcmocks.NewMockProviderGovcClient(ctrl)
ctx := context.Background()
g := NewWithT(t)

v := Validator{
govc: govc,
}

govc.EXPECT().SearchTemplate(ctx, "", "").Return("", nil)

_, err := v.getTemplatePath(ctx, "", "")
g.Expect(err).To(Not(BeNil()))

govc.EXPECT().SearchTemplate(ctx, "", "").Return("", fmt.Errorf("not found"))
_, err = v.getTemplatePath(ctx, "", "")
g.Expect(err).To(MatchError("validating template: not found"))
}

func TestValidateBRHardDiskSize(t *testing.T) {
ctrl := gomock.NewController(t)
govc := govcmocks.NewMockProviderGovcClient(ctrl)
Expand Down

0 comments on commit 7f767dc

Please sign in to comment.