-
Notifications
You must be signed in to change notification settings - Fork 16
Add unit tests for support package #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 6 commits
30ae434
51f2ab2
ec26208
7a3ef44
156eb48
cd87d08
181f8e0
af39294
79b9c56
75624a2
c900833
f7e8390
b7de453
8e9c140
97ff393
f99160f
f20180c
9ece9d0
2909449
6f531c5
1801e8f
49cc6d7
8328226
3b3b8b1
320ebaa
4b870a8
d96697b
87da9cd
3dae5d6
7310aae
ffd54b2
9c552ad
83832c9
369b325
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
name: Verify Unit tests | ||
|
||
on: | ||
pull_request: | ||
paths: | ||
- 'support/**' | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- name: Checkout code | ||
uses: actions/checkout@v4 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v4 | ||
with: | ||
go-version: v1.19 | ||
|
||
|
||
- name: Run unit tests | ||
run: go test ./support/. -v | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright 2023. | ||
|
||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
|
||
http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package support | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
|
||
batchv1 "k8s.io/api/batch/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func NewFakeKubeClientWithObjects(objects ...runtime.Object) *fake.Clientset { | ||
fakeClient := fake.NewSimpleClientset(objects...) | ||
return fakeClient | ||
} | ||
|
||
func TestGetJob(t *testing.T) { | ||
|
||
g := gomega.NewGomegaWithT(t) | ||
// Create a fake client that returns different Job objects. | ||
fakeJobs := []runtime.Object{ | ||
&batchv1.Job{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "my-job-1", | ||
Namespace: "my-namespace", | ||
}, | ||
}, | ||
} | ||
fakeClient := NewFakeKubeClientWithObjects(fakeJobs...) | ||
|
||
test := With(t).(*T) | ||
test.client = &testClient{ | ||
core: fakeClient, | ||
} | ||
// testInstance := &T{ | ||
// WithT: gomega.NewWithT(t), | ||
// t: t, | ||
// ctx: ctx, | ||
// client: &testClient{ | ||
// core: fakeClient, | ||
// }, | ||
// } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be removed? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I will remove it |
||
|
||
// Call the Job function using the fake client | ||
jobFunc := Job(test, "my-namespace", "my-job-1") | ||
job := jobFunc(g) | ||
|
||
//fmt.Printf("Retrieved job object: %+v\n", job) | ||
|
||
// Assertions | ||
g.Expect(job.Name).To(gomega.Equal("my-job-1")) | ||
g.Expect(job.Namespace).To(gomega.Equal("my-namespace")) | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package support |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
package support | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestGetCodeFlareSDKVersion(t *testing.T) { | ||
sutaakar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// Set the environment variable. | ||
os.Setenv(CodeFlareTestSdkVersion, "1.2.3") | ||
|
||
// Get the version. | ||
version := GetCodeFlareSDKVersion() | ||
|
||
// Assert that the version is correct. | ||
if version != "1.2.3" { | ||
t.Errorf("Expected version 1.2.3, but got %s", version) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In the code above there is used Gomega for assertions. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure , will do. thanks for the suggestion |
||
} | ||
|
||
func TestGetRayVersion(t *testing.T) { | ||
// Set the environment variable. | ||
os.Setenv(CodeFlareTestRayVersion, "1.4.5") | ||
|
||
// Get the version. | ||
version := GetRayVersion() | ||
|
||
// Assert that the version is correct. | ||
if version != "1.4.5" { | ||
t.Errorf("Expected version 1.4.5, but got %s", version) | ||
} | ||
} | ||
|
||
func TestGetRayImage(t *testing.T) { | ||
// Set the environment variable. | ||
os.Setenv(CodeFlareTestRayImage, "ray/ray:latest") | ||
|
||
// Get the image. | ||
image := GetRayImage() | ||
|
||
// Assert that the image is correct. | ||
if image != "ray/ray:latest" { | ||
t.Errorf("Expected image ray/ray:latest, but got %s", image) | ||
} | ||
} | ||
|
||
func TestGetPyTorchImage(t *testing.T) { | ||
// Set the environment variable. | ||
os.Setenv(CodeFlareTestPyTorchImage, "pytorch/pytorch:latest") | ||
|
||
// Get the image. | ||
image := GetPyTorchImage() | ||
|
||
// Assert that the image is correct. | ||
if image != "pytorch/pytorch:latest" { | ||
t.Errorf("Expected image pytorch/pytorch:latest, but got %s", image) | ||
} | ||
} | ||
|
||
func TestGetOsdClusterID(t *testing.T) { | ||
os.Setenv(OsdClusterID, "my-cluster-id") | ||
clusterId, ok := GetOsdClusterId() | ||
if !ok { | ||
t.Errorf("Expected GetOsdClusterId() to return true, but got false.") | ||
} | ||
if clusterId != "my-cluster-id" { | ||
t.Errorf("Expected GetOsdClusterId() to return 'my-cluster-id', but got '%s'.", clusterId) | ||
} | ||
|
||
} | ||
func TestGetInstascaleOcmSecret(t *testing.T) { | ||
// Set the Instascale OCM secret environment variable. | ||
os.Setenv(InstaScaleOcmSecret, "default/instascale-ocm-secret") | ||
// Get the Instascale OCM secret namespace and secret name. | ||
namespace, secretName := GetInstascaleOcmSecret() | ||
|
||
// Verify that the namespace and secret name are correct. | ||
if namespace != "default" || secretName != "instascale-ocm-secret" { | ||
t.Errorf("Expected GetInstascaleOcmSecret() to return 'default/instascale-ocm-secret', but got '%s/%s'.", namespace, secretName) | ||
} | ||
|
||
} | ||
|
||
func TestGetClusterType(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
envVarValue string | ||
expected ClusterType | ||
}{ | ||
{ | ||
name: "OSD cluster", | ||
envVarValue: "OSD", | ||
expected: OsdCluster, | ||
}, | ||
{ | ||
name: "OCP cluster", | ||
envVarValue: "OCP", | ||
expected: OcpCluster, | ||
}, | ||
{ | ||
name: "Hypershift cluster", | ||
envVarValue: "HYPERSHIFT", | ||
expected: HypershiftCluster, | ||
}, | ||
{ | ||
name: "KIND cluster", | ||
envVarValue: "KIND", | ||
expected: KindCluster, | ||
}, | ||
{ | ||
name: "Undefined cluster", | ||
envVarValue: "INVALID", | ||
expected: UndefinedCluster, | ||
}, | ||
} | ||
ttt := With(t) | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
os.Setenv(ClusterTypeEnvVar, tt.envVarValue) | ||
actual := GetClusterType(ttt) // Pass tt as an argument to GetClusterType | ||
if actual != tt.expected { | ||
t.Errorf("Expected GetClusterType() to return %v, but got %v", tt.expected, actual) | ||
} | ||
}) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package support | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetDefaultEventValueIfNull(t *testing.T) { | ||
tests := []struct { | ||
input string | ||
expected string | ||
}{ | ||
{"World", "World"}, | ||
} | ||
|
||
for _, test := range tests { | ||
actual := getDefaultEventValueIfNull(test.input) | ||
if actual != test.expected { | ||
t.Errorf("getDefaultEventValueIfNull(%s) = %s; expected %s", test.input, actual, test.expected) | ||
} | ||
} | ||
} | ||
|
||
func TestGetWhitespaceStr(t *testing.T) { | ||
tests := []struct { | ||
size int | ||
expected string | ||
}{ | ||
{0, ""}, | ||
{1, " "}, | ||
{5, " "}, | ||
{10, " "}, | ||
} | ||
|
||
for _, test := range tests { | ||
actual := getWhitespaceStr(test.size) | ||
if actual != test.expected { | ||
t.Errorf("getWhitespaceStr(%d) = %s; expected %s", test.size, actual, test.expected) | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What is testify used for?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I missed running
go mod tidy
.testify
It's not needed.