diff --git a/ray-operator/test/sampleyaml/raycluster_test.go b/ray-operator/test/sampleyaml/raycluster_test.go index f652501f6a..42388bc418 100644 --- a/ray-operator/test/sampleyaml/raycluster_test.go +++ b/ray-operator/test/sampleyaml/raycluster_test.go @@ -1,6 +1,7 @@ package sampleyaml import ( + "path" "testing" . "github.com/onsi/gomega" @@ -62,10 +63,11 @@ func TestRayCluster(t *testing.T) { test := With(t) g := NewWithT(t) + yamlFilePath := path.Join(GetSampleYAMLDir(test), tt.name) namespace := test.NewTestNamespace() test.StreamKubeRayOperatorLogs() - rayClusterFromYaml := DeserializeRayClusterSampleYAML(test, tt.name) - KubectlApplyYAML(test, tt.name, namespace.Name) + rayClusterFromYaml := DeserializeRayClusterYAML(test, yamlFilePath) + KubectlApplyYAML(test, yamlFilePath, namespace.Name) rayCluster, err := GetRayCluster(test, namespace.Name, rayClusterFromYaml.Name) g.Expect(err).NotTo(HaveOccurred()) @@ -85,7 +87,7 @@ func TestRayCluster(t *testing.T) { } } g.Eventually(WorkerPods(test, rayCluster), TestTimeoutShort).Should(HaveLen(int(desiredWorkerReplicas))) - g.Expect(rayCluster.Status.DesiredWorkerReplicas).To(Equal(desiredWorkerReplicas)) + g.Expect(GetRayCluster(test, namespace.Name, rayCluster.Name)).To(WithTransform(RayClusterDesiredWorkerReplicas, Equal(desiredWorkerReplicas))) // Check if the head pod is ready g.Eventually(HeadPod(test, rayCluster), TestTimeoutShort).Should(WithTransform(IsPodRunningAndReady, BeTrue())) diff --git a/ray-operator/test/sampleyaml/rayjob_test.go b/ray-operator/test/sampleyaml/rayjob_test.go index cbec7c2439..f2c0a175f8 100644 --- a/ray-operator/test/sampleyaml/rayjob_test.go +++ b/ray-operator/test/sampleyaml/rayjob_test.go @@ -1,6 +1,7 @@ package sampleyaml import ( + "path" "testing" . "github.com/onsi/gomega" @@ -35,10 +36,11 @@ func TestRayJob(t *testing.T) { test := With(t) g := NewWithT(t) + yamlFilePath := path.Join(GetSampleYAMLDir(test), tt.name) namespace := test.NewTestNamespace() test.StreamKubeRayOperatorLogs() - rayJobFromYaml := DeserializeRayJobSampleYAML(test, tt.name) - KubectlApplyYAML(test, tt.name, namespace.Name) + rayJobFromYaml := DeserializeRayJobYAML(test, yamlFilePath) + KubectlApplyYAML(test, yamlFilePath, namespace.Name) rayJob, err := GetRayJob(test, namespace.Name, rayJobFromYaml.Name) g.Expect(err).NotTo(HaveOccurred()) @@ -65,9 +67,8 @@ func TestRayJob(t *testing.T) { desiredWorkerReplicas += *workerGroupSpec.Replicas } } - g.Eventually(WorkerPods(test, rayCluster), TestTimeoutShort).Should(HaveLen(int(desiredWorkerReplicas))) - g.Expect(rayCluster.Status.DesiredWorkerReplicas).To(Equal(desiredWorkerReplicas)) + g.Expect(GetRayCluster(test, namespace.Name, rayCluster.Name)).To(WithTransform(RayClusterDesiredWorkerReplicas, Equal(desiredWorkerReplicas))) // Check if the head pod is ready g.Eventually(HeadPod(test, rayCluster), TestTimeoutShort).Should(WithTransform(IsPodRunningAndReady, BeTrue())) diff --git a/ray-operator/test/sampleyaml/rayservice_test.go b/ray-operator/test/sampleyaml/rayservice_test.go index fcf6a25786..09286083cf 100644 --- a/ray-operator/test/sampleyaml/rayservice_test.go +++ b/ray-operator/test/sampleyaml/rayservice_test.go @@ -1,6 +1,7 @@ package sampleyaml import ( + "path" "testing" . "github.com/onsi/gomega" @@ -32,10 +33,11 @@ func TestRayService(t *testing.T) { test := With(t) g := NewWithT(t) + yamlFilePath := path.Join(GetSampleYAMLDir(test), tt.name) namespace := test.NewTestNamespace() test.StreamKubeRayOperatorLogs() - rayServiceFromYaml := DeserializeRayServiceSampleYAML(test, tt.name) - KubectlApplyYAML(test, tt.name, namespace.Name) + rayServiceFromYaml := DeserializeRayServiceYAML(test, yamlFilePath) + KubectlApplyYAML(test, yamlFilePath, namespace.Name) rayService, err := GetRayService(test, namespace.Name, rayServiceFromYaml.Name) g.Expect(err).NotTo(HaveOccurred()) diff --git a/ray-operator/test/sampleyaml/support.go b/ray-operator/test/sampleyaml/support.go index 68b033f1f6..768ea3ba9c 100644 --- a/ray-operator/test/sampleyaml/support.go +++ b/ray-operator/test/sampleyaml/support.go @@ -2,7 +2,6 @@ package sampleyaml import ( "os" - "os/exec" "path/filepath" "runtime" @@ -11,11 +10,10 @@ import ( corev1 "k8s.io/api/core/v1" rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" - rayscheme "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/scheme" . "github.com/ray-project/kuberay/ray-operator/test/support" ) -func getSampleYAMLDir(t Test) string { +func GetSampleYAMLDir(t Test) string { t.T().Helper() _, b, _, _ := runtime.Caller(0) sampleYAMLDir := filepath.Join(filepath.Dir(b), "../../config/samples") @@ -25,55 +23,6 @@ func getSampleYAMLDir(t Test) string { return sampleYAMLDir } -func readYAML(t Test, filename string) []byte { - t.T().Helper() - sampleYAMLDir := getSampleYAMLDir(t) - yamlFile := filepath.Join(sampleYAMLDir, filename) - yamlFileContent, err := os.ReadFile(yamlFile) - assert.NoError(t.T(), err) - return yamlFileContent -} - -func DeserializeRayClusterSampleYAML(t Test, filename string) *rayv1.RayCluster { - t.T().Helper() - yamlFileContent := readYAML(t, filename) - decoder := rayscheme.Codecs.UniversalDecoder() - rayCluster := &rayv1.RayCluster{} - _, _, err := decoder.Decode(yamlFileContent, nil, rayCluster) - assert.NoError(t.T(), err) - return rayCluster -} - -func DeserializeRayServiceSampleYAML(t Test, filename string) *rayv1.RayService { - t.T().Helper() - yamlFileContent := readYAML(t, filename) - decoder := rayscheme.Codecs.UniversalDecoder() - rayService := &rayv1.RayService{} - _, _, err := decoder.Decode(yamlFileContent, nil, rayService) - assert.NoError(t.T(), err) - return rayService -} - -func DeserializeRayJobSampleYAML(t Test, filename string) *rayv1.RayJob { - t.T().Helper() - yamlFileContent := readYAML(t, filename) - decoder := rayscheme.Codecs.UniversalDecoder() - rayJob := &rayv1.RayJob{} - _, _, err := decoder.Decode(yamlFileContent, nil, rayJob) - assert.NoError(t.T(), err) - return rayJob -} - -func KubectlApplyYAML(t Test, filename string, namespace string) { - t.T().Helper() - sampleYAMLDir := getSampleYAMLDir(t) - sampleYAMLPath := filepath.Join(sampleYAMLDir, filename) - kubectlCmd := exec.CommandContext(t.Ctx(), "kubectl", "apply", "-f", sampleYAMLPath, "-n", namespace) - err := kubectlCmd.Run() - assert.NoError(t.T(), err) - t.T().Logf("Successfully applied %s", filename) -} - func IsPodRunningAndReady(pod *corev1.Pod) bool { if pod.Status.Phase != corev1.PodRunning { return false diff --git a/ray-operator/test/support/yaml.go b/ray-operator/test/support/yaml.go new file mode 100644 index 0000000000..aa3a4c8969 --- /dev/null +++ b/ray-operator/test/support/yaml.go @@ -0,0 +1,57 @@ +package support + +import ( + "os" + "os/exec" + + "github.com/stretchr/testify/assert" + + "k8s.io/apimachinery/pkg/runtime" + + rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" + rayscheme "github.com/ray-project/kuberay/ray-operator/pkg/client/clientset/versioned/scheme" +) + +func deserializeYAML(filename string, into runtime.Object) error { + yamlFileContent, err := os.ReadFile(filename) + if err != nil { + return err + } + decoder := rayscheme.Codecs.UniversalDecoder() + if _, _, err = decoder.Decode(yamlFileContent, nil, into); err != nil { + return err + } + return nil +} + +func DeserializeRayClusterYAML(t Test, filename string) *rayv1.RayCluster { + t.T().Helper() + rayCluster := &rayv1.RayCluster{} + err := deserializeYAML(filename, rayCluster) + assert.NoError(t.T(), err) + return rayCluster +} + +func DeserializeRayJobYAML(t Test, filename string) *rayv1.RayJob { + t.T().Helper() + rayJob := &rayv1.RayJob{} + err := deserializeYAML(filename, rayJob) + assert.NoError(t.T(), err) + return rayJob +} + +func DeserializeRayServiceYAML(t Test, filename string) *rayv1.RayService { + t.T().Helper() + rayService := &rayv1.RayService{} + err := deserializeYAML(filename, rayService) + assert.NoError(t.T(), err) + return rayService +} + +func KubectlApplyYAML(t Test, filename string, namespace string) { + t.T().Helper() + kubectlCmd := exec.CommandContext(t.Ctx(), "kubectl", "apply", "-f", filename, "-n", namespace) + err := kubectlCmd.Run() + assert.NoError(t.T(), err) + t.T().Logf("Successfully applied %s", filename) +}