-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathtest_util.go
More file actions
209 lines (166 loc) · 6.11 KB
/
test_util.go
File metadata and controls
209 lines (166 loc) · 6.11 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
package e2e
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"os/exec"
"strings"
"time"
appsv1 "k8s.io/api/apps/v1"
"github.com/feast-dev/feast/infra/feast-operator/api/v1alpha1"
)
// dynamically checks if all conditions of custom resource featurestore are in "Ready" state.
func checkIfFeatureStoreCustomResourceConditionsInReady(featureStoreName, namespace string) error {
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
}
// 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")
}