Skip to content

Commit 95c76c7

Browse files
Merge pull request #1301 from jiajliu/revert-1297
TRT-2511:Reapply "OTA-1604: migrate ocp-46922 from otp to cvo repo"
2 parents e444b8e + eda0390 commit 95c76c7

3 files changed

Lines changed: 174 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: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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

713
"github.com/openshift/cluster-version-operator/test/oc"
814
ocapi "github.com/openshift/cluster-version-operator/test/oc/api"
15+
"github.com/openshift/cluster-version-operator/test/util"
916
)
1017

1118
var logger = g.GinkgoLogr.WithName("cluster-version-operator-tests")
1219

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

test/util/util.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package util
2+
3+
import (
4+
"context"
5+
"time"
6+
7+
g "github.com/onsi/ginkgo/v2"
8+
configv1 "github.com/openshift/api/config/v1"
9+
clientconfigv1 "github.com/openshift/client-go/config/clientset/versioned"
10+
corev1 "k8s.io/api/core/v1"
11+
apierrors "k8s.io/apimachinery/pkg/api/errors"
12+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
13+
"k8s.io/apimachinery/pkg/util/wait"
14+
"k8s.io/client-go/kubernetes"
15+
"k8s.io/client-go/rest"
16+
"k8s.io/client-go/tools/clientcmd"
17+
)
18+
19+
// IsHypershift checks if running on a HyperShift hosted cluster
20+
// Refer to https://github.com/openshift/origin/blob/31704414237b8bd5c66ad247c105c94abc9470b1/test/extended/util/framework.go#L2301
21+
func IsHypershift(ctx context.Context, restConfig *rest.Config) (bool, error) {
22+
configClient, err := GetConfigClient(restConfig)
23+
if err != nil {
24+
return false, err
25+
}
26+
infrastructure, err := configClient.ConfigV1().Infrastructures().Get(ctx, "cluster", metav1.GetOptions{})
27+
if err != nil {
28+
// If the Infrastructure resource doesn't exist (e.g., in MicroShift),it's not a HyperShift
29+
if apierrors.IsNotFound(err) {
30+
return false, nil
31+
}
32+
return false, err
33+
}
34+
return infrastructure.Status.ControlPlaneTopology == configv1.ExternalTopologyMode, nil
35+
}
36+
37+
// SkipIfHypershift skips the test if running on a HyperShift hosted cluster
38+
func SkipIfHypershift(ctx context.Context, restConfig *rest.Config) error {
39+
isHypershift, err := IsHypershift(ctx, restConfig)
40+
if err != nil {
41+
return err
42+
}
43+
if isHypershift {
44+
g.Skip("Skipping test: running on HyperShift hosted cluster!")
45+
}
46+
return nil
47+
}
48+
49+
// IsMicroshift checks if running on a MicroShift cluster
50+
// Refer to https://github.com/openshift/origin/blob/31704414237b8bd5c66ad247c105c94abc9470b1/test/extended/util/framework.go#L2312
51+
func IsMicroshift(ctx context.Context, restConfig *rest.Config) (bool, error) {
52+
kubeClient, err := GetKubeClient(restConfig)
53+
if err != nil {
54+
return false, err
55+
}
56+
var cm *corev1.ConfigMap
57+
duration := 5 * time.Minute
58+
if err := wait.PollUntilContextTimeout(ctx, 10*time.Second, duration, true, func(ctx context.Context) (bool, error) {
59+
// MicroShift cluster contains "microshift-version" configmap in "kube-public" namespace
60+
var err error
61+
cm, err = kubeClient.CoreV1().ConfigMaps("kube-public").Get(ctx, "microshift-version", metav1.GetOptions{})
62+
if err == nil {
63+
return true, nil
64+
}
65+
if apierrors.IsNotFound(err) {
66+
cm = nil
67+
return true, nil
68+
}
69+
g.GinkgoLogr.Info("error accessing microshift-version configmap", "error", err)
70+
return false, nil
71+
}); err != nil {
72+
g.GinkgoLogr.Info("failed to find microshift-version configmap while polling", "duration", duration, "error", err)
73+
return false, err
74+
}
75+
if cm == nil {
76+
g.GinkgoLogr.Info("microshift-version configmap not found")
77+
return false, nil
78+
}
79+
g.GinkgoLogr.Info("MicroShift cluster detected", "version", cm.Data["version"])
80+
return true, nil
81+
}
82+
83+
// SkipIfMicroshift skips the test if running on a MicroShift cluster
84+
func SkipIfMicroshift(ctx context.Context, restConfig *rest.Config) error {
85+
isMicroshift, err := IsMicroshift(ctx, restConfig)
86+
if err != nil {
87+
return err
88+
}
89+
if isMicroshift {
90+
g.Skip("Skipping test: running on MicroShift cluster!")
91+
}
92+
return nil
93+
}
94+
95+
// GetRestConfig loads the Kubernetes REST configuration from KUBECONFIG environment variable.
96+
func GetRestConfig() (*rest.Config, error) {
97+
return clientcmd.NewNonInteractiveDeferredLoadingClientConfig(clientcmd.NewDefaultClientConfigLoadingRules(), &clientcmd.ConfigOverrides{}).ClientConfig()
98+
}
99+
100+
// GetKubeClient creates a Kubernetes client from the given REST config.
101+
func GetKubeClient(restConfig *rest.Config) (kubernetes.Interface, error) {
102+
return kubernetes.NewForConfig(restConfig)
103+
}
104+
105+
// GetConfigClient creates an OpenShift config client from the given REST config.
106+
func GetConfigClient(restConfig *rest.Config) (clientconfigv1.Interface, error) {
107+
return clientconfigv1.NewForConfig(restConfig)
108+
}

0 commit comments

Comments
 (0)