|
| 1 | +/* |
| 2 | +Copyright 2024 Red Hat |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package helpers |
| 15 | + |
| 16 | +import ( |
| 17 | + "github.com/onsi/gomega" |
| 18 | + "k8s.io/apimachinery/pkg/types" |
| 19 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 20 | + |
| 21 | + networkv1 "github.com/k8snetworkplumbingwg/network-attachment-definition-client/pkg/apis/k8s.cni.cncf.io/v1" |
| 22 | +) |
| 23 | + |
| 24 | +// GetNAD - retrieves a NetworkAttachmentDefinition resource. |
| 25 | +// |
| 26 | +// Example usage: |
| 27 | +// |
| 28 | +// th.GetNAD(types.NamespacedName{Name: "test-nad", Namespace: "test-namespace"}) |
| 29 | +func (tc *TestHelper) GetNAD(name types.NamespacedName) *networkv1.NetworkAttachmentDefinition { |
| 30 | + nad := &networkv1.NetworkAttachmentDefinition{} |
| 31 | + gomega.Eventually(func(g gomega.Gomega) { |
| 32 | + g.Expect(tc.K8sClient.Get(tc.Ctx, name, nad)).Should(gomega.Succeed()) |
| 33 | + }, tc.Timeout, tc.Interval).Should(gomega.Succeed()) |
| 34 | + return nad |
| 35 | +} |
| 36 | + |
| 37 | +// CreateNAD creates a new NetworkAttachmentDefinition resource with the provided spec. |
| 38 | +// |
| 39 | +// Example usage: |
| 40 | +// |
| 41 | +// spec := map[string]interface{}{"key": "value"} |
| 42 | +// p := th.CreateNAD(types.NamespacedName{Namespace: "default", Name: "example"}, spec) |
| 43 | +func (tc *TestHelper) CreateNAD(name types.NamespacedName, spec map[string]interface{}) client.Object { |
| 44 | + raw := map[string]interface{}{ |
| 45 | + "apiVersion": "k8s.cni.cncf.io/v1", |
| 46 | + "kind": "NetworkAttachmentDefinition", |
| 47 | + "metadata": map[string]interface{}{ |
| 48 | + "name": name.Name, |
| 49 | + "namespace": name.Namespace, |
| 50 | + }, |
| 51 | + "spec": spec, |
| 52 | + } |
| 53 | + return tc.CreateUnstructured(raw) |
| 54 | +} |
0 commit comments