Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
errordeveloper committed Sep 14, 2018
1 parent f319edc commit 8cf22b1
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 42 deletions.
87 changes: 45 additions & 42 deletions pkg/cfn/builder/api_test.go
Expand Up @@ -7,6 +7,8 @@ import (
"strings"

cfn "github.com/aws/aws-sdk-go/service/cloudformation"
// gfn "github.com/awslabs/goformation/cloudformation"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
. "github.com/weaveworks/eksctl/pkg/cfn/builder"
Expand All @@ -15,6 +17,11 @@ import (
"github.com/weaveworks/eksctl/pkg/nodebootstrap"
)

const (
totalNodeResources = 11
clusterName = "ferocious-mushroom-1532594698"
)

type Template struct {
Resources map[string]struct {
Properties struct {
Expand Down Expand Up @@ -50,20 +57,22 @@ var _ = Describe("CloudFormation template builder API", func() {
Describe("GetAllOutputsFromClusterStack", func() {
caCert := "LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUN5RENDQWJDZ0F3SUJBZ0lCQURBTkJna3Foa2lHOXcwQkFRc0ZBREFWTVJNd0VRWURWUVFERXdwcmRXSmwKY201bGRHVnpNQjRYRFRFNE1EWXdOekExTlRBMU5Wb1hEVEk0TURZd05EQTFOVEExTlZvd0ZURVRNQkVHQTFVRQpBeE1LYTNWaVpYSnVaWFJsY3pDQ0FTSXdEUVlKS29aSWh2Y05BUUVCQlFBRGdnRVBBRENDQVFvQ2dnRUJBTWJoCnpvZElYR0drckNSZE1jUmVEN0YvMnB1NFZweTdvd3FEVDgrdk9zeGs2bXFMNWxQd3ZicFhmYkE3R0xzMDVHa0wKaDdqL0ZjcU91cnMwUFZSK3N5REtuQXltdDFORWxGNllGQktSV1dUQ1hNd2lwN1pweW9XMXdoYTlJYUlPUGxCTQpPTEVlckRabFVrVDFVV0dWeVdsMmxPeFgxa2JhV2gvakptWWdkeW5jMXhZZ3kxa2JybmVMSkkwLzVUVTRCajJxClB1emtrYW5Xd3lKbGdXQzhBSXlpWW82WFh2UVZmRzYrM3RISE5XM1F1b3ZoRng2MTFOYnl6RUI3QTdtZGNiNmgKR0ZpWjdOeThHZnFzdjJJSmI2Nk9FVzBSdW9oY1k3UDZPdnZmYnlKREhaU2hqTStRWFkxQXN5b3g4Ri9UelhHSgpQUWpoWUZWWEVhZU1wQmJqNmNFQ0F3RUFBYU1qTUNFd0RnWURWUjBQQVFIL0JBUURBZ0trTUE4R0ExVWRFd0VCCi93UUZNQU1CQWY4d0RRWUpLb1pJaHZjTkFRRUxCUUFEZ2dFQkFCa2hKRVd4MHk1LzlMSklWdXJ1c1hZbjN6Z2EKRkZ6V0JsQU44WTlqUHB3S2t0Vy9JNFYyUGg3bWY2Z3ZwZ3Jhc2t1Slk1aHZPcDdBQmcxSTFhaHUxNUFpMUI0ZApuMllRaDlOaHdXM2pKMmhuRXk0VElpb0gza2JFdHRnUVB2bWhUQzNEYUJreEpkbmZJSEJCV1RFTTU1czRwRmxUClpzQVJ3aDc1Q3hYbjdScVU0akpKcWNPaTRjeU5qeFVpRDBqR1FaTmNiZWEyMkRCeTJXaEEzUWZnbGNScGtDVGUKRDVPS3NOWlF4MW9MZFAwci9TSmtPT1NPeUdnbVJURTIrODQxN21PRW02Z3RPMCszdWJkbXQ0aENsWEtFTTZYdwpuQWNlK0JxVUNYblVIN2ZNS3p2TDE5UExvMm5KbFU1TnlCbU1nL1pNVHVlUy80eFZmKy94WnpsQ0Q1WT0KLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo="
caCertData, err := base64.StdEncoding.DecodeString(caCert)
It("should not error", func() { Expect(err).NotTo(HaveOccurred()) })
It("should not error", func() { Expect(err).ShouldNot(HaveOccurred()) })

expected := &api.ClusterConfig{
ClusterName: clusterName,
SecurityGroup: "sg-0b44c48bcba5b7362",
Subnets: []string{"subnet-0f98135715dfcf55f", "subnet-0ade11bad78dced9e", "subnet-0e2e63ff1712bf6ef"},
VPC: "vpc-0e265ad953062b94b",
Endpoint: "https://DE37D8AFB23F7275D2361AD6B2599143.yl4.us-west-2.eks.amazonaws.com",
CertificateAuthorityData: caCertData,
ARN: "arn:aws:eks:us-west-2:376248598259:cluster/ferocious-mushroom-1532594698",
ARN: "arn:aws:eks:us-west-2:376248598259:cluster/" + clusterName,
NodeInstanceRoleARN: "",
AvailabilityZones: testAZs,
}

initial := &api.ClusterConfig{
ClusterName: clusterName,
AvailabilityZones: testAZs,
}

Expand All @@ -76,13 +85,13 @@ var _ = Describe("CloudFormation template builder API", func() {
"VPC": "vpc-0e265ad953062b94b",
"Endpoint": "https://DE37D8AFB23F7275D2361AD6B2599143.yl4.us-west-2.eks.amazonaws.com",
"CertificateAuthorityData": caCert,
"ARN": "arn:aws:eks:us-west-2:376248598259:cluster/ferocious-mushroom-1532594698",
"ARN": "arn:aws:eks:us-west-2:376248598259:cluster/" + clusterName,
"ClusterStackName": "",
})

It("should not error", func() {
err := rs.GetAllOutputs(sampleStack)
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
})

It("should be equal", func() {
Expand All @@ -92,28 +101,40 @@ var _ = Describe("CloudFormation template builder API", func() {

Describe("AutoNameTag", func() {
rs := NewNodeGroupResourceSet(&api.ClusterConfig{
ClusterName: clusterName,
AvailabilityZones: testAZs,
NodeType: "t2.medium",
Region: "us-west-2",
}, "eksctl-test-123-cluster", 0)
rs.AddAllResources()

template, err := rs.RenderJSON()
err := rs.AddAllResources()
It("should add all resources without errors", func() {
Expect(err).ShouldNot(HaveOccurred())
t := rs.Template()
Expect(t.Resources).ToNot(BeEmpty())
Expect(len(t.Resources)).To(Equal(totalNodeResources))
templateBody, err := t.JSON()
Expect(err).ShouldNot(HaveOccurred())
Expect(templateBody).ShouldNot(BeEmpty())
})

templateBody, err := rs.RenderJSON()
It("should serialise JSON without errors", func() {
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
Expect(templateBody).ShouldNot(BeEmpty())
})

obj := Template{}
It("should parse JSON withon errors", func() {
err := json.Unmarshal(template, &obj)
Expect(err).NotTo(HaveOccurred())
err := json.Unmarshal(templateBody, &obj)
Expect(err).ShouldNot(HaveOccurred())
})

It("SG should have correct tags", func() {
Expect(len(obj.Resources)).ToNot(Equal(0))
Expect(obj.Resources).ToNot(BeNil())
Expect(len(obj.Resources)).To(Equal(totalNodeResources))
Expect(len(obj.Resources["SG"].Properties.Tags)).To(Equal(2))
Expect(obj.Resources["SG"].Properties.Tags[0].Key).To(Equal(map[string]interface{}{
"Fn::Sub": "kubernetes.io/cluster/${ClusterName}",
}))
Expect(obj.Resources["SG"].Properties.Tags[0].Key).To(Equal("kubernetes.io/cluster/" + clusterName))
Expect(obj.Resources["SG"].Properties.Tags[0].Value).To(Equal("owned"))
Expect(obj.Resources["SG"].Properties.Tags[1].Key).To(Equal("Name"))
Expect(obj.Resources["SG"].Properties.Tags[1].Value).To(Equal(map[string]interface{}{
Expand All @@ -124,6 +145,7 @@ var _ = Describe("CloudFormation template builder API", func() {

Describe("NodeGroupTags", func() {
rs := NewNodeGroupResourceSet(&api.ClusterConfig{
ClusterName: clusterName,
AvailabilityZones: testAZs,
NodeType: "t2.medium",
Region: "us-west-2",
Expand All @@ -132,32 +154,29 @@ var _ = Describe("CloudFormation template builder API", func() {

template, err := rs.RenderJSON()
It("should serialise JSON without errors", func() {
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
})
obj := Template{}
It("should parse JSON withon errors", func() {
err := json.Unmarshal(template, &obj)
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
})

It("NodeGroup should have correct tags", func() {
Expect(len(obj.Resources)).ToNot(Equal(0))
Expect(len(obj.Resources["NodeGroup"].Properties.Tags)).To(Equal(2))
Expect(obj.Resources["NodeGroup"].Properties.Tags[0].Key).To(Equal("Name"))
Expect(obj.Resources["NodeGroup"].Properties.Tags[0].Value).To(Equal(map[string]interface{}{
"Fn::Sub": "${ClusterName}-${NodeGroupID}-Node",
}))
Expect(obj.Resources["NodeGroup"].Properties.Tags[0].Value).To(Equal(clusterName + "-0-Node"))
Expect(obj.Resources["NodeGroup"].Properties.Tags[0].PropagateAtLaunch).To(Equal("true"))
Expect(obj.Resources["NodeGroup"].Properties.Tags[1].Key).To(Equal(map[string]interface{}{
"Fn::Sub": "kubernetes.io/cluster/${ClusterName}",
}))
Expect(obj.Resources["NodeGroup"].Properties.Tags[1].Key).To(Equal("kubernetes.io/cluster/" + clusterName))
Expect(obj.Resources["NodeGroup"].Properties.Tags[1].Value).To(Equal("owned"))
Expect(obj.Resources["NodeGroup"].Properties.Tags[1].PropagateAtLaunch).To(Equal("true"))
})
})

Describe("UserData", func() {
rs := NewNodeGroupResourceSet(&api.ClusterConfig{
ClusterName: clusterName,
AvailabilityZones: testAZs,
NodeType: "m5.large",
Region: "us-west-2",
Expand All @@ -171,31 +190,26 @@ var _ = Describe("CloudFormation template builder API", func() {

template, err := rs.RenderJSON()
It("should serialise JSON without errors", func() {
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
})

It("should parse JSON withon errors and extract valid cloud-config using our implementation", func() {
obj := Template{}
err = json.Unmarshal(template, &obj)
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
Expect(len(obj.Resources)).ToNot(Equal(0))

userData := obj.Resources["NodeLaunchConfig"].Properties.UserData
Expect(userData).ToNot(BeEmpty())

c, err = cloudconfig.DecodeCloudConfig(userData)
Expect(err).NotTo(HaveOccurred())
Expect(err).ShouldNot(HaveOccurred())
})

It("should have packages, scripts and commands in cloud-config", func() {
Expect(c).ToNot(BeNil())

Expect(c.Packages).To(Equal([]string{"jq"}))

cmds := len(c.Commands)
Expect(c.Commands[0]).To(Equal([]interface{}{"pip", "install", "--upgrade", "awscli"}))
Expect(c.Commands[cmds-2]).To(Equal([]interface{}{"systemctl", "daemon-reload"}))
Expect(c.Commands[cmds-1]).To(Equal([]interface{}{"systemctl", "restart", "kubelet"}))
Expect(c.Packages).Should(BeEmpty())

getFile := func(p string) *cloudconfig.File {
for _, f := range c.WriteFiles {
Expand All @@ -213,16 +227,6 @@ var _ = Describe("CloudFormation template builder API", func() {
Expect(expectedContent).To(Equal(string(assetContent)))
}

authenticator := getFile("/etc/eksctl/authenticator.sh")
Expect(authenticator).ToNot(BeNil())
Expect(authenticator.Permissions).To(Equal("0755"))
checkAsset("authenticator.sh", authenticator.Content)

kubeconfig := getFile("/etc/eksctl/kubeconfig.yaml")
Expect(kubeconfig).ToNot(BeNil())
Expect(kubeconfig.Permissions).To(Equal("0644"))
checkAsset("kubeconfig.yaml", kubeconfig.Content)

kubeletEnv := getFile("/etc/eksctl/kubelet.env")
Expect(kubeletEnv).ToNot(BeNil())
Expect(kubeletEnv.Permissions).To(Equal("0644"))
Expand Down Expand Up @@ -252,8 +256,7 @@ var _ = Describe("CloudFormation template builder API", func() {
}
}

checkScript("/var/lib/cloud/scripts/per-instance/get_metadata.sh", true)
checkScript("/var/lib/cloud/scripts/per-instance/get_credentials.sh", true)
checkScript("/var/lib/cloud/scripts/per-instance/bootstrap.al2.sh", true)
})
})
})
4 changes: 4 additions & 0 deletions pkg/cfn/builder/cluster.go
Expand Up @@ -55,6 +55,10 @@ func (c *clusterResourceSet) RenderJSON() ([]byte, error) {
return c.rs.renderJSON()
}

func (c *clusterResourceSet) Template() gfn.Template {
return *c.rs.template
}

func (c *clusterResourceSet) newResource(name string, resource interface{}) *gfn.Value {
return c.rs.newResource(name, resource)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/cfn/builder/nodegroup.go
Expand Up @@ -68,6 +68,10 @@ func (n *nodeGroupResourceSet) RenderJSON() ([]byte, error) {
return n.rs.renderJSON()
}

func (n *nodeGroupResourceSet) Template() gfn.Template {
return *n.rs.template
}

func (n *nodeGroupResourceSet) newResource(name string, resource interface{}) *gfn.Value {
return n.rs.newResource(name, resource)
}
Expand Down

0 comments on commit 8cf22b1

Please sign in to comment.