-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsuite_test.go
More file actions
159 lines (133 loc) · 5.05 KB
/
suite_test.go
File metadata and controls
159 lines (133 loc) · 5.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
// SPDX-FileCopyrightText: 2025 SAP SE or an SAP affiliate company and IronCore contributors
// SPDX-License-Identifier: MIT
package metal
import (
"fmt"
"os"
"path/filepath"
"runtime"
"testing"
"time"
"gopkg.in/yaml.v2"
"github.com/ironcore-dev/controller-utils/modutils"
"github.com/ironcore-dev/fedhcp/internal/api"
"github.com/ironcore-dev/fedhcp/internal/kubernetes"
ipamv1alpha1 "github.com/ironcore-dev/ipam/api/ipam/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
. "sigs.k8s.io/controller-runtime/pkg/envtest/komega"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"
metalv1alpha1 "github.com/ironcore-dev/metal-operator/api/v1alpha1"
//+kubebuilder:scaffold:imports
)
const (
pollingInterval = 50 * time.Millisecond
eventuallyTimeout = 3 * time.Second
consistentlyDuration = 1 * time.Second
machineWithIPAddressName = "machine-with-ip-address"
machineWithoutIPAddressName = "machine-without-ip-address"
machineWithIPAddressMACAddress = "11:22:33:44:55:66"
machineWithoutIPAddressMACAddress = "47:11:47:11:47:11"
unknownMachineMACAddress = "11:11:11:11:11:11"
machineWithIPAddressMACAddressPrefFilter = "11:22:33"
linkLocalIPV6Prefix = "fe80::"
privateIPV4Address = "192.168.47.11"
inventoryConfigFile = "config.yaml"
)
var (
cfg *rest.Config
k8sClient client.Client
testEnv *envtest.Environment
)
func TestControllers(t *testing.T) {
SetDefaultConsistentlyPollingInterval(pollingInterval)
SetDefaultEventuallyPollingInterval(pollingInterval)
SetDefaultEventuallyTimeout(eventuallyTimeout)
SetDefaultConsistentlyDuration(consistentlyDuration)
RegisterFailHandler(Fail)
RunSpecs(t, "Controller Suite")
}
var _ = BeforeSuite(func() {
logf.SetLogger(zap.New(zap.WriteTo(GinkgoWriter), zap.UseDevMode(true)))
By("bootstrapping test environment")
testEnv = &envtest.Environment{
CRDDirectoryPaths: []string{
modutils.Dir("github.com/ironcore-dev/metal-operator", "config", "crd", "bases"),
modutils.Dir("github.com/ironcore-dev/ipam", "config", "crd", "bases"),
},
ErrorIfCRDPathMissing: true,
// The BinaryAssetsDirectory is only required if you want to run the tests directly
// without call the makefile target test. If not informed it will look for the
// default path defined in controller-runtime which is /usr/local/kubebuilder/.
// Note that you must have the required binaries setup under the bin directory to perform
// the tests directly. When we run make test it will be setup and used automatically.
BinaryAssetsDirectory: filepath.Join("..", "..", "bin", "k8s",
fmt.Sprintf("1.34.0-%s-%s", runtime.GOOS, runtime.GOARCH)),
}
var err error
// cfg is defined in this file globally.
cfg, err = testEnv.Start()
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
DeferCleanup(testEnv.Stop)
Expect(ipamv1alpha1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
Expect(metalv1alpha1.AddToScheme(scheme.Scheme)).NotTo(HaveOccurred())
//+kubebuilder:scaffold:scheme
k8sClient, err = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(err).NotTo(HaveOccurred())
Expect(k8sClient).NotTo(BeNil())
// set komega client
SetClient(k8sClient)
// assign global k8s client in plugin
kubernetes.SetClient(&k8sClient)
})
func SetupTest() *corev1.Namespace {
ns := &corev1.Namespace{}
BeforeEach(func(ctx SpecContext) {
*ns = corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-",
},
}
Expect(k8sClient.Create(ctx, ns)).To(Succeed(), "failed to create test namespace")
DeferCleanup(k8sClient.Delete, ns)
configFile := inventoryConfigFile
data := api.MetalConfig{
NamePrefix: "server-",
Inventories: []api.Inventory{
{
Name: machineWithIPAddressName,
MacAddress: machineWithIPAddressMACAddress,
},
{
Name: machineWithoutIPAddressName,
MacAddress: machineWithoutIPAddressMACAddress,
},
},
Filter: api.Filter{
MacPrefix: []string{machineWithIPAddressMACAddressPrefFilter},
},
}
configData, err := yaml.Marshal(data)
Expect(err).NotTo(HaveOccurred())
file, err := os.CreateTemp(GinkgoT().TempDir(), configFile)
Expect(err).NotTo(HaveOccurred())
defer func() {
_ = file.Close()
}()
Expect(os.WriteFile(file.Name(), configData, 0644)).To(Succeed())
inventory, err = loadConfig(file.Name())
Expect(err).NotTo(HaveOccurred())
Expect(inventory.Entries).To(HaveKeyWithValue(machineWithIPAddressMACAddress, machineWithIPAddressName))
Expect(inventory.Entries).To(HaveKeyWithValue(machineWithoutIPAddressMACAddress, machineWithoutIPAddressName))
Expect(inventory.Strategy).To(Equal(OnBoardingStrategyStatic))
})
return ns
}