From 5cbb187a6de700a0b895ed25c5e6017b334d1c63 Mon Sep 17 00:00:00 2001 From: Ilya Dmitrichenko Date: Thu, 28 Mar 2019 15:07:10 +0000 Subject: [PATCH] Fix unit tests --- pkg/ctl/cmdutils/nodegroup_filter_test.go | 243 ++++++++++++++++++ pkg/ctl/create/nodegroup_utils_test.go | 299 ---------------------- 2 files changed, 243 insertions(+), 299 deletions(-) delete mode 100644 pkg/ctl/create/nodegroup_utils_test.go diff --git a/pkg/ctl/cmdutils/nodegroup_filter_test.go b/pkg/ctl/cmdutils/nodegroup_filter_test.go index de1f2093ec..508b7d8c71 100644 --- a/pkg/ctl/cmdutils/nodegroup_filter_test.go +++ b/pkg/ctl/cmdutils/nodegroup_filter_test.go @@ -1,10 +1,13 @@ package cmdutils_test import ( + "bytes" + . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4" + "github.com/weaveworks/eksctl/pkg/printers" . "github.com/weaveworks/eksctl/pkg/ctl/cmdutils" ) @@ -13,6 +16,48 @@ var _ = Describe("nodegroup filter", func() { Context("CheckEachNodeGroup", func() { + It("should iterate over unique nodegroups and apply defaults with NewNodeGroupChecker", func() { + cfg := newClusterConfig() + addGroupA(cfg) + addGroupB(cfg) + + filter := NewNodeGroupFilter() + printer := printers.NewJSONPrinter() + names := []string{} + + filter.ValidateNodeGroupsAndSetDefaults(cfg.NodeGroups) + + filter.CheckEachNodeGroup(cfg.NodeGroups, func(i int, nodeGroup *api.NodeGroup) error { + Expect(nodeGroup).To(Equal(cfg.NodeGroups[i])) + names = append(names, nodeGroup.Name) + return nil + }) + Expect(names).To(Equal([]string{"test-ng1a", "test-ng2a", "test-ng3a", "test-ng1b", "test-ng2b", "test-ng3b"})) + + w := &bytes.Buffer{} + + printer.PrintObj(cfg, w) + + Expect(w.Bytes()).To(MatchJSON(expected)) + }) + + It("should be able to skip all nodegroups", func() { + cfg := newClusterConfig() + addGroupA(cfg) + addGroupB(cfg) + + filter := NewNodeGroupFilter() + filter.SkipAll = true + filter.ValidateNodeGroupsAndSetDefaults(cfg.NodeGroups) + + callback := false + filter.CheckEachNodeGroup(cfg.NodeGroups, func(_ int, _ *api.NodeGroup) error { + callback = true + return nil + }) + Expect(callback).To(BeFalse()) + }) + It("should iterate over unique nodegroups", func() { cfg := newClusterConfig() addGroupA(cfg) @@ -128,3 +173,201 @@ func addGroupB(cfg *api.ClusterConfig) { ng.SecurityGroups.WithLocal = api.NewBoolFalse() ng.Labels = map[string]string{"group": "b", "seq": "1"} } + +const expected = ` + { + "kind": "ClusterConfig", + "apiVersion": "eksctl.io/v1alpha4", + "metadata": { + "name": "test-3x3-ngs", + "region": "eu-central-1", + "version": "1.11" + }, + "iam": {}, + "vpc": { + "cidr": "192.168.0.0/16" + }, + "nodeGroups": [ + { + "name": "test-ng1a", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m5.large", + "privateNetworking": false, + "securityGroups": { + "withShared": true, + "withLocal": true + }, + "volumeSize": 768, + "volumeType": "io1", + "labels": { + "group": "a", + "seq": "1" + }, + "allowSSH": false, + "iam": { + "attachPolicyARNs": [ + "foo" + ], + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + } + }, + { + "name": "test-ng2a", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m5.large", + "privateNetworking": false, + "securityGroups": { + "withShared": true, + "withLocal": true + }, + "volumeSize": 0, + "volumeType": "gp2", + "labels": { + "group": "a", + "seq": "2" + }, + "allowSSH": false, + "iam": { + "attachPolicyARNs": [ + "bar" + ], + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + } + }, + { + "name": "test-ng3a", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m3.large", + "privateNetworking": false, + "securityGroups": { + "withShared": true, + "withLocal": true + }, + "volumeSize": 0, + "volumeType": "gp2", + "labels": { + "group": "a", + "seq": "3" + }, + "allowSSH": true, + "sshPublicKeyPath": "~/.ssh/id_rsa.pub", + "iam": { + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + }, + "clusterDNS": "1.2.3.4" + }, + { + "name": "test-ng1b", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m5.large", + "privateNetworking": false, + "securityGroups": { + "withShared": true, + "withLocal": true + }, + "volumeSize": 0, + "volumeType": "gp2", + "labels": { + "group": "b", + "seq": "1" + }, + "allowSSH": true, + "sshPublicKeyPath": "~/.ssh/id_rsa.pub", + "iam": { + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + } + }, + { + "name": "test-ng2b", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m5.xlarge", + "privateNetworking": false, + "securityGroups": { + "attachIDs": [ + "sg-1", + "sg-2" + ], + "withShared": true, + "withLocal": false + }, + "volumeSize": 0, + "volumeType": "gp2", + "labels": { + "group": "b", + "seq": "1" + }, + "allowSSH": false, + "iam": { + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + }, + "clusterDNS": "4.2.8.14" + }, + { + "name": "test-ng3b", + "ami": "static", + "amiFamily": "AmazonLinux2", + "instanceType": "m5.large", + "privateNetworking": false, + "securityGroups": { + "attachIDs": [ + "sg-1", + "sg-2" + ], + "withShared": true, + "withLocal": false + }, + "volumeSize": 192, + "volumeType": "gp2", + "labels": { + "group": "b", + "seq": "1" + }, + "allowSSH": false, + "iam": { + "withAddonPolicies": { + "imageBuilder": false, + "autoScaler": false, + "externalDNS": false, + "appMesh": false, + "ebs": false + } + } + } + ] + } +` diff --git a/pkg/ctl/create/nodegroup_utils_test.go b/pkg/ctl/create/nodegroup_utils_test.go deleted file mode 100644 index 76e5fe7fa8..0000000000 --- a/pkg/ctl/create/nodegroup_utils_test.go +++ /dev/null @@ -1,299 +0,0 @@ -package create_test - -import ( - "bytes" - - . "github.com/onsi/ginkgo" - . "github.com/onsi/gomega" - - api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha4" - "github.com/weaveworks/eksctl/pkg/ctl/cmdutils" - "github.com/weaveworks/eksctl/pkg/printers" - - . "github.com/weaveworks/eksctl/pkg/ctl/create" -) - -var _ = Describe("create nodegroup utils", func() { - - Context("apply nodegroup defaults", func() { - - It("should iterate over unique nodegroups and apply defaults with NewNodeGroupChecker", func() { - cfg := newClusterConfig() - addGroupA(cfg) - addGroupB(cfg) - - filter := cmdutils.NewNodeGroupFilter() - printer := printers.NewJSONPrinter() - names := []string{} - - filter.CheckEachNodeGroup(cfg.NodeGroups, NewNodeGroupChecker) - - filter.CheckEachNodeGroup(cfg.NodeGroups, func(i int, nodeGroup *api.NodeGroup) error { - Expect(nodeGroup).To(Equal(cfg.NodeGroups[i])) - names = append(names, nodeGroup.Name) - return nil - }) - Expect(names).To(Equal([]string{"test-ng1a", "test-ng2a", "test-ng3a", "test-ng1b", "test-ng2b", "test-ng3b"})) - - w := &bytes.Buffer{} - - printer.PrintObj(cfg, w) - - Expect(w.Bytes()).To(MatchJSON(expected)) - }) - }) -}) - -func newClusterConfig() *api.ClusterConfig { - cfg := api.NewClusterConfig() - - cfg.Metadata.Name = "test-3x3-ngs" - cfg.Metadata.Region = "eu-central-1" - - return cfg -} - -func addGroupA(cfg *api.ClusterConfig) { - var ng *api.NodeGroup - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng1a" - ng.VolumeSize = 768 - ng.VolumeType = "io1" - ng.IAM.AttachPolicyARNs = []string{"foo"} - ng.Labels = map[string]string{"group": "a", "seq": "1"} - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng2a" - ng.IAM.AttachPolicyARNs = []string{"bar"} - ng.Labels = map[string]string{"group": "a", "seq": "2"} - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng3a" - ng.ClusterDNS = "1.2.3.4" - ng.InstanceType = "m3.large" - ng.AllowSSH = true - ng.Labels = map[string]string{"group": "a", "seq": "3"} -} - -func addGroupB(cfg *api.ClusterConfig) { - var ng *api.NodeGroup - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng1b" - ng.AllowSSH = true - ng.Labels = map[string]string{"group": "b", "seq": "1"} - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng2b" - ng.ClusterDNS = "4.2.8.14" - ng.InstanceType = "m5.xlarge" - ng.SecurityGroups.AttachIDs = []string{"sg-1", "sg-2"} - ng.SecurityGroups.WithLocal = api.NewBoolFalse() - ng.Labels = map[string]string{"group": "b", "seq": "1"} - - ng = cfg.NewNodeGroup() - ng.Name = "test-ng3b" - ng.VolumeSize = 192 - ng.SecurityGroups.AttachIDs = []string{"sg-1", "sg-2"} - ng.SecurityGroups.WithLocal = api.NewBoolFalse() - ng.Labels = map[string]string{"group": "b", "seq": "1"} -} - -const expected = ` - { - "kind": "ClusterConfig", - "apiVersion": "eksctl.io/v1alpha4", - "metadata": { - "name": "test-3x3-ngs", - "region": "eu-central-1", - "version": "1.11" - }, - "iam": {}, - "vpc": { - "cidr": "192.168.0.0/16" - }, - "nodeGroups": [ - { - "name": "test-ng1a", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m5.large", - "privateNetworking": false, - "securityGroups": { - "withShared": true, - "withLocal": true - }, - "volumeSize": 768, - "volumeType": "io1", - "labels": { - "group": "a", - "seq": "1" - }, - "allowSSH": false, - "iam": { - "attachPolicyARNs": [ - "foo" - ], - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - } - }, - { - "name": "test-ng2a", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m5.large", - "privateNetworking": false, - "securityGroups": { - "withShared": true, - "withLocal": true - }, - "volumeSize": 0, - "volumeType": "gp2", - "labels": { - "group": "a", - "seq": "2" - }, - "allowSSH": false, - "iam": { - "attachPolicyARNs": [ - "bar" - ], - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - } - }, - { - "name": "test-ng3a", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m3.large", - "privateNetworking": false, - "securityGroups": { - "withShared": true, - "withLocal": true - }, - "volumeSize": 0, - "volumeType": "gp2", - "labels": { - "group": "a", - "seq": "3" - }, - "allowSSH": true, - "sshPublicKeyPath": "~/.ssh/id_rsa.pub", - "iam": { - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - }, - "clusterDNS": "1.2.3.4" - }, - { - "name": "test-ng1b", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m5.large", - "privateNetworking": false, - "securityGroups": { - "withShared": true, - "withLocal": true - }, - "volumeSize": 0, - "volumeType": "gp2", - "labels": { - "group": "b", - "seq": "1" - }, - "allowSSH": true, - "sshPublicKeyPath": "~/.ssh/id_rsa.pub", - "iam": { - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - } - }, - { - "name": "test-ng2b", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m5.xlarge", - "privateNetworking": false, - "securityGroups": { - "attachIDs": [ - "sg-1", - "sg-2" - ], - "withShared": true, - "withLocal": false - }, - "volumeSize": 0, - "volumeType": "gp2", - "labels": { - "group": "b", - "seq": "1" - }, - "allowSSH": false, - "iam": { - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - }, - "clusterDNS": "4.2.8.14" - }, - { - "name": "test-ng3b", - "ami": "static", - "amiFamily": "AmazonLinux2", - "instanceType": "m5.large", - "privateNetworking": false, - "securityGroups": { - "attachIDs": [ - "sg-1", - "sg-2" - ], - "withShared": true, - "withLocal": false - }, - "volumeSize": 192, - "volumeType": "gp2", - "labels": { - "group": "b", - "seq": "1" - }, - "allowSSH": false, - "iam": { - "withAddonPolicies": { - "imageBuilder": false, - "autoScaler": false, - "externalDNS": false, - "appMesh": false, - "ebs": false - } - } - } - ] - } -`