Skip to content

Commit 90e4726

Browse files
committed
Update delete instancegroup tests
1 parent 580a596 commit 90e4726

2 files changed

Lines changed: 85 additions & 84 deletions

File tree

pkg/instancegroups/delete_test.go

Lines changed: 48 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ package instancegroups
1919
import (
2020
"context"
2121
"testing"
22-
"time"
2322

2423
"github.com/stretchr/testify/assert"
2524
"github.com/stretchr/testify/require"
@@ -33,22 +32,12 @@ import (
3332
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
3433
)
3534

36-
func TestDeleteInstanceGroup_GCEWaitOnInstanceDeletion(t *testing.T) {
35+
func TestDeleteInstanceGroupGCE_WaitOnInstanceDeletion(t *testing.T) {
3736
h := testutils.NewIntegrationTestHarness(t)
3837
defer h.Close()
3938

40-
gce.PollingInterval = 5 * time.Millisecond
41-
defer func() {
42-
gce.PollingInterval = 5 * time.Second
43-
}()
44-
4539
clusterName := "test.k8s.io"
46-
cloud := h.SetupMockGCE()
47-
48-
zones, err := cloud.Zones()
49-
require.NoError(t, err)
50-
require.NotEmpty(t, zones)
51-
zone := zones[0]
40+
cloud, zone := getGCETestSetup(t, h)
5241

5342
ctx := context.Background()
5443
f := util.NewFactory(&util.FactoryOptions{
@@ -59,6 +48,7 @@ func TestDeleteInstanceGroup_GCEWaitOnInstanceDeletion(t *testing.T) {
5948

6049
clientset, err := f.KopsClient()
6150
require.NoError(t, err)
51+
6252
_, err = clientset.CreateCluster(ctx, cluster)
6353
require.NoError(t, err)
6454

@@ -67,34 +57,20 @@ func TestDeleteInstanceGroup_GCEWaitOnInstanceDeletion(t *testing.T) {
6757
_, err = clientset.InstanceGroupsFor(cluster).Create(context.TODO(), &ig, metav1.CreateOptions{})
6858
require.NoError(t, err)
6959

70-
templateName := "test-template"
71-
templateURL := "https://www.googleapis.com/compute/v1/projects/testproject/global/instanceTemplates/" + templateName
60+
migName := gce.NameForInstanceGroupManager(clusterName, ig.Name, zone)
7261

73-
migName := gce.NameForInstanceGroupManager(clusterName, "nodes-1", zone)
62+
instanceTemplate := instanceTemplate("test-template", clusterName)
63+
_, err = cloud.Compute().InstanceTemplates().Insert(cloud.Project(),
64+
instanceTemplate)
65+
require.NoError(t, err)
7466

7567
_, err = cloud.Compute().InstanceGroupManagers().Insert(cloud.Project(), zone, &compute.InstanceGroupManager{
7668
Name: migName,
77-
InstanceTemplate: templateURL,
69+
InstanceTemplate: instanceTemplate.SelfLink,
7870
Zone: zone,
7971
})
8072
require.NoError(t, err)
8173

82-
_, err = cloud.Compute().InstanceTemplates().Insert(cloud.Project(),
83-
&compute.InstanceTemplate{
84-
Name: templateName,
85-
Properties: &compute.InstanceProperties{
86-
Metadata: &compute.Metadata{
87-
Items: []*compute.MetadataItems{
88-
{
89-
Key: "cluster-name",
90-
Value: fi.PtrTo(clusterName),
91-
},
92-
},
93-
},
94-
},
95-
})
96-
require.NoError(t, err)
97-
9874
d := &DeleteInstanceGroup{
9975
Cluster: cluster,
10076
Cloud: cloud,
@@ -121,22 +97,12 @@ func TestDeleteInstanceGroup_GCEWaitOnInstanceDeletion(t *testing.T) {
12197
assert.Len(t, instanceTemplates, 0)
12298
}
12399

124-
func TestDeleteInstanceGroup(t *testing.T) {
100+
func TestDeleteInstanceGroupGCE(t *testing.T) {
125101
h := testutils.NewIntegrationTestHarness(t)
126102
defer h.Close()
127103

128-
gce.PollingInterval = 5 * time.Millisecond
129-
defer func() {
130-
gce.PollingInterval = 5 * time.Second
131-
}()
132-
133104
clusterName := "test.k8s.io"
134-
cloud := h.SetupMockGCE()
135-
136-
zones, err := cloud.Zones()
137-
require.NoError(t, err)
138-
require.NotEmpty(t, zones)
139-
zone := zones[0]
105+
cloud, zone := getGCETestSetup(t, h)
140106

141107
ctx := context.Background()
142108
f := util.NewFactory(&util.FactoryOptions{
@@ -152,31 +118,13 @@ func TestDeleteInstanceGroup(t *testing.T) {
152118

153119
ig := testutils.BuildMinimalNodeInstanceGroup("nodes-1")
154120

155-
templateName := "test-template"
156-
templateURL := "https://www.googleapis.com/compute/v1/projects/testproject/global/instanceTemplates/" + templateName
157-
158-
migName := gce.NameForInstanceGroupManager(clusterName, "nodes-1", zone)
159-
160-
template := &compute.InstanceTemplate{
161-
Name: templateName,
162-
Properties: &compute.InstanceProperties{
163-
Metadata: &compute.Metadata{
164-
Items: []*compute.MetadataItems{
165-
{
166-
Key: "cluster-name",
167-
Value: &clusterName,
168-
},
169-
},
170-
},
171-
},
172-
}
173-
121+
template := instanceTemplate("test-template", clusterName)
174122
_, err = cloud.Compute().InstanceTemplates().Insert(cloud.Project(), template)
175123
assert.NoError(t, err, "error creating InstanceTemplate")
176124

177125
igm := &compute.InstanceGroupManager{
178-
Name: migName,
179-
InstanceTemplate: templateURL,
126+
Name: gce.NameForInstanceGroupManager(clusterName, ig.Name, zone),
127+
InstanceTemplate: template.SelfLink,
180128
Zone: zone,
181129
}
182130

@@ -200,15 +148,14 @@ func TestDeleteInstanceGroup(t *testing.T) {
200148
assert.True(t, errors.IsNotFound(err), "unexpected error when getting deleted instance group: %v", err)
201149
}
202150

203-
func TestDeleteInstanceGroup_MissingInstance(t *testing.T) {
151+
func TestDeleteInstanceGroupGCE_MissingInstance(t *testing.T) {
204152
h := testutils.NewIntegrationTestHarness(t)
205153
defer h.Close()
206154

207155
clusterName := "test.k8s.io"
208-
209-
cloud := h.SetupMockGCE()
210-
156+
cloud, _ := getGCETestSetup(t, h)
211157
ctx := context.Background()
158+
212159
f := util.NewFactory(&util.FactoryOptions{
213160
RegistryPath: "memfs://tests",
214161
})
@@ -220,20 +167,7 @@ func TestDeleteInstanceGroup_MissingInstance(t *testing.T) {
220167
_, err = clientset.CreateCluster(ctx, cluster)
221168
assert.NoError(t, err, "error creating cluster")
222169

223-
template := &compute.InstanceTemplate{
224-
Name: "test-template",
225-
Properties: &compute.InstanceProperties{
226-
Metadata: &compute.Metadata{
227-
Items: []*compute.MetadataItems{
228-
{
229-
Key: "cluster-name",
230-
Value: &clusterName,
231-
},
232-
},
233-
},
234-
},
235-
}
236-
170+
template := instanceTemplate("test-template", clusterName)
237171
_, err = cloud.Compute().InstanceTemplates().Insert(cloud.Project(), template)
238172
assert.NoError(t, err, "error creating InstanceTemplate")
239173

@@ -277,3 +211,33 @@ func TestDeleteInstanceGroup_MissingInstance(t *testing.T) {
277211
assert.NoError(t, err)
278212
assert.Len(t, groups, 0)
279213
}
214+
215+
func instanceTemplate(name, clusterName string) *compute.InstanceTemplate {
216+
templateURL := "https://www.googleapis.com/compute/v1/projects/testproject/global/instanceTemplates/" + name
217+
218+
return &compute.InstanceTemplate{
219+
Name: name,
220+
SelfLink: templateURL,
221+
Properties: &compute.InstanceProperties{
222+
Metadata: &compute.Metadata{
223+
Items: []*compute.MetadataItems{
224+
{
225+
Key: "cluster-name",
226+
Value: &clusterName,
227+
},
228+
},
229+
},
230+
},
231+
}
232+
}
233+
234+
func getGCETestSetup(t *testing.T, h *testutils.IntegrationTestHarness) (gce.GCECloud, string) {
235+
cloud := h.SetupMockGCE()
236+
237+
zones, err := cloud.Zones()
238+
require.NoError(t, err)
239+
require.NotEmpty(t, zones)
240+
zone := zones[0]
241+
242+
return cloud, zone
243+
}

pkg/instancegroups/main_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
Copyright The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package instancegroups
18+
19+
import (
20+
"os"
21+
"testing"
22+
"time"
23+
24+
"k8s.io/klog/v2"
25+
"k8s.io/kops/upup/pkg/fi/cloudup/gce"
26+
)
27+
28+
// TestMain executes the tests for this package
29+
func TestMain(m *testing.M) {
30+
gce.PollingInterval = 5 * time.Millisecond
31+
32+
klog.InitFlags(nil)
33+
exitCode := m.Run()
34+
35+
gce.PollingInterval = 5 * time.Second
36+
os.Exit(exitCode)
37+
}

0 commit comments

Comments
 (0)