Skip to content

Commit

Permalink
fix: Error check
Browse files Browse the repository at this point in the history
  • Loading branch information
jpayne3506 authored and paulyufan2 committed Jun 20, 2023
1 parent 685dd9b commit 7875da5
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 101 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build connection

package connection

import (
Expand All @@ -8,36 +10,40 @@ import (

"github.com/Azure/azure-container-networking/test/internal/datapath"
"github.com/Azure/azure-container-networking/test/internal/k8sutils"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"

apiv1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
)

const (
WindowsDeployYamlPath = "../manifests/datapath/windowsdeploy.yaml"
WindowsDeployYamlPath = "../manifests/datapath/windows-deployment.yaml"
podLabelKey = "app"
podCount = 2
nodepoolKey = "agentpool"
)

var (
podPrefix = flag.String("podName", "datapod", "Prefix for test pods")
podNamespace = flag.String("namespace", "datapath-win", "Namespace for test pods")
nodepoolNodeSelector = flag.String("nodepoolLabelSelector", "npwin", "Provides nodepool as a Node-Selector for pods")
podPrefix = flag.String("podName", "datapod", "Prefix for test pods")
podNamespace = flag.String("namespace", "datapath-win", "Namespace for test pods")
nodepoolSelector = flag.String("nodepoolSelector", "npwin", "Provides nodepool as a Node-Selector for pods")
)

/*
This test assumes that you have the current credentials loaded in your kubeconfig for a
This test assumes that you have the current credentials loaded in your default kubeconfig for a
k8s cluster with a windows nodepool consisting of at least 2 windows nodes.
*** The expected nodepool name is npwin, if the nodepool has a diferent name ensure that you change nodepoolSelector with:
-nodepoolSelector="yournodepoolname"
To run the test use one of the following commands:
go test -count=1 test/integration/datapath/datapath_win_test.go -timeout 3m -tags connection -run ^TestDatapathWin$ -tags=connection
or
go test -count=1 test/integration/datapath/datapath_win_test.go -timeout 3m -tags connection -run ^TestDatapathWin$ -podName=acnpod -nodepoolSelector=npwina -tags=connection
To run the test use the following command as an example:
go test -timeout 3m -count=1 test/integration/datapath/datapath-win_test.go -podName=acnpod -nodepoolLabelSelector=npwina
This test checks pod to pod, pod to node, and pod to internet for datapath connectivity.
Timeout context is controled by the -timeout flag.
***Test takes 70s ( 35s for test - 35s for image creation)
*/

Expand All @@ -54,9 +60,8 @@ func TestDatapathWin(t *testing.T) {

t.Log("Create Label Selectors")
podLabelSelector := fmt.Sprintf("%s=%s", podLabelKey, *podPrefix)
nodeLabelSelector := fmt.Sprintf("%s=%s", nodepoolKey, *nodepoolNodeSelector)
nodeLabelSelector := fmt.Sprintf("%s=%s", nodepoolKey, *nodepoolSelector)

// Get NodeList WindowsNodePoolName
t.Log("Get Nodes")
nodes, err := k8sutils.GetNodeListByLabelSelector(ctx, clientset, nodeLabelSelector)
if err != nil {
Expand All @@ -65,58 +70,62 @@ func TestDatapathWin(t *testing.T) {

// Test Namespace
t.Log("Create Namespace")
err = k8sutils.CreateNamespace(ctx, clientset, *podNamespace)
createPodFlag := apierrors.IsAlreadyExists(err)
err = k8sutils.MustCreateNamespace(ctx, clientset, *podNamespace)
createPodFlag := !(apierrors.IsAlreadyExists(err))

if !createPodFlag {
if createPodFlag {
t.Log("Creating Windows pods through deployment")
deployment, err := k8sutils.MustParseDeployment(WindowsDeployYamlPath)
if err != nil {
t.Fatal(err)
require.NoError(t, err)
}

// Fields for overwritting existing deployment yaml.
// Defaults from flags will not change anything
deployment.Spec.Selector.MatchLabels[podLabelKey] = *podPrefix
deployment.Spec.Template.ObjectMeta.Labels[podLabelKey] = *podPrefix
deployment.Spec.Template.Spec.NodeSelector[nodepoolKey] = *nodepoolNodeSelector
deployment.Spec.Template.Spec.NodeSelector[nodepoolKey] = *nodepoolSelector
deployment.Name = *podPrefix
deployment.Namespace = *podNamespace

deploymentsClient := clientset.AppsV1().Deployments(*podNamespace)
err = k8sutils.MustCreateDeployment(ctx, deploymentsClient, deployment)
if err != nil {
t.Fatal(err)
require.NoError(t, err)
}

t.Log("Checking pods are running")
t.Log("Waiting for pods to be running state")
err = k8sutils.WaitForPodsRunning(ctx, clientset, *podNamespace, podLabelSelector)
if err != nil {
t.Fatal(err)
require.NoError(t, err)
}
t.Log("Successfully created customer windows pods")
} else if createPodFlag {
// Checks namespace already exists from previous test
} else {
// Checks namespace already exists from previous attempt
t.Log("Namespace already exists")

t.Log("Checking Windows test environment ")
for _, node := range nodes.Items {
t.Log("Checking for pods to be running state")
err = k8sutils.WaitForPodsRunning(ctx, clientset, *podNamespace, podLabelSelector)
if err != nil {
require.NoError(t, err)
}
}
t.Log("Checking Windows test environment ")
for _, node := range nodes.Items {

pods, err := k8sutils.GetPodsByNode(ctx, clientset, *podNamespace, podLabelSelector, node.Name)
if err != nil {
require.NoError(t, err, "could not get k8s clientset: %v", err)
}
if len(pods.Items) < 2 {
require.NoError(t, err, "Only %d pods on node %s, requires at least 2 pods", len(pods.Items), node.Name)
}
pods, err := k8sutils.GetPodsByNode(ctx, clientset, *podNamespace, podLabelSelector, node.Name)
if err != nil {
require.NoError(t, err, "could not get k8s clientset: %v", err)
}
if len(pods.Items) <= 1 {
t.Logf("%s", node.Name)
require.NoError(t, errors.New("Less than 2 pods on node"))
}
t.Log("Windows test environment ready")
} else {
t.Fatal("Create test environment skipped. Tearing test down.")
}
t.Log("Windows test environment ready")

t.Run("Windows ping tests pod -> node", func(t *testing.T) {
// Windows ping tests between pods and node
t.Log("Windows Pod to Host Ping tests")
for _, node := range nodes.Items {
t.Log("Windows ping tests (1)")
nodeIP := ""
Expand All @@ -130,25 +139,18 @@ func TestDatapathWin(t *testing.T) {
}

err := datapath.WindowsPodToNode(ctx, clientset, node.Name, nodeIP, *podNamespace, podLabelSelector, restConfig)
require.NoError(t, err, "Windows pod to node, ping test failed with %+v", err)
require.NoError(t, err, "Windows pod to node, ping test failed with: %+v", err)
t.Logf("Windows pod to node, passed for node: %s", node.Name)
}
})

t.Run("Windows ping tests pod -> pod", func(t *testing.T) {
// Get NodeList WindowsNodePoolName
nodes, err := k8sutils.GetNodeListByLabelSelector(ctx, clientset, nodeLabelSelector)
if err != nil {
require.NoError(t, err, "could not get k8s node list: %v", err)
}

// Windows pod ping tests
// Pod to pod same node
for _, node := range nodes.Items {
if node.Status.NodeInfo.OperatingSystem == string(apiv1.Windows) {
// Pod to pod same node
t.Log("Windows ping tests (2) - Same Node")
err := datapath.WindowsPodToPodPingTestSameNode(ctx, clientset, node.Name, *podNamespace, podLabelSelector, restConfig)
require.NoError(t, err, "Windows pod to pod, same node, ping test failed with %+v", err)
require.NoError(t, err, "Windows pod to pod, same node, ping test failed with: %+v", err)
t.Logf("Windows pod to windows pod, same node, passed for node: %s", node.ObjectMeta.Name)
}
}
Expand All @@ -159,23 +161,19 @@ func TestDatapathWin(t *testing.T) {
firstNode := nodes.Items[i%2].Name
secondNode := nodes.Items[(i+1)%2].Name
err = datapath.WindowsPodToPodPingTestDiffNode(ctx, clientset, firstNode, secondNode, *podNamespace, podLabelSelector, restConfig)
require.NoError(t, err, "Windows pod to pod, different node, ping test failed with %+v", err)
require.NoError(t, err, "Windows pod to pod, different node, ping test failed with: %+v", err)
t.Logf("Windows pod to windows pod, different node, passed for node: %s -> %s", firstNode, secondNode)

}
})

t.Run("Windows url tests pod -> internet", func(t *testing.T) {
// From windows pod to IWR a URL
t.Log("Windows ping tests (3) - Pod to Internet tests")
nodes, err := k8sutils.GetNodeListByLabelSelector(ctx, clientset, nodeLabelSelector)
if err != nil {
require.NoError(t, err, "could not get k8s node list: %v", err)
}
// From windows pod, IWR a URL
for _, node := range nodes.Items {
if node.Status.NodeInfo.OperatingSystem == string(apiv1.Windows) {
t.Log("Windows ping tests (3) - Pod to Internet tests")
err := datapath.WindowsPodToInternet(ctx, clientset, node.Name, *podNamespace, podLabelSelector, restConfig)
require.NoError(t, err, "Windows pod to internet url %+v", err)
require.NoError(t, err, "Windows pod to internet test failed with: %+v", err)
t.Logf("Windows pod to Internet url tests")
}
}
Expand Down
12 changes: 0 additions & 12 deletions test/integration/manifests/datapath/windowspod.yaml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/Azure/azure-container-networking/test/internal/k8sutils"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"

apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
Expand All @@ -19,27 +18,28 @@ func podTest(ctx context.Context, clientset *kubernetes.Clientset, srcPod *apiv1
logrus.Infof("podTest() - %v %v", srcPod.Name, cmd)
output, err := k8sutils.ExecCmdOnPod(ctx, clientset, srcPod.Namespace, srcPod.Name, cmd, rc)
if err != nil {
return err
return errors.Wrapf(err, "failed to execute command on pod: %v", srcPod.Name)
}
return passFunc(string(output))
}

func WindowsPodToPodPingTestSameNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName string, podNamespace string, labelSelector string, rc *restclient.Config) error {
func WindowsPodToPodPingTestSameNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName, podNamespace, labelSelector string, rc *restclient.Config) error {
logrus.Infof("Get Pods for Node: %s", nodeName)
pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName)
if err != nil {
logrus.Fatalf("could not get k8s clientset: %v", err)
logrus.Error(err)
return errors.Wrap(err, "k8s api call")
}
if len(pods.Items) < 2 {
return fmt.Errorf("Only %d pods on node %s, requires at least 2 pods", len(pods.Items), nodeName)
if len(pods.Items) <= 1 {
return errors.New("Less than 2 pods on node")
}

// Get first pod on this node
firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err))
}
logrus.Infof("First pod: %v", firstPod.Name)
logrus.Infof("First pod: %v %v", firstPod.Name, firstPod.Status.PodIP)

// Get the second pod on this node
secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[1].Name, metav1.GetOptions{})
Expand All @@ -52,24 +52,26 @@ func WindowsPodToPodPingTestSameNode(ctx context.Context, clientset *kubernetes.
return podTest(ctx, clientset, firstPod, []string{"ping", secondPod.Status.PodIP}, rc, pingPassedWindows)
}

func WindowsPodToPodPingTestDiffNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName1 string, nodeName2 string, podNamespace string, labelSelector string, rc *restclient.Config) error {
func WindowsPodToPodPingTestDiffNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName1, nodeName2, podNamespace, labelSelector string, rc *restclient.Config) error {
logrus.Infof("Get Pods for Node 1: %s", nodeName1)
// Node 1
pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName1)
if err != nil {
logrus.Fatalf("could not get k8s clientset: %v", err)
logrus.Error(err)
return errors.Wrap(err, "k8s api call")
}
firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{})
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", firstPod.Name, err))
}
logrus.Infof("First pod: %v", firstPod.Name)
logrus.Infof("First pod: %v %v", firstPod.Name, firstPod.Status.PodIP)

logrus.Infof("Get Pods for Node 2: %s", nodeName2)
// Node 2
pods, err = k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName2)
if err != nil {
logrus.Fatalf("could not get k8s clientset: %v", err)
logrus.Error(err)
return errors.Wrap(err, "k8s api call")
}
secondPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{})
if err != nil {
Expand All @@ -81,14 +83,15 @@ func WindowsPodToPodPingTestDiffNode(ctx context.Context, clientset *kubernetes.
return podTest(ctx, clientset, firstPod, []string{"ping", secondPod.Status.PodIP}, rc, pingPassedWindows)
}

func WindowsPodToNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName string, nodeIP string, podNamespace string, labelSelector string, rc *restclient.Config) error {
logrus.Infof("Get Pods by Node: %s", nodeName)
func WindowsPodToNode(ctx context.Context, clientset *kubernetes.Clientset, nodeName, nodeIP, podNamespace, labelSelector string, rc *restclient.Config) error {
logrus.Infof("Get Pods by Node: %s %s", nodeName, nodeIP)
pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName)
if err != nil {
logrus.Fatalf("could not get k8s clientset: %v", err)
logrus.Error(err)
return errors.Wrap(err, "k8s api call")
}
if len(pods.Items) < 2 {
return fmt.Errorf("Only %d pods on node %s, requires at least 2 pods", len(pods.Items), nodeName)
if len(pods.Items) <= 1 {
return errors.New("Less than 2 pods on node")
}
// Get first pod on this node
firstPod, err := clientset.CoreV1().Pods(podNamespace).Get(ctx, pods.Items[0].Name, metav1.GetOptions{})
Expand Down Expand Up @@ -119,11 +122,15 @@ func WindowsPodToNode(ctx context.Context, clientset *kubernetes.Clientset, node
return nil
}

func WindowsPodToInternet(ctx context.Context, clientset *kubernetes.Clientset, nodeName string, podNamespace string, labelSelector string, rc *restclient.Config) error {
func WindowsPodToInternet(ctx context.Context, clientset *kubernetes.Clientset, nodeName, podNamespace, labelSelector string, rc *restclient.Config) error {
logrus.Infof("Get Pods by Node: %s", nodeName)
pods, err := k8sutils.GetPodsByNode(ctx, clientset, podNamespace, labelSelector, nodeName)
if err != nil {
logrus.Fatalf("could not get k8s clientset: %v", err)
logrus.Error(err)
return errors.Wrap(err, "k8s api call")
}
if len(pods.Items) <= 1 {
return errors.New("Less than 2 pods on node")
}

// Get first pod on this node
Expand All @@ -139,9 +146,9 @@ func WindowsPodToInternet(ctx context.Context, clientset *kubernetes.Clientset,
return errors.Wrap(err, fmt.Sprintf("Getting pod %s failed with %v", secondPod.Name, err))
}
logrus.Infof("Second pod: %v", secondPod.Name)
// Can use curl, but need to have a certain version of powershell. Calls IWR by reference so use IWR.
resultOne := podTest(ctx, clientset, firstPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, invokeWebRequestPassedWindows)
resultTwo := podTest(ctx, clientset, secondPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, invokeWebRequestPassedWindows)

resultOne := podTest(ctx, clientset, firstPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, webRequestPassedWindows)
resultTwo := podTest(ctx, clientset, secondPod, []string{"powershell", "Invoke-WebRequest", "www.bing.com", "-UseBasicParsing"}, rc, webRequestPassedWindows)

if resultOne != nil {
return resultOne
Expand All @@ -154,18 +161,18 @@ func WindowsPodToInternet(ctx context.Context, clientset *kubernetes.Clientset,
return nil
}

func invokeWebRequestPassedWindows(output string) error {
func webRequestPassedWindows(output string) error {
const searchString = "200 OK"
if strings.Contains(output, searchString) {
return nil
}
return fmt.Errorf("Output did not contain \"%s\", considered failed, output was: %s", searchString, output)
return errors.Wrapf(errors.New("Output did not contain \"200 OK\""), "output was: %s", output)
}

func pingPassedWindows(output string) error {
const searchString = "0% loss"
if strings.Contains(output, searchString) {
return nil
}
return fmt.Errorf("Output did not contain \"%s\", considered failed, output was: %s", searchString, output)
return errors.Wrapf(errors.New("Ping did not contain\"0% loss\""), "output was: %s", output)
}
17 changes: 5 additions & 12 deletions test/internal/k8sutils/utils_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ func MustCreateOrUpdatePod(ctx context.Context, podI typedcorev1.PodInterface, p
return err
}
}
_, err := podI.Create(ctx, &pod, metav1.CreateOptions{})
return err
if _, err := podI.Create(ctx, &pod, metav1.CreateOptions{}); err != nil {
return errors.Wrapf(err, "failed to create pod %v", pod.Name)
}

return nil
}

func MustCreateDaemonset(ctx context.Context, daemonsets typedappsv1.DaemonSetInterface, ds appsv1.DaemonSet) error {
Expand Down Expand Up @@ -171,13 +174,3 @@ func MustCreateNamespace(ctx context.Context, clienset *kubernetes.Clientset, na
}
return nil
}

func CreateNamespace(ctx context.Context, clienset *kubernetes.Clientset, namespace string) error {
_, err := clienset.CoreV1().Namespaces().Create(ctx, &corev1.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: namespace,
},
}, metav1.CreateOptions{})

return err
}
Loading

0 comments on commit 7875da5

Please sign in to comment.