Skip to content

Commit b657fb2

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

15 files changed

+564
-5
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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+
- name: Run unit tests
23+
run: go test ./support/. -v

go.mod

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212
k8s.io/api v0.26.3
1313
k8s.io/apimachinery v0.26.3
1414
k8s.io/client-go v0.26.3
15+
sigs.k8s.io/controller-runtime v0.14.6
1516
)
1617

1718
require (
@@ -21,6 +22,8 @@ require (
2122
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2223
github.com/davecgh/go-spew v1.1.1 // indirect
2324
github.com/emicklei/go-restful/v3 v3.10.1 // indirect
25+
github.com/evanphx/json-patch v4.12.0+incompatible // indirect
26+
github.com/evanphx/json-patch/v5 v5.6.0 // indirect
2427
github.com/go-logr/logr v1.2.4 // indirect
2528
github.com/go-openapi/jsonpointer v0.19.6 // indirect
2629
github.com/go-openapi/jsonreference v0.20.1 // indirect
@@ -43,6 +46,7 @@ require (
4346
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4447
github.com/modern-go/reflect2 v1.0.2 // indirect
4548
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
49+
github.com/pkg/errors v0.9.1 // indirect
4650
github.com/prometheus/client_golang v1.14.0 // indirect
4751
github.com/prometheus/client_model v0.3.0 // indirect
4852
github.com/prometheus/common v0.37.0 // indirect
@@ -62,7 +66,6 @@ require (
6266
k8s.io/klog/v2 v2.90.1 // indirect
6367
k8s.io/kube-openapi v0.0.0-20230303024457-afdc3dddf62d // indirect
6468
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect
65-
sigs.k8s.io/controller-runtime v0.14.6 // indirect
6669
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
6770
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect
6871
sigs.k8s.io/yaml v1.3.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ 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=
8687
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
8788
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
89+
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
8890
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
8991
github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU=
9092
github.com/go-gl/glfw/v3.3/glfw v0.0.0-20191125211704-12ad95a8df72/go.mod h1:tQ2UAYgL5IevRw8kRxooKSPJfGvJ9fJQFa0TUsXzTg8=
@@ -100,6 +102,7 @@ github.com/go-logfmt/logfmt v0.5.1/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KE
100102
github.com/go-logr/logr v1.2.0/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
101103
github.com/go-logr/logr v1.2.4 h1:g01GSCwiDw2xSZfjJ2/T9M+S6pFdcNtFYsp+Y43HYDQ=
102104
github.com/go-logr/logr v1.2.4/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
105+
github.com/go-logr/zapr v1.2.3 h1:a9vnzlIBPQBBkeaR9IuMUfmVOrQlkoC4YfPoFkX3T7A=
103106
github.com/go-openapi/jsonpointer v0.19.6 h1:eCs3fxoIi3Wh6vtgmLTOjdhSpiqphQ+DaPn38N2ZdrE=
104107
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
105108
github.com/go-openapi/jsonreference v0.20.1 h1:FBLnyygC4/IZZr893oiomc9XaghoveYTrLC1F86HID8=
@@ -407,13 +410,16 @@ go.uber.org/atomic v1.3.2/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
407410
go.uber.org/atomic v1.4.0/go.mod h1:gD2HeocX3+yG+ygLZcrzQJaqmWj9AIm7n08wl/qW/PE=
408411
go.uber.org/atomic v1.5.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
409412
go.uber.org/atomic v1.6.0/go.mod h1:sABNBOSYdrvTF6hTgEIbc7YasKWGhgEQZyfxyTvoXHQ=
413+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
410414
go.uber.org/multierr v1.1.0/go.mod h1:wR5kodmAFQ0UK8QlbwjlSNy0Z68gJhDJUG5sjR94q/0=
411415
go.uber.org/multierr v1.3.0/go.mod h1:VgVr7evmIr6uPjLBxg28wmKNXyqE9akIJ5XnfpiKl+4=
412416
go.uber.org/multierr v1.5.0/go.mod h1:FeouvMocqHpRaaGuG9EjoKcStLC43Zu/fmqdUMPcKYU=
417+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
413418
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee/go.mod h1:vJERXedbb3MVM5f9Ejo0C68/HhF8uaILCdgjnY+goOA=
414419
go.uber.org/zap v1.9.1/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
415420
go.uber.org/zap v1.10.0/go.mod h1:vwi/ZaCAaUcBkycHslxD9B2zi4UTXhF60s6SWpuDF0Q=
416421
go.uber.org/zap v1.13.0/go.mod h1:zwrFLgMcdUuIBviXEYEH1YKNaOBnKXsx2IPda5bBwHM=
422+
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
417423
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
418424
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
419425
golang.org/x/crypto v0.0.0-20190411191339-88737f569e3a/go.mod h1:WFFai1msRO1wXaEeE5yQxYXgSfI8pQAWXbQop6sCtWE=
@@ -658,6 +664,7 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T
658664
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
659665
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
660666
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
667+
gomodules.xyz/jsonpatch/v2 v2.2.0 h1:4pT439QV83L+G9FkcCriY6EkpcK6r6bK+A5FBUMI7qY=
661668
google.golang.org/api v0.4.0/go.mod h1:8k5glujaEP+g9n7WNsDg8QP6cUVNI86fCNMcbazEtwE=
662669
google.golang.org/api v0.7.0/go.mod h1:WtwebWUNSVBH/HAw79HIFXZNqEvBhG+Ra+ax0hx3E3M=
663670
google.golang.org/api v0.8.0/go.mod h1:o4eAsZoiT+ibD93RtjEohWalFOjRDx6CVaqeizhEnKg=
@@ -778,6 +785,7 @@ honnef.co/go/tools v0.0.1-2020.1.3/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9
778785
honnef.co/go/tools v0.0.1-2020.1.4/go.mod h1:X/FiERA/W4tHapMX5mGpAtMSVEeEUOyHaw9vFzvIQ3k=
779786
k8s.io/api v0.26.3 h1:emf74GIQMTik01Aum9dPP0gAypL8JTLl/lHa4V9RFSU=
780787
k8s.io/api v0.26.3/go.mod h1:PXsqwPMXBSBcL1lJ9CYDKy7kIReUydukS5JiRlxC3qE=
788+
k8s.io/apiextensions-apiserver v0.26.1 h1:cB8h1SRk6e/+i3NOrQgSFij1B2S0Y0wDoNl66bn8RMI=
781789
k8s.io/apimachinery v0.26.3 h1:dQx6PNETJ7nODU3XPtrwkfuubs6w7sX0M8n61zHIV/k=
782790
k8s.io/apimachinery v0.26.3/go.mod h1:ats7nN1LExKHvJ9TmwootT00Yz05MuYqPXEXaVeOy5I=
783791
k8s.io/client-go v0.26.3 h1:k1UY+KXfkxV2ScEL3gilKcF7761xkYsSD6BC9szIu8s=

support/batch_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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+
"k8s.io/apimachinery/pkg/runtime"
27+
)
28+
29+
func TestGetJob(t *testing.T) {
30+
31+
g := gomega.NewGomegaWithT(t)
32+
33+
fakeJobs := []runtime.Object{
34+
&batchv1.Job{
35+
ObjectMeta: metav1.ObjectMeta{
36+
Name: "my-job-1",
37+
Namespace: "my-namespace",
38+
},
39+
},
40+
}
41+
fakeClient := NewFakeKubeClientWithObjects(fakeJobs...)
42+
43+
test := With(t).(*T)
44+
test.client = &testClient{
45+
core: fakeClient,
46+
}
47+
48+
// Call the Job function using the fake client
49+
jobFunc := Job(test, "my-namespace", "my-job-1")
50+
job := jobFunc(g)
51+
52+
g.Expect(job.Name).To(gomega.Equal("my-job-1"))
53+
g.Expect(job.Namespace).To(gomega.Equal("my-namespace"))
54+
55+
}

support/core_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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+
"k8s.io/client-go/kubernetes/fake"
11+
)
12+
13+
func TestGetPods(t *testing.T) {
14+
// Create a fake Kubernetes client for testing
15+
fakeClient := fake.NewSimpleClientset(&corev1.Pod{
16+
ObjectMeta: metav1.ObjectMeta{
17+
Name: "test-pod",
18+
Namespace: "test-namespace",
19+
},
20+
})
21+
22+
test := With(t).(*T)
23+
test.client = &testClient{
24+
core: fakeClient,
25+
}
26+
27+
// Call the GetPods function with the fake client and namespace
28+
pods := GetPods(test, "test-namespace", metav1.ListOptions{})
29+
30+
test.Expect(pods).Should(gomega.HaveLen(1), "Expected 1 pod, but got %d", len(pods))
31+
test.Expect(pods[0].Name).To(gomega.Equal("test-pod"), "Expected pod name 'test-pod', but got '%s'", pods[0].Name)
32+
}
33+
34+
func TestGetNodes(t *testing.T) {
35+
// Create a fake Kubernetes client for testing
36+
fakeClient := fake.NewSimpleClientset(&corev1.Node{
37+
ObjectMeta: metav1.ObjectMeta{
38+
Name: "test-node",
39+
},
40+
})
41+
42+
test := With(t).(*T)
43+
test.client = &testClient{
44+
core: fakeClient,
45+
}
46+
nodes := GetNodes(test)
47+
48+
test.Expect(nodes).Should(gomega.HaveLen(1), "Expected 1 node, but got %d", len(nodes))
49+
test.Expect(nodes[0].Name).To(gomega.Equal("test-node"), "Expected node name 'test-node', but got '%s'", nodes[0].Name)
50+
51+
}

support/environment_test.go

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

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+
}

0 commit comments

Comments
 (0)