Skip to content

Commit e35f937

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 e35f937

File tree

4 files changed

+120
-9
lines changed

4 files changed

+120
-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": {}

cmd/cluster-version-operator-tests/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ After [installing-ginkgo](https://onsi.github.io/ginkgo/#installing-ginkgo):
3232
```console
3333
$ ginkgo ./test/...
3434
```
35+
or run a specific test
36+
```console
37+
$ ginkgo --focus "<test case name>" ./test/...
38+
```
39+
`test case name` is the text in g.It()
3540

3641
The output looks nicer this way.
3742

test/cvo/cvo.go

Lines changed: 77 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,84 @@
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+
"k8s.io/client-go/kubernetes"
12+
13+
v1 "github.com/openshift/client-go/config/clientset/versioned/typed/config/v1"
14+
"github.com/openshift/cluster-version-operator/test/utilities"
615
)
716

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

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)