Skip to content

Commit f4bb394

Browse files
Add ResourceName generic function for raycluster_webhook tests
1 parent 3b89d26 commit f4bb394

File tree

1 file changed

+32
-42
lines changed

1 file changed

+32
-42
lines changed

pkg/controllers/raycluster_webhook_test.go

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ limitations under the License.
1717
package controllers
1818

1919
import (
20+
"fmt"
21+
"reflect"
2022
"testing"
2123

2224
. "github.com/onsi/gomega"
@@ -103,7 +105,7 @@ func TestRayClusterWebhookDefault(t *testing.T) {
103105
To(
104106
And(
105107
HaveLen(1),
106-
ContainElement(WithTransform(ContainerName, Equal(oauthProxyContainerName))),
108+
ContainElement(WithTransform(ResourceName, Equal(oauthProxyContainerName))),
107109
),
108110
"Expected the OAuth proxy container to be present in the head group",
109111
)
@@ -114,9 +116,9 @@ func TestRayClusterWebhookDefault(t *testing.T) {
114116
To(
115117
And(
116118
HaveLen(3),
117-
ContainElement(WithTransform(VolumeName, Equal(oauthProxyVolumeName))),
118-
ContainElement(WithTransform(VolumeName, Equal("ca-vol"))),
119-
ContainElement(WithTransform(VolumeName, Equal("server-cert"))),
119+
ContainElement(WithTransform(ResourceName, Equal(oauthProxyVolumeName))),
120+
ContainElement(WithTransform(ResourceName, Equal("ca-vol"))),
121+
ContainElement(WithTransform(ResourceName, Equal("server-cert"))),
120122
),
121123
"Expected the OAuth proxy TLS secret and CA volumes to be present in the head group",
122124
)
@@ -133,12 +135,12 @@ func TestRayClusterWebhookDefault(t *testing.T) {
133135
To(
134136
And(
135137
HaveLen(6),
136-
ContainElement(WithTransform(EnvVarName, Equal("COOKIE_SECRET"))),
137-
ContainElement(WithTransform(EnvVarName, Equal("MY_POD_IP"))),
138-
ContainElement(WithTransform(EnvVarName, Equal("RAY_USE_TLS"))),
139-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_SERVER_CERT"))),
140-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_SERVER_KEY"))),
141-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_CA_CERT"))),
138+
ContainElement(WithTransform(ResourceName, Equal("COOKIE_SECRET"))),
139+
ContainElement(WithTransform(ResourceName, Equal("MY_POD_IP"))),
140+
ContainElement(WithTransform(ResourceName, Equal("RAY_USE_TLS"))),
141+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_SERVER_CERT"))),
142+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_SERVER_KEY"))),
143+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_CA_CERT"))),
142144
),
143145
"Expected the required environment variables to be present in the head group",
144146
)
@@ -149,7 +151,7 @@ func TestRayClusterWebhookDefault(t *testing.T) {
149151
To(
150152
And(
151153
HaveLen(1),
152-
ContainElement(WithTransform(ContainerName, Equal(initContainerName))),
154+
ContainElement(WithTransform(ResourceName, Equal(initContainerName))),
153155
),
154156
"Expected the create-cert init container to be present in the head group",
155157
)
@@ -160,9 +162,9 @@ func TestRayClusterWebhookDefault(t *testing.T) {
160162
To(
161163
And(
162164
HaveLen(3),
163-
ContainElement(WithTransform(VolumeMountName, Equal(oauthProxyVolumeName))),
164-
ContainElement(WithTransform(VolumeMountName, Equal("ca-vol"))),
165-
ContainElement(WithTransform(VolumeMountName, Equal("server-cert"))),
165+
ContainElement(WithTransform(ResourceName, Equal(oauthProxyVolumeName))),
166+
ContainElement(WithTransform(ResourceName, Equal("ca-vol"))),
167+
ContainElement(WithTransform(ResourceName, Equal("server-cert"))),
166168
),
167169
"Expected the OAuth proxy TLS secret and CA volume mounts to be present in the head group",
168170
)
@@ -174,11 +176,11 @@ func TestRayClusterWebhookDefault(t *testing.T) {
174176
To(
175177
And(
176178
HaveLen(5),
177-
ContainElement(WithTransform(EnvVarName, Equal("MY_POD_IP"))),
178-
ContainElement(WithTransform(EnvVarName, Equal("RAY_USE_TLS"))),
179-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_SERVER_CERT"))),
180-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_SERVER_KEY"))),
181-
ContainElement(WithTransform(EnvVarName, Equal("RAY_TLS_CA_CERT"))),
179+
ContainElement(WithTransform(ResourceName, Equal("MY_POD_IP"))),
180+
ContainElement(WithTransform(ResourceName, Equal("RAY_USE_TLS"))),
181+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_SERVER_CERT"))),
182+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_SERVER_KEY"))),
183+
ContainElement(WithTransform(ResourceName, Equal("RAY_TLS_CA_CERT"))),
182184
),
183185
"Expected the required environment variables to be present in each worker group",
184186
)
@@ -191,8 +193,8 @@ func TestRayClusterWebhookDefault(t *testing.T) {
191193
To(
192194
And(
193195
HaveLen(2),
194-
ContainElement(WithTransform(VolumeName, Equal("ca-vol"))),
195-
ContainElement(WithTransform(VolumeName, Equal("server-cert"))),
196+
ContainElement(WithTransform(ResourceName, Equal("ca-vol"))),
197+
ContainElement(WithTransform(ResourceName, Equal("server-cert"))),
196198
),
197199
"Expected the required CA volumes to be present in each worker group",
198200
)
@@ -205,8 +207,8 @@ func TestRayClusterWebhookDefault(t *testing.T) {
205207
To(
206208
And(
207209
HaveLen(2),
208-
ContainElement(WithTransform(VolumeMountName, Equal("ca-vol"))),
209-
ContainElement(WithTransform(VolumeMountName, Equal("server-cert"))),
210+
ContainElement(WithTransform(ResourceName, Equal("ca-vol"))),
211+
ContainElement(WithTransform(ResourceName, Equal("server-cert"))),
210212
),
211213
"Expected the required certificate volume mounts to be present in each worker group",
212214
)
@@ -219,7 +221,7 @@ func TestRayClusterWebhookDefault(t *testing.T) {
219221
To(
220222
And(
221223
HaveLen(1),
222-
ContainElement(WithTransform(ContainerName, Equal(initContainerName))),
224+
ContainElement(WithTransform(ResourceName, Equal(initContainerName))),
223225
),
224226
"Expected the required init container to be present in each worker group",
225227
)
@@ -646,22 +648,10 @@ func TestValidateUpdate(t *testing.T) {
646648
})
647649
}
648650

649-
func ContainerName(container corev1.Container) string {
650-
return container.Name
651-
}
652-
653-
func VolumeName(volume corev1.Volume) string {
654-
return volume.Name
655-
}
656-
657-
func ServiceAccountName(serviceAccount corev1.ServiceAccount) string {
658-
return serviceAccount.Name
659-
}
660-
661-
func VolumeMountName(volumeMount corev1.VolumeMount) string {
662-
return volumeMount.Name
663-
}
664-
665-
func EnvVarName(envVar corev1.EnvVar) string {
666-
return envVar.Name
651+
func ResourceName(obj any) (string, error) {
652+
value := reflect.ValueOf(obj)
653+
if value.Kind() != reflect.Struct {
654+
return "", fmt.Errorf("input must be a struct")
655+
}
656+
return value.FieldByName("Name").String(), nil
667657
}

0 commit comments

Comments
 (0)