Skip to content

Commit 1102f9c

Browse files
committed
Finished first E2E test case by openshift/client-go and Kubernetes/client-go
openshift/client-go gives us more efficient functions to access cluster
1 parent 7201f45 commit 1102f9c

File tree

3 files changed

+103
-9
lines changed

3 files changed

+103
-9
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[
22
{
3-
"name": "[Jira:\"Cluster Version Operator\"] cluster-version-operator-tests should support passing tests the sanity test should pass",
3+
"name": "[Jira:\"Cluster Version Operator\"] cluster-version-operator-tests should support passing tests",
44
"labels": {},
55
"resources": {
66
"isolation": {}

test/cvo/cvo.go

Lines changed: 65 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,72 @@
11
package cvo
22

33
import (
4-
. "github.com/onsi/ginkgo/v2"
5-
. "github.com/onsi/gomega"
4+
"context"
5+
"fmt"
6+
7+
g "github.com/onsi/ginkgo/v2"
8+
o "github.com/onsi/gomega"
9+
kerrors "k8s.io/apimachinery/pkg/api/errors"
10+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
11+
12+
"github.com/openshift/cluster-version-operator/test/utilities"
613
)
714

8-
var _ = Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator-tests`, func() {
9-
It("should support passing tests", func() {
10-
Expect(true).To(BeTrue())
15+
var _ = g.Describe(`[Jira:"Cluster Version Operator"] cluster-version-operator-tests`, g.Label("cvo"), func() {
16+
g.It("should support passing tests", func() {
17+
o.Expect(true).To(o.BeTrue())
18+
})
19+
20+
g.It(`should not install resources annotated with release.openshift.io/delete=true`, g.Label("Conformance", "High", "42543"), func() {
21+
annotation := "release.openshift.io/delete"
22+
23+
client := utilities.MustGetV1Client()
24+
auths, err := client.Authentications().List(context.TODO(), metav1.ListOptions{})
25+
o.Expect(kerrors.IsNotFound(err)).To(o.BeFalse(), "The NotFound error should occur when lising authentications")
26+
27+
fmt.Println("Test Authentication...")
28+
for _, auth := range auths.Items {
29+
if _, ok := auth.Annotations[annotation]; ok {
30+
o.Expect(ok).NotTo(o.BeTrue(), fmt.Sprintf("Unexpectedly installed authentication %s which has release.openshift.io/delete annotation", auth.Name))
31+
}
32+
}
33+
34+
kubeclient := utilities.MustGetKubeClient()
35+
namespaces, err := kubeclient.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{})
36+
o.Expect(kerrors.IsNotFound(err)).To(o.BeFalse(), "The NotFound error should occur when lising namespaces")
37+
38+
for _, ns := range namespaces.Items {
39+
namespace := ns.Name
40+
fmt.Printf("namespace: %s\n", namespace)
41+
42+
fmt.Println(" - Test services...")
43+
services, err := kubeclient.CoreV1().Services(namespace).List(context.TODO(), metav1.ListOptions{})
44+
o.Expect(kerrors.IsNotFound(err)).To(o.BeFalse(), "The NotFound error should occur when lising services")
45+
for _, service := range services.Items {
46+
if _, ok := service.Annotations[annotation]; ok {
47+
o.Expect(ok).NotTo(o.BeTrue(), fmt.Sprintf("Unexpectedly installed service %s which has release.openshift.io/delete annotation", service.Name))
48+
}
49+
}
50+
51+
fmt.Println(" - Test RoleBinding...")
52+
rolebindings, err := kubeclient.RbacV1().RoleBindings(namespace).List(context.TODO(), metav1.ListOptions{})
53+
o.Expect(kerrors.IsNotFound(err)).To(o.BeFalse(), "The NotFound error should occur when lising rolebindings")
54+
for _, rb := range rolebindings.Items {
55+
if _, ok := rb.Annotations[annotation]; ok {
56+
o.Expect(ok).NotTo(o.BeTrue(), fmt.Sprintf("Unexpectedly installed RoleBinding %s which has release.openshift.io/delete annotation", rb.Name))
57+
}
58+
}
59+
60+
fmt.Println(" - Test CronJob...")
61+
cronjobs, err := kubeclient.BatchV1().CronJobs(namespace).List(context.TODO(), metav1.ListOptions{})
62+
o.Expect(kerrors.IsNotFound(err)).To(o.BeFalse(), "The NotFound error should occur when lising cronjobs")
63+
for _, cj := range cronjobs.Items {
64+
if _, ok := cj.Annotations[annotation]; ok {
65+
o.Expect(ok).NotTo(o.BeTrue(), fmt.Sprintf("Unexpectedly installed CronJob %s which has release.openshift.io/delete annotation", cj.Name))
66+
}
67+
}
68+
69+
fmt.Println("success")
70+
}
1171
})
1272
})

test/utilities/connection.go

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,25 @@ import (
66
"os"
77

88
"k8s.io/client-go/kubernetes"
9+
"k8s.io/client-go/rest"
910
"k8s.io/client-go/tools/clientcmd"
11+
12+
configclientv1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
1013
)
1114

12-
// getKubeClient creates a Kubernetes clientset.
13-
func getKubeClient() (*kubernetes.Clientset, error) {
15+
// getKubeConfig get KUBECONFIG file from environment variable
16+
func getKubeConfig() (*rest.Config, error) {
1417
configPath, present := os.LookupEnv("KUBECONFIG")
1518
if !present {
1619
return nil, errors.New("the environment variable KUBECONFIG must be set")
1720
}
1821
config, err := clientcmd.BuildConfigFromFlags("", configPath)
22+
return config, err
23+
}
24+
25+
// getKubeClient creates a kubernetes.Clientset instance.
26+
func getKubeClient() (*kubernetes.Clientset, error) {
27+
config, err := getKubeConfig()
1928
if err != nil {
2029
return nil, fmt.Errorf("unable to load build config: %w", err)
2130
}
@@ -24,14 +33,39 @@ func getKubeClient() (*kubernetes.Clientset, error) {
2433
if err != nil {
2534
return nil, fmt.Errorf("unable to create a Kubernetes clientset: %w", err)
2635
}
36+
2737
return clientset, nil
2838
}
2939

30-
// MustGetKubeClient creates a Kubernetes clientset, or panics on failures.
40+
// getV1Client creates a configclientv1.ConfigV1Client instance.
41+
func getV1Client() (*configclientv1.ConfigV1Client, error) {
42+
config, err := getKubeConfig()
43+
if err != nil {
44+
return nil, fmt.Errorf("unable to load build config: %w", err)
45+
}
46+
// Create the Clientset
47+
clientset, err := configclientv1.NewForConfig(config)
48+
if err != nil {
49+
return nil, fmt.Errorf("unable to create a configclientv1 clientset: %w", err)
50+
}
51+
52+
return clientset, nil
53+
}
54+
55+
// MustGetKubeClient creates a kubernetes.Clientset instance, or panics on failures.
3156
func MustGetKubeClient() *kubernetes.Clientset {
3257
clientset, err := getKubeClient()
3358
if err != nil {
3459
panic("unable to create a Kubernetes clientset: " + err.Error())
3560
}
3661
return clientset
3762
}
63+
64+
// MustGetV1Client creates a configclientv1.ConfigV1Client instance, or panics on failures.
65+
func MustGetV1Client() *configclientv1.ConfigV1Client {
66+
clientset, err := getV1Client()
67+
if err != nil {
68+
panic("unable to create a configclientv1 clientset: " + err.Error())
69+
}
70+
return clientset
71+
}

0 commit comments

Comments
 (0)