Skip to content

Commit

Permalink
[test] Fix upgradeable condition test
Browse files Browse the repository at this point in the history
This commit fixes an issue where the upgradeable condition was
not being tested in the e2e test suite. This was the case
because the OPERATOR_CONDITION_NAME env var is not being set in
the CI env when using the OLM upgrade workflow. The recommendation
to fix this was to instead get it from the deployment spec as the
condition is present once the operator is deployed. The cache
client had to be replaced with the OLM client to get the operator
condition using current context.

(cherry picked from commit 0ffe33d)
  • Loading branch information
mansikulkarni96 committed Nov 8, 2022
1 parent 47f3d98 commit cee94c5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
8 changes: 4 additions & 4 deletions test/e2e/clusterinfo/openshift.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import (
mapiClient "github.com/openshift/client-go/machine/clientset/versioned/typed/machine/v1beta1"
operatorClient "github.com/openshift/client-go/operator/clientset/versioned/typed/operator/v1"
routeClient "github.com/openshift/client-go/route/clientset/versioned/typed/route/v1"
"github.com/operator-framework/operator-lifecycle-manager/pkg/api/client/clientset/versioned"
monitoringClient "github.com/prometheus-operator/prometheus-operator/pkg/client/versioned/typed/monitoring/v1"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sclient "k8s.io/client-go/kubernetes"
"sigs.k8s.io/controller-runtime/pkg/client"
ctrlruntimecfg "sigs.k8s.io/controller-runtime/pkg/client/config"
)

Expand All @@ -26,7 +26,7 @@ type OpenShift struct {
Route routeClient.RouteV1Interface
K8s k8sclient.Interface
Images imageClient.ImageV1Interface
Cache client.Client
Olm versioned.Interface
}

// GetOpenShift creates client for the current OpenShift cluster. If KUBECONFIG env var is set, it is used to
Expand Down Expand Up @@ -69,7 +69,7 @@ func GetOpenShift() (*OpenShift, error) {
if err != nil {
return nil, err
}
c, err := client.New(rc, client.Options{})
olmc, err := versioned.NewForConfig(rc)
if err != nil {
return nil, err
}
Expand All @@ -81,7 +81,7 @@ func GetOpenShift() (*OpenShift, error) {
Route: routec,
K8s: kc,
Images: ic,
Cache: c,
Olm: olmc,
}, nil
}

Expand Down
42 changes: 32 additions & 10 deletions test/e2e/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"strings"
Expand All @@ -21,7 +20,6 @@ import (
rbac "k8s.io/api/rbac/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
meta "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"

"github.com/openshift/windows-machine-config-operator/controllers"
Expand All @@ -36,6 +34,11 @@ import (
"github.com/openshift/windows-machine-config-operator/pkg/windows"
)

const (
// wmcoContainerName is the name of the container in the deployment spec of the operator
wmcoContainerName = "manager"
)

// winService contains information regarding a Windows service's current state
type winService struct {
state string
Expand Down Expand Up @@ -650,14 +653,12 @@ func (tc *testContext) findNodeCSRs(nodeName string) ([]certificates.Certificate

// validateUpgradeableCondition ensures that the operator's Upgradeable condition is correctly communicated to OLM
func (tc *testContext) validateUpgradeableCondition(expected meta.ConditionStatus) error {
ocName, present := os.LookupEnv(condition.OperatorConditionName)
if !present {
// Implies operator is not OLM-managed
return nil
}
err := wait.Poll(retry.Interval, retry.ResourceChangeTimeout, func() (bool, error) {
oc := &operators.OperatorCondition{}
err := tc.client.Cache.Get(context.TODO(), types.NamespacedName{Namespace: tc.namespace, Name: ocName}, oc)
ocName, err := tc.getOperatorConditionName()
if err != nil {
return err
}
err = wait.Poll(retry.Interval, retry.ResourceChangeTimeout, func() (bool, error) {
oc, err := tc.client.Olm.OperatorsV2().OperatorConditions(tc.namespace).Get(context.TODO(), ocName, meta.GetOptions{})
if err != nil {
log.Printf("unable to get OperatorCondition %s from namespace %s", ocName, tc.namespace)
return false, nil
Expand All @@ -672,3 +673,24 @@ func (tc *testContext) validateUpgradeableCondition(expected meta.ConditionStatu
}
return nil
}

// getOperatorConditionName returns the operator condition name using the env var present in the deployment
func (tc *testContext) getOperatorConditionName() (string, error) {
deployment, err := tc.client.K8s.AppsV1().Deployments(tc.namespace).Get(context.TODO(), resourceName,
meta.GetOptions{})
if err != nil {
return "", errors.Wrap(err, "error getting operator deployment")
}
// Get the operator condition name using the deployment spec
for _, container := range deployment.Spec.Template.Spec.Containers {
if container.Name != wmcoContainerName {
continue
}
for _, envVar := range container.Env {
if envVar.Name == condition.OperatorConditionName {
return envVar.Value, nil
}
}
}
return "", errors.Errorf("unable to get operatorCondition name from namespace %s", tc.namespace)
}

0 comments on commit cee94c5

Please sign in to comment.