Skip to content
This repository was archived by the owner on Sep 30, 2020. It is now read-only.

Commit d94e129

Browse files
committed
test: Integration testing with real AWS services including S3, KMS, CloudFormation
Integration tests are executed only when you've explicitly set non-empty value for the environment variable KUBE_AWS_INTEGRATION_TEST. Also note that these tests pass only when other environment variables referenced in `test/integration/aws_test.go` are properly set.
1 parent 72b3bc7 commit d94e129

File tree

2 files changed

+203
-0
lines changed

2 files changed

+203
-0
lines changed

nodepool/cluster/cluster.go

+4
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ func (c *Cluster) Create(stackBody string, s3URI string) error {
5858
return c.stackProvisioner().CreateStackAndWait(cfSvc, s3Svc, stackBody, s3URI)
5959
}
6060

61+
func (c *Cluster) ValidateStack(stackBody string, s3URI string) (string, error) {
62+
return c.stackProvisioner().Validate(stackBody, s3URI)
63+
}
64+
6165
func (c *Cluster) Info() (*Info, error) {
6266
var info Info
6367
{

test/integration/aws_test.go

+199
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,199 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"github.com/coreos/kube-aws/nodepool/cluster"
6+
"github.com/coreos/kube-aws/nodepool/config"
7+
"github.com/coreos/kube-aws/test/helper"
8+
"os"
9+
"testing"
10+
)
11+
12+
var yaml string
13+
14+
type testEnv struct {
15+
t *testing.T
16+
}
17+
18+
func (r testEnv) get(name string) string {
19+
v := os.Getenv(name)
20+
21+
if v == "" {
22+
r.t.Errorf("%s must be set", name)
23+
r.t.FailNow()
24+
}
25+
26+
return v
27+
}
28+
29+
type integrationSettings struct {
30+
nodePoolName string
31+
externalDNSName string
32+
keyName string
33+
kmsKeyArn string
34+
region string
35+
}
36+
37+
func newIntegrationSettings(t *testing.T) integrationSettings {
38+
env := testEnv{t: t}
39+
40+
nodePoolName := env.get("KUBE_AWS_NODE_POOL_NAME")
41+
return integrationSettings{
42+
nodePoolName: nodePoolName,
43+
externalDNSName: fmt.Sprintf("%s.%s", nodePoolName, env.get("KUBE_AWS_DOMAIN")),
44+
keyName: env.get("KUBE_AWS_KEY_NAME"),
45+
kmsKeyArn: env.get("KUBE_AWS_KMS_KEY_ARN"),
46+
region: env.get("KUBE_AWS_REGION"),
47+
}
48+
}
49+
50+
func insufficientConfigYamlFromEnv(t *testing.T) string {
51+
settings := newIntegrationSettings(t)
52+
53+
yaml = fmt.Sprintf(`clusterName: mycluster
54+
nodePoolName: %s
55+
externalDNSName: "%s"
56+
keyName: "%s"
57+
kmsKeyArn: "%s"
58+
region: "%s"
59+
`,
60+
settings.nodePoolName,
61+
settings.externalDNSName,
62+
settings.keyName,
63+
settings.kmsKeyArn,
64+
settings.region,
65+
)
66+
67+
return yaml
68+
}
69+
70+
// Integration testing with real AWS services including S3, KMS, CloudFormation
71+
func TestAwsIntegration(t *testing.T) {
72+
if os.Getenv("KUBE_AWS_INTEGRATION_TEST") == "" {
73+
t.Skipf("`export KUBE_AWS_INTEGRATION_TEST=1` is required to run integration tests. Skipping.")
74+
t.SkipNow()
75+
}
76+
77+
yaml := insufficientConfigYamlFromEnv(t)
78+
79+
minimalValidConfigYaml := yaml + `
80+
availabilityZone: us-west-1c
81+
dnsServiceIP: "10.3.0.10"
82+
etcdEndpoints: "10.0.0.1"
83+
`
84+
validCases := []struct {
85+
context string
86+
configYaml string
87+
}{
88+
{
89+
context: "WithMinimalValidConfig",
90+
configYaml: minimalValidConfigYaml,
91+
},
92+
{
93+
context: "WithVpcIdSpecified",
94+
configYaml: minimalValidConfigYaml + `
95+
vpcId: vpc-1a2b3c4d
96+
`,
97+
},
98+
{
99+
context: "WithVpcIdAndRouteTableIdSpecified",
100+
configYaml: minimalValidConfigYaml + `
101+
vpcId: vpc-1a2b3c4d
102+
routeTableId: rtb-1a2b3c4d
103+
`,
104+
},
105+
{
106+
context: "WithSpotFleetEnabled",
107+
configYaml: minimalValidConfigYaml + `
108+
worker:
109+
spotFleet:
110+
targetCapacity: 10
111+
`,
112+
},
113+
{
114+
context: "WithSpotFleetWithCustomGp2RootVolumeSettings",
115+
configYaml: minimalValidConfigYaml + `
116+
worker:
117+
spotFleet:
118+
targetCapacity: 10
119+
unitRootVolumeSize: 40
120+
launchSpecifications:
121+
- weightedCapacity: 1
122+
instanceType: m3.medium
123+
- weightedCapacity: 2
124+
instanceType: m3.large
125+
rootVolumeSize: 100
126+
`,
127+
},
128+
{
129+
context: "WithSpotFleetWithCustomIo1RootVolumeSettings",
130+
configYaml: minimalValidConfigYaml + `
131+
worker:
132+
spotFleet:
133+
targetCapacity: 10
134+
rootVolumeType: io1
135+
unitRootVolumeSize: 40
136+
unitRootVolumeIOPS: 100
137+
launchSpecifications:
138+
- weightedCapacity: 1
139+
instanceType: m3.medium
140+
- weightedCapacity: 2
141+
instanceType: m3.large
142+
rootVolumeIOPS: 500
143+
`,
144+
},
145+
}
146+
147+
for _, validCase := range validCases {
148+
t.Run(validCase.context, func(t *testing.T) {
149+
configBytes := validCase.configYaml
150+
providedConfig, err := config.ClusterFromBytes([]byte(configBytes))
151+
if err != nil {
152+
t.Errorf("failed to parse config %s: %v", configBytes, err)
153+
t.FailNow()
154+
}
155+
156+
helper.WithDummyCredentials(func(dummyTlsAssetsDir string) {
157+
var stackTemplateOptions = config.StackTemplateOptions{
158+
TLSAssetsDir: dummyTlsAssetsDir,
159+
WorkerTmplFile: "../../config/templates/cloud-config-worker",
160+
StackTemplateTmplFile: "../../nodepool/config/templates/stack-template.json",
161+
}
162+
163+
t.Run("ValidateUserData", func(t *testing.T) {
164+
if err := providedConfig.ValidateUserData(stackTemplateOptions); err != nil {
165+
t.Errorf("failed to validate user data: %v", err)
166+
}
167+
})
168+
169+
t.Run("RenderStackTemplate", func(t *testing.T) {
170+
if _, err := providedConfig.RenderStackTemplate(stackTemplateOptions); err != nil {
171+
t.Errorf("failed to render stack template: %v", err)
172+
}
173+
})
174+
175+
t.Run("ValidateStack", func(t *testing.T) {
176+
stackTemplate, err := providedConfig.RenderStackTemplate(stackTemplateOptions)
177+
178+
if err != nil {
179+
t.Errorf("failed to render stack template: %v", err)
180+
}
181+
182+
cluster := cluster.New(providedConfig, true)
183+
s3URI, exists := os.LookupEnv("KUBE_AWS_S3_DIR_URI")
184+
185+
if !exists {
186+
t.Errorf("failed to obtain value for KUBE_AWS_S3_DIR_URI")
187+
t.FailNow()
188+
}
189+
190+
report, err := cluster.ValidateStack(string(stackTemplate), s3URI)
191+
192+
if err != nil {
193+
t.Errorf("failed to validate stack: %s", report)
194+
}
195+
})
196+
})
197+
})
198+
}
199+
}

0 commit comments

Comments
 (0)