|
| 1 | +package internal |
| 2 | + |
| 3 | +import ( |
| 4 | + appsv1 "k8s.io/api/apps/v1" |
| 5 | + corev1 "k8s.io/api/core/v1" |
| 6 | + "reflect" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + |
| 10 | + "github.com/operator-framework/api/pkg/manifests" |
| 11 | + operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | +) |
| 14 | + |
| 15 | +func TestValidateDeprecatedImage(t *testing.T) { |
| 16 | + type args struct { |
| 17 | + bundleDir string |
| 18 | + } |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + args args |
| 22 | + modifyBundle func(bundle *manifests.Bundle) |
| 23 | + wantErr []string |
| 24 | + want map[string]string |
| 25 | + }{ |
| 26 | + { |
| 27 | + name: "should return no error or warning for a valid bundle", |
| 28 | + args: args{ |
| 29 | + bundleDir: "./testdata/valid_bundle", |
| 30 | + }, |
| 31 | + modifyBundle: func(bundle *manifests.Bundle) { |
| 32 | + // Do not modify the bundle |
| 33 | + }, |
| 34 | + wantErr: []string{}, |
| 35 | + want: map[string]string{}, |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "should detect deprecated image in RelatedImages", |
| 39 | + args: args{ |
| 40 | + bundleDir: "./testdata/valid_bundle", |
| 41 | + }, |
| 42 | + modifyBundle: func(bundle *manifests.Bundle) { |
| 43 | + bundle.CSV.Spec.RelatedImages = append(bundle.CSV.Spec.RelatedImages, operatorsv1alpha1.RelatedImage{ |
| 44 | + Name: "kube-rbac-proxy", |
| 45 | + Image: "gcr.io/kubebuilder/kube-rbac-proxy", |
| 46 | + }) |
| 47 | + }, |
| 48 | + wantErr: []string{ |
| 49 | + "Your bundle uses the image `gcr.io/kubebuilder/kube-rbac-proxy`", |
| 50 | + }, |
| 51 | + want: map[string]string{ |
| 52 | + "RelatedImage": "gcr.io/kubebuilder/kube-rbac-proxy", |
| 53 | + }, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "should detect deprecated image in DeploymentSpecs", |
| 57 | + args: args{ |
| 58 | + bundleDir: "./testdata/valid_bundle", |
| 59 | + }, |
| 60 | + modifyBundle: func(bundle *manifests.Bundle) { |
| 61 | + bundle.CSV.Spec.InstallStrategy.StrategySpec.DeploymentSpecs = append( |
| 62 | + bundle.CSV.Spec.InstallStrategy.StrategySpec.DeploymentSpecs, |
| 63 | + operatorsv1alpha1.StrategyDeploymentSpec{ |
| 64 | + Name: "test-deployment", |
| 65 | + Spec: appsv1.DeploymentSpec{ |
| 66 | + Template: corev1.PodTemplateSpec{ |
| 67 | + Spec: corev1.PodSpec{ |
| 68 | + Containers: []corev1.Container{ |
| 69 | + { |
| 70 | + Name: "kube-rbac-proxy", |
| 71 | + Image: "gcr.io/kubebuilder/kube-rbac-proxy", |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + ) |
| 79 | + }, |
| 80 | + wantErr: []string{ |
| 81 | + "Your bundle uses the image `gcr.io/kubebuilder/kube-rbac-proxy`", |
| 82 | + }, |
| 83 | + want: map[string]string{ |
| 84 | + "DeploymentSpec": "gcr.io/kubebuilder/kube-rbac-proxy", |
| 85 | + }, |
| 86 | + }, |
| 87 | + } |
| 88 | + |
| 89 | + for _, tt := range tests { |
| 90 | + t.Run(tt.name, func(t *testing.T) { |
| 91 | + bundle, err := manifests.GetBundleFromDir(tt.args.bundleDir) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + tt.modifyBundle(bundle) |
| 95 | + result := validateDeprecatedImage(bundle) |
| 96 | + |
| 97 | + var gotWarnings []string |
| 98 | + for _, warn := range result.Warnings { |
| 99 | + gotWarnings = append(gotWarnings, warn.Error()) |
| 100 | + } |
| 101 | + |
| 102 | + for _, expectedErr := range tt.wantErr { |
| 103 | + found := false |
| 104 | + for _, gotWarning := range gotWarnings { |
| 105 | + if strings.Contains(gotWarning, expectedErr) { |
| 106 | + found = true |
| 107 | + break |
| 108 | + } |
| 109 | + } |
| 110 | + if !found { |
| 111 | + t.Errorf("Expected warning containing '%s' but did not find it", expectedErr) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + gotImages := make(map[string]string) |
| 116 | + if bundle.CSV != nil { |
| 117 | + for _, relatedImage := range bundle.CSV.Spec.RelatedImages { |
| 118 | + for deprecatedImage := range DeprecatedImages { |
| 119 | + if relatedImage.Image == deprecatedImage { |
| 120 | + gotImages["RelatedImage"] = deprecatedImage |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + for _, deploymentSpec := range bundle.CSV.Spec.InstallStrategy.StrategySpec.DeploymentSpecs { |
| 125 | + for _, container := range deploymentSpec.Spec.Template.Spec.Containers { |
| 126 | + for deprecatedImage := range DeprecatedImages { |
| 127 | + if container.Image == deprecatedImage { |
| 128 | + gotImages["DeploymentSpec"] = deprecatedImage |
| 129 | + } |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + require.True(t, reflect.DeepEqual(tt.want, gotImages)) |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments