-
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 4 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@v2 | ||
|
||
- name: Set up Go | ||
uses: actions/setup-go@v2 | ||
sutaakar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
with: | ||
go-version: v1.19 | ||
import-path: github.com/project-codeflare/codeflare-common | ||
sutaakar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
- name: Run unit tests | ||
run: go test ./support/. -v | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,9 +9,11 @@ require ( | |
github.com/openshift/client-go v0.0.0-20221019143426-16aed247da5c | ||
github.com/project-codeflare/multi-cluster-app-dispatcher v1.37.0 | ||
github.com/ray-project/kuberay/ray-operator v0.0.0-20231016183545-097828931d15 | ||
github.com/stretchr/testify v1.8.4 | ||
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. What is testify used for? 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. I missed running |
||
k8s.io/api v0.26.3 | ||
k8s.io/apimachinery v0.26.3 | ||
k8s.io/client-go v0.26.3 | ||
sigs.k8s.io/controller-runtime v0.14.6 | ||
) | ||
|
||
require ( | ||
|
@@ -21,6 +23,8 @@ require ( | |
github.com/cespare/xxhash/v2 v2.1.2 // indirect | ||
github.com/davecgh/go-spew v1.1.1 // indirect | ||
github.com/emicklei/go-restful/v3 v3.10.1 // indirect | ||
github.com/evanphx/json-patch v4.12.0+incompatible // indirect | ||
github.com/evanphx/json-patch/v5 v5.6.0 // indirect | ||
github.com/go-logr/logr v1.2.4 // indirect | ||
github.com/go-openapi/jsonpointer v0.19.6 // indirect | ||
github.com/go-openapi/jsonreference v0.20.1 // indirect | ||
|
@@ -43,6 +47,8 @@ require ( | |
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect | ||
github.com/modern-go/reflect2 v1.0.2 // indirect | ||
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect | ||
github.com/pkg/errors v0.9.1 // indirect | ||
github.com/pmezard/go-difflib v1.0.0 // indirect | ||
github.com/prometheus/client_golang v1.14.0 // indirect | ||
github.com/prometheus/client_model v0.3.0 // indirect | ||
github.com/prometheus/common v0.37.0 // indirect | ||
|
@@ -62,7 +68,6 @@ require ( | |
k8s.io/klog/v2 v2.90.1 // indirect | ||
k8s.io/kube-openapi v0.0.0-20230303024457-afdc3dddf62d // indirect | ||
k8s.io/utils v0.0.0-20230220204549-a5ecb0141aa5 // indirect | ||
sigs.k8s.io/controller-runtime v0.14.6 // indirect | ||
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect | ||
sigs.k8s.io/structured-merge-diff/v4 v4.2.3 // indirect | ||
sigs.k8s.io/yaml v1.3.0 // indirect | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
/* | ||
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 ( | ||
"context" | ||
"fmt" | ||
"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...) | ||
|
||
// Explicitly create the jobs in the fake client. | ||
for _, obj := range fakeJobs { | ||
job, ok := obj.(*batchv1.Job) | ||
if !ok { | ||
t.Fatalf("Expected object of type *batchv1.Job, got %T", obj) | ||
} | ||
_, err := fakeClient.BatchV1().Jobs(job.Namespace).Create(context.TODO(), job, metav1.CreateOptions{}) | ||
g.Expect(err).To(gomega.HaveOccurred()) | ||
} | ||
sutaakar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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,82 @@ | ||
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) | ||
} | ||
|
||
} |
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) | ||
} | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.