Skip to content

Commit 8887105

Browse files
committed
add e2e test
1 parent 56aa8bc commit 8887105

File tree

4 files changed

+220
-11
lines changed

4 files changed

+220
-11
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
apiVersion: operators.coreos.com/v1
2+
kind: OLMConfig
3+
metadata:
4+
name: cluster
5+
spec:
6+
features:
7+
disableCopiedCSVs: false

pkg/controller/operators/openshift/suite_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
. "github.com/onsi/gomega"
1111
configv1 "github.com/openshift/api/config/v1"
1212
"github.com/operator-framework/api/crds"
13+
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1314
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1415
"k8s.io/client-go/rest"
1516
ctrl "sigs.k8s.io/controller-runtime"
@@ -54,8 +55,8 @@ var _ = BeforeSuite(func() {
5455
base := filepath.Join("..", "..", "..", "..", "vendor", "github.com", "openshift", "api", "config", "v1")
5556
testEnv = &envtest.Environment{
5657
ErrorIfCRDPathMissing: true,
57-
CRDs: []client.Object{
58-
crds.ClusterServiceVersion(),
58+
CRDs: []apiextensionsv1.CustomResourceDefinition{
59+
*crds.ClusterServiceVersion(),
5960
},
6061
CRDDirectoryPaths: []string{
6162
filepath.Join(base, "0000_00_cluster-version-operator_01_clusteroperator.crd.yaml"),

pkg/controller/operators/suite_test.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,14 +88,15 @@ var _ = BeforeSuite(func() {
8888
useExisting := false
8989
testEnv = &envtest.Environment{
9090
UseExistingCluster: &useExisting,
91-
CRDs: []client.Object{
92-
crds.CatalogSource(),
93-
crds.ClusterServiceVersion(),
94-
crds.InstallPlan(),
95-
crds.Subscription(),
96-
crds.OperatorGroup(),
97-
crds.Operator(),
98-
crds.OperatorCondition(),
91+
CRDs: []apiextensionsv1.CustomResourceDefinition{
92+
*crds.CatalogSource(),
93+
*crds.ClusterServiceVersion(),
94+
*crds.InstallPlan(),
95+
*crds.Subscription(),
96+
*crds.OperatorGroup(),
97+
*crds.Operator(),
98+
*crds.OperatorCondition(),
99+
*crds.OLMConfig(),
99100
},
100101
}
101102

test/e2e/csv_e2e_test.go

Lines changed: 201 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,13 @@ import (
1818
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
1919
"k8s.io/apimachinery/pkg/api/equality"
2020
k8serrors "k8s.io/apimachinery/pkg/api/errors"
21+
"k8s.io/apimachinery/pkg/api/meta"
2122
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/labels"
2224
k8slabels "k8s.io/apimachinery/pkg/labels"
2325
"k8s.io/apimachinery/pkg/runtime"
26+
"k8s.io/apimachinery/pkg/selection"
27+
apitypes "k8s.io/apimachinery/pkg/types"
2428
"k8s.io/apimachinery/pkg/util/diff"
2529
"k8s.io/apimachinery/pkg/util/intstr"
2630
"k8s.io/apimachinery/pkg/util/wait"
@@ -38,7 +42,7 @@ import (
3842
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
3943
)
4044

41-
var _ = Describe("ClusterServiceVersion", func() {
45+
var _ = FDescribe("ClusterServiceVersion", func() {
4246
HavePhase := func(goal operatorsv1alpha1.ClusterServiceVersionPhase) types.GomegaMatcher {
4347
return WithTransform(func(csv *operatorsv1alpha1.ClusterServiceVersion) operatorsv1alpha1.ClusterServiceVersionPhase {
4448
return csv.Status.Phase
@@ -4170,6 +4174,202 @@ var _ = Describe("ClusterServiceVersion", func() {
41704174

41714175
Expect(apiService2.Spec.CABundle).Should(Equal(apiService1.Spec.CABundle))
41724176
})
4177+
4178+
It("Disabling copied csvs", func() {
4179+
ns := &corev1.Namespace{
4180+
ObjectMeta: metav1.ObjectMeta{
4181+
Name: genName("csv-toggle-test-"),
4182+
},
4183+
}
4184+
4185+
// Create a new operator group for the new namespace
4186+
operatorGroup := v1.OperatorGroup{
4187+
ObjectMeta: metav1.ObjectMeta{
4188+
Name: genName("csv-toggle-test-"),
4189+
Namespace: ns.GetName(),
4190+
},
4191+
}
4192+
4193+
csv := v1alpha1.ClusterServiceVersion{
4194+
ObjectMeta: metav1.ObjectMeta{
4195+
Name: genName("csv-toggle-test-"),
4196+
Namespace: ns.GetName(),
4197+
},
4198+
Spec: v1alpha1.ClusterServiceVersionSpec{
4199+
InstallStrategy: newNginxInstallStrategy(genName("csv-toggle-test-"), nil, nil),
4200+
InstallModes: []v1alpha1.InstallMode{
4201+
{
4202+
Type: v1alpha1.InstallModeTypeAllNamespaces,
4203+
Supported: true,
4204+
},
4205+
},
4206+
},
4207+
}
4208+
Context("When an operator is installed in all namespace mode", func() {
4209+
Eventually(func() error {
4210+
if err := ctx.Ctx().Client().Create(context.TODO(), ns); err != nil && !k8serrors.IsAlreadyExists(err) {
4211+
ctx.Ctx().Logf("Unable to create ns: %v", err)
4212+
return err
4213+
}
4214+
4215+
if err := ctx.Ctx().Client().Create(context.TODO(), &operatorGroup); err != nil && !k8serrors.IsAlreadyExists(err) {
4216+
ctx.Ctx().Logf("Unable to create og: %v", err)
4217+
return err
4218+
}
4219+
4220+
if err := ctx.Ctx().Client().Create(context.TODO(), &csv); err != nil && !k8serrors.IsAlreadyExists(err) {
4221+
ctx.Ctx().Logf("Unable to create csv: %v", err)
4222+
return err
4223+
}
4224+
4225+
return nil
4226+
}).Should(Succeed())
4227+
})
4228+
4229+
Context("Copied CSVs should be generated in all other namespaces", func() {
4230+
Eventually(func() error {
4231+
requirement, err := labels.NewRequirement(v1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
4232+
if err != nil {
4233+
return err
4234+
}
4235+
4236+
var copiedCSVs v1alpha1.ClusterServiceVersionList
4237+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
4238+
LabelSelector: labels.NewSelector().Add(*requirement),
4239+
})
4240+
if err != nil {
4241+
return err
4242+
}
4243+
4244+
var namespaces corev1.NamespaceList
4245+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{}); err != nil {
4246+
return err
4247+
}
4248+
4249+
if len(namespaces.Items) != len(copiedCSVs.Items)+1 {
4250+
return fmt.Errorf("CSVs are not copied to every namespace %v vs %v", len(namespaces.Items), (copiedCSVs.Items))
4251+
}
4252+
4253+
return nil
4254+
}).Should(Succeed())
4255+
})
4256+
4257+
When("Copied CSVs should be generated in all other namespaces", func() {
4258+
Eventually(func() error {
4259+
var olmConfig v1.OLMConfig
4260+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
4261+
ctx.Ctx().Logf("Error getting olmConfig %v", err)
4262+
return err
4263+
}
4264+
4265+
olmConfig.Spec.Features.DisableCopiedCSVs = true
4266+
4267+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
4268+
ctx.Ctx().Logf("Error setting olmConfig %v", err)
4269+
return err
4270+
}
4271+
4272+
return nil
4273+
}).Should(Succeed())
4274+
})
4275+
Context("Should delete existing copied csvs", func() {
4276+
Eventually(func() error {
4277+
requirement, err := labels.NewRequirement(v1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
4278+
if err != nil {
4279+
return err
4280+
}
4281+
4282+
var copiedCSVs v1alpha1.ClusterServiceVersionList
4283+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
4284+
LabelSelector: labels.NewSelector().Add(*requirement),
4285+
})
4286+
if err != nil {
4287+
return err
4288+
}
4289+
4290+
if len(copiedCSVs.Items) != 0 {
4291+
return fmt.Errorf("Number of copied csvs should be 0")
4292+
}
4293+
return nil
4294+
}).Should(Succeed())
4295+
})
4296+
4297+
Context("Should updated the olmConfig status to reflect that the cluster is in the expected state", func() {
4298+
Eventually(func() error {
4299+
var olmConfig v1.OLMConfig
4300+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
4301+
return err
4302+
}
4303+
4304+
if !meta.IsStatusConditionPresentAndEqual(olmConfig.Status.Conditions, "ready", metav1.ConditionTrue) {
4305+
return fmt.Errorf("Copied CSVs not ready not ready")
4306+
}
4307+
4308+
return nil
4309+
}).Should(Succeed())
4310+
})
4311+
When("Copied CSVs are toggled back on", func() {
4312+
Eventually(func() error {
4313+
4314+
var olmConfig v1.OLMConfig
4315+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
4316+
return err
4317+
}
4318+
4319+
olmConfig.Spec.Features.DisableCopiedCSVs = false
4320+
4321+
if err := ctx.Ctx().Client().Update(context.TODO(), &olmConfig); err != nil {
4322+
return err
4323+
}
4324+
4325+
return nil
4326+
}).Should(Succeed())
4327+
})
4328+
4329+
Context("OLM should recreate existing copied csvs", func() {
4330+
Eventually(func() error {
4331+
// find copied csvs...
4332+
requirement, err := labels.NewRequirement(v1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
4333+
if err != nil {
4334+
return err
4335+
}
4336+
4337+
var copiedCSVs v1alpha1.ClusterServiceVersionList
4338+
err = ctx.Ctx().Client().List(context.TODO(), &copiedCSVs, &client.ListOptions{
4339+
LabelSelector: labels.NewSelector().Add(*requirement),
4340+
})
4341+
if err != nil {
4342+
return err
4343+
}
4344+
4345+
var namespaces corev1.NamespaceList
4346+
if err := ctx.Ctx().Client().List(context.TODO(), &namespaces, &client.ListOptions{}); err != nil {
4347+
return err
4348+
}
4349+
4350+
if len(namespaces.Items) != len(copiedCSVs.Items)+1 {
4351+
return fmt.Errorf("CSVs are not copied to every namespace")
4352+
}
4353+
4354+
return nil
4355+
}).Should(Succeed())
4356+
})
4357+
4358+
Context("Should updated the olmConfig status to reflect that the cluster is in the expected state", func() {
4359+
Eventually(func() error {
4360+
var olmConfig v1.OLMConfig
4361+
if err := ctx.Ctx().Client().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
4362+
return err
4363+
}
4364+
4365+
if !meta.IsStatusConditionPresentAndEqual(olmConfig.Status.Conditions, "ready", metav1.ConditionTrue) {
4366+
return fmt.Errorf("Copied CSVs not ready not ready")
4367+
}
4368+
4369+
return nil
4370+
}).Should(Succeed())
4371+
})
4372+
})
41734373
})
41744374

41754375
var singleInstance = int32(1)

0 commit comments

Comments
 (0)