Skip to content

Commit

Permalink
Refactor static canary tests.
Browse files Browse the repository at this point in the history
- Remove any config changes to aws-node pod in BeforeSuite.
- Remove dependency on multiple EC2 apis.
  • Loading branch information
orsenthil committed Jun 21, 2024
1 parent 7619bdb commit 37d46ec
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 25 deletions.
2 changes: 1 addition & 1 deletion scripts/run-static-canary.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function run_ginkgo_test() {
local focus=$1
echo "Running ginkgo tests with focus: $focus"

(CGO_ENABLED=0 ginkgo $EXTRA_GINKGO_FLAGS --no-color --focus="$focus" -v --timeout 10m --fail-on-pending $GINKGO_TEST_BUILD/cni.test -- \
(CGO_ENABLED=0 ginkgo $EXTRA_GINKGO_FLAGS --no-color --focus="$focus" -v --timeout 10m --fail-on-pending $GINKGO_TEST_BUILD/az-traffic.test -- \
--cluster-kubeconfig="$KUBE_CONFIG_PATH" \
--cluster-name="$CLUSTER_NAME" \
--aws-region="$REGION" \
Expand Down
49 changes: 49 additions & 0 deletions test/integration/az-traffic/pod_az_traffic_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.

package az_traffic

import (
"fmt"
"testing"

"github.com/aws/amazon-vpc-cni-k8s/test/framework"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/utils"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)

func TestAZConnectivity(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "CNI AZ Traffic Test Suite")
}

var _ = BeforeSuite(func() {
f = framework.New(framework.GlobalOptions)

By("creating test namespace")
f.K8sResourceManagers.NamespaceManager().CreateNamespace(utils.DefaultTestNamespace)

By(fmt.Sprintf("getting the node with the node label key %s and value %s",
f.Options.NgNameLabelKey, f.Options.NgNameLabelVal))
_, err := f.K8sResourceManagers.NodeManager().GetNodes(f.Options.NgNameLabelKey, f.Options.NgNameLabelVal)
Expect(err).ToNot(HaveOccurred())

})

var _ = AfterSuite(func() {
By("deleting test namespace")
f.K8sResourceManagers.NamespaceManager().
DeleteAndWaitTillNamespaceDeleted(utils.DefaultTestNamespace)
})
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cni
package az_traffic

import (
"fmt"
Expand All @@ -7,10 +7,10 @@ import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/cloudwatch"

"github.com/aws/amazon-vpc-cni-k8s/test/framework"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/manifest"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/utils"
"github.com/aws/amazon-vpc-cni-k8s/test/integration/common"
"github.com/aws/aws-sdk-go/service/ec2"
coreV1 "k8s.io/api/core/v1"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -24,6 +24,8 @@ var (

const MetricNamespace = "NetworkingAZConnectivity"

var f *framework.Framework

var _ = Describe("[STATIC_CANARY] AZ Node Presence", FlakeAttempts(retries), func() {

Context("While testing AZ availability", func() {
Expand Down Expand Up @@ -69,7 +71,6 @@ var _ = Describe("[STATIC_CANARY] test pod networking", FlakeAttempts(retries),
var (
err error
serverPort int
protocol string

// The command to run on server pods, to allow incoming
// connections for different traffic type
Expand Down Expand Up @@ -99,16 +100,6 @@ var _ = Describe("[STATIC_CANARY] test pod networking", FlakeAttempts(retries),
)

JustBeforeEach(func() {
By("authorizing security group ingress on instance security group")
err = f.CloudServices.EC2().
AuthorizeSecurityGroupIngress(instanceSecurityGroupID, protocol, serverPort, serverPort, "0.0.0.0/0", false)
Expect(err).ToNot(HaveOccurred())

By("authorizing security group egress on instance security group")
err = f.CloudServices.EC2().
AuthorizeSecurityGroupEgress(instanceSecurityGroupID, protocol, serverPort, serverPort, "0.0.0.0/0")
Expect(err).ToNot(HaveOccurred())

netcatContainer := manifest.
NewNetCatAlpineContainer(f.Options.TestImageRegistry).
Command(serverListenCmd).
Expand Down Expand Up @@ -138,16 +129,6 @@ var _ = Describe("[STATIC_CANARY] test pod networking", FlakeAttempts(retries),
})

JustAfterEach(func() {
By("revoking security group ingress on instance security group")
err = f.CloudServices.EC2().
RevokeSecurityGroupIngress(instanceSecurityGroupID, protocol, serverPort, serverPort, "0.0.0.0/0", false)
Expect(err).ToNot(HaveOccurred())

By("revoking security group egress on instance security group")
err = f.CloudServices.EC2().
RevokeSecurityGroupEgress(instanceSecurityGroupID, protocol, serverPort, serverPort, "0.0.0.0/0")
Expect(err).ToNot(HaveOccurred())

By("deleting the Daemonset.")
err = f.K8sResourceManagers.DaemonSetManager().DeleteAndWaitTillDaemonSetIsDeleted(testDaemonSet, utils.DefaultDeploymentReadyTimeout)
Expect(err).ToNot(HaveOccurred())
Expand All @@ -157,7 +138,6 @@ var _ = Describe("[STATIC_CANARY] test pod networking", FlakeAttempts(retries),

BeforeEach(func() {
serverPort = 2273
protocol = ec2.ProtocolTcp
// Test tcp connection using netcat
serverListenCmd = []string{"nc"}
// The nc flag "-l" for listen mode, "-k" to keep server up and not close
Expand Down Expand Up @@ -380,3 +360,23 @@ func RunCommandOnPod(receiverPod coreV1.Pod, command []string) (string, string,
}
}
}

// testConnectivity verifies connectivity between tester and server
func testConnectivity(senderPod coreV1.Pod, receiverPod coreV1.Pod, expectedStdout string,
expectedStderr string, port int, getTestCommandFunc func(receiverPod coreV1.Pod, port int) []string) {

testerCommand := getTestCommandFunc(receiverPod, port)

fmt.Fprintf(GinkgoWriter, "verifying connectivity from pod %s on node %s with IP %s to pod"+
" %s on node %s with IP %s\n", senderPod.Name, senderPod.Spec.NodeName, senderPod.Status.PodIP,
receiverPod.Name, receiverPod.Spec.NodeName, receiverPod.Status.PodIP)

stdOut, stdErr, err := f.K8sResourceManagers.PodManager().
PodExec(senderPod.Namespace, senderPod.Name, testerCommand)
Expect(err).ToNot(HaveOccurred())

fmt.Fprintf(GinkgoWriter, "stdout: %s and stderr: %s\n", stdOut, stdErr)

Expect(stdErr).To(ContainSubstring(expectedStderr))
Expect(stdOut).To(ContainSubstring(expectedStdout))
}

0 comments on commit 37d46ec

Please sign in to comment.