|
| 1 | +package validation |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/google/go-cmp/cmp" |
| 7 | + |
| 8 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 9 | + "k8s.io/apimachinery/pkg/util/validation/field" |
| 10 | + |
| 11 | + configv1 "github.com/openshift/api/config/v1" |
| 12 | +) |
| 13 | + |
| 14 | +func TestValidateClusterVersion(t *testing.T) { |
| 15 | + tests := []struct { |
| 16 | + name string |
| 17 | + config *configv1.ClusterVersion |
| 18 | + shouldReconcileAcceptRisks bool |
| 19 | + expect field.ErrorList |
| 20 | + }{ |
| 21 | + { |
| 22 | + name: "allow accept risks if shouldReconcileAcceptRisks is true", |
| 23 | + shouldReconcileAcceptRisks: true, |
| 24 | + config: &configv1.ClusterVersion{ |
| 25 | + ObjectMeta: metav1.ObjectMeta{ |
| 26 | + Name: "version", |
| 27 | + }, |
| 28 | + Spec: configv1.ClusterVersionSpec{ |
| 29 | + DesiredUpdate: &configv1.Update{ |
| 30 | + AcceptRisks: []configv1.AcceptRisk{{ |
| 31 | + Name: "SomeInfrastructureThing", |
| 32 | + }, { |
| 33 | + Name: "SomeChannelThing", |
| 34 | + }}, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "forbid accept risks if shouldReconcileAcceptRisks is false", |
| 41 | + config: &configv1.ClusterVersion{ |
| 42 | + ObjectMeta: metav1.ObjectMeta{ |
| 43 | + Name: "version", |
| 44 | + }, |
| 45 | + Spec: configv1.ClusterVersionSpec{ |
| 46 | + DesiredUpdate: &configv1.Update{ |
| 47 | + AcceptRisks: []configv1.AcceptRisk{{ |
| 48 | + Name: "SomeInfrastructureThing", |
| 49 | + }, { |
| 50 | + Name: "SomeChannelThing", |
| 51 | + }}, |
| 52 | + }, |
| 53 | + }, |
| 54 | + }, |
| 55 | + expect: append(make(field.ErrorList, 0), field.Forbidden(field.NewPath("spec", "desiredUpdate", "acceptRisks"), "acceptRisks must not be specified")), |
| 56 | + }, |
| 57 | + { |
| 58 | + name: "empty desired update is not allowed if shouldReconcileAcceptRisks is true", |
| 59 | + shouldReconcileAcceptRisks: true, |
| 60 | + config: &configv1.ClusterVersion{ |
| 61 | + ObjectMeta: metav1.ObjectMeta{ |
| 62 | + Name: "version", |
| 63 | + }, |
| 64 | + Spec: configv1.ClusterVersionSpec{DesiredUpdate: &configv1.Update{}}, |
| 65 | + }, |
| 66 | + expect: append(make(field.ErrorList, 0), field.Required(field.NewPath("spec", "desiredUpdate"), "must specify architecture, version, image, or acceptRisks")), |
| 67 | + }, |
| 68 | + { |
| 69 | + name: "empty desired update is not allowed if shouldReconcileAcceptRisks is false", |
| 70 | + config: &configv1.ClusterVersion{ |
| 71 | + ObjectMeta: metav1.ObjectMeta{ |
| 72 | + Name: "version", |
| 73 | + }, |
| 74 | + Spec: configv1.ClusterVersionSpec{DesiredUpdate: &configv1.Update{}}, |
| 75 | + }, |
| 76 | + expect: append(make(field.ErrorList, 0), field.Required(field.NewPath("spec", "desiredUpdate"), "must specify architecture, version, or image")), |
| 77 | + }, |
| 78 | + } |
| 79 | + for _, test := range tests { |
| 80 | + t.Run(test.name, func(t *testing.T) { |
| 81 | + actual := ValidateClusterVersion(test.config, test.shouldReconcileAcceptRisks) |
| 82 | + if diff := cmp.Diff(test.expect, actual); diff != "" { |
| 83 | + t.Errorf("(-want, +got): %s", diff) |
| 84 | + } |
| 85 | + }) |
| 86 | + } |
| 87 | +} |
0 commit comments