-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_util.go
More file actions
448 lines (366 loc) · 16.8 KB
/
Copy pathtest_util.go
File metadata and controls
448 lines (366 loc) · 16.8 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
package utils
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os"
"os/exec"
"strings"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
appsv1 "k8s.io/api/apps/v1"
"github.com/feast-dev/feast/infra/feast-operator/api/feastversion"
"github.com/feast-dev/feast/infra/feast-operator/api/v1alpha1"
)
const (
FeastControllerNamespace = "feast-operator-system"
Timeout = 3 * time.Minute
ControllerDeploymentName = "feast-operator-controller-manager"
FeastPrefix = "feast-"
FeatureStoreName = "simple-feast-setup"
FeastResourceName = FeastPrefix + FeatureStoreName
)
// dynamically checks if all conditions of custom resource featurestore are in "Ready" state.
func checkIfFeatureStoreCustomResourceConditionsInReady(featureStoreName, namespace string) error {
// Wait 10 seconds to lets the feature store status update
time.Sleep(1 * time.Minute)
cmd := exec.Command("kubectl", "get", "featurestore", featureStoreName, "-n", namespace, "-o", "json")
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to get resource %s in namespace %s. Error: %v. Stderr: %s",
featureStoreName, namespace, err, stderr.String())
}
// Parse the JSON into FeatureStore
var resource v1alpha1.FeatureStore
if err := json.Unmarshal(out.Bytes(), &resource); err != nil {
return fmt.Errorf("failed to parse the resource JSON. Error: %v", err)
}
// Validate all conditions
for _, condition := range resource.Status.Conditions {
if condition.Status != "True" {
return fmt.Errorf(" FeatureStore=%s condition '%s' is not in 'Ready' state. Status: %s",
featureStoreName, condition.Type, condition.Status)
}
}
return nil
}
// CheckIfDeploymentExistsAndAvailable - validates if a deployment exists and also in the availability state as True.
func CheckIfDeploymentExistsAndAvailable(namespace string, deploymentName string, timeout time.Duration) error {
var output, errOutput bytes.Buffer
ticker := time.NewTicker(2 * time.Second)
defer ticker.Stop()
timeoutChan := time.After(timeout)
for {
select {
case <-timeoutChan:
return fmt.Errorf("timed out waiting for deployment %s to become available", deploymentName)
case <-ticker.C:
// Run kubectl command
cmd := exec.Command("kubectl", "get", "deployment", deploymentName, "-n", namespace, "-o", "json")
cmd.Stdout = &output
cmd.Stderr = &errOutput
if err := cmd.Run(); err != nil {
// Log error and retry
fmt.Printf("Deployment not yet found, we may try again to find the updated status: %s\n", errOutput.String())
continue
}
// Parse the JSON output into Deployment
var result appsv1.Deployment
if err := json.Unmarshal(output.Bytes(), &result); err != nil {
return fmt.Errorf("failed to parse deployment JSON: %v", err)
}
// Check for Available condition
for _, condition := range result.Status.Conditions {
if condition.Type == "Available" && condition.Status == "True" {
return nil // Deployment is available
}
}
// Reset buffers for the next loop iteration
output.Reset()
errOutput.Reset()
}
}
}
// validates if a service account exists using the kubectl CLI.
func checkIfServiceAccountExists(namespace, saName string) error {
cmd := exec.Command("kubectl", "get", "sa", saName, "-n", namespace)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to find service account %s in namespace %s. Error: %v. Stderr: %s",
saName, namespace, err, stderr.String())
}
// Check the output to confirm presence
if !strings.Contains(out.String(), saName) {
return fmt.Errorf("service account %s not found in namespace %s", saName, namespace)
}
return nil
}
// validates if a config map exists using the kubectl CLI.
func checkIfConfigMapExists(namespace, configMapName string) error {
cmd := exec.Command("kubectl", "get", "cm", configMapName, "-n", namespace)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to find config map %s in namespace %s. Error: %v. Stderr: %s",
configMapName, namespace, err, stderr.String())
}
// Check the output to confirm presence
if !strings.Contains(out.String(), configMapName) {
return fmt.Errorf("config map %s not found in namespace %s", configMapName, namespace)
}
return nil
}
// validates if a kubernetes service exists using the kubectl CLI.
func checkIfKubernetesServiceExists(namespace, serviceName string) error {
cmd := exec.Command("kubectl", "get", "service", serviceName, "-n", namespace)
var out bytes.Buffer
var stderr bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &stderr
if err := cmd.Run(); err != nil {
return fmt.Errorf("failed to find kubernetes service %s in namespace %s. Error: %v. Stderr: %s",
serviceName, namespace, err, stderr.String())
}
// Check the output to confirm presence
if !strings.Contains(out.String(), serviceName) {
return fmt.Errorf("kubernetes service %s not found in namespace %s", serviceName, namespace)
}
return nil
}
func isFeatureStoreHavingRemoteRegistry(namespace, featureStoreName string) (bool, error) {
timeout := time.Second * 30
interval := time.Second * 2 // Poll every 2 seconds
startTime := time.Now()
for time.Since(startTime) < timeout {
cmd := exec.Command("kubectl", "get", "featurestore", featureStoreName, "-n", namespace,
"-o=jsonpath='{.status.applied.services.registry}'")
output, err := cmd.Output()
if err != nil {
// Retry only on transient errors
if _, ok := err.(*exec.ExitError); ok {
time.Sleep(interval)
continue
}
return false, err // Return immediately on non-transient errors
}
// Convert output to string and trim any extra spaces
result := strings.TrimSpace(string(output))
// Remove single quotes if present
if strings.HasPrefix(result, "'") && strings.HasSuffix(result, "'") {
result = strings.Trim(result, "'")
}
if result == "" {
time.Sleep(interval) // Retry if result is empty
continue
}
// Parse the JSON into a map
var registryConfig v1alpha1.Registry
if err := json.Unmarshal([]byte(result), ®istryConfig); err != nil {
return false, err // Return false on JSON parsing failure
}
if registryConfig.Remote == nil {
return false, nil
}
hasHostname := registryConfig.Remote.Hostname != nil
hasValidFeastRef := registryConfig.Remote.FeastRef != nil &&
registryConfig.Remote.FeastRef.Name != ""
return hasHostname || hasValidFeastRef, nil
}
return false, errors.New("timeout waiting for featurestore registry status to be ready")
}
func validateTheFeatureStoreCustomResource(namespace string, featureStoreName string, feastResourceName string, feastK8sResourceNames []string, timeout time.Duration) {
hasRemoteRegistry, err := isFeatureStoreHavingRemoteRegistry(namespace, featureStoreName)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"Error occurred while checking FeatureStore %s is having remote registry or not. \nError: %v\n",
featureStoreName, err))
k8sResourceNames := []string{feastResourceName}
if !hasRemoteRegistry {
feastK8sResourceNames = append(feastK8sResourceNames, feastResourceName+"-registry")
}
for _, deploymentName := range k8sResourceNames {
By(fmt.Sprintf("validate the feast deployment: %s is up and in availability state.", deploymentName))
err = CheckIfDeploymentExistsAndAvailable(namespace, deploymentName, timeout)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"Deployment %s is not available but expected to be available. \nError: %v\n",
deploymentName, err,
))
fmt.Printf("Feast Deployment %s is available\n", deploymentName)
}
By("Check if the feast client - kubernetes config map exists.")
configMapName := feastResourceName + "-client"
err = checkIfConfigMapExists(namespace, configMapName)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"config map %s is not available but expected to be available. \nError: %v\n",
configMapName, err,
))
fmt.Printf("Feast Deployment client config map %s is available\n", configMapName)
for _, serviceAccountName := range k8sResourceNames {
By(fmt.Sprintf("validate the feast service account: %s is available.", serviceAccountName))
err = checkIfServiceAccountExists(namespace, serviceAccountName)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"Service account %s does not exist in namespace %s. Error: %v",
serviceAccountName, namespace, err,
))
fmt.Printf("Service account %s exists in namespace %s\n", serviceAccountName, namespace)
}
for _, serviceName := range feastK8sResourceNames {
By(fmt.Sprintf("validate the kubernetes service name: %s is available.", serviceName))
err = checkIfKubernetesServiceExists(namespace, serviceName)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"kubernetes service %s is not available but expected to be available. \nError: %v\n",
serviceName, err,
))
fmt.Printf("kubernetes service %s is available\n", serviceName)
}
By(fmt.Sprintf("Checking FeatureStore customer resource: %s is in Ready Status.", featureStoreName))
err = checkIfFeatureStoreCustomResourceConditionsInReady(featureStoreName, namespace)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"FeatureStore custom resource %s all conditions are not in ready state. \nError: %v\n",
featureStoreName, err,
))
fmt.Printf("FeatureStore custom resource %s conditions are in Ready State\n", featureStoreName)
}
// GetTestDeploySimpleCRFunc - returns a simple CR deployment function
func GetTestDeploySimpleCRFunc(testDir string, crYaml string, featureStoreName string, feastResourceName string, feastK8sResourceNames []string) func() {
return func() {
By("deploying the Simple Feast Custom Resource to Kubernetes")
namespace := "default"
cmd := exec.Command("kubectl", "apply", "-f", crYaml, "-n", namespace)
_, cmdOutputerr := Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputerr).NotTo(HaveOccurred())
validateTheFeatureStoreCustomResource(namespace, featureStoreName, feastResourceName, feastK8sResourceNames, Timeout)
By("deleting the feast deployment")
cmd = exec.Command("kubectl", "delete", "-f", crYaml)
_, cmdOutputerr = Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputerr).NotTo(HaveOccurred())
}
}
// GetTestWithRemoteRegistryFunc - returns a CR deployment with a remote registry function
func GetTestWithRemoteRegistryFunc(testDir string, crYaml string, remoteRegistryCRYaml string, featureStoreName string, feastResourceName string, feastK8sResourceNames []string) func() {
return func() {
By("deploying the Simple Feast Custom Resource to Kubernetes")
namespace := "default"
cmd := exec.Command("kubectl", "apply", "-f", crYaml, "-n", namespace)
_, cmdOutputErr := Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputErr).NotTo(HaveOccurred())
validateTheFeatureStoreCustomResource(namespace, featureStoreName, feastResourceName, feastK8sResourceNames, Timeout)
var remoteRegistryNs = "remote-registry"
By(fmt.Sprintf("Creating the remote registry namespace=%s", remoteRegistryNs))
cmd = exec.Command("kubectl", "create", "ns", remoteRegistryNs)
_, _ = Run(cmd, testDir)
By("deploying the Simple Feast remote registry Custom Resource on Kubernetes")
cmd = exec.Command("kubectl", "apply", "-f", remoteRegistryCRYaml, "-n", remoteRegistryNs)
_, cmdOutputErr = Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputErr).NotTo(HaveOccurred())
remoteFeatureStoreName := "simple-feast-remote-setup"
remoteFeastResourceName := FeastPrefix + remoteFeatureStoreName
fixRemoteFeastK8sResourceNames(feastK8sResourceNames, remoteFeastResourceName)
validateTheFeatureStoreCustomResource(remoteRegistryNs, remoteFeatureStoreName, remoteFeastResourceName, feastK8sResourceNames, Timeout)
By("deleting the feast remote registry deployment")
cmd = exec.Command("kubectl", "delete", "-f", remoteRegistryCRYaml, "-n", remoteRegistryNs)
_, cmdOutputErr = Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputErr).NotTo(HaveOccurred())
By("deleting the feast deployment")
cmd = exec.Command("kubectl", "delete", "-f", crYaml, "-n", namespace)
_, cmdOutputErr = Run(cmd, testDir)
ExpectWithOffset(1, cmdOutputErr).NotTo(HaveOccurred())
}
}
func fixRemoteFeastK8sResourceNames(feastK8sResourceNames []string, remoteFeastResourceName string) {
for i, feastK8sResourceName := range feastK8sResourceNames {
if index := strings.LastIndex(feastK8sResourceName, "-"); index != -1 {
feastK8sResourceNames[i] = remoteFeastResourceName + feastK8sResourceName[index:]
}
}
}
// DeployOperatorFromCode - Creates the images for the operator and deploys it
func DeployOperatorFromCode(testDir string, skipBuilds bool) {
_, isRunOnOpenShiftCI := os.LookupEnv("RUN_ON_OPENSHIFT_CI")
if !isRunOnOpenShiftCI {
By("creating manager namespace")
cmd := exec.Command("kubectl", "create", "ns", FeastControllerNamespace)
_, _ = Run(cmd, testDir)
var err error
// projectimage stores the name of the image used in the example
var projectimage = "localhost/feast-operator:v0.0.1"
// this image will be built in above make target.
var feastImage = "feastdev/feature-server:dev"
var feastLocalImage = "localhost/feastdev/feature-server:dev"
if !skipBuilds {
By("building the manager(Operator) image")
cmd = exec.Command("make", "docker-build", fmt.Sprintf("IMG=%s", projectimage))
_, err = Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("loading the the manager(Operator) image on Kind")
err = LoadImageToKindClusterWithName(projectimage, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("building the feast image")
cmd = exec.Command("make", "feast-ci-dev-docker-img")
_, err = Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("Tag the local feast image for the integration tests")
cmd = exec.Command("docker", "image", "tag", feastImage, feastLocalImage)
_, err = Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("loading the the feast image on Kind cluster")
err = LoadImageToKindClusterWithName(feastLocalImage, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
By("installing CRDs")
cmd = exec.Command("make", "install")
_, err = Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
By("deploying the controller-manager")
cmd = exec.Command("make", "deploy", fmt.Sprintf("IMG=%s", projectimage), fmt.Sprintf("FS_IMG=%s", feastLocalImage))
_, err = Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
By("Validating that the controller-manager deployment is in available state")
err := CheckIfDeploymentExistsAndAvailable(FeastControllerNamespace, ControllerDeploymentName, Timeout)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"Deployment %s is not available but expected to be available. \nError: %v\n",
ControllerDeploymentName, err,
))
fmt.Printf("Feast Control Manager Deployment %s is available\n", ControllerDeploymentName)
}
// DeleteOperatorDeployment - Deletes the operator deployment
func DeleteOperatorDeployment(testDir string) {
_, isRunOnOpenShiftCI := os.LookupEnv("RUN_ON_OPENSHIFT_CI")
if !isRunOnOpenShiftCI {
By("Uninstalling the feast CRD")
cmd := exec.Command("kubectl", "delete", "deployment", ControllerDeploymentName, "-n", FeastControllerNamespace)
_, err := Run(cmd, testDir)
ExpectWithOffset(1, err).NotTo(HaveOccurred())
}
}
// DeployPreviousVersionOperator - Deploys the previous version of the operator
func DeployPreviousVersionOperator() {
var err error
cmd := exec.Command("kubectl", "apply", "-f", fmt.Sprintf("https://raw.githubusercontent.com/feast-dev/feast/refs/tags/v%s/infra/feast-operator/dist/install.yaml", feastversion.FeastVersion))
_, err = Run(cmd, "/test/upgrade")
ExpectWithOffset(1, err).NotTo(HaveOccurred())
err = CheckIfDeploymentExistsAndAvailable(FeastControllerNamespace, ControllerDeploymentName, Timeout)
Expect(err).ToNot(HaveOccurred(), fmt.Sprintf(
"Deployment %s is not available but expected to be available. \nError: %v\n",
ControllerDeploymentName, err,
))
fmt.Printf("Feast Control Manager Deployment %s is available\n", ControllerDeploymentName)
}
// GetSimplePreviousVerCR - Get The previous version simple CR for tests
func GetSimplePreviousVerCR() string {
return fmt.Sprintf("https://raw.githubusercontent.com/feast-dev/feast/refs/tags/v%s/infra/feast-operator/test/testdata/feast_integration_test_crs/v1alpha1_default_featurestore.yaml", feastversion.FeastVersion)
}
// GetRemoteRegistryPreviousVerCR - Get The previous version remote registry CR for tests
func GetRemoteRegistryPreviousVerCR() string {
return fmt.Sprintf("https://raw.githubusercontent.com/feast-dev/feast/refs/tags/v%s/infra/feast-operator/test/testdata/feast_integration_test_crs/v1alpha1_remote_registry_featurestore.yaml", feastversion.FeastVersion)
}