Skip to content

Commit eda0390

Browse files
committed
skip microshift and handle NotFound error
1 parent b3debe7 commit eda0390

3 files changed

Lines changed: 116 additions & 61 deletions

File tree

test/cvo/cvo.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212

1313
"github.com/openshift/cluster-version-operator/test/oc"
1414
ocapi "github.com/openshift/cluster-version-operator/test/oc/api"
15+
"github.com/openshift/cluster-version-operator/test/util"
1516
)
1617

1718
var logger = g.GinkgoLogr.WithName("cluster-version-operator-tests")
@@ -44,24 +45,26 @@ var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator`,
4445
g.BeforeEach(func() {
4546
var err error
4647
// Respects KUBECONFIG env var
47-
restCfg, err = GetRestConfig()
48+
restCfg, err = util.GetRestConfig()
4849
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to load Kubernetes configuration. Please ensure KUBECONFIG environment variable is set.")
4950

50-
kubeClient, err = GetKubeClient(restCfg)
51+
kubeClient, err = util.GetKubeClient(restCfg)
5152
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to create Kubernetes client")
5253
})
5354

5455
// Migrated from case NonHyperShiftHOST-Author:jiajliu-Low-46922-check runlevel and scc in cvo ns
5556
// Refer to https://github.com/openshift/openshift-tests-private/blob/40374cf20946ff03c88712839a5626af2c88ab31/test/extended/ota/cvo/cvo.go#L1081
5657
g.It("should have correct runlevel and scc", func() {
5758
ctx := context.Background()
58-
err := SkipIfHypershift(ctx, restCfg)
59+
err := util.SkipIfHypershift(ctx, restCfg)
5960
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")
6063

6164
g.By("Checking that the 'openshift.io/run-level' label exists on the namespace and has the empty value")
6265
ns, err := kubeClient.CoreV1().Namespaces().Get(ctx, cvoNamespace, metav1.GetOptions{})
6366
o.Expect(err).NotTo(o.HaveOccurred(), "Failed to get namespace %s", cvoNamespace)
64-
runLevel, exists := ns.ObjectMeta.Labels["openshift.io/run-level"]
67+
runLevel, exists := ns.Labels["openshift.io/run-level"]
6568
o.Expect(exists).To(o.BeTrue(), "The 'openshift.io/run-level' label on namespace %s does not exist", cvoNamespace)
6669
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)
6770

@@ -74,7 +77,7 @@ var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator`,
7477
o.Expect(podList.Items).To(o.HaveLen(1), "Expected exactly one running CVO pod, but found: %d", len(podList.Items))
7578

7679
cvoPod := podList.Items[0]
77-
sccAnnotation := cvoPod.ObjectMeta.Annotations["openshift.io/scc"]
80+
sccAnnotation := cvoPod.Annotations["openshift.io/scc"]
7881
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)
7982
})
8083
})

test/cvo/util.go

Lines changed: 0 additions & 56 deletions
This file was deleted.

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)