Skip to content

Commit 9796f77

Browse files
authored
Merge pull request #588 from stuggi/pod_helper
[test] Add additional test helpers for pod and NAD
2 parents cbba204 + 2386c86 commit 9796f77

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

modules/common/test/helpers/nad.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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+
}

modules/common/test/helpers/pod.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/onsi/gomega"
1818
corev1 "k8s.io/api/core/v1"
1919
"k8s.io/apimachinery/pkg/types"
20+
"sigs.k8s.io/controller-runtime/pkg/client"
2021
)
2122

2223
// GetPod - retrieves a Pod resource.
@@ -84,3 +85,25 @@ func (tc *TestHelper) SimulatePodReady(name types.NamespacedName) {
8485
}, tc.Timeout, tc.Interval).Should(gomega.Succeed())
8586
tc.Logger.Info("Simulated pod ready state", "on", name)
8687
}
88+
89+
// CreatePod creates a new Pod resource with the provided annotations and spec.
90+
//
91+
// Example usage:
92+
//
93+
// annotations := map[string]string{}{"key": "value"}
94+
// spec := map[string]interface{}{"key": "value"}
95+
// p := th.CreatePod(types.NamespacedName{Namespace: "default", Name: "example"}, annotations, spec)
96+
func (tc *TestHelper) CreatePod(name types.NamespacedName, annotations map[string]string, spec map[string]interface{}) client.Object {
97+
raw := map[string]interface{}{
98+
"apiVersion": "v1",
99+
"kind": "Pod",
100+
"metadata": map[string]interface{}{
101+
"annotations": annotations,
102+
"name": name.Name,
103+
"namespace": name.Namespace,
104+
},
105+
"spec": spec,
106+
}
107+
108+
return tc.CreateUnstructured(raw)
109+
}

0 commit comments

Comments
 (0)