diff --git a/support/core.go b/support/core.go index 673eebb..2e30bd8 100644 --- a/support/core.go +++ b/support/core.go @@ -18,7 +18,9 @@ package support import ( "encoding/json" + "fmt" "io" + "reflect" "github.com/onsi/gomega" @@ -184,3 +186,11 @@ func GetNodeInternalIP(t Test, node corev1.Node) (IP string) { return } + +func ResourceName(obj any) (string, error) { + value := reflect.ValueOf(obj) + if value.Kind() != reflect.Struct { + return "", fmt.Errorf("input must be a struct") + } + return value.FieldByName("Name").String(), nil +} diff --git a/support/core_test.go b/support/core_test.go index 031d329..a180930 100644 --- a/support/core_test.go +++ b/support/core_test.go @@ -43,3 +43,16 @@ func TestGetNodes(t *testing.T) { test.Expect(nodes[0].Name).To(gomega.Equal("test-node"), "Expected node name 'test-node', but got '%s'", nodes[0].Name) } + +func TestResourceName(t *testing.T) { + type TestStruct struct { + Name string + } + test := NewTest(t) + + obj := TestStruct{Name: "test-resource"} + resourceName, err := ResourceName(obj) + + test.Expect(err).To(gomega.BeNil(), "Expected no error, but got '%v'", err) + test.Expect(resourceName).To(gomega.Equal("test-resource"), "Expected resource name 'test-resource', but got '%s'", resourceName) +}