Skip to content

Commit 253f773

Browse files
committed
Added unitest for support-package
1 parent 2b6c0f1 commit 253f773

14 files changed

+511
-4
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Verify Unit tests
2+
3+
on:
4+
pull_request:
5+
paths:
6+
- 'support/**'
7+
8+
jobs:
9+
test:
10+
runs-on: ubuntu-latest
11+
12+
steps:
13+
- name: Checkout code
14+
uses: actions/checkout@v4
15+
16+
- name: Set up Go
17+
uses: actions/setup-go@v4
18+
with:
19+
go-version: v1.19
20+
21+
22+
23+
- name: Run unit tests
24+
run: go test ./support/. -v

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ require (
2121
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2222
github.com/davecgh/go-spew v1.1.1 // indirect
2323
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
24+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
2425
github.com/go-logr/logr v1.2.4 // indirect
2526
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2627
github.com/go-openapi/jsonreference v0.20.1 // indirect
@@ -43,6 +44,7 @@ require (
4344
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4445
github.com/modern-go/reflect2 v1.0.2 // indirect
4546
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
47+
github.com/pkg/errors v0.9.1 // indirect
4648
github.com/prometheus/client_golang v1.14.0 // indirect
4749
github.com/prometheus/client_model v0.3.0 // indirect
4850
github.com/prometheus/common v0.37.0 // indirect

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ github.com/envoyproxy/go-control-plane v0.9.9-0.20201210154907-fd9021fe5dad/go.m
8080
github.com/envoyproxy/go-control-plane v0.9.9-0.20210512163311-63b5d3c536b0/go.mod h1:hliV/p42l8fGbc6Y9bQ70uLwIvmJyVE5k4iMKlh8wCQ=
8181
github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c=
8282
github.com/evanphx/json-patch v4.12.0+incompatible h1:4onqiflcdA9EOZ4RxV643DvftH5pOlLGNtQ5lPWQu84=
83+
github.com/evanphx/json-patch v4.12.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
8384
github.com/evanphx/json-patch/v5 v5.6.0 h1:b91NhWfaz02IuVxO9faSllyAtNXHMPkC5J8sJCLunww=
8485
github.com/evanphx/json-patch/v5 v5.6.0/go.mod h1:G79N1coSVB93tBe7j6PhzjmR3/2VvlbKOFpnXhI9Bw4=
8586
github.com/flowstack/go-jsonschema v0.1.1/go.mod h1:yL7fNggx1o8rm9RlgXv7hTBWxdBM0rVwpMwimd3F3N0=

support/batch_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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+
"testing"
21+
22+
"github.com/onsi/gomega"
23+
24+
batchv1 "k8s.io/api/batch/v1"
25+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26+
)
27+
28+
func TestGetJob(t *testing.T) {
29+
30+
test := NewTest(t)
31+
32+
Job := &batchv1.Job{
33+
ObjectMeta: metav1.ObjectMeta{
34+
Name: "my-job-1",
35+
Namespace: "my-namespace",
36+
},
37+
}
38+
39+
test.client.Core().BatchV1().Jobs("my-namespace").Create(test.ctx, Job, metav1.CreateOptions{})
40+
41+
// Call the Job function using the fake client
42+
jobs := GetJob(test, "my-namespace", "my-job-1")
43+
44+
test.Expect(jobs.Name).To(gomega.Equal("my-job-1"))
45+
test.Expect(jobs.Namespace).To(gomega.Equal("my-namespace"))
46+
47+
}

support/core_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package support
2+
3+
import (
4+
"testing"
5+
6+
"github.com/onsi/gomega"
7+
8+
corev1 "k8s.io/api/core/v1"
9+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
)
11+
12+
func TestGetPods(t *testing.T) {
13+
test := NewTest(t)
14+
15+
pod := &corev1.Pod{
16+
ObjectMeta: metav1.ObjectMeta{
17+
Name: "test-pod",
18+
Namespace: "test-namespace",
19+
},
20+
}
21+
22+
test.client.Core().CoreV1().Pods("test-namespace").Create(test.ctx, pod, metav1.CreateOptions{})
23+
24+
// Call the GetPods function with the fake client and namespace
25+
pods := GetPods(test, "test-namespace", metav1.ListOptions{})
26+
27+
test.Expect(pods).Should(gomega.HaveLen(1), "Expected 1 pod, but got %d", len(pods))
28+
test.Expect(pods[0].Name).To(gomega.Equal("test-pod"), "Expected pod name 'test-pod', but got '%s'", pods[0].Name)
29+
}
30+
31+
func TestGetNodes(t *testing.T) {
32+
test := NewTest(t)
33+
node := &corev1.Node{
34+
ObjectMeta: metav1.ObjectMeta{
35+
Name: "test-node",
36+
},
37+
}
38+
39+
test.client.Core().CoreV1().Nodes().Create(test.ctx, node, metav1.CreateOptions{})
40+
nodes := GetNodes(test)
41+
42+
test.Expect(nodes).Should(gomega.HaveLen(1), "Expected 1 node, but got %d", len(nodes))
43+
test.Expect(nodes[0].Name).To(gomega.Equal("test-node"), "Expected node name 'test-node', but got '%s'", nodes[0].Name)
44+
45+
}

support/environment_test.go

Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
package support
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/onsi/gomega"
9+
)
10+
11+
func TestGetCodeFlareSDKVersion(t *testing.T) {
12+
13+
g := gomega.NewGomegaWithT(t)
14+
// Set the environment variable.
15+
os.Setenv(CodeFlareTestSdkVersion, "1.2.3")
16+
17+
// Get the version.
18+
version := GetCodeFlareSDKVersion()
19+
20+
// Assert that the version is correct.
21+
22+
g.Expect(version).To(gomega.Equal("1.2.3"), "Expected version 1.2.3, but got %s", version)
23+
24+
}
25+
26+
func TestGetRayVersion(t *testing.T) {
27+
28+
g := gomega.NewGomegaWithT(t)
29+
// Set the environment variable.
30+
os.Setenv(CodeFlareTestRayVersion, "1.4.5")
31+
32+
// Get the version.
33+
version := GetRayVersion()
34+
35+
// Assert that the version is correct.
36+
37+
g.Expect(version).To(gomega.Equal("1.4.5"), "Expected version 1.4.5, but got %s", version)
38+
39+
}
40+
41+
func TestGetRayImage(t *testing.T) {
42+
43+
g := gomega.NewGomegaWithT(t)
44+
// Set the environment variable.
45+
os.Setenv(CodeFlareTestRayImage, "ray/ray:latest")
46+
47+
// Get the image.
48+
image := GetRayImage()
49+
50+
// Assert that the image is correct.
51+
52+
g.Expect(image).To(gomega.Equal("ray/ray:latest"), "Expected image ray/ray:latest, but got %s", image)
53+
54+
}
55+
56+
func TestGetPyTorchImage(t *testing.T) {
57+
58+
g := gomega.NewGomegaWithT(t)
59+
// Set the environment variable.
60+
os.Setenv(CodeFlareTestPyTorchImage, "pytorch/pytorch:latest")
61+
62+
// Get the image.
63+
image := GetPyTorchImage()
64+
65+
// Assert that the image is correct.
66+
67+
g.Expect(image).To(gomega.Equal("pytorch/pytorch:latest"), "Expected image pytorch/pytorch:latest, but got %s", image)
68+
69+
}
70+
71+
func TestGetClusterID(t *testing.T) {
72+
73+
g := gomega.NewGomegaWithT(t)
74+
os.Setenv(ClusterID, "my-cluster-id")
75+
clusterId, ok := GetClusterId()
76+
if !ok {
77+
gomega.Expect(ok).To(gomega.BeTrue(), "Expected GetClusterId() to return true, but got false.")
78+
}
79+
80+
g.Expect(clusterId).To(gomega.Equal("my-cluster-id"), "Expected GetClusterId() to return 'my-cluster-id', but got '%s'.", clusterId)
81+
82+
}
83+
84+
func TestGetInstascaleOcmSecret(t *testing.T) {
85+
86+
g := gomega.NewGomegaWithT(t)
87+
// Set the Instascale OCM secret environment variable.
88+
os.Setenv(InstaScaleOcmSecret, "default/instascale-ocm-secret")
89+
// Get the Instascale OCM secret namespace and secret name.
90+
namespace, secretName := GetInstascaleOcmSecret()
91+
92+
// Verify that the namespace and secret name are correct.
93+
94+
g.Expect(fmt.Sprintf("%s/%s", namespace, secretName)).To(
95+
gomega.Equal("default/instascale-ocm-secret"),
96+
"Expected GetInstascaleOcmSecret() to return 'default/instascale-ocm-secret', but got '%s/%s'.",
97+
namespace, secretName,
98+
)
99+
100+
}
101+
102+
func TestGetClusterType(t *testing.T) {
103+
104+
g := gomega.NewGomegaWithT(t)
105+
106+
tests := []struct {
107+
name string
108+
envVarValue string
109+
expected ClusterType
110+
}{
111+
{
112+
name: "OSD cluster",
113+
envVarValue: "OSD",
114+
expected: OsdCluster,
115+
},
116+
{
117+
name: "OCP cluster",
118+
envVarValue: "OCP",
119+
expected: OcpCluster,
120+
},
121+
{
122+
name: "Hypershift cluster",
123+
envVarValue: "HYPERSHIFT",
124+
expected: HypershiftCluster,
125+
},
126+
{
127+
name: "KIND cluster",
128+
envVarValue: "KIND",
129+
expected: KindCluster,
130+
},
131+
}
132+
ttt := With(t)
133+
for _, tt := range tests {
134+
t.Run(tt.name, func(t *testing.T) {
135+
os.Setenv(ClusterTypeEnvVar, tt.envVarValue)
136+
actual := GetClusterType(ttt)
137+
138+
g.Expect(actual).To(
139+
gomega.Equal(tt.expected),
140+
"Expected GetClusterType() to return %v, but got %v", tt.expected, actual,
141+
)
142+
143+
})
144+
}
145+
}

support/events_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package support
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestGetDefaultEventValueIfNull(t *testing.T) {
8+
tests := []struct {
9+
input string
10+
expected string
11+
}{
12+
{"World", "World"},
13+
}
14+
15+
for _, test := range tests {
16+
actual := getDefaultEventValueIfNull(test.input)
17+
if actual != test.expected {
18+
t.Errorf("getDefaultEventValueIfNull(%s) = %s; expected %s", test.input, actual, test.expected)
19+
}
20+
}
21+
}
22+
23+
func TestGetWhitespaceStr(t *testing.T) {
24+
tests := []struct {
25+
size int
26+
expected string
27+
}{
28+
{0, ""},
29+
{1, " "},
30+
{5, " "},
31+
{10, " "},
32+
}
33+
34+
for _, test := range tests {
35+
actual := getWhitespaceStr(test.size)
36+
if actual != test.expected {
37+
t.Errorf("getWhitespaceStr(%d) = %s; expected %s", test.size, actual, test.expected)
38+
}
39+
}
40+
}

support/fakeclient.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package support
2+
3+
import (
4+
"testing"
5+
6+
fakeray "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/fake"
7+
8+
"k8s.io/apimachinery/pkg/runtime"
9+
fakeCore "k8s.io/client-go/kubernetes/fake"
10+
11+
fakeimage "github.com/openshift/client-go/image/clientset/versioned/fake"
12+
fakeMachine "github.com/openshift/client-go/machine/clientset/versioned/fake"
13+
fakeroute "github.com/openshift/client-go/route/clientset/versioned/fake"
14+
)
15+
16+
func NewTest(t *testing.T, objects ...runtime.Object) *T {
17+
fakeCoreClient := fakeCore.NewSimpleClientset()
18+
fakemachineClient := fakeMachine.NewSimpleClientset()
19+
fakeimageClient := fakeimage.NewSimpleClientset()
20+
fakerouteClient := fakeroute.NewSimpleClientset()
21+
fakerayClient := fakeray.NewSimpleClientset()
22+
23+
test := With(t).(*T)
24+
test.client = &testClient{
25+
core: fakeCoreClient,
26+
machine: fakemachineClient,
27+
image: fakeimageClient,
28+
route: fakerouteClient,
29+
ray: fakerayClient,
30+
}
31+
return test
32+
}

support/image_test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package support
2+
3+
import (
4+
"testing"
5+
6+
"github.com/onsi/gomega"
7+
8+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
9+
10+
imagev1 "github.com/openshift/api/image/v1"
11+
)
12+
13+
func TestGetImageStream(t *testing.T) {
14+
15+
test := NewTest(t)
16+
17+
ImageStream := &imagev1.ImageStream{
18+
ObjectMeta: metav1.ObjectMeta{
19+
Name: "my-imagestream-1",
20+
Namespace: "my-namespace",
21+
},
22+
}
23+
24+
test.client.Image().ImageV1().ImageStreams("my-namespace").Create(test.ctx, ImageStream, metav1.CreateOptions{})
25+
26+
image := GetImageStream(test, "my-namespace", "my-imagestream-1")
27+
28+
test.Expect(image.Name).To(gomega.Equal("my-imagestream-1"))
29+
test.Expect(image.Namespace).To(gomega.Equal("my-namespace"))
30+
}

0 commit comments

Comments
 (0)