Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Test cases for EnvVars check on CNI daemonset #1431

Merged
merged 13 commits into from
May 6, 2021
1 change: 0 additions & 1 deletion test/agent/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/aws/amazon-vpc-cni-k8s/test/agent
go 1.14

require (
github.com/go-kit/kit v0.10.0
github.com/vishvananda/netlink v1.1.0
golang.org/x/sys v0.0.0-20210426230700-d19ff857e887
)
356 changes: 0 additions & 356 deletions test/agent/go.sum

Large diffs are not rendered by default.

7 changes: 0 additions & 7 deletions test/framework/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,5 @@ func (options *Options) Validate() error {
if len(options.AWSVPCID) == 0 {
return errors.Errorf("%s must be set!", "aws-vpc-id")
}
if len(options.NgNameLabelKey) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-key")
}
if len(options.NgNameLabelVal) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-val")
}

return nil
}
23 changes: 22 additions & 1 deletion test/framework/resources/k8s/manifest/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ type DeploymentBuilder struct {
nodeSelector map[string]string
terminationGracePeriod int
nodeName string
hostNetwork bool
volume []corev1.Volume
volumeMount []corev1.VolumeMount
}

func NewBusyBoxDeploymentBuilder() *DeploymentBuilder {
Expand Down Expand Up @@ -93,8 +96,19 @@ func (d *DeploymentBuilder) PodLabel(labelKey string, labelValue string) *Deploy
return d
}

func (d *DeploymentBuilder) HostNetwork(hostNetwork bool) *DeploymentBuilder {
d.hostNetwork = hostNetwork
return d
}

func (d *DeploymentBuilder) MountVolume(volume []corev1.Volume, volumeMount []corev1.VolumeMount) *DeploymentBuilder {
d.volume = volume
d.volumeMount = volumeMount
return d
}

func (d *DeploymentBuilder) Build() *v1.Deployment {
return &v1.Deployment{
deploymentSpec := &v1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: d.name,
Namespace: d.namespace,
Expand All @@ -110,6 +124,7 @@ func (d *DeploymentBuilder) Build() *v1.Deployment {
Labels: d.labels,
},
Spec: corev1.PodSpec{
HostNetwork: d.hostNetwork,
NodeSelector: d.nodeSelector,
Containers: []corev1.Container{d.container},
TerminationGracePeriodSeconds: aws.Int64(int64(d.terminationGracePeriod)),
Expand All @@ -118,4 +133,10 @@ func (d *DeploymentBuilder) Build() *v1.Deployment {
},
},
}

if len(d.volume) > 0 && len(d.volumeMount) > 0 {
deploymentSpec.Spec.Template.Spec.Volumes = d.volume
deploymentSpec.Spec.Template.Spec.Containers[0].VolumeMounts = d.volumeMount
}
return deploymentSpec
}
11 changes: 11 additions & 0 deletions test/framework/resources/k8s/resources/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

type NodeManager interface {
GetNodes(nodeLabelKey string, nodeLabelVal string) (v1.NodeList, error)
GetAllNodes() (v1.NodeList, error)
UpdateNode(oldNode *v1.Node, newNode *v1.Node) error
WaitTillNodesReady(nodeLabelKey string, nodeLabelVal string, asgSize int) error
}
Expand All @@ -38,6 +39,9 @@ func NewDefaultNodeManager(k8sClient client.DelegatingClient) NodeManager {
}

func (d *defaultNodeManager) GetNodes(nodeLabelKey string, nodeLabelVal string) (v1.NodeList, error) {
if nodeLabelVal == "" {
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved
return d.GetAllNodes()
}
ctx := context.Background()
nodeList := v1.NodeList{}
err := d.k8sClient.List(ctx, &nodeList, client.MatchingLabels{
Expand All @@ -46,6 +50,13 @@ func (d *defaultNodeManager) GetNodes(nodeLabelKey string, nodeLabelVal string)
return nodeList, err
}

func (d *defaultNodeManager) GetAllNodes() (v1.NodeList, error) {
ctx := context.Background()
nodeList := v1.NodeList{}
err := d.k8sClient.List(ctx, &nodeList)
return nodeList, err
}

func (d *defaultNodeManager) UpdateNode(oldNode *v1.Node, newNode *v1.Node) error {
return d.k8sClient.Patch(context.Background(), newNode, client.MergeFrom(oldNode))
}
Expand Down
2 changes: 2 additions & 0 deletions test/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -530,9 +530,11 @@ github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijb
github.com/vektah/gqlparser v1.1.2/go.mod h1:1ycwN7Ij5njmMkPPAOaRFY4rET2Enx7IkVv3vaXspKw=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852 h1:cPXZWzzG0NllBLdjWoD1nDfaqu98YMv+OneaKc8sPOA=
github.com/vishvananda/netlink v1.1.1-0.20201029203352-d40f9887b852/go.mod h1:twkDnbuQxJYemMlGd4JFIcuhgX83tXhKS2B/PRMpOho=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae h1:4hwBBUfQCFe3Cym0ZtKyq7L16eZUtYKs+BaHDN6mAns=
github.com/vishvananda/netns v0.0.0-20200728191858-db3c7e526aae/go.mod h1:DD4vA1DwXk04H54A1oHXtwZmA0grkVMdPxx/VGLCah0=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
Expand Down
212 changes: 212 additions & 0 deletions test/integration-new/ipamd/env_vars_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,212 @@
package ipamd

import (
"regexp"

"github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/manifest"
k8sUtils "github.com/aws/amazon-vpc-cni-k8s/test/framework/resources/k8s/utils"
"github.com/aws/amazon-vpc-cni-k8s/test/framework/utils"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
appsV1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
)

var (
ds *appsV1.DaemonSet
hostNetworkDeploymentSpec *appsV1.Deployment
hostNetworkPod v1.Pod
)

const (
AWS_VPC_ENI_MTU = "AWS_VPC_ENI_MTU"
AWS_VPC_K8S_CNI_LOG_FILE = "AWS_VPC_K8S_CNI_LOG_FILE"
AWS_VPC_K8S_CNI_VETHPREFIX = "AWS_VPC_K8S_CNI_VETHPREFIX"
POD_VOL_LABEL_KEY = "MountVolume"
POD_VOL_LABEL_VAL = "true"
VOLUME_NAME = "ipamd-logs"
VOLUME_MOUNT_PATH = "/var/log/aws-routed-eni/"
)

var _ = Describe("cni env test", func() {
Context("CNI Environment Variables", func() {
It("Changing AWS_VPC_ENI_MTU and AWS_VPC_K8S_CNI_VETHPREFIX", func() {
setupHostNetworkPod()

currMTUVal := getEnvValueForKey(AWS_VPC_ENI_MTU)
Expect(currMTUVal).NotTo(Equal(""))

currVETHPrefix := getEnvValueForKey(AWS_VPC_K8S_CNI_VETHPREFIX)
Expect(currVETHPrefix).NotTo(Equal(""))

k8sUtils.AddEnvVarToDaemonSetAndWaitTillUpdated(f, DAEMONSET, NAMESPACE, DAEMONSET, map[string]string{
AWS_VPC_ENI_MTU: "1300",
AWS_VPC_K8S_CNI_VETHPREFIX: "veth",
})

By("Deploying a BusyBox deployment")
deploymentSpec := manifest.NewBusyBoxDeploymentBuilder().
Namespace("default").
Name("busybox").
Replicas(1).
NodeName(primaryNode.Name).
Build()

_, err := f.K8sResourceManagers.
DeploymentManager().
CreateAndWaitTillDeploymentIsReady(deploymentSpec, utils.DefaultDeploymentReadyTimeout)
Expect(err).ToNot(HaveOccurred())

stdout, _, err := f.K8sResourceManagers.PodManager().PodExec("default", hostNetworkPod.Name, []string{"ifconfig"})
Expect(err).NotTo(HaveOccurred())

re := regexp.MustCompile(`\n`)
input := re.ReplaceAllString(stdout, "")

re = regexp.MustCompile(`eth.*lo`)
eth := re.FindStringSubmatch(input)[0]

re = regexp.MustCompile(`MTU:[0-9]*`)
mtus := re.FindAllStringSubmatch(eth, -1)

By("Validating new MTU value")
// Validate MTU
for _, m := range mtus {
Expect(m[0]).To(Equal("MTU:1300"))
}

By("Validating new VETH Prefix")
// Validate VETH Prefix
// Adding the new MTU value to below regex ensures that we are checking the recently created
// veth and not any older entries
re = regexp.MustCompile(`veth.*MTU:1300`)
veth := re.FindAllString(input, -1)

Expect(len(veth)).NotTo(Equal(0))

By("Deleting BusyBox Deployment")
err = f.K8sResourceManagers.DeploymentManager().DeleteAndWaitTillDeploymentIsDeleted(deploymentSpec)
Expect(err).NotTo(HaveOccurred())

By("Restoring old value on daemonset")
restoreOldValues(map[string]string{
AWS_VPC_ENI_MTU: currMTUVal,
AWS_VPC_K8S_CNI_VETHPREFIX: currVETHPrefix,
})
cleanupHostNetworkPod()
})

It("Changing AWS_VPC_K8S_CNI_LOG_FILE", func() {
By("Deploying a host network deployment with Volume mount")
curlContainer := manifest.NewBusyBoxContainerBuilder().Image("curlimages/curl:7.76.1").Name("curler").Build()

volume := []v1.Volume{
{
Name: VOLUME_NAME,
VolumeSource: v1.VolumeSource{
HostPath: &v1.HostPathVolumeSource{
Path: VOLUME_MOUNT_PATH,
},
},
},
}

volumeMount := []v1.VolumeMount{
{
Name: VOLUME_NAME,
MountPath: VOLUME_NAME,
},
}

deploymentSpecWithVol := manifest.NewDefaultDeploymentBuilder().
Namespace("default").
Name("host-network").
Replicas(1).
HostNetwork(true).
Container(curlContainer).
PodLabel(POD_VOL_LABEL_KEY, POD_VOL_LABEL_VAL).
MountVolume(volume, volumeMount).
NodeName(primaryNode.Name).
Build()

_, err := f.K8sResourceManagers.
DeploymentManager().
CreateAndWaitTillDeploymentIsReady(deploymentSpecWithVol, utils.DefaultDeploymentReadyTimeout)
Expect(err).ToNot(HaveOccurred())

pods, err := f.K8sResourceManagers.PodManager().GetPodsWithLabelSelector(POD_VOL_LABEL_KEY, POD_VOL_LABEL_VAL)
Expect(err).NotTo(HaveOccurred())
Expect(len(pods.Items)).Should(BeNumerically(">", 0))

podWithVol := pods.Items[0]
currLogFilepath := getEnvValueForKey(AWS_VPC_K8S_CNI_LOG_FILE)
Expect(currLogFilepath).NotTo(Equal(""))

newLogFile := "ipamd_test.log"
k8sUtils.AddEnvVarToDaemonSetAndWaitTillUpdated(f, DAEMONSET, NAMESPACE, DAEMONSET, map[string]string{
AWS_VPC_K8S_CNI_LOG_FILE: "/host/var/log/aws-routed-eni/" + newLogFile,
})

stdout, _, err := f.K8sResourceManagers.PodManager().PodExec("default", podWithVol.Name, []string{"tail", "-n", "5", "ipamd-logs/ipamd_test.log"})
Expect(err).NotTo(HaveOccurred())
Expect(stdout).NotTo(Equal(""))

By("Restoring old value on daemonset")
restoreOldValues(map[string]string{
AWS_VPC_K8S_CNI_LOG_FILE: currLogFilepath,
})

By("Deleing deployment with Volume Mount")
err = f.K8sResourceManagers.DeploymentManager().DeleteAndWaitTillDeploymentIsDeleted(deploymentSpecWithVol)
Expect(err).NotTo(HaveOccurred())
})
})
})

func getEnvValueForKey(key string) string {
ds, err = f.K8sResourceManagers.DaemonSetManager().GetDaemonSet(NAMESPACE, DAEMONSET)
Expect(err).NotTo(HaveOccurred())

envVar := ds.Spec.Template.Spec.Containers[0].Env
for _, env := range envVar {
if env.Name == key {
return env.Value
}
}
return ""
}

func setupHostNetworkPod() {
By("Deploying a Host Network Pod")
curlContainer := manifest.NewBusyBoxContainerBuilder().Image("curlimages/curl:7.76.1").Name("curler").Build()

hostNetworkDeploymentSpec = manifest.NewDefaultDeploymentBuilder().
Namespace("default").
Name("host-network").
Replicas(1).
HostNetwork(true).
Container(curlContainer).
PodLabel(HOST_POD_LABEL_KEY, HOST_POD_LABEL_VAL).
NodeName(primaryNode.Name).
Build()

_, err := f.K8sResourceManagers.
DeploymentManager().
CreateAndWaitTillDeploymentIsReady(hostNetworkDeploymentSpec, utils.DefaultDeploymentReadyTimeout)
Expect(err).NotTo(HaveOccurred())

pods, err := f.K8sResourceManagers.PodManager().GetPodsWithLabelSelector(HOST_POD_LABEL_KEY, HOST_POD_LABEL_VAL)
Expect(err).NotTo(HaveOccurred())

hostNetworkPod = pods.Items[0]
}

func cleanupHostNetworkPod() {
err = f.K8sResourceManagers.DeploymentManager().
DeleteAndWaitTillDeploymentIsDeleted(hostNetworkDeploymentSpec)
Expect(err).ToNot(HaveOccurred())
}

func restoreOldValues(oldVals map[string]string) {
k8sUtils.AddEnvVarToDaemonSetAndWaitTillUpdated(f, DAEMONSET, NAMESPACE, DAEMONSET, oldVals)
}
5 changes: 4 additions & 1 deletion test/integration-new/ipamd/ipamd_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ var err error
var f *framework.Framework
var primaryNode v1.Node
var primaryInstance *ec2.Instance
var numOfNodes int

func TestIPAMD(t *testing.T) {
RegisterFailHandler(Fail)
Expand All @@ -40,7 +41,9 @@ var _ = BeforeSuite(func() {
nodeList, err := f.K8sResourceManagers.NodeManager().GetNodes(f.Options.NgNameLabelKey,
f.Options.NgNameLabelVal)
Expect(err).ToNot(HaveOccurred())
Expect(len(nodeList.Items)).Should(BeNumerically(">", 1))

numOfNodes = len(nodeList.Items)
Expect(numOfNodes).Should(BeNumerically(">", 1))

// Nominate the first node as the primary node
primaryNode = nodeList.Items[0]
Expand Down
Loading