|
| 1 | +package e2e |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + . "github.com/onsi/gomega" |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + |
| 9 | + rayv1 "github.com/ray-project/kuberay/ray-operator/apis/ray/v1" |
| 10 | + "github.com/ray-project/kuberay/ray-operator/controllers/ray/utils" |
| 11 | + rayv1ac "github.com/ray-project/kuberay/ray-operator/pkg/client/applyconfiguration/ray/v1" |
| 12 | + . "github.com/ray-project/kuberay/ray-operator/test/support" |
| 13 | +) |
| 14 | + |
| 15 | +// NewRayClusterSpecWithAuth creates a new RayClusterSpec with the specified AuthMode. |
| 16 | +func NewRayClusterSpecWithAuth(authMode rayv1.AuthMode) *rayv1ac.RayClusterSpecApplyConfiguration { |
| 17 | + return NewRayClusterSpec(). |
| 18 | + WithAuthOptions(rayv1ac.AuthOptions().WithMode(authMode)) |
| 19 | +} |
| 20 | + |
| 21 | +func TestRayClusterAuthOptions(t *testing.T) { |
| 22 | + test := With(t) |
| 23 | + g := NewWithT(t) |
| 24 | + |
| 25 | + namespace := test.NewTestNamespace() |
| 26 | + |
| 27 | + test.T().Run("RayCluster with token authentication enabled", func(t *testing.T) { |
| 28 | + t.Parallel() |
| 29 | + |
| 30 | + rayClusterAC := rayv1ac.RayCluster("raycluster-auth-token", namespace.Name). |
| 31 | + WithSpec(NewRayClusterSpecWithAuth(rayv1.AuthModeToken).WithRayVersion("2.51")) |
| 32 | + |
| 33 | + rayCluster, err := test.Client().Ray().RayV1().RayClusters(namespace.Name).Apply(test.Ctx(), rayClusterAC, TestApplyOptions) |
| 34 | + g.Expect(err).NotTo(HaveOccurred()) |
| 35 | + LogWithTimestamp(test.T(), "Created RayCluster %s/%s successfully with AuthModeToken", rayCluster.Namespace, rayCluster.Name) |
| 36 | + |
| 37 | + LogWithTimestamp(test.T(), "Waiting for RayCluster %s/%s to become ready", rayCluster.Namespace, rayCluster.Name) |
| 38 | + g.Eventually(RayCluster(test, rayCluster.Namespace, rayCluster.Name), TestTimeoutMedium). |
| 39 | + Should(WithTransform(RayClusterState, Equal(rayv1.Ready))) |
| 40 | + |
| 41 | + headPod, err := GetHeadPod(test, rayCluster) |
| 42 | + g.Expect(err).NotTo(HaveOccurred()) |
| 43 | + g.Expect(headPod).NotTo(BeNil()) |
| 44 | + verifyAuthTokenEnvVars(t, rayCluster, *headPod) |
| 45 | + |
| 46 | + workerPods, err := GetWorkerPods(test, rayCluster) |
| 47 | + g.Expect(err).NotTo(HaveOccurred()) |
| 48 | + g.Expect(workerPods).ToNot(BeEmpty()) |
| 49 | + for _, workerPod := range workerPods { |
| 50 | + verifyAuthTokenEnvVars(t, rayCluster, workerPod) |
| 51 | + } |
| 52 | + |
| 53 | + // TODO(andrewsykim): add job submission test with and without token once a Ray version with token support is released. |
| 54 | + }) |
| 55 | +} |
| 56 | + |
| 57 | +func verifyAuthTokenEnvVars(t *testing.T, rayCluster *rayv1.RayCluster, pod corev1.Pod) { |
| 58 | + g := NewWithT(t) |
| 59 | + |
| 60 | + var rayAuthModeEnvVar *corev1.EnvVar |
| 61 | + for _, envVar := range pod.Spec.Containers[0].Env { |
| 62 | + if envVar.Name == utils.RAY_AUTH_MODE_ENV_VAR { |
| 63 | + rayAuthModeEnvVar = &envVar |
| 64 | + break |
| 65 | + } |
| 66 | + } |
| 67 | + g.Expect(rayAuthModeEnvVar).NotTo(BeNil(), "RAY_AUTH_MODE environment variable should be set") |
| 68 | + g.Expect(rayAuthModeEnvVar.Value).To(Equal(string(rayv1.AuthModeToken)), "RAY_AUTH_MODE should be %s", rayv1.AuthModeToken) |
| 69 | + |
| 70 | + var rayAuthTokenEnvVar *corev1.EnvVar |
| 71 | + for _, envVar := range pod.Spec.Containers[0].Env { |
| 72 | + if envVar.Name == utils.RAY_AUTH_TOKEN_ENV_VAR { |
| 73 | + rayAuthTokenEnvVar = &envVar |
| 74 | + break |
| 75 | + } |
| 76 | + } |
| 77 | + g.Expect(rayAuthTokenEnvVar).NotTo(BeNil(), "RAY_AUTH_TOKEN environment variable should be set for AuthModeToken") |
| 78 | + g.Expect(rayAuthTokenEnvVar.ValueFrom).NotTo(BeNil(), "RAY_AUTH_TOKEN should be populated from a secret") |
| 79 | + g.Expect(rayAuthTokenEnvVar.ValueFrom.SecretKeyRef).NotTo(BeNil(), "RAY_AUTH_TOKEN should be populated from a secret key ref") |
| 80 | + g.Expect(rayAuthTokenEnvVar.ValueFrom.SecretKeyRef.Name).To(ContainSubstring(rayCluster.Name), "Secret name should contain RayCluster name") |
| 81 | + g.Expect(rayAuthTokenEnvVar.ValueFrom.SecretKeyRef.Key).To(Equal(utils.RAY_AUTH_TOKEN_SECRET_KEY), "Secret key should be %s", utils.RAY_AUTH_TOKEN_SECRET_KEY) |
| 82 | +} |
0 commit comments