Skip to content

Commit 9073432

Browse files
committed
Add ODH CFO test specific support functions
1 parent 93d0349 commit 9073432

File tree

5 files changed

+263
-1
lines changed

5 files changed

+263
-1
lines changed

support/authentication.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package support
18+
19+
import (
20+
"github.com/onsi/gomega"
21+
22+
authenticationv1 "k8s.io/api/authentication/v1"
23+
corev1 "k8s.io/api/core/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
)
26+
27+
func CreateToken(t Test, namespace string, serviceAccount *corev1.ServiceAccount) string {
28+
t.T().Helper()
29+
30+
treq := &authenticationv1.TokenRequest{
31+
Spec: authenticationv1.TokenRequestSpec{
32+
ExpirationSeconds: Ptr(int64(3600)),
33+
},
34+
}
35+
treq, err := t.Client().Core().CoreV1().ServiceAccounts(namespace).CreateToken(t.Ctx(), serviceAccount.Name, treq, metav1.CreateOptions{})
36+
t.Expect(err).NotTo(gomega.HaveOccurred())
37+
t.T().Logf("Created TokenRequest %s/%s successfully", treq.Namespace, treq.Name)
38+
39+
return treq.Status.Token
40+
}

support/config.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package support
18+
19+
import (
20+
"github.com/onsi/gomega"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
24+
"k8s.io/apimachinery/pkg/runtime/schema"
25+
)
26+
27+
var ingressConfigResource = schema.GroupVersionResource{Group: "config.openshift.io", Version: "v1", Resource: "ingresses"}
28+
var infrastructureConfigResource = schema.GroupVersionResource{Group: "config.openshift.io", Version: "v1", Resource: "infrastructures"}
29+
30+
func GetOpenShiftIngressDomain(test Test) string {
31+
test.T().Helper()
32+
33+
cluster, err := test.Client().Dynamic().Resource(ingressConfigResource).Get(test.Ctx(), "cluster", metav1.GetOptions{})
34+
test.Expect(err).NotTo(gomega.HaveOccurred())
35+
36+
ingressDomain, found, err := unstructured.NestedString(cluster.UnstructuredContent(), "spec", "domain")
37+
test.Expect(err).NotTo(gomega.HaveOccurred())
38+
test.Expect(found).To(gomega.BeTrue())
39+
40+
test.T().Logf("Ingress domain: %s", ingressDomain)
41+
return ingressDomain
42+
}
43+
44+
func GetOpenShiftApiUrl(test Test) string {
45+
test.T().Helper()
46+
47+
cluster, err := test.Client().Dynamic().Resource(infrastructureConfigResource).Get(test.Ctx(), "cluster", metav1.GetOptions{})
48+
test.Expect(err).NotTo(gomega.HaveOccurred())
49+
50+
openShiftApiUrl, found, err := unstructured.NestedString(cluster.UnstructuredContent(), "status", "apiServerURL")
51+
test.Expect(err).NotTo(gomega.HaveOccurred())
52+
test.Expect(found).To(gomega.BeTrue())
53+
54+
test.T().Logf("OpenShift API URL: %s", openShiftApiUrl)
55+
return openShiftApiUrl
56+
}

support/core.go

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@ import (
2323
"github.com/onsi/gomega"
2424

2525
corev1 "k8s.io/api/core/v1"
26+
"k8s.io/apimachinery/pkg/api/resource"
2627
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2728
"k8s.io/apimachinery/pkg/runtime"
2829
)
2930

3031
func CreateConfigMap(t Test, namespace string, content map[string][]byte) *corev1.ConfigMap {
32+
t.T().Helper()
33+
3134
configMap := &corev1.ConfigMap{
3235
TypeMeta: metav1.TypeMeta{
3336
APIVersion: corev1.SchemeGroupVersion.String(),
@@ -98,7 +101,10 @@ func storeContainerLog(t Test, namespace *corev1.Namespace, podName, containerNa
98101

99102
options := corev1.PodLogOptions{Container: containerName}
100103
stream, err := t.Client().Core().CoreV1().Pods(namespace.Name).GetLogs(podName, &options).Stream(t.Ctx())
101-
t.Expect(err).NotTo(gomega.HaveOccurred())
104+
if err != nil {
105+
t.T().Logf("Pod Container %s/%s/%s not found, logs cannot be stored", namespace.Name, podName, containerName)
106+
return
107+
}
102108

103109
defer func() {
104110
t.Expect(stream.Close()).To(gomega.Succeed())
@@ -110,3 +116,51 @@ func storeContainerLog(t Test, namespace *corev1.Namespace, podName, containerNa
110116
containerLogFileName := "pod-" + podName + "-" + containerName
111117
WriteToOutputDir(t, containerLogFileName, Log, bytes)
112118
}
119+
120+
func CreateServiceAccount(t Test, namespace string) *corev1.ServiceAccount {
121+
t.T().Helper()
122+
123+
serviceAccount := &corev1.ServiceAccount{
124+
TypeMeta: metav1.TypeMeta{
125+
APIVersion: corev1.SchemeGroupVersion.String(),
126+
Kind: "ServiceAccount",
127+
},
128+
ObjectMeta: metav1.ObjectMeta{
129+
GenerateName: "sa-",
130+
Namespace: namespace,
131+
},
132+
}
133+
serviceAccount, err := t.Client().Core().CoreV1().ServiceAccounts(namespace).Create(t.Ctx(), serviceAccount, metav1.CreateOptions{})
134+
t.Expect(err).NotTo(gomega.HaveOccurred())
135+
t.T().Logf("Created ServiceAccount %s/%s successfully", serviceAccount.Namespace, serviceAccount.Name)
136+
137+
return serviceAccount
138+
}
139+
140+
func CreatePersistentVolumeClaim(t Test, namespace string, storageSize string, accessMode ...corev1.PersistentVolumeAccessMode) *corev1.PersistentVolumeClaim {
141+
t.T().Helper()
142+
143+
pvc := &corev1.PersistentVolumeClaim{
144+
TypeMeta: metav1.TypeMeta{
145+
APIVersion: corev1.SchemeGroupVersion.String(),
146+
Kind: "PersistentVolumeClaim",
147+
},
148+
ObjectMeta: metav1.ObjectMeta{
149+
GenerateName: "pvc-",
150+
Namespace: namespace,
151+
},
152+
Spec: corev1.PersistentVolumeClaimSpec{
153+
AccessModes: accessMode,
154+
Resources: corev1.ResourceRequirements{
155+
Requests: corev1.ResourceList{
156+
corev1.ResourceStorage: resource.MustParse(storageSize),
157+
},
158+
},
159+
},
160+
}
161+
pvc, err := t.Client().Core().CoreV1().PersistentVolumeClaims(namespace).Create(t.Ctx(), pvc, metav1.CreateOptions{})
162+
t.Expect(err).NotTo(gomega.HaveOccurred())
163+
t.T().Logf("Created PersistentVolumeClaim %s/%s successfully", pvc.Namespace, pvc.Name)
164+
165+
return pvc
166+
}

support/image.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package support
18+
19+
import (
20+
"github.com/onsi/gomega"
21+
22+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
23+
24+
imagev1 "github.com/openshift/api/image/v1"
25+
)
26+
27+
func GetImageStream(t Test, namespace string, name string) *imagev1.ImageStream {
28+
t.T().Helper()
29+
30+
is, err := t.Client().Image().ImageV1().ImageStreams(namespace).Get(t.Ctx(), name, metav1.GetOptions{})
31+
t.Expect(err).NotTo(gomega.HaveOccurred())
32+
33+
return is
34+
}

support/rbac.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
Copyright 2023.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package support
18+
19+
import (
20+
"github.com/onsi/gomega"
21+
22+
corev1 "k8s.io/api/core/v1"
23+
rbacv1 "k8s.io/api/rbac/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
)
26+
27+
func CreateRole(t Test, namespace string, policyRules []rbacv1.PolicyRule) *rbacv1.Role {
28+
t.T().Helper()
29+
30+
role := &rbacv1.Role{
31+
TypeMeta: metav1.TypeMeta{
32+
APIVersion: rbacv1.SchemeGroupVersion.String(),
33+
Kind: "Role",
34+
},
35+
ObjectMeta: metav1.ObjectMeta{
36+
GenerateName: "role-",
37+
Namespace: namespace,
38+
},
39+
Rules: policyRules,
40+
}
41+
role, err := t.Client().Core().RbacV1().Roles(namespace).Create(t.Ctx(), role, metav1.CreateOptions{})
42+
t.Expect(err).NotTo(gomega.HaveOccurred())
43+
t.T().Logf("Created Role %s/%s successfully", role.Namespace, role.Name)
44+
45+
return role
46+
}
47+
48+
func CreateRoleBinding(t Test, namespace string, serviceAccount *corev1.ServiceAccount, role *rbacv1.Role) *rbacv1.RoleBinding {
49+
t.T().Helper()
50+
51+
roleBinding := &rbacv1.RoleBinding{
52+
TypeMeta: metav1.TypeMeta{
53+
APIVersion: rbacv1.SchemeGroupVersion.String(),
54+
Kind: "RoleBinding",
55+
},
56+
ObjectMeta: metav1.ObjectMeta{
57+
GenerateName: "rb-",
58+
},
59+
RoleRef: rbacv1.RoleRef{
60+
APIGroup: rbacv1.SchemeGroupVersion.Group,
61+
Kind: "Role",
62+
Name: role.Name,
63+
},
64+
Subjects: []rbacv1.Subject{
65+
{
66+
Kind: "ServiceAccount",
67+
APIGroup: corev1.SchemeGroupVersion.Group,
68+
Name: serviceAccount.Name,
69+
Namespace: serviceAccount.Namespace,
70+
},
71+
},
72+
}
73+
rb, err := t.Client().Core().RbacV1().RoleBindings(namespace).Create(t.Ctx(), roleBinding, metav1.CreateOptions{})
74+
t.Expect(err).NotTo(gomega.HaveOccurred())
75+
t.T().Logf("Created RoleBinding %s/%s successfully", role.Namespace, role.Name)
76+
77+
return rb
78+
}

0 commit comments

Comments
 (0)