From 7f767dc39bb1f410a680458acd470c8e80005299 Mon Sep 17 00:00:00 2001 From: Tanvir Tatla Date: Wed, 22 Nov 2023 16:45:38 +0000 Subject: [PATCH] Use Full Template Path in Tag Validation (#6437) * Use Full Template Path in Tag Validation * vsphere full template unit test --- pkg/providers/vsphere/validator.go | 13 +++++++------ pkg/providers/vsphere/validator_test.go | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/pkg/providers/vsphere/validator.go b/pkg/providers/vsphere/validator.go index 7f702128af10..7b34f5301cb1 100644 --- a/pkg/providers/vsphere/validator.go +++ b/pkg/providers/vsphere/validator.go @@ -209,11 +209,12 @@ 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 } } @@ -221,17 +222,17 @@ func (v *Validator) validateTemplates(ctx context.Context, spec *Spec) error { 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 { diff --git a/pkg/providers/vsphere/validator_test.go b/pkg/providers/vsphere/validator_test.go index ad47e21a1be4..9c75ce139290 100644 --- a/pkg/providers/vsphere/validator_test.go +++ b/pkg/providers/vsphere/validator_test.go @@ -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)