Skip to content

Commit

Permalink
test: add retry for anp/banp connection test
Browse files Browse the repository at this point in the history
Signed-off-by: 马洪贞 <[email protected]>
  • Loading branch information
hongzhen-ma committed Nov 15, 2024
1 parent d1aa393 commit 5be85c8
Showing 1 changed file with 33 additions and 23 deletions.
56 changes: 33 additions & 23 deletions conformance/utils/kubernetes/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,32 +74,42 @@ func PokeServer(t *testing.T, client k8sclient.Interface, kubeConfig *rest.Confi
protocolArg,
ipPortArg), " ")

stdout, stderr, err := RunCommandFromPod(client, kubeConfig, clientNamespace, clientPod, connectCommand)
// TODO(tssurya): See if we need to add a wait&retry mechanism to test connectivity
// See https://github.com/kubernetes-sigs/network-policy-api/issues/108 for details.
if err != nil && stderr == "" {
// If err != nil and stderr == "", then it means this probe failed because of the command instead of connectivity.
t.Logf("FAILED to execute command %s on pod %s/%s: %v", connectCommand, clientNamespace, clientPod, err.Error())
return false
}
if shouldConnect && len(stderr) > 0 {
t.Logf("FAILED Command was %s", connectCommand)
t.Logf("Expected connection to succeed from %s/%s to %s, but instead it miserably failed. stderr: %v",
clientNamespace, clientPod, targetHost, stderr)
return false
} else if !shouldConnect {
if stdout == "" && stderr == "" {
t.Logf("FAILED Command was %s", connectCommand)
t.Logf("Expected connection to fail from %s/%s to %s, but instead it successfully connected.",
clientNamespace, clientPod, targetHost)
return false
} else if !strings.Contains(stderr, "TIMEOUT") {
var stdout, stderr string
var err error
// reuse the RequestTimeout parameter as context timeout
waitErr := wait.PollUntilContextTimeout(context.Background(), 1*time.Second, timeout, true, func(ctx context.Context) (bool, error) {
stdout, stderr, err = RunCommandFromPod(client, kubeConfig, clientNamespace, clientPod, connectCommand)
// See https://github.com/kubernetes-sigs/network-policy-api/issues/108 for details.
if err != nil && stderr == "" {
// If err != nil and stderr == "", then it means this probe failed because of the command instead of connectivity.
t.Logf("FAILED to execute command %s on pod %s/%s: %v", connectCommand, clientNamespace, clientPod, err.Error())
return false, err
}

if shouldConnect && len(stderr) > 0 {
t.Logf("FAILED Command was %s", connectCommand)
// Other possible results include "REFUSED" for example, signaling the connection is rejected.
t.Logf("Expected connection to be dropped from %s/%s to %s, but instead it returned a different status: %s",
t.Logf("Expected connection to succeed from %s/%s to %s, but instead it miserably failed. stderr: %v",
clientNamespace, clientPod, targetHost, stderr)
return false
return false, fmt.Errorf("expected connection to succeed but failed")
} else if !shouldConnect {
if stdout == "" && stderr == "" {
t.Logf("FAILED Command was %s", connectCommand)
t.Logf("Expected connection to fail from %s/%s to %s, but instead it successfully connected.",
clientNamespace, clientPod, targetHost)
return false, fmt.Errorf("expected connection to fail but succeed")
} else if !strings.Contains(stderr, "TIMEOUT") {
t.Logf("FAILED Command was %s", connectCommand)
// Other possible results include "REFUSED" for example, signaling the connection is rejected.
t.Logf("Expected connection to be dropped from %s/%s to %s, but instead it returned a different status: %s",
clientNamespace, clientPod, targetHost, stderr)
return false, fmt.Errorf("expected connection to be dropped, but it returned a different status")
}
}
return true, nil
})
if waitErr != nil {
t.Logf("Connection test failed after retrying with timeout %v, FAILED Command was %s", timeout, connectCommand)
return false
}
return true
}
Expand Down

0 comments on commit 5be85c8

Please sign in to comment.