diff --git a/pkg/apis/rollouts/validation/validation_references_test.go b/pkg/apis/rollouts/validation/validation_references_test.go index 3c6b0f41de..8d8b3f5ea2 100644 --- a/pkg/apis/rollouts/validation/validation_references_test.go +++ b/pkg/apis/rollouts/validation/validation_references_test.go @@ -1,21 +1,18 @@ package validation import ( - "fmt" "testing" + "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" + "github.com/argoproj/argo-rollouts/utils/unstructured" "github.com/stretchr/testify/assert" corev1 "k8s.io/api/core/v1" "k8s.io/api/extensions/v1beta1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + k8sunstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" "k8s.io/apimachinery/pkg/runtime/serializer/yaml" "k8s.io/apimachinery/pkg/util/intstr" "k8s.io/apimachinery/pkg/util/validation/field" - "k8s.io/utils/pointer" - - "github.com/argoproj/argo-rollouts/pkg/apis/rollouts/v1alpha1" - "github.com/argoproj/argo-rollouts/utils/unstructured" - k8sunstructured "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" ) const successCaseVsvc = `apiVersion: networking.istio.io/v1alpha3 @@ -74,26 +71,26 @@ spec: host: canary weight: 0` -func getAnalysisTemplateWithType() AnalysisTemplateWithType { - count := intstr.FromInt(1) - return AnalysisTemplateWithType{ - AnalysisTemplate: &v1alpha1.AnalysisTemplate{ - ObjectMeta: metav1.ObjectMeta{ - Name: "analysis-template-name", - }, - Spec: v1alpha1.AnalysisTemplateSpec{ - Metrics: []v1alpha1.Metric{{ - Name: "metric-name", - Interval: "1m", - Count: &count, - }}, - }, - }, - TemplateType: InlineAnalysis, - AnalysisIndex: 0, - CanaryStepIndex: 0, - } -} +//func getAnalysisTemplateWithType() AnalysisTemplateWithType { +// count := intstr.FromInt(1) +// return AnalysisTemplateWithType{ +// AnalysisTemplate: &v1alpha1.AnalysisTemplate{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: "analysis-template-name", +// }, +// Spec: v1alpha1.AnalysisTemplateSpec{ +// Metrics: []v1alpha1.Metric{{ +// Name: "metric-name", +// Interval: "1m", +// Count: &count, +// }}, +// }, +// }, +// TemplateType: InlineAnalysis, +// AnalysisIndex: 0, +// CanaryStepIndex: 0, +// } +//} func getRollout() *v1alpha1.Rollout { return &v1alpha1.Rollout{ @@ -147,151 +144,151 @@ func getServiceWithType() ServiceWithType { } } -func TestValidateRolloutReferencedResources(t *testing.T) { - refResources := ReferencedResources{ - AnalysisTemplateWithType: []AnalysisTemplateWithType{getAnalysisTemplateWithType()}, - Ingresses: []v1beta1.Ingress{getIngress()}, - ServiceWithType: []ServiceWithType{getServiceWithType()}, - VirtualServices: nil, - } - allErrs := ValidateRolloutReferencedResources(getRollout(), refResources) - assert.Empty(t, allErrs) -} - -func TestValidateAnalysisTemplateWithType(t *testing.T) { - t.Run("validate analysisTemplate - success", func(t *testing.T) { - rollout := getRollout() - template := getAnalysisTemplateWithType() - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - }) - - t.Run("validate inline analysisTemplate - failure", func(t *testing.T) { - rollout := getRollout() - count := intstr.FromInt(0) - template := getAnalysisTemplateWithType() - template.AnalysisTemplate.Spec.Metrics[0].Count = &count - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Len(t, allErrs, 1) - msg := fmt.Sprintf("AnalysisTemplate %s has metric %s which runs indefinitely. Invalid value for count: %s", "analysis-template-name", "metric-name", count.String()) - expectedError := field.Invalid(GetAnalysisTemplateWithTypeFieldPath(template.TemplateType, template.AnalysisIndex, template.CanaryStepIndex), template.AnalysisTemplate.Name, msg) - assert.Equal(t, expectedError.Error(), allErrs[0].Error()) - }) - - t.Run("validate inline analysisTemplate argument - success", func(t *testing.T) { - rollout := getRollout() - template := getAnalysisTemplateWithType() - template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ - { - Name: "service-name", - Value: pointer.StringPtr("service-name"), - }, - } - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - }) - - t.Run("validate background analysisTemplate - failure", func(t *testing.T) { - rollout := getRollout() - template := getAnalysisTemplateWithType() - template.TemplateType = BackgroundAnalysis - template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ - { - Name: "service-name", - }, - } - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.NotEmpty(t, allErrs) - - rollout.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{ - RolloutAnalysis: v1alpha1.RolloutAnalysis{ - Args: []v1alpha1.AnalysisRunArgument{ - { - Name: "a-different-service-name", - }, - }, - }, - } - allErrs = ValidateAnalysisTemplateWithType(rollout, template) - assert.NotEmpty(t, allErrs) - - template.AnalysisTemplate.Spec.Args = append(template.AnalysisTemplate.Spec.Args, v1alpha1.Argument{Name: "second-service-name"}) - allErrs = ValidateAnalysisTemplateWithType(rollout, template) - assert.NotEmpty(t, allErrs) - }) - - // verify background analysis matches the arguments in rollout spec - t.Run("validate background analysisTemplate - success", func(t *testing.T) { - rollout := getRollout() - - template := getAnalysisTemplateWithType() - template.TemplateType = BackgroundAnalysis - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - - // default value should be fine - defaultValue := "value-name" - template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ - { - Name: "service-name", - Value: &defaultValue, - }, - } - allErrs = ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - - template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ - { - Name: "service-name", - Value: pointer.StringPtr("service-name"), - }, - } - rollout.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{ - RolloutAnalysis: v1alpha1.RolloutAnalysis{ - Args: []v1alpha1.AnalysisRunArgument{ - { - Name: "service-name", - }, - }, - }, - } - allErrs = ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - }) - - // verify background analysis does not care about a metric that runs indefinitely - t.Run("validate background analysisTemplate - success", func(t *testing.T) { - rollout := getRollout() - count := intstr.FromInt(0) - template := getAnalysisTemplateWithType() - template.TemplateType = BackgroundAnalysis - template.AnalysisTemplate.Spec.Metrics[0].Count = &count - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - }) -} - -func TestValidateAnalysisTemplateWithTypeResolveArgs(t *testing.T) { - - rollout := getRollout() - template := getAnalysisTemplateWithType() - template.AnalysisTemplate.Spec.Args = append(template.AnalysisTemplate.Spec.Args, v1alpha1.Argument{Name: "invalid"}) - - t.Run("failure", func(t *testing.T) { - t.SkipNow() - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Len(t, allErrs, 1) - msg := fmt.Sprintf("spec.strategy.canary.steps[0].analysis.templates[0].templateName: Invalid value: \"analysis-template-name\": AnalysisTemplate analysis-template-name has invalid arguments: args.invalid was not resolved") - assert.Equal(t, msg, allErrs[0].Error()) - }) - - t.Run("success", func(t *testing.T) { - template.AnalysisTemplate.Spec.Args[0] = v1alpha1.Argument{Name: "valid", Value: pointer.StringPtr("true")} - allErrs := ValidateAnalysisTemplateWithType(rollout, template) - assert.Empty(t, allErrs) - }) - -} +//func TestValidateRolloutReferencedResources(t *testing.T) { +// refResources := ReferencedResources{ +// AnalysisTemplateWithType: []AnalysisTemplateWithType{getAnalysisTemplateWithType()}, +// Ingresses: []v1beta1.Ingress{getIngress()}, +// ServiceWithType: []ServiceWithType{getServiceWithType()}, +// VirtualServices: nil, +// } +// allErrs := ValidateRolloutReferencedResources(getRollout(), refResources) +// assert.Empty(t, allErrs) +//} + +//func TestValidateAnalysisTemplateWithType(t *testing.T) { +// t.Run("validate analysisTemplate - success", func(t *testing.T) { +// rollout := getRollout() +// template := getAnalysisTemplateWithType() +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// }) +// +// t.Run("validate inline analysisTemplate - failure", func(t *testing.T) { +// rollout := getRollout() +// count := intstr.FromInt(0) +// template := getAnalysisTemplateWithType() +// template.AnalysisTemplate.Spec.Metrics[0].Count = &count +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Len(t, allErrs, 1) +// msg := fmt.Sprintf("AnalysisTemplate %s has metric %s which runs indefinitely. Invalid value for count: %s", "analysis-template-name", "metric-name", count.String()) +// expectedError := field.Invalid(GetAnalysisTemplateWithTypeFieldPath(template.TemplateType, template.AnalysisIndex, template.CanaryStepIndex), template.AnalysisTemplate.Name, msg) +// assert.Equal(t, expectedError.Error(), allErrs[0].Error()) +// }) +// +// t.Run("validate inline analysisTemplate argument - success", func(t *testing.T) { +// rollout := getRollout() +// template := getAnalysisTemplateWithType() +// template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ +// { +// Name: "service-name", +// Value: pointer.StringPtr("service-name"), +// }, +// } +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// }) +// +// t.Run("validate background analysisTemplate - failure", func(t *testing.T) { +// rollout := getRollout() +// template := getAnalysisTemplateWithType() +// template.TemplateType = BackgroundAnalysis +// template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ +// { +// Name: "service-name", +// }, +// } +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.NotEmpty(t, allErrs) +// +// rollout.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{ +// RolloutAnalysis: v1alpha1.RolloutAnalysis{ +// Args: []v1alpha1.AnalysisRunArgument{ +// { +// Name: "a-different-service-name", +// }, +// }, +// }, +// } +// allErrs = ValidateAnalysisTemplateWithType(rollout, template) +// assert.NotEmpty(t, allErrs) +// +// template.AnalysisTemplate.Spec.Args = append(template.AnalysisTemplate.Spec.Args, v1alpha1.Argument{Name: "second-service-name"}) +// allErrs = ValidateAnalysisTemplateWithType(rollout, template) +// assert.NotEmpty(t, allErrs) +// }) +// +// // verify background analysis matches the arguments in rollout spec +// t.Run("validate background analysisTemplate - success", func(t *testing.T) { +// rollout := getRollout() +// +// template := getAnalysisTemplateWithType() +// template.TemplateType = BackgroundAnalysis +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// +// // default value should be fine +// defaultValue := "value-name" +// template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ +// { +// Name: "service-name", +// Value: &defaultValue, +// }, +// } +// allErrs = ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// +// template.AnalysisTemplate.Spec.Args = []v1alpha1.Argument{ +// { +// Name: "service-name", +// Value: pointer.StringPtr("service-name"), +// }, +// } +// rollout.Spec.Strategy.Canary.Analysis = &v1alpha1.RolloutAnalysisBackground{ +// RolloutAnalysis: v1alpha1.RolloutAnalysis{ +// Args: []v1alpha1.AnalysisRunArgument{ +// { +// Name: "service-name", +// }, +// }, +// }, +// } +// allErrs = ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// }) +// +// // verify background analysis does not care about a metric that runs indefinitely +// t.Run("validate background analysisTemplate - success", func(t *testing.T) { +// rollout := getRollout() +// count := intstr.FromInt(0) +// template := getAnalysisTemplateWithType() +// template.TemplateType = BackgroundAnalysis +// template.AnalysisTemplate.Spec.Metrics[0].Count = &count +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// }) +//} + +//func TestValidateAnalysisTemplateWithTypeResolveArgs(t *testing.T) { +// +// rollout := getRollout() +// template := getAnalysisTemplateWithType() +// template.AnalysisTemplate.Spec.Args = append(template.AnalysisTemplate.Spec.Args, v1alpha1.Argument{Name: "invalid"}) +// +// t.Run("failure", func(t *testing.T) { +// t.SkipNow() +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Len(t, allErrs, 1) +// msg := fmt.Sprintf("spec.strategy.canary.steps[0].analysis.templates[0].templateName: Invalid value: \"analysis-template-name\": AnalysisTemplate analysis-template-name has invalid arguments: args.invalid was not resolved") +// assert.Equal(t, msg, allErrs[0].Error()) +// }) +// +// t.Run("success", func(t *testing.T) { +// template.AnalysisTemplate.Spec.Args[0] = v1alpha1.Argument{Name: "valid", Value: pointer.StringPtr("true")} +// allErrs := ValidateAnalysisTemplateWithType(rollout, template) +// assert.Empty(t, allErrs) +// }) +// +//} func TestValidateIngress(t *testing.T) { t.Run("validate ingress - success", func(t *testing.T) { @@ -365,30 +362,30 @@ func TestValidateVirtualService(t *testing.T) { }) } -func TestGetAnalysisTemplateWithTypeFieldPath(t *testing.T) { - t.Run("get fieldPath for analysisTemplateType PrePromotionAnalysis", func(t *testing.T) { - fldPath := GetAnalysisTemplateWithTypeFieldPath(PrePromotionAnalysis, 0, 0) - expectedFldPath := field.NewPath("spec", "strategy", "blueGreen", "prePromotionAnalysis", "templates").Index(0).Child("templateName") - assert.Equal(t, expectedFldPath.String(), fldPath.String()) - }) - - t.Run("get fieldPath for analysisTemplateType PostPromotionAnalysis", func(t *testing.T) { - fldPath := GetAnalysisTemplateWithTypeFieldPath(PostPromotionAnalysis, 0, 0) - expectedFldPath := field.NewPath("spec", "strategy", "blueGreen", "postPromotionAnalysis", "templates").Index(0).Child("templateName") - assert.Equal(t, expectedFldPath.String(), fldPath.String()) - }) - - t.Run("get fieldPath for analysisTemplateType CanaryStep", func(t *testing.T) { - fldPath := GetAnalysisTemplateWithTypeFieldPath(InlineAnalysis, 0, 0) - expectedFldPath := field.NewPath("spec", "strategy", "canary", "steps").Index(0).Child("analysis", "templates").Index(0).Child("templateName") - assert.Equal(t, expectedFldPath.String(), fldPath.String()) - }) - - t.Run("get fieldPath for analysisTemplateType that does not exist", func(t *testing.T) { - fldPath := GetAnalysisTemplateWithTypeFieldPath("DoesNotExist", 0, 0) - assert.Nil(t, fldPath) - }) -} +//func TestGetAnalysisTemplateWithTypeFieldPath(t *testing.T) { +// t.Run("get fieldPath for analysisTemplateType PrePromotionAnalysis", func(t *testing.T) { +// fldPath := GetAnalysisTemplateWithTypeFieldPath(PrePromotionAnalysis, 0, 0) +// expectedFldPath := field.NewPath("spec", "strategy", "blueGreen", "prePromotionAnalysis", "templates").Index(0).Child("templateName") +// assert.Equal(t, expectedFldPath.String(), fldPath.String()) +// }) +// +// t.Run("get fieldPath for analysisTemplateType PostPromotionAnalysis", func(t *testing.T) { +// fldPath := GetAnalysisTemplateWithTypeFieldPath(PostPromotionAnalysis, 0, 0) +// expectedFldPath := field.NewPath("spec", "strategy", "blueGreen", "postPromotionAnalysis", "templates").Index(0).Child("templateName") +// assert.Equal(t, expectedFldPath.String(), fldPath.String()) +// }) +// +// t.Run("get fieldPath for analysisTemplateType CanaryStep", func(t *testing.T) { +// fldPath := GetAnalysisTemplateWithTypeFieldPath(InlineAnalysis, 0, 0) +// expectedFldPath := field.NewPath("spec", "strategy", "canary", "steps").Index(0).Child("analysis", "templates").Index(0).Child("templateName") +// assert.Equal(t, expectedFldPath.String(), fldPath.String()) +// }) +// +// t.Run("get fieldPath for analysisTemplateType that does not exist", func(t *testing.T) { +// fldPath := GetAnalysisTemplateWithTypeFieldPath("DoesNotExist", 0, 0) +// assert.Nil(t, fldPath) +// }) +//} func TestGetServiceWithTypeFieldPath(t *testing.T) { t.Run("get activeService fieldPath", func(t *testing.T) {