From 2ced747ee5812361ae1bcef7ae6e4aca1556f49b Mon Sep 17 00:00:00 2001 From: Martin Schuppert Date: Wed, 11 Dec 2024 16:02:17 +0100 Subject: [PATCH] [test] Add GetNAD and CreateNAD test helper GetNAD - retrieves a NetworkAttachmentDefinition resource. Example usage: ~~~ th.GetNAD(types.NamespacedName{Name: "test-nad", Namespace: "test-namespace"}) ~~~ CreateNAD creates a new NetworkAttachmentDefinition resource with the provided spec. Example usage: ~~~ spec := map[string]interface{}{"key": "value"} p := th.CreateNAD(types.NamespacedName{Namespace: "default", Name: "example"}, spec) ~~~ Signed-off-by: Martin Schuppert --- modules/common/test/helpers/nad.go | 54 ++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 modules/common/test/helpers/nad.go diff --git a/modules/common/test/helpers/nad.go b/modules/common/test/helpers/nad.go new file mode 100644 index 00000000..8fd75cb4 --- /dev/null +++ b/modules/common/test/helpers/nad.go @@ -0,0 +1,54 @@ +/* +Copyright 2024 Red Hat +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + http://www.apache.org/licenses/LICENSE-2.0 +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package helpers + +import ( + "github.com/onsi/gomega" + "k8s.io/apimachinery/pkg/types" + "sigs.k8s.io/controller-runtime/pkg/client" + + networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" +) + +// GetNAD - retrieves a NetworkAttachmentDefinition resource. +// +// example usage: +// +// th.GetNAD(types.NamespacedName{Name: "test-nad", Namespace: "test-namespace"}) +func (tc *TestHelper) GetNAD(name types.NamespacedName) *networkv1.NetworkAttachmentDefinition { + nad := &networkv1.NetworkAttachmentDefinition{} + gomega.Eventually(func(g gomega.Gomega) { + g.Expect(tc.K8sClient.Get(tc.Ctx, name, nad)).Should(gomega.Succeed()) + }, tc.Timeout, tc.Interval).Should(gomega.Succeed()) + return nad +} + +// CreateNAD creates a new NetworkAttachmentDefinition resource with the provided spec. +// +// Example usage: +// +// spec := map[string]interface{}{"key": "value"} +// p := th.CreateNAD(types.NamespacedName{Namespace: "default", Name: "example"}, spec) +func (tc *TestHelper) CreateNAD(name types.NamespacedName, spec map[string]interface{}) client.Object { + raw := map[string]interface{}{ + "apiVersion": "k8s.cni.cncf.io/v1", + "kind": "NetworkAttachmentDefinition", + "metadata": map[string]interface{}{ + "name": name.Name, + "namespace": name.Namespace, + }, + "spec": spec, + } + return tc.CreateUnstructured(raw) +}