Skip to content

Commit

Permalink
Add e2e client and refactor csv and catalog source suites to use it
Browse files Browse the repository at this point in the history
Signed-off-by: perdasilva <perdasilva@redhat.com>
  • Loading branch information
perdasilva committed Mar 23, 2022
1 parent 14c0b96 commit d5b2e13
Show file tree
Hide file tree
Showing 10 changed files with 3,998 additions and 3,789 deletions.
23 changes: 14 additions & 9 deletions test/e2e/catalog_e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
crc versioned.Interface
ns corev1.Namespace
)

BeforeEach(func() {
c = newKubeClient()
crc = newCRClient()
// Create a new e2e kube client session tracking and tagging created resources
ctx.Ctx().NewClientSession()

namespaceName := genName("catsrc-e2e-")
og := operatorsv1.OperatorGroup{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -56,6 +60,9 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins

AfterEach(func() {
TeardownNamespace(ns.GetName())

// Tear down e2e client and thereby any test resources created with it
Expect(ctx.Ctx().EndClientSession()).To(Succeed())
})

It("loading between restarts", func() {
Expand Down Expand Up @@ -496,7 +503,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
},
}

addressSource, err = crc.OperatorsV1alpha1().CatalogSources(ns.GetName()).Create(context.Background(), addressSource, metav1.CreateOptions{})
err = ctx.Ctx().E2EClient().Create(context.TODO(), addressSource)
Expect(err).ShouldNot(HaveOccurred())
defer func() {
err := crc.OperatorsV1alpha1().CatalogSources(ns.GetName()).Delete(context.Background(), addressSourceName, metav1.DeleteOptions{})
Expand Down Expand Up @@ -622,7 +629,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
},
}

source, err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Create(context.Background(), source, metav1.CreateOptions{})
err := ctx.Ctx().E2EClient().Create(context.TODO(), source)
Expect(err).ShouldNot(HaveOccurred())

// Wait for a new registry pod to be created
Expand Down Expand Up @@ -757,7 +764,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
},
}

source, err = crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Create(context.Background(), source, metav1.CreateOptions{})
err = ctx.Ctx().E2EClient().Create(context.TODO(), source)
Expect(err).ShouldNot(HaveOccurred())
defer func() {
err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Delete(context.Background(), source.GetName(), metav1.DeleteOptions{})
Expand Down Expand Up @@ -914,7 +921,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
},
}

source, err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Create(context.Background(), source, metav1.CreateOptions{})
err := ctx.Ctx().E2EClient().Create(context.TODO(), source)
Expect(err).ShouldNot(HaveOccurred())
defer func() {
err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Delete(context.Background(), source.GetName(), metav1.DeleteOptions{})
Expand Down Expand Up @@ -1004,9 +1011,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
// a) the new update pod is spun up roughly in line with the registry polling interval
// b) the update pod is removed quickly when the image is found to not have changed
// This is more of a behavioral test that ensures the feature is working as designed.

c := newKubeClient()
crc := newCRClient()

sourceName := genName("catalog-")
source := &v1alpha1.CatalogSource{
Expand All @@ -1030,7 +1035,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins
},
}

source, err := crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Create(context.Background(), source, metav1.CreateOptions{})
err := ctx.Ctx().E2EClient().Create(context.TODO(), source)
Expect(err).ToNot(HaveOccurred())

// wait for new catalog source pod to be created and report ready
Expand Down Expand Up @@ -1095,7 +1100,7 @@ var _ = Describe("Catalog represents a store of bundles which OLM can use to ins

var err error
Eventually(func() error {
source, err = crc.OperatorsV1alpha1().CatalogSources(source.GetNamespace()).Create(context.Background(), source, metav1.CreateOptions{})
err := ctx.Ctx().E2EClient().Create(context.TODO(), source)
return err
}).Should(Succeed())

Expand Down Expand Up @@ -1221,7 +1226,7 @@ func replicateCatalogPod(c operatorclient.ClientInterface, catalog *v1alpha1.Cat
Spec: pod.Spec,
}

copied, err = c.KubernetesInterface().CoreV1().Pods(catalog.GetNamespace()).Create(context.Background(), copied, metav1.CreateOptions{})
err = ctx.Ctx().E2EClient().Create(context.TODO(), copied)
Expect(err).ToNot(HaveOccurred())

return copied
Expand Down
260 changes: 260 additions & 0 deletions test/e2e/copied_csv_e2e_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,260 @@
package e2e

import (
"context"
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
operatorsv1 "github.com/operator-framework/api/pkg/operators/v1"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
"github.com/operator-framework/operator-lifecycle-manager/test/e2e/ctx"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
k8slabels "k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/selection"
apitypes "k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)

var _ = Describe("Disabling copied CSVs", func() {
var (
ns corev1.Namespace
csv operatorsv1alpha1.ClusterServiceVersion
)

BeforeEach(func() {
// Create a new e2e kube client session tracking and tagging created resources
ctx.Ctx().NewClientSession()

nsname := genName("csv-toggle-test-")
og := operatorsv1.OperatorGroup{
ObjectMeta: metav1.ObjectMeta{
Name: fmt.Sprintf("%s-operatorgroup", nsname),
Namespace: nsname,
},
}
ns = SetupGeneratedTestNamespaceWithOperatorGroup(nsname, og)

csv = operatorsv1alpha1.ClusterServiceVersion{
ObjectMeta: metav1.ObjectMeta{
Name: genName("csv-toggle-test-"),
Namespace: nsname,
},
Spec: operatorsv1alpha1.ClusterServiceVersionSpec{
InstallStrategy: newNginxInstallStrategy(genName("csv-toggle-test-"), nil, nil),
InstallModes: []operatorsv1alpha1.InstallMode{
{
Type: operatorsv1alpha1.InstallModeTypeAllNamespaces,
Supported: true,
},
},
},
}
err := ctx.Ctx().Client().Create(context.Background(), &csv)
Expect(err).ShouldNot(HaveOccurred())
})

AfterEach(func() {
TeardownNamespace(ns.GetName())

// Tear down e2e client and thereby any test resources created with it
Expect(ctx.Ctx().EndClientSession()).To(Succeed())
})

When("an operator is installed in AllNamespace mode", func() {
It("should have Copied CSVs in all other namespaces", func() {
Eventually(func() error {
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
if err != nil {
return err
}

var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
err = ctx.Ctx().E2EClient().List(context.TODO(), &copiedCSVs, &client.ListOptions{
LabelSelector: k8slabels.NewSelector().Add(*requirement),
})
if err != nil {
return err
}

var namespaces corev1.NamespaceList
if err := ctx.Ctx().E2EClient().List(context.TODO(), &namespaces, &client.ListOptions{}); err != nil {
return err
}

if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
}

return nil
}).Should(Succeed())
})
})

When("Copied CSVs are disabled", func() {
BeforeEach(func() {
Eventually(func() error {
var olmConfig operatorsv1.OLMConfig
if err := ctx.Ctx().E2EClient().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
ctx.Ctx().Logf("Error getting olmConfig %v", err)
return err
}

// Exit early if copied CSVs are disabled.
if !olmConfig.CopiedCSVsAreEnabled() {
return nil
}

olmConfig.Spec = operatorsv1.OLMConfigSpec{
Features: &operatorsv1.Features{
DisableCopiedCSVs: getPointer(true),
},
}

if err := ctx.Ctx().E2EClient().Update(context.TODO(), &olmConfig); err != nil {
ctx.Ctx().Logf("Error setting olmConfig %v", err)
return err
}

return nil
}).Should(Succeed())
})

It("should not have any copied CSVs", func() {
Eventually(func() error {
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
if err != nil {
return err
}

var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
err = ctx.Ctx().E2EClient().List(context.TODO(), &copiedCSVs, &client.ListOptions{
LabelSelector: k8slabels.NewSelector().Add(*requirement),
})
if err != nil {
return err
}

if numCSVs := len(copiedCSVs.Items); numCSVs != 0 {
return fmt.Errorf("Found %d copied CSVs, should be 0", numCSVs)
}
return nil
}).Should(Succeed())
})

It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
Eventually(func() error {
var olmConfig operatorsv1.OLMConfig
if err := ctx.Ctx().E2EClient().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
return err
}

foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
if foundCondition == nil {
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
}

expectedCondition := metav1.Condition{
Reason: "NoCopiedCSVsFound",
Message: "Copied CSVs are disabled and none were found for operators installed in AllNamespace mode",
Status: metav1.ConditionTrue,
}

if foundCondition.Reason != expectedCondition.Reason ||
foundCondition.Message != expectedCondition.Message ||
foundCondition.Status != expectedCondition.Status {
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
}

return nil
}).Should(Succeed())
})
})

When("Copied CSVs are toggled back on", func() {
BeforeEach(func() {
Eventually(func() error {
var olmConfig operatorsv1.OLMConfig
if err := ctx.Ctx().E2EClient().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
return err
}

// Exit early if copied CSVs are enabled.
if olmConfig.CopiedCSVsAreEnabled() {
return nil
}

olmConfig.Spec = operatorsv1.OLMConfigSpec{
Features: &operatorsv1.Features{
DisableCopiedCSVs: getPointer(false),
},
}

if err := ctx.Ctx().E2EClient().Update(context.TODO(), &olmConfig); err != nil {
return err
}

return nil
}).Should(Succeed())
})

It("should have copied CSVs in all other Namespaces", func() {
Eventually(func() error {
// find copied csvs...
requirement, err := k8slabels.NewRequirement(operatorsv1alpha1.CopiedLabelKey, selection.Equals, []string{csv.GetNamespace()})
if err != nil {
return err
}

var copiedCSVs operatorsv1alpha1.ClusterServiceVersionList
err = ctx.Ctx().E2EClient().List(context.TODO(), &copiedCSVs, &client.ListOptions{
LabelSelector: k8slabels.NewSelector().Add(*requirement),
})
if err != nil {
return err
}

var namespaces corev1.NamespaceList
if err := ctx.Ctx().E2EClient().List(context.TODO(), &namespaces, &client.ListOptions{FieldSelector: fields.SelectorFromSet(map[string]string{"status.phase": "Active"})}); err != nil {
return err
}

if len(namespaces.Items)-1 != len(copiedCSVs.Items) {
return fmt.Errorf("%d copied CSVs found, expected %d", len(copiedCSVs.Items), len(namespaces.Items)-1)
}

return nil
}).Should(Succeed())
})

It("should be reflected in the olmConfig.Status.Condition array that the expected number of copied CSVs exist", func() {
Eventually(func() error {
var olmConfig operatorsv1.OLMConfig
if err := ctx.Ctx().E2EClient().Get(context.TODO(), apitypes.NamespacedName{Name: "cluster"}, &olmConfig); err != nil {
return err
}
foundCondition := meta.FindStatusCondition(olmConfig.Status.Conditions, operatorsv1.DisabledCopiedCSVsConditionType)
if foundCondition == nil {
return fmt.Errorf("%s condition not found", operatorsv1.DisabledCopiedCSVsConditionType)
}

expectedCondition := metav1.Condition{
Reason: "CopiedCSVsEnabled",
Message: "Copied CSVs are enabled and present across the cluster",
Status: metav1.ConditionFalse,
}

if foundCondition.Reason != expectedCondition.Reason ||
foundCondition.Message != expectedCondition.Message ||
foundCondition.Status != expectedCondition.Status {
return fmt.Errorf("condition does not have expected reason, message, and status. Expected %v, got %v", expectedCondition, foundCondition)
}

return nil
}).Should(Succeed())
})
})
})
Loading

0 comments on commit d5b2e13

Please sign in to comment.