Skip to content

Commit 828b8d8

Browse files
authored
Update e2e test case (vmware-tanzu#888)
* Use CRD ClientSet to get CRD resource * Improve some test cases * Update the test-e2e script * Remove the `ippool` parameter * Add a `debug` parameter to improve the test log output --------- Signed-off-by: Wenqi Qiu <[email protected]>
1 parent 3ce44b7 commit 828b8d8

File tree

6 files changed

+365
-383
lines changed

6 files changed

+365
-383
lines changed

hack/test-e2e.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export GOROOT=/usr/local/go
1414
export PATH=$GOROOT/bin:$PATH
1515
go version
1616
go env
17+
go clean -cache -modcache -testcache
1718

1819
# if manual trigger, use the default email and author
1920
if [ -z ${ghprbActualCommitAuthorEmail+x} ]; then
@@ -57,7 +58,6 @@ kubectl exec $pod_name -c nsx-ncp -n vmware-system-nsx -- cat /var/run/secrets/k
5758
kubectl exec $pod_name -c nsx-ncp -n vmware-system-nsx -- cat /etc/nsx-ujo/vc/username > /etc/nsx-ujo/vc/username
5859
kubectl exec $pod_name -c nsx-ncp -n vmware-system-nsx -- cat /etc/nsx-ujo/vc/password > /etc/nsx-ujo/vc/password
5960

60-
cp -r /root/test $NSX_OPERATOR_DIR/
6161
# Use the -run parameter to run only specific test cases within certain modules.
6262
# -run '.*Subnet.*|.*SubnetSet.*|.*SecurityPolicy.*|.*NetworkInfo.*'
6363
e2e=true go test -v ./test/e2e -coverpkg=./pkg/... -remote.kubeconfig /root/.kube/config -operator-cfg-path /etc/ncp.ini -test.timeout 15m -coverprofile cover-e2e.out

test/e2e/framework.go

Lines changed: 1 addition & 143 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ type TestOptions struct {
6363
logsExportDir string
6464
operatorConfigPath string
6565
logsExportOnSuccess bool
66-
withIPPool bool
66+
debugLog bool
6767
}
6868

6969
var testOptions TestOptions
@@ -304,148 +304,6 @@ func (data *TestData) deleteNamespace(namespace string, timeout time.Duration) e
304304

305305
type PodCondition func(*corev1.Pod) (bool, error)
306306

307-
// waitForSecurityPolicyReady polls the K8s apiServer until the specified CR is in the "True" state (or until
308-
// the provided timeout expires).
309-
func (data *TestData) waitForCRReadyOrDeleted(timeout time.Duration, cr string, namespace string, name string, status Status) error {
310-
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, timeout, false, func(ctx context.Context) (bool, error) {
311-
cmd := fmt.Sprintf("kubectl get %s %s -n %s -o jsonpath='{.status.conditions[?(@.type==\"Ready\")].status}'", cr, name, namespace)
312-
rc, stdout, stderr, err := RunCommandOnNode(clusterInfo.masterNodeName, cmd)
313-
log.Printf("Command: %s\n, result: %d, stdout: %s, stderr:%s, error: %+v\n", cmd, rc, stdout, stderr, err)
314-
if err != nil || rc != 0 {
315-
if status == Deleted {
316-
return true, nil
317-
}
318-
return false, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", cmd, err, stdout)
319-
} else {
320-
if status == Ready {
321-
if stdout == "True" {
322-
return true, nil
323-
}
324-
return false, nil
325-
}
326-
return false, nil
327-
}
328-
})
329-
if err != nil {
330-
return err
331-
}
332-
return nil
333-
}
334-
335-
func (data *TestData) getCRPropertiesByJson(timeout time.Duration, crType, crName, namespace, key string) (string, error) {
336-
value := ""
337-
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, timeout, false, func(ctx context.Context) (bool, error) {
338-
cmd := ""
339-
// for cluster scope resource, namespace is empty
340-
if namespace == "" {
341-
cmd = fmt.Sprintf("kubectl get %s %s -o json | jq '%s'", crType, crName, key)
342-
} else {
343-
cmd = fmt.Sprintf("kubectl get %s %s -n %s -o json | jq '%s'", crType, crName, namespace, key)
344-
}
345-
log.Printf("%s", cmd)
346-
rc, stdout, _, err := RunCommandOnNode(clusterInfo.masterNodeName, cmd)
347-
if err != nil || rc != 0 {
348-
return false, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", cmd, err, stdout)
349-
} else {
350-
// check if 'null' in stdout
351-
if strings.Contains(stdout, "null") {
352-
return false, nil
353-
}
354-
value = stdout
355-
return true, nil
356-
}
357-
})
358-
if err != nil {
359-
return value, err
360-
}
361-
return value, nil
362-
}
363-
364-
// For CRs that we do not know the name, return the CR list for upper logic to identify
365-
func (data *TestData) getCRResources(timeout time.Duration, crtype, namespace string) (map[string]string, error) {
366-
crs := map[string]string{}
367-
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, timeout, false, func(ctx context.Context) (bool, error) {
368-
cmd := ""
369-
if namespace == "" { // for cluster scope resources
370-
cmd = fmt.Sprintf("kubectl get %s", crtype)
371-
} else if crtype == "namespaces" {
372-
cmd = fmt.Sprintf("kubectl get %s %s", crtype, namespace)
373-
} else {
374-
cmd = fmt.Sprintf("kubectl get %s -n %s", crtype, namespace)
375-
}
376-
log.Printf("%s", cmd)
377-
rc, stdout, _, err := RunCommandOnNode(clusterInfo.masterNodeName, cmd)
378-
if err != nil || rc != 0 {
379-
return false, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", cmd, err, stdout)
380-
} else {
381-
crs_raw := strings.Split(stdout, "\n")
382-
// for each resource, get json structure as value
383-
for i, c := range crs_raw {
384-
if i == 0 {
385-
// first line is table header
386-
continue
387-
}
388-
r := regexp.MustCompile("[^\\s]+")
389-
parts := r.FindAllString(c, -1)
390-
if len(parts) < 1 { // to avoid empty lines
391-
continue
392-
}
393-
uid_cmd := ""
394-
if namespace == "" || crtype == "namespaces" {
395-
uid_cmd = fmt.Sprintf("kubectl get %s %s -o yaml | grep uid", crtype, parts[0])
396-
} else {
397-
uid_cmd = fmt.Sprintf("kubectl get %s %s -n %s -o yaml | grep uid", crtype, parts[0], namespace)
398-
}
399-
log.Printf("trying to get uid for cr: %s", uid_cmd)
400-
rc, stdout, _, err := RunCommandOnNode(clusterInfo.masterNodeName, uid_cmd)
401-
if err != nil || rc != 0 {
402-
return false, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", uid_cmd, err, stdout)
403-
}
404-
uid := strings.Split(stdout, ":")[1]
405-
crs[parts[0]] = uid
406-
}
407-
return true, nil
408-
}
409-
})
410-
if err != nil {
411-
return crs, err
412-
}
413-
return crs, nil
414-
}
415-
416-
// Return a singe CR via CR name
417-
func (data *TestData) getCRResource(timeout time.Duration, crtype, crname, namespace string) (string, error) {
418-
ret := ""
419-
err := wait.PollUntilContextTimeout(context.TODO(), 1*time.Second, timeout, false, func(ctx context.Context) (bool, error) {
420-
// If namespace is empty, then this is a cluster scope resource.
421-
uid_cmd := ""
422-
if namespace == "" {
423-
uid_cmd = fmt.Sprintf("kubectl get %s %s -o yaml | grep uid", crtype, crname)
424-
} else if crtype == "namespaces" {
425-
uid_cmd = fmt.Sprintf("kubectl get %s %s -o yaml | grep uid", crtype, namespace)
426-
} else {
427-
uid_cmd = fmt.Sprintf("kubectl get %s %s -n %s -o yaml | grep uid", crtype, crname, namespace)
428-
}
429-
log.Printf("%s", uid_cmd)
430-
rc, stdout, _, err := RunCommandOnNode(clusterInfo.masterNodeName, uid_cmd)
431-
if err != nil || rc != 0 {
432-
return false, fmt.Errorf("error when running the following command `%s` on master Node: %v, %s", uid_cmd, err, stdout)
433-
} else {
434-
parts := strings.Split(stdout, ":")
435-
if len(parts) < 2 {
436-
return false, nil
437-
}
438-
uid := parts[1]
439-
ret = uid
440-
}
441-
return true, nil
442-
})
443-
if err != nil {
444-
return "", err
445-
}
446-
return ret, nil
447-
}
448-
449307
// deploymentWaitForNames polls the K8s apiServer once the specific pods are created, no matter they are running or not.
450308
func (data *TestData) deploymentWaitForNames(timeout time.Duration, namespace, deployment string) ([]string, error) {
451309
var podNames []string

test/e2e/main_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ func testMain(m *testing.M) int {
2121
flag.StringVar(&testOptions.logsExportDir, "logs-export-dir", "", "Export directory for test logs")
2222
flag.StringVar(&testOptions.operatorConfigPath, "operator-cfg-path", "/etc/nsx-ujo/ncp.ini", "config file for operator")
2323
flag.BoolVar(&testOptions.logsExportOnSuccess, "logs-export-on-success", false, "Export logs even when a test is successful")
24-
flag.BoolVar(&testOptions.withIPPool, "ippool", false, "Run tests include IPPool tests")
24+
flag.BoolVar(&testOptions.debugLog, "debug", false, "")
2525
flag.Parse()
2626

27-
logf.SetLogger(logger.ZapLogger(true, 2))
27+
if testOptions.debugLog {
28+
logf.SetLogger(logger.ZapLogger(true, 2))
29+
} else {
30+
logf.SetLogger(logger.ZapLogger(false, 0))
31+
32+
}
2833

2934
if err := initProvider(); err != nil {
3035
log.Fatalf("Error when initializing provider: %v", err)

0 commit comments

Comments
 (0)