Skip to content

Commit 19c2d29

Browse files
Add ResourceName generic function
1 parent a4268f6 commit 19c2d29

File tree

2 files changed

+15
-69
lines changed

2 files changed

+15
-69
lines changed

support/core.go

+8-28
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ package support
1818

1919
import (
2020
"encoding/json"
21+
"fmt"
2122
"io"
23+
"reflect"
2224

2325
"github.com/onsi/gomega"
2426

@@ -185,32 +187,10 @@ func GetNodeInternalIP(t Test, node corev1.Node) (IP string) {
185187
return
186188
}
187189

188-
func GetContainerName(t Test, container corev1.Container) string {
189-
t.T().Helper()
190-
t.Expect(container.Name).Should(gomega.Not(gomega.BeEmpty()))
191-
return container.Name
192-
}
193-
194-
func GetVolumeName(t Test, volume corev1.Volume) string {
195-
t.T().Helper()
196-
t.Expect(volume.Name).Should(gomega.Not(gomega.BeEmpty()))
197-
return volume.Name
198-
}
199-
200-
func GetServiceAccountName(t Test, serviceAccount corev1.ServiceAccount) string {
201-
t.T().Helper()
202-
t.Expect(serviceAccount.Name).Should(gomega.Not(gomega.BeEmpty()))
203-
return serviceAccount.Name
204-
}
205-
206-
func GetVolumeMountName(t Test, volumeMount corev1.VolumeMount) string {
207-
t.T().Helper()
208-
t.Expect(volumeMount.Name).Should(gomega.Not(gomega.BeEmpty()))
209-
return volumeMount.Name
210-
}
211-
212-
func GetEnvVarName(t Test, envVar corev1.EnvVar) string {
213-
t.T().Helper()
214-
t.Expect(envVar.Name).Should(gomega.Not(gomega.BeEmpty()))
215-
return envVar.Name
190+
func ResourceName(obj any) (string, error) {
191+
value := reflect.ValueOf(obj)
192+
if value.Kind() != reflect.Struct {
193+
return "", fmt.Errorf("input must be a struct")
194+
}
195+
return value.FieldByName("Name").String(), nil
216196
}

support/core_test.go

+7-41
Original file line numberDiff line numberDiff line change
@@ -44,49 +44,15 @@ func TestGetNodes(t *testing.T) {
4444

4545
}
4646

47-
func TestGetContainerName(t *testing.T) {
48-
test := NewTest(t)
49-
container := corev1.Container{
50-
Name: "test-container",
51-
}
52-
containerName := GetContainerName(test, container)
53-
test.Expect(containerName).To(gomega.Equal("test-container"), "Expected container name 'test-container', but got '%s'", containerName)
54-
}
55-
56-
func TestGetVolumeName(t *testing.T) {
57-
test := NewTest(t)
58-
volume := corev1.Volume{
59-
Name: "test-volume",
47+
func TestResourceName(t *testing.T) {
48+
type TestStruct struct {
49+
Name string
6050
}
61-
volumeName := GetVolumeName(test, volume)
62-
test.Expect(volumeName).To(gomega.Equal("test-volume"), "Expected volume name 'test-volume', but got '%s'", volumeName)
63-
}
64-
65-
func TestGetServiceAccountName(t *testing.T) {
6651
test := NewTest(t)
67-
serviceAccount := corev1.ServiceAccount{
68-
ObjectMeta: metav1.ObjectMeta{
69-
Name: "test-service-account",
70-
},
71-
}
72-
serviceAccountName := GetServiceAccountName(test, serviceAccount)
73-
test.Expect(serviceAccountName).To(gomega.Equal("test-service-account"), "Expected service account name 'test-service-account', but got '%s'", serviceAccountName)
74-
}
7552

76-
func TestGetVolumeMountName(t *testing.T) {
77-
test := NewTest(t)
78-
volumeMount := corev1.VolumeMount{
79-
Name: "test-volume-mount",
80-
}
81-
volumeMountName := GetVolumeMountName(test, volumeMount)
82-
test.Expect(volumeMountName).To(gomega.Equal("test-volume-mount"), "Expected volume mount name 'test-volume-mount', but got '%s'", volumeMountName)
83-
}
53+
obj := TestStruct{Name: "test-resource"}
54+
resourceName, err := ResourceName(obj)
8455

85-
func TestGetEnvVarName(t *testing.T) {
86-
test := NewTest(t)
87-
envVar := corev1.EnvVar{
88-
Name: "test-env-var",
89-
}
90-
envVarName := GetEnvVarName(test, envVar)
91-
test.Expect(envVarName).To(gomega.Equal("test-env-var"), "Expected env var name 'test-env-var', but got '%s'", envVarName)
56+
test.Expect(err).To(gomega.BeNil(), "Expected no error, but got '%v'", err)
57+
test.Expect(resourceName).To(gomega.Equal("test-resource"), "Expected resource name 'test-resource', but got '%s'", resourceName)
9258
}

0 commit comments

Comments
 (0)