Skip to content

Commit

Permalink
Add e2e tests for Service without Endpoint (antrea-io#5000)
Browse files Browse the repository at this point in the history
Signed-off-by: Hongliang Liu <lhongliang@vmware.com>
  • Loading branch information
hongliangl authored Jun 6, 2023
1 parent 42db3b1 commit 4baa43f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 30 deletions.
3 changes: 3 additions & 0 deletions test/e2e/networkpolicy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -936,6 +937,7 @@ func createAndWaitForPodWithExactName(t *testing.T, data *TestData, createFunc f
cleanupFunc()
t.Fatalf("Error when waiting for IP for Pod '%s': %v", name, err)
}
require.NoError(t, data.podWaitForRunning(defaultTimeout, name, ns))
return name, podIP, cleanupFunc
}

Expand All @@ -952,6 +954,7 @@ func createAndWaitForPodWithServiceAccount(t *testing.T, data *TestData, createF
cleanupFunc()
t.Fatalf("Error when waiting for IP for Pod '%s': %v", name, err)
}
require.NoError(t, data.podWaitForRunning(defaultTimeout, name, ns))
return name, podIP, cleanupFunc
}

Expand Down
62 changes: 32 additions & 30 deletions test/e2e/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ package e2e
import (
"fmt"
"net"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/wait"
)

func TestClusterIPv4(t *testing.T) {
Expand Down Expand Up @@ -63,7 +61,7 @@ func (data *TestData) testClusterIP(t *testing.T, isIPv6 bool, clientNamespace,
defer cleanupFunc()
}

nginx := fmt.Sprintf("nginx-%v", isIPv6)
nginx := fmt.Sprintf("nginx-%v-", isIPv6)
hostNginx := fmt.Sprintf("nginx-host-%v", isIPv6)
ipProtocol := corev1.IPv4Protocol
if isIPv6 {
Expand All @@ -75,54 +73,58 @@ func (data *TestData) testClusterIP(t *testing.T, isIPv6 bool, clientNamespace,
require.NotEqual(t, "", clusterIPSvc.Spec.ClusterIP, "ClusterIP should not be empty")
url := net.JoinHostPort(clusterIPSvc.Spec.ClusterIP, "80")

_, _, cleanupFunc := createAndWaitForPod(t, data, data.createNginxPodOnNode, nginx, nodeName(0), serverNamespace, false)
defer cleanupFunc()
t.Run("No Endpoint", func(t *testing.T) {
skipIfNamespaceIsNotEqual(t, serverNamespace, data.testNamespace)
testClusterIPCases(t, data, url, clients, hostNetworkClients, clientNamespace, Rejected)
})

t.Run("Non-HostNetwork Endpoints", func(t *testing.T) {
testClusterIPCases(t, data, url, clients, hostNetworkClients, clientNamespace)
_, _, cleanupFunc := createAndWaitForPod(t, data, data.createNginxPodOnNode, nginx, nodeName(0), serverNamespace, false)
defer cleanupFunc()
// Wait for complete synchronization of the test Service after updating the Endpoints.
time.Sleep(time.Second)
testClusterIPCases(t, data, url, clients, hostNetworkClients, clientNamespace, Connected)
})

require.NoError(t, data.DeletePod(serverNamespace, nginx))
_, _, cleanupFunc = createAndWaitForPod(t, data, data.createNginxPodOnNode, hostNginx, nodeName(0), serverNamespace, true)
defer cleanupFunc()
t.Run("HostNetwork Endpoints", func(t *testing.T) {
_, _, cleanupFunc := createAndWaitForPod(t, data, data.createNginxPodOnNode, hostNginx, nodeName(0), serverNamespace, true)
defer cleanupFunc()
skipIfNamespaceIsNotEqual(t, serverNamespace, data.testNamespace)
testClusterIPCases(t, data, url, clients, hostNetworkClients, clientNamespace)
// Wait for complete synchronization of the test Service after updating the Endpoints.
time.Sleep(time.Second)
testClusterIPCases(t, data, url, clients, hostNetworkClients, clientNamespace, Connected)
})
}

func testClusterIPCases(t *testing.T, data *TestData, url string, clients, hostNetworkClients map[string]string, namespace string) {
t.Run("All Nodes can access Service ClusterIP", func(t *testing.T) {
func testClusterIPCases(t *testing.T, data *TestData, url string, clients, hostNetworkClients map[string]string, namespace string, expectedConnectivity PodConnectivityMark) {
t.Run("Connect to Service ClusterIP from Node", func(t *testing.T) {
skipIfProxyAllDisabled(t, data)
skipIfKubeProxyEnabled(t, data)
skipIfNamespaceIsNotEqual(t, namespace, data.testNamespace)
for node, pod := range hostNetworkClients {
testClusterIPFromPod(t, data, url, node, pod, true, namespace)
testClusterIPFromPod(t, data, url, node, pod, true, namespace, expectedConnectivity)
}
})
t.Run("Pods from all Nodes can access Service ClusterIP", func(t *testing.T) {
t.Run("Connect to Service ClusterIP from Pod", func(t *testing.T) {
for node, pod := range clients {
testClusterIPFromPod(t, data, url, node, pod, false, namespace)
testClusterIPFromPod(t, data, url, node, pod, false, namespace, expectedConnectivity)
}
})
}

func testClusterIPFromPod(t *testing.T, data *TestData, url, nodeName, podName string, hostNetwork bool, namespace string) {
cmd := []string{"/agnhost", "connect", url, "--timeout=1s", "--protocol=tcp"}
err := wait.PollImmediate(1*time.Second, 5*time.Second, func() (bool, error) {
t.Logf(strings.Join(cmd, " "))
stdout, stderr, err := data.RunCommandFromPod(namespace, podName, agnhostContainerName, cmd)
t.Logf("stdout: %s - stderr: %s - err: %v", stdout, stderr, err)
if err == nil {
return true, nil
func testClusterIPFromPod(t *testing.T, data *TestData, url, nodeName, podName string, hostNetwork bool, namespace string, expectedConnectivity PodConnectivityMark) {
cmd := fmt.Sprintf("for i in $(seq 1 3); do /agnhost connect %s --timeout=1s --protocol=tcp; done;", url)
stdout, stderr, err := data.RunCommandFromPod(namespace, podName, agnhostContainerName, []string{"sh", "-c", cmd})
connectivity := Connected
if err != nil || stderr != "" {
// If err != nil and stderr == "", then it means this probe failed because of the command instead of connectivity.
// For example, container name doesn't exist.
if stderr == "" {
connectivity = Error
}
return false, nil
})
if err != nil {
t.Errorf(
"Pod '%s' on Node '%s' (hostNetwork: %t) should be able to connect to Service ClusterIP",
podName, nodeName, hostNetwork,
)
connectivity = DecideProbeResult(stderr, 3)
}
require.Equal(t, expectedConnectivity, connectivity, "Accessing ClusterIP from Pod got unexpected result", "Pod", podName, "hostNetwork", hostNetwork, "Node", nodeName, "url", url, "stdout", stdout, "stderr", stderr, "err", err)
}

// TestNodePortWindows tests NodePort Service on Windows Node. It is a temporary test to replace upstream Kubernetes one:
Expand Down

0 comments on commit 4baa43f

Please sign in to comment.