Skip to content

Commit b3debe7

Browse files
committed
Reapply "OTA-1604: migrate ocp-46922 from otp to cvo repo"
This reverts commit 278da7b.
1 parent e444b8e commit b3debe7

3 files changed

Lines changed: 119 additions & 0 deletions

File tree

.openshift-tests-extension/openshift_payload_cluster-version-operator.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,15 @@
1818
"source": "openshift:payload:cluster-version-operator",
1919
"lifecycle": "blocking",
2020
"environmentSelector": {}
21+
},
22+
{
23+
"name": "[Jira:\"Cluster Version Operator\"] cluster-version-operator should have correct runlevel and scc",
24+
"labels": {},
25+
"resources": {
26+
"isolation": {}
27+
},
28+
"source": "openshift:payload:cluster-version-operator",
29+
"lifecycle": "blocking",
30+
"environmentSelector": {}
2131
}
2232
]

test/cvo/cvo.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
package cvo
22

33
import (
4+
"context"
5+
6+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
7+
"k8s.io/client-go/kubernetes"
8+
"k8s.io/client-go/rest"
9+
410
g "github.com/onsi/ginkgo/v2"
511
o "github.com/onsi/gomega"
612

@@ -10,6 +16,8 @@ import (
1016

1117
var logger = g.GinkgoLogr.WithName("cluster-version-operator-tests")
1218

19+
const cvoNamespace = "openshift-cluster-version"
20+
1321
var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator-tests`, func() {
1422
g.It("should support passing tests", func() {
1523
o.Expect(true).To(o.BeTrue())
@@ -25,3 +33,48 @@ var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator-t
2533
o.Expect(output).To(o.ContainSubstring("Client Version:"))
2634
})
2735
})
36+
37+
// CVO tests which need access the live cluster will be placed here
38+
var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator`, func() {
39+
var (
40+
restCfg *rest.Config
41+
kubeClient kubernetes.Interface
42+
)
43+
44+
g.BeforeEach(func() {
45+
var err error
46+
// Respects KUBECONFIG env var
47+
restCfg, err = GetRestConfig()
48+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to load Kubernetes configuration. Please ensure KUBECONFIG environment variable is set.")
49+
50+
kubeClient, err = GetKubeClient(restCfg)
51+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create Kubernetes client")
52+
})
53+
54+
// Migrated from case NonHyperShiftHOST-Author:jiajliu-Low-46922-check runlevel and scc in cvo ns
55+
// Refer to https://github.com/openshift/openshift-tests-private/blob/40374cf20946ff03c88712839a5626af2c88ab31/test/extended/ota/cvo/cvo.go#L1081
56+
g.It("should have correct runlevel and scc", func() {
57+
ctx := context.Background()
58+
err := SkipIfHypershift(ctx, restCfg)
59+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to determine if cluster is HyperShift")
60+
61+
g.By("Checking that the 'openshift.io/run-level' label exists on the namespace and has the empty value")
62+
ns, err := kubeClient.CoreV1().Namespaces().Get(ctx, cvoNamespace, metav1.GetOptions{})
63+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to get namespace %s", cvoNamespace)
64+
runLevel, exists := ns.ObjectMeta.Labels["openshift.io/run-level"]
65+
o.Expect(exists).To(o.BeTrue(), "The 'openshift.io/run-level' label on namespace %s does not exist", cvoNamespace)
66+
o.Expect(runLevel).To(o.BeEmpty(), "Expected the 'openshift.io/run-level' label value on namespace %s has the empty value, but got %s", cvoNamespace, runLevel)
67+
68+
g.By("Checking that the annotation 'openshift.io/scc annotation' on the CVO pod has the value hostaccess")
69+
podList, err := kubeClient.CoreV1().Pods(cvoNamespace).List(ctx, metav1.ListOptions{
70+
LabelSelector: "k8s-app=cluster-version-operator",
71+
FieldSelector: "status.phase=Running",
72+
})
73+
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to list running CVO pods")
74+
o.Expect(podList.Items).To(o.HaveLen(1), "Expected exactly one running CVO pod, but found: %d", len(podList.Items))
75+
76+
cvoPod := podList.Items[0]
77+
sccAnnotation := cvoPod.ObjectMeta.Annotations["openshift.io/scc"]
78+
o.Expect(sccAnnotation).To(o.Equal("hostaccess"), "Expected the annotation 'openshift.io/scc annotation' on pod %s to have the value 'hostaccess', but got %s", cvoPod.Name, sccAnnotation)
79+
})
80+
})

test/cvo/util.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package cvo
2+
3+
import (
4+
"context"
5+
6+
. "github.com/onsi/ginkgo/v2"
7+
configv1 "github.com/openshift/api/config/v1"
8+
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/client-go/kubernetes"
11+
"k8s.io/client-go/rest"
12+
"k8s.io/client-go/tools/clientcmd"
13+
)
14+
15+
// IsHypershift checks if running on a HyperShift hosted cluster
16+
// Refer to https://github.com/openshift/origin/blob/31704414237b8bd5c66ad247c105c94abc9470b1/test/extended/util/framework.go#L2301
17+
func IsHypershift(ctx context.Context, restConfig *rest.Config) (bool, error) {
18+
configClient, err := GetConfigClient(restConfig)
19+
if err != nil {
20+
return false, err
21+
}
22+
23+
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
24+
if err != nil {
25+
return false, err
26+
}
27+
28+
return infrastructure.Status.ControlPlaneTopology == configv1.ExternalTopologyMode, nil
29+
}
30+
31+
// SkipIfHypershift skips the test if running on a HyperShift hosted cluster
32+
func SkipIfHypershift(ctx context.Context, restConfig *rest.Config) error {
33+
isHypershift, err := IsHypershift(ctx, restConfig)
34+
if err != nil {
35+
return err
36+
}
37+
if isHypershift {
38+
Skip("Skipping test: running on HyperShift hosted cluster!")
39+
}
40+
return nil
41+
}
42+
43+
// GetRestConfig loads the Kubernetes REST configuration from KUBECONFIG environment variable.
44+
func GetRestConfig() (*rest.Config, error) {
45+
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig()
46+
}
47+
48+
// GetKubeClient creates a Kubernetes client from the given REST config.
49+
func GetKubeClient(restConfig *rest.Config) (kubernetes.Interface, error) {
50+
return kubernetes.NewForConfig(restConfig)
51+
}
52+
53+
// GetConfigClient creates an OpenShift config client from the given REST config.
54+
func GetConfigClient(restConfig *rest.Config) (clientconfigv1.Interface, error) {
55+
return clientconfigv1.NewForConfig(restConfig)
56+
}

0 commit comments

Comments
 (0)