forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationtestharness.go
250 lines (213 loc) · 7.97 KB
/
integrationtestharness.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/*
Copyright 2017 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package testutils
import (
"io/ioutil"
"os"
"path"
"path/filepath"
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/service/route53"
"k8s.io/klog"
kopsroot "k8s.io/kops"
"k8s.io/kops/cloudmock/aws/mockautoscaling"
"k8s.io/kops/cloudmock/aws/mockec2"
"k8s.io/kops/cloudmock/aws/mockelb"
"k8s.io/kops/cloudmock/aws/mockelbv2"
"k8s.io/kops/cloudmock/aws/mockiam"
"k8s.io/kops/cloudmock/aws/mockroute53"
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/pki"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
"k8s.io/kops/util/pkg/vfs"
)
type IntegrationTestHarness struct {
TempDir string
T *testing.T
// The original kops DefaultChannelBase value, restored on Close
originalDefaultChannelBase string
// originalKopsVersion is the original kops.Version value, restored on Close
originalKopsVersion string
// originalPKIDefaultPrivateKeySize is the saved pki.DefaultPrivateKeySize value, restored on Close
originalPKIDefaultPrivateKeySize int
}
func NewIntegrationTestHarness(t *testing.T) *IntegrationTestHarness {
h := &IntegrationTestHarness{}
tempDir, err := ioutil.TempDir("", "test")
if err != nil {
t.Fatalf("failed to create temp dir: %v", err)
}
h.TempDir = tempDir
vfs.Context.ResetMemfsContext(true)
// Generate much smaller keys, as this is often the bottleneck for tests
h.originalPKIDefaultPrivateKeySize = pki.DefaultPrivateKeySize
pki.DefaultPrivateKeySize = 512
// Replace the default channel path with a local filesystem path, so we don't try to retrieve it from a server
{
channelPath, err := filepath.Abs(path.Join("../../channels/"))
if err != nil {
t.Fatalf("error resolving stable channel path: %v", err)
}
channelPath += "/"
h.originalDefaultChannelBase = kops.DefaultChannelBase
// Make sure any platform-specific separators that aren't /, are converted to / for use in a file: protocol URL
kops.DefaultChannelBase = "file://" + filepath.ToSlash(channelPath)
}
return h
}
func (h *IntegrationTestHarness) Close() {
if h.TempDir != "" {
if os.Getenv("KEEP_TEMP_DIR") != "" {
klog.Infof("NOT removing temp directory, because KEEP_TEMP_DIR is set: %s", h.TempDir)
} else {
err := os.RemoveAll(h.TempDir)
if err != nil {
h.T.Fatalf("failed to remove temp dir %q: %v", h.TempDir, err)
}
}
}
if h.originalKopsVersion != "" {
kopsroot.Version = h.originalKopsVersion
}
if h.originalDefaultChannelBase != "" {
kops.DefaultChannelBase = h.originalDefaultChannelBase
}
if h.originalPKIDefaultPrivateKeySize != 0 {
pki.DefaultPrivateKeySize = h.originalPKIDefaultPrivateKeySize
}
}
func (h *IntegrationTestHarness) SetupMockAWS() *awsup.MockAWSCloud {
cloud := awsup.InstallMockAWSCloud("us-test-1", "abc")
mockEC2 := &mockec2.MockEC2{}
cloud.MockEC2 = mockEC2
mockRoute53 := &mockroute53.MockRoute53{}
cloud.MockRoute53 = mockRoute53
mockELB := &mockelb.MockELB{}
cloud.MockELB = mockELB
mockELBV2 := &mockelbv2.MockELBV2{}
cloud.MockELBV2 = mockELBV2
mockIAM := &mockiam.MockIAM{}
cloud.MockIAM = mockIAM
mockAutoscaling := &mockautoscaling.MockAutoscaling{}
cloud.MockAutoscaling = mockAutoscaling
mockRoute53.MockCreateZone(&route53.HostedZone{
Id: aws.String("/hostedzone/Z1AFAKE1ZON3YO"),
Name: aws.String("example.com."),
Config: &route53.HostedZoneConfig{
PrivateZone: aws.Bool(false),
},
}, nil)
mockRoute53.MockCreateZone(&route53.HostedZone{
Id: aws.String("/hostedzone/Z2AFAKE1ZON3NO"),
Name: aws.String("internal.example.com."),
Config: &route53.HostedZoneConfig{
PrivateZone: aws.Bool(true),
},
}, []*route53.VPC{{
VPCId: aws.String("vpc-23456789"),
}})
mockRoute53.MockCreateZone(&route53.HostedZone{
Id: aws.String("/hostedzone/Z3AFAKE1ZOMORE"),
Name: aws.String("private.example.com."),
Config: &route53.HostedZoneConfig{
PrivateZone: aws.Bool(true),
},
}, []*route53.VPC{{
VPCId: aws.String("vpc-12345678"),
}})
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
CreationDate: aws.String("2016-10-21T20:07:19.000Z"),
ImageId: aws.String("ami-12345678"),
Name: aws.String("k8s-1.4-debian-jessie-amd64-hvm-ebs-2016-10-21"),
OwnerId: aws.String(awsup.WellKnownAccountKopeio),
RootDeviceName: aws.String("/dev/xvda"),
})
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
CreationDate: aws.String("2017-01-09T17:08:27.000Z"),
ImageId: aws.String("ami-15000000"),
Name: aws.String("k8s-1.5-debian-jessie-amd64-hvm-ebs-2017-01-09"),
OwnerId: aws.String(awsup.WellKnownAccountKopeio),
RootDeviceName: aws.String("/dev/xvda"),
})
mockEC2.Images = append(mockEC2.Images, &ec2.Image{
CreationDate: aws.String("2019-08-06T00:00:00.000Z"),
ImageId: aws.String("ami-11400000"),
Name: aws.String("k8s-1.14-debian-stretch-amd64-hvm-ebs-2019-08-16"),
OwnerId: aws.String(awsup.WellKnownAccountKopeio),
RootDeviceName: aws.String("/dev/xvda"),
})
mockEC2.CreateVpcWithId(&ec2.CreateVpcInput{
CidrBlock: aws.String("172.20.0.0/16"),
}, "vpc-12345678")
mockEC2.CreateInternetGateway(&ec2.CreateInternetGatewayInput{})
mockEC2.AttachInternetGateway(&ec2.AttachInternetGatewayInput{
InternetGatewayId: aws.String("igw-1"),
VpcId: aws.String("vpc-12345678"),
})
mockEC2.CreateRouteTableWithId(&ec2.CreateRouteTableInput{
VpcId: aws.String("vpc-12345678"),
}, "rtb-12345678")
mockEC2.CreateSubnetWithId(&ec2.CreateSubnetInput{
VpcId: aws.String("vpc-12345678"),
AvailabilityZone: aws.String("us-test-1a"),
CidrBlock: aws.String("172.20.32.0/19"),
}, "subnet-12345678")
mockEC2.AssociateRouteTable(&ec2.AssociateRouteTableInput{
RouteTableId: aws.String("rtb-12345678"),
SubnetId: aws.String("subnet-12345678"),
})
mockEC2.CreateSubnetWithId(&ec2.CreateSubnetInput{
VpcId: aws.String("vpc-12345678"),
AvailabilityZone: aws.String("us-test-1a"),
CidrBlock: aws.String("172.20.4.0/22"),
}, "subnet-abcdef")
mockEC2.CreateSubnetWithId(&ec2.CreateSubnetInput{
VpcId: aws.String("vpc-12345678"),
AvailabilityZone: aws.String("us-test-1b"),
CidrBlock: aws.String("172.20.8.0/22"),
}, "subnet-b2345678")
mockEC2.AssociateRouteTable(&ec2.AssociateRouteTableInput{
RouteTableId: aws.String("rtb-12345678"),
SubnetId: aws.String("subnet-abcdef"),
})
mockEC2.AllocateAddressWithId(&ec2.AllocateAddressInput{
Address: aws.String("123.45.67.8"),
}, "eipalloc-12345678")
mockEC2.CreateNatGatewayWithId(&ec2.CreateNatGatewayInput{
SubnetId: aws.String("subnet-12345678"),
AllocationId: aws.String("eipalloc-12345678"),
}, "nat-a2345678")
mockEC2.AllocateAddressWithId(&ec2.AllocateAddressInput{
Address: aws.String("2.22.22.22"),
}, "eipalloc-b2345678")
mockEC2.CreateNatGatewayWithId(&ec2.CreateNatGatewayInput{
SubnetId: aws.String("subnet-b2345678"),
AllocationId: aws.String("eipalloc-b2345678"),
}, "nat-b2345678")
return cloud
}
// SetupMockGCE configures a mock GCE cloud provider
func (h *IntegrationTestHarness) SetupMockGCE() {
gce.InstallMockGCECloud("us-test1", "testproject")
}
// MockKopsVersion will set the kops version to the specified value, until Close is called
func (h *IntegrationTestHarness) MockKopsVersion(version string) {
if h.originalKopsVersion != "" {
h.T.Fatalf("MockKopsVersion called twice (%s and %s)", version, h.originalKopsVersion)
}
h.originalKopsVersion = kopsroot.Version
kopsroot.Version = version
}