-
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
Closed
Closed
Changes from all commits
Commits
Show all changes
34 commits
Select commit
Hold shift + click to select a range
30ae434
Add unit tests for support package
Raghul-M 51f2ab2
Merge branch 'unittestsupport#239' of github.com:Raghul-M/codeflare-c…
Raghul-M ec26208
Add unit tests for support package
Raghul-M 7a3ef44
Add unit tests for support package
Raghul-M 156eb48
Fixed Changes in Comments and added Ingress_test
Raghul-M cd87d08
Fixed Make Imports Failure
Raghul-M 181f8e0
Made Changes and added image_test.go
Raghul-M af39294
Added: Machine_test.go
Raghul-M 79b9c56
Added route_test.go
Raghul-M 75624a2
Added route_test.go
Raghul-M c900833
Added TestGetRayCluster
Raghul-M f7e8390
Added core_test.go and Modified batch_test.go
Raghul-M b7de453
Modified code for Actions failed
Raghul-M 8e9c140
Add unit tests for support package
Raghul-M 97ff393
Add unit tests for support package
Raghul-M f99160f
Add unit tests for support package
Raghul-M f20180c
Fixed Changes in Comments and added Ingress_test
Raghul-M 9ece9d0
Fixed Make Imports Failure
Raghul-M 2909449
Made Changes and added image_test.go
Raghul-M 6f531c5
Added: Machine_test.go
Raghul-M 1801e8f
Added route_test.go
Raghul-M 49cc6d7
Added route_test.go
Raghul-M 8328226
Added TestGetRayCluster
Raghul-M 3b3b8b1
Added core_test.go and Modified batch_test.go
Raghul-M 320ebaa
Modified code for Actions failed
Raghul-M 4b870a8
Made PR Checks issues clearance
Raghul-M d96697b
Cleared Merge conflicts
Raghul-M 87da9cd
Updated code
Raghul-M 3dae5d6
Merge remote-tracking branch 'origin/main' into unittestsupport#239
Raghul-M 7310aae
Updated code
Raghul-M ffd54b2
Resolved Merge Conflicts
Raghul-M 9c552ad
Resolved Merge Conflicts
Raghul-M 83832c9
Generalized Fakeclient
Raghul-M 369b325
Final Commit for PR Merge
Raghul-M File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
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" | ||
) | ||
|
||
func TestGetJob(t *testing.T) { | ||
|
||
g := gomega.NewGomegaWithT(t) | ||
|
||
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, | ||
} | ||
|
||
// Call the Job function using the fake client | ||
jobFunc := Job(test, "my-namespace", "my-job-1") | ||
job := jobFunc(g) | ||
|
||
g.Expect(job.Name).To(gomega.Equal("my-job-1")) | ||
g.Expect(job.Namespace).To(gomega.Equal("my-namespace")) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package support | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestGetPods(t *testing.T) { | ||
// Create a fake Kubernetes client for testing | ||
fakeClient := fake.NewSimpleClientset(&corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-pod", | ||
Namespace: "test-namespace", | ||
}, | ||
}) | ||
|
||
test := With(t).(*T) | ||
test.client = &testClient{ | ||
core: fakeClient, | ||
} | ||
|
||
// Call the GetPods function with the fake client and namespace | ||
pods := GetPods(test, "test-namespace", metav1.ListOptions{}) | ||
|
||
test.Expect(pods).Should(gomega.HaveLen(1), "Expected 1 pod, but got %d", len(pods)) | ||
test.Expect(pods[0].Name).To(gomega.Equal("test-pod"), "Expected pod name 'test-pod', but got '%s'", pods[0].Name) | ||
} | ||
|
||
func TestGetNodes(t *testing.T) { | ||
// Create a fake Kubernetes client for testing | ||
fakeClient := fake.NewSimpleClientset(&corev1.Node{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "test-node", | ||
}, | ||
}) | ||
|
||
test := With(t).(*T) | ||
test.client = &testClient{ | ||
core: fakeClient, | ||
} | ||
nodes := GetNodes(test) | ||
|
||
test.Expect(nodes).Should(gomega.HaveLen(1), "Expected 1 node, but got %d", len(nodes)) | ||
test.Expect(nodes[0].Name).To(gomega.Equal("test-node"), "Expected node name 'test-node', but got '%s'", nodes[0].Name) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
package support | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/onsi/gomega" | ||
) | ||
|
||
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" { | ||
gomega.Expect(version).To(gomega.Equal("1.2.3"), "Expected version 1.2.3, but got %s", version) | ||
|
||
} | ||
} | ||
|
||
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" { | ||
gomega.Expect(version).To(gomega.Equal("1.2.3"), "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" { | ||
gomega.Expect(image).To(gomega.Equal("ray/ray:latest"), "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" { | ||
gomega.Expect(image).To(gomega.Equal("pytorch/pytorch:latest"), "Expected image pytorch/pytorch:latest, but got %s", image) | ||
|
||
} | ||
} | ||
|
||
func TestGetClusterID(t *testing.T) { | ||
os.Setenv(ClusterID, "my-cluster-id") | ||
clusterId, ok := GetClusterId() | ||
if !ok { | ||
gomega.Expect(ok).To(gomega.BeTrue(), "Expected GetClusterId() to return true, but got false.") | ||
} | ||
if clusterId != "my-cluster-id" { | ||
gomega.Expect(clusterId).To(gomega.Equal("my-cluster-id"), "Expected GetClusterId() 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" { | ||
gomega.Expect(fmt.Sprintf("%s/%s", namespace, secretName)).To( | ||
gomega.Equal("default/instascale-ocm-secret"), | ||
"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, | ||
}, | ||
} | ||
ttt := With(t) | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
os.Setenv(ClusterTypeEnvVar, tt.envVarValue) | ||
actual := GetClusterType(ttt) | ||
if actual != tt.expected { | ||
gomega.Expect(actual).To( | ||
gomega.Equal(tt.expected), | ||
"Expected GetClusterType() to return %v, but got %v", tt.expected, actual, | ||
) | ||
|
||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.