-
Notifications
You must be signed in to change notification settings - Fork 72
OCPBUGS-858: Package Server Manager should enforce expected csv values #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,8 +3,10 @@ package controllers | |
import ( | ||
"testing" | ||
|
||
semver "github.com/blang/semver/v4" | ||
configv1 "github.com/openshift/api/config/v1" | ||
"github.com/openshift/operator-framework-olm/pkg/manifests" | ||
"github.com/operator-framework/api/pkg/lib/version" | ||
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1" | ||
"github.com/stretchr/testify/require" | ||
appsv1 "k8s.io/api/apps/v1" | ||
|
@@ -72,10 +74,39 @@ func intOrStr(val int) *intstr.IntOrString { | |
return &tmp | ||
} | ||
|
||
type testCSVOption func(*olmv1alpha1.ClusterServiceVersion) | ||
|
||
func withVersion(v semver.Version) func(*olmv1alpha1.ClusterServiceVersion) { | ||
return func(csv *olmv1alpha1.ClusterServiceVersion) { | ||
csv.Spec.Version = version.OperatorVersion{v} | ||
} | ||
} | ||
|
||
func withDescription(description string) func(*olmv1alpha1.ClusterServiceVersion) { | ||
return func(csv *olmv1alpha1.ClusterServiceVersion) { | ||
csv.Spec.Description = description | ||
} | ||
} | ||
|
||
func withAffinity(affinity *corev1.Affinity) func(*olmv1alpha1.ClusterServiceVersion) { | ||
return func(csv *olmv1alpha1.ClusterServiceVersion) { | ||
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Template.Spec.Affinity = affinity | ||
} | ||
} | ||
func withRollingUpdateStrategy(strategy *appsv1.RollingUpdateDeployment) func(*olmv1alpha1.ClusterServiceVersion) { | ||
return func(csv *olmv1alpha1.ClusterServiceVersion) { | ||
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Strategy.RollingUpdate = strategy | ||
} | ||
} | ||
|
||
func withReplicas(replicas *int32) func(*olmv1alpha1.ClusterServiceVersion) { | ||
return func(csv *olmv1alpha1.ClusterServiceVersion) { | ||
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec.Replicas = replicas | ||
} | ||
} | ||
|
||
func newTestCSV( | ||
replicas *int32, | ||
strategy *appsv1.RollingUpdateDeployment, | ||
affinity *corev1.Affinity, | ||
options ...testCSVOption, | ||
) *olmv1alpha1.ClusterServiceVersion { | ||
csv, err := manifests.NewPackageServerCSV( | ||
manifests.WithName(name), | ||
|
@@ -84,11 +115,10 @@ func newTestCSV( | |
if err != nil { | ||
return nil | ||
} | ||
deployment := csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec | ||
deployment.Template.Spec.Affinity = affinity | ||
deployment.Replicas = replicas | ||
deployment.Strategy.RollingUpdate = strategy | ||
csv.Spec.InstallStrategy.StrategySpec.DeploymentSpecs[0].Spec = deployment | ||
|
||
for _, o := range options { | ||
o(csv) | ||
} | ||
|
||
return csv | ||
} | ||
|
@@ -133,83 +163,104 @@ func TestEnsureCSV(t *testing.T) { | |
singleReplicas := pointer.Int32(singleReplicaCount) | ||
image := getImageFromManifest() | ||
|
||
type wanted struct { | ||
expectedBool bool | ||
expectedErr error | ||
} | ||
|
||
tt := []struct { | ||
name string | ||
inputCSV *olmv1alpha1.ClusterServiceVersion | ||
expectedCSV *olmv1alpha1.ClusterServiceVersion | ||
highlyAvailable bool | ||
want bool | ||
want wanted | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. all test cases seem to set expectedErr to nil. Is it worth exercising the failure path as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's correct, all tests expect no error from this method. The request to add an error testcase seems reasonable, but the only instance where the If you feel strongly that we should test this path, I can update it when I'm back from vacation next week. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. my preference would be to not increase the scope of this PR either, specially with the need to backport in mind. We could always have a follow up PR for the test |
||
}{ | ||
{ | ||
name: "Modified/HighlyAvailable/CorrectReplicasIncorrectRolling", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: true, | ||
inputCSV: newTestCSV(defaultReplicas, emptyRollout, defaultAffinity), | ||
expectedCSV: newTestCSV(defaultReplicas, defaultRollout, defaultAffinity), | ||
inputCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(defaultAffinity)), | ||
expectedCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
}, | ||
{ | ||
name: "Modified/HighlyAvailable/IncorrectReplicasCorrectRolling", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: true, | ||
inputCSV: newTestCSV(singleReplicas, defaultRollout, defaultAffinity), | ||
expectedCSV: newTestCSV(defaultReplicas, defaultRollout, defaultAffinity), | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
expectedCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
}, | ||
{ | ||
name: "Modified/HighlyAvailable/IncorrectPodAntiAffinity", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: true, | ||
inputCSV: newTestCSV(singleReplicas, defaultRollout, newPodAffinity(&corev1.PodAntiAffinity{ | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(newPodAffinity(&corev1.PodAntiAffinity{ | ||
PreferredDuringSchedulingIgnoredDuringExecution: []corev1.WeightedPodAffinityTerm{ | ||
{ | ||
Weight: 1, | ||
}, | ||
}, | ||
})), | ||
expectedCSV: newTestCSV(defaultReplicas, defaultRollout, defaultAffinity), | ||
}))), | ||
expectedCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
}, | ||
{ | ||
name: "NotModified/HighlyAvailable", | ||
want: false, | ||
want: wanted{false, nil}, | ||
highlyAvailable: true, | ||
inputCSV: newTestCSV(defaultReplicas, defaultRollout, defaultAffinity), | ||
expectedCSV: newTestCSV(defaultReplicas, defaultRollout, defaultAffinity), | ||
inputCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
expectedCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(defaultAffinity)), | ||
}, | ||
|
||
{ | ||
name: "Modified/SingleReplica/CorrectReplicasIncorrectRolling", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(singleReplicas, defaultRollout, &corev1.Affinity{}), | ||
expectedCSV: newTestCSV(singleReplicas, emptyRollout, &corev1.Affinity{}), | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(defaultRollout), withAffinity(&corev1.Affinity{})), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
{ | ||
name: "Modified/SingleReplica/IncorrectReplicasCorrectRolling", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(defaultReplicas, emptyRollout, &corev1.Affinity{}), | ||
expectedCSV: newTestCSV(singleReplicas, emptyRollout, &corev1.Affinity{}), | ||
inputCSV: newTestCSV(withReplicas(defaultReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
{ | ||
name: "Modified/SingleReplica/IncorrectPodAntiAffinity", | ||
want: true, | ||
want: wanted{true, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(defaultAffinity)), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
{ | ||
name: "Modified/SingleReplica/IncorrectVersion", | ||
want: wanted{true, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{}), withVersion(semver.Version{Major: 0, Minor: 0, Patch: 0})), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
{ | ||
name: "Modified/SingleReplica/IncorrectDescription", | ||
want: wanted{true, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(singleReplicas, emptyRollout, defaultAffinity), | ||
expectedCSV: newTestCSV(singleReplicas, emptyRollout, &corev1.Affinity{}), | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{}), withDescription("foo")), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
{ | ||
name: "NotModified/SingleReplica", | ||
want: false, | ||
want: wanted{false, nil}, | ||
highlyAvailable: false, | ||
inputCSV: newTestCSV(singleReplicas, emptyRollout, &corev1.Affinity{}), | ||
expectedCSV: newTestCSV(singleReplicas, emptyRollout, &corev1.Affinity{}), | ||
inputCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
expectedCSV: newTestCSV(withReplicas(singleReplicas), withRollingUpdateStrategy(emptyRollout), withAffinity(&corev1.Affinity{})), | ||
}, | ||
} | ||
|
||
for _, tc := range tt { | ||
tc := tc | ||
|
||
t.Run(tc.name, func(t *testing.T) { | ||
got := ensureCSV(logger, image, tc.inputCSV, tc.highlyAvailable) | ||
require.EqualValues(t, tc.want, got) | ||
gotBool, gotErr := ensureCSV(logger, image, tc.inputCSV, tc.highlyAvailable) | ||
require.EqualValues(t, tc.want.expectedBool, gotBool) | ||
require.EqualValues(t, tc.want.expectedErr, gotErr) | ||
require.EqualValues(t, tc.inputCSV.Spec, tc.expectedCSV.Spec) | ||
}) | ||
} | ||
|
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should add a check that the csv's annotations and label maps are not nil @grokspawn. If they are nil, set them to new maps and populate with the following for loops.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
'''
if csv.GetAnnotations() == nil {
csv.SetAnnotations(make(map[string]string))
modified = true
}
'''
Could add a check if the expectedCSV has annotations, but it always should.