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
5 changes: 3 additions & 2 deletions test/framework/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Options struct {
AWSVPCID string
NgNameLabelKey string
NgNameLabelVal string
IgnoreOptional bool
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved
}

func (options *Options) BindFlags() {
Expand All @@ -57,10 +58,10 @@ 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 {
if !options.IgnoreOptional && len(options.NgNameLabelKey) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-key")
}
if len(options.NgNameLabelVal) == 0 {
if !options.IgnoreOptional && len(options.NgNameLabelVal) == 0 {
return errors.Errorf("%s must be set!", "ng-name-label-val")
}

Expand Down
6 changes: 3 additions & 3 deletions test/framework/resources/k8s/manifest/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ type Container struct {
args []string
}

func NewBusyBoxContainerBuilder() *Container {
func NewContainerWithCurlerBuilder() *Container {
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved
return &Container{
name: "busybox",
image: "busybox",
name: "curler",
image: "curlimages/curl:7.76.1",
imagePullPolicy: v1.PullIfNotPresent,
command: []string{"sleep", "3600"},
args: []string{},
Expand Down
40 changes: 38 additions & 2 deletions test/framework/resources/k8s/manifest/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,17 @@ type DeploymentBuilder struct {
labels map[string]string
terminationGracePeriod int
nodeName string
hostNetwork bool
volume []corev1.Volume
volumeMount []corev1.VolumeMount
}

func NewBusyBoxDeploymentBuilder() *DeploymentBuilder {
return &DeploymentBuilder{
namespace: utils.DefaultTestNamespace,
name: "deployment-test",
replicas: 10,
container: NewBusyBoxContainerBuilder().Build(),
container: NewContainerWithCurlerBuilder().Build(),
labels: map[string]string{"role": "test"},
terminationGracePeriod: 0,
}
Expand Down Expand Up @@ -86,8 +89,34 @@ 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(name string, mountpath string) *DeploymentBuilder {
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved
d.volume = []corev1.Volume{
{
Name: name,
VolumeSource: corev1.VolumeSource{
HostPath: &corev1.HostPathVolumeSource{
Path: mountpath,
},
},
},
}

d.volumeMount = []corev1.VolumeMount{
{
Name: name,
MountPath: name,
},
}
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 @@ -103,11 +132,18 @@ func (d *DeploymentBuilder) Build() *v1.Deployment {
Labels: d.labels,
},
Spec: corev1.PodSpec{
HostNetwork: d.hostNetwork,
Containers: []corev1.Container{d.container},
TerminationGracePeriodSeconds: aws.Int64(int64(d.terminationGracePeriod)),
NodeName: d.nodeName,
},
},
},
}

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
}
8 changes: 8 additions & 0 deletions test/framework/resources/k8s/resources/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

type NodeManager interface {
GetNodes(nodeLabelKey string, nodeLabelVal string) (v1.NodeList, error)
GetAllNodes() (v1.NodeList, error)
}

type defaultNodeManager struct {
Expand All @@ -40,3 +41,10 @@ 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
}
77 changes: 77 additions & 0 deletions test/integration-new/cni/eni_config/config_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package env_vars
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved

import (
"testing"

"github.com/aws/amazon-vpc-cni-k8s/test/framework"
"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/onsi/ginkgo"
. "github.com/onsi/gomega"
appsV1 "k8s.io/api/apps/v1"
v1 "k8s.io/api/core/v1"
)

const (
NAMESPACE = "kube-system"
DAEMONSET = "aws-node"
HOST_POD_LABEL_KEY = "network"
HOST_POD_LABEL_VAL = "host"
)

var (
primaryNode v1.Node
primaryInstanceId string
ds *appsV1.DaemonSet
f *framework.Framework
hostNetworkDeploymentSpec *appsV1.Deployment
hostNetworkDeployment *appsV1.Deployment
err error
hostNetworkPod v1.Pod
)

func TestCni(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Cni Suite")
}

var _ = BeforeSuite(func() {
globalOptions := framework.GlobalOptions
globalOptions.IgnoreOptional = true
f = framework.New(globalOptions)
ds, err = f.K8sResourceManagers.DaemonSetManager().GetDaemonSet(NAMESPACE, DAEMONSET)
Expect(err).NotTo(HaveOccurred())

nodes, err := f.K8sResourceManagers.NodeManager().GetAllNodes()
Expect(err).NotTo(HaveOccurred())
Expect(len(nodes.Items)).To(BeNumerically(">", 0))

primaryNode = nodes.Items[0]
primaryInstanceId = k8sUtils.GetInstanceIDFromNode(primaryNode)

hostNetworkDeploymentSpec = manifest.NewBusyBoxDeploymentBuilder().
Namespace("default").
Name("host-network").
Replicas(1).
HostNetwork(true).
PodLabel(HOST_POD_LABEL_KEY, HOST_POD_LABEL_VAL).
NodeName(primaryNode.Name).
MountVolume("ipamd-logs", "/var/log/aws-routed-eni/").
Build()

//f.K8sResourceManagers.DeploymentManager().MountVolume(hostNetworkDeploymentSpec, "ipamd-logs", "/var/log/aws-routed-eni/")
hostNetworkDeployment, err = f.K8sResourceManagers.
DeploymentManager().
CreateAndWaitTillDeploymentIsReady(hostNetworkDeploymentSpec)
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]
})

var _ = AfterSuite(func() {
err = f.K8sResourceManagers.DeploymentManager().DeleteAndWaitTillDeploymentIsDeleted(hostNetworkDeploymentSpec)
Expect(err).NotTo(HaveOccurred())
})
145 changes: 145 additions & 0 deletions test/integration-new/cni/eni_config/env_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package env_vars

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/onsi/ginkgo"
. "github.com/onsi/gomega"
)

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"
)

var _ = Describe("cni env test", func() {

Context("CNI Environment Variables", func() {
It("Verifying that secondary ENI is created", func() {
nodes, err := f.K8sResourceManagers.NodeManager().GetAllNodes()
Expect(err).NotTo(HaveOccurred())

for _, node := range nodes.Items {
instanceId := k8sUtils.GetInstanceIDFromNode(node)
instance, err := f.CloudServices.EC2().DescribeInstance(instanceId)
Expect(err).NotTo(HaveOccurred())

len := len(instance.NetworkInterfaces)
Expect(len).To(BeNumerically(">=", 2))
}
})

It("Changing AWS_VPC_ENI_MTU and AWS_VPC_K8S_CNI_VETHPREFIX", func() {
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")
{
cgchinmay marked this conversation as resolved.
Show resolved Hide resolved
deploymentSpec := manifest.NewBusyBoxDeploymentBuilder().
Namespace("default").
Name("busybox").
Replicas(1).
NodeName(primaryNode.Name).
Build()

_, err := f.K8sResourceManagers.
DeploymentManager().
CreateAndWaitTillDeploymentIsReady(deploymentSpec)

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,
})
}
})

It("Changing AWS_VPC_K8S_CNI_LOG_FILE", func() {
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", hostNetworkPod.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,
})
}
})
})
})

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

func restoreOldValues(oldVals map[string]string) {
k8sUtils.AddEnvVarToDaemonSetAndWaitTillUpdated(f, DAEMONSET, NAMESPACE, DAEMONSET, oldVals)
}
Loading