Skip to content

Commit 56ba269

Browse files
committed
e2e: making sure there is no leftovers in the testing session
1 parent 425b4a7 commit 56ba269

File tree

4 files changed

+99
-3
lines changed

4 files changed

+99
-3
lines changed

scripts/ci-conformance.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ source "${REPO_ROOT}/hack/ensure-kind.sh"
3131
# shellcheck source=../hack/ensure-kubectl.sh
3232
source "${REPO_ROOT}/hack/ensure-kubectl.sh"
3333
# shellcheck source=../hack/ensure-kustomize.sh
34-
source "${REPO_ROOT}/hack/ensure-kustomize.sh"
34+
source "${REPO_ROOT}/hack/ensure-kustomize.sh"
3535

3636
random-string() {
3737
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w ${1:-32} | head -n 1

scripts/ci-e2e.sh

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,4 @@ export REGISTRY=e2e
3939
make test-e2e
4040
test_status="${?}"
4141

42-
# TODO last chance to clean up resources if prow job leaves something behind
43-
4442
exit "${test_status}"

test/e2e/azure_suite_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"context"
2424
"fmt"
2525
"io/ioutil"
26+
"log"
2627
"os"
2728
"path"
2829
"path/filepath"
@@ -42,6 +43,7 @@ import (
4243
infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha3"
4344
"sigs.k8s.io/cluster-api-provider-azure/test/e2e/auth"
4445
"sigs.k8s.io/cluster-api-provider-azure/test/e2e/generators"
46+
"sigs.k8s.io/cluster-api-provider-azure/test/e2e/utils"
4547
capiv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
4648
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
4749
bootstrapv1 "sigs.k8s.io/cluster-api/bootstrap/kubeadm/api/v1alpha3"
@@ -271,6 +273,7 @@ func writeLogs(mgmt *kind.Cluster, namespace, deploymentName, logDir string) err
271273

272274
func ensureCAPZArtifactsDeleted(input *ControlPlaneClusterInput) {
273275
input.SetDefaults()
276+
groupName := input.Management.GetName()
274277

275278
mgmtClient, err := input.Management.GetClient()
276279
Expect(err).NotTo(HaveOccurred(), "stack: %+v", err)
@@ -286,4 +289,15 @@ func ensureCAPZArtifactsDeleted(input *ControlPlaneClusterInput) {
286289
Expect(c.List(ctx, &clusters)).NotTo(HaveOccurred())
287290
return clusters.Items
288291
}, input.DeleteTimeout, 20*time.Second).Should(HaveLen(0))
292+
293+
By("Making sure there are all Azure resources are deleted")
294+
_, err = utils.GetGroup(ctx, creds, groupName)
295+
if err == nil {
296+
log.Printf("resource group %s still exist, cleaning\n", groupName)
297+
err = utils.CleanupE2EResources(ctx, creds, groupName)
298+
if err != nil {
299+
log.Printf("failed to delete the group %s: %s\n", groupName, err.Error())
300+
return
301+
}
302+
}
289303
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Copyright 2020 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 utils
18+
19+
import (
20+
"context"
21+
"log"
22+
23+
"sigs.k8s.io/cluster-api-provider-azure/test/e2e/auth"
24+
25+
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2019-05-01/resources"
26+
"github.com/Azure/go-autorest/autorest"
27+
azureAuth "github.com/Azure/go-autorest/autorest/azure/auth"
28+
)
29+
30+
// GetGroup gets the resource group if exist
31+
func GetGroup(ctx context.Context, creds auth.Creds, resourceName string) (resources.Group, error) {
32+
groupClient, err := getGroupsClient(creds)
33+
if err != nil {
34+
return resources.Group{}, err
35+
}
36+
return groupClient.Get(ctx, resourceName)
37+
}
38+
39+
// CleanupE2EResources deletes the resource group created during the testing
40+
func CleanupE2EResources(ctx context.Context, creds auth.Creds, resourceName string) error {
41+
groupClient, err := getGroupsClient(creds)
42+
if err != nil {
43+
return err
44+
}
45+
46+
deleteGroupFuture, err := groupClient.Delete(ctx, resourceName)
47+
if err != nil {
48+
log.Printf("failed to delete group %s: %s\n", resourceName, err.Error())
49+
}
50+
51+
autorestClient := autorest.Client{
52+
PollingDelay: autorest.DefaultPollingDelay,
53+
PollingDuration: autorest.DefaultPollingDuration,
54+
RetryAttempts: autorest.DefaultRetryAttempts,
55+
RetryDuration: autorest.DefaultRetryDuration,
56+
Authorizer: groupClient.Authorizer,
57+
}
58+
59+
log.Println("waiting for the group deletion")
60+
61+
err = deleteGroupFuture.WaitForCompletionRef(context.Background(), autorestClient)
62+
if err != nil {
63+
log.Printf("failed to wait for deletion %s\n", err.Error())
64+
return err
65+
}
66+
67+
log.Printf("group %s deleted\n", resourceName)
68+
return nil
69+
}
70+
71+
func getGroupsClient(creds auth.Creds) (resources.GroupsClient, error) {
72+
groupsClient := resources.NewGroupsClient(creds.SubscriptionID)
73+
a, err := getAuthorizer(creds)
74+
if err != nil {
75+
return resources.GroupsClient{}, err
76+
}
77+
groupsClient.Authorizer = a
78+
return groupsClient, nil
79+
}
80+
81+
func getAuthorizer(creds auth.Creds) (autorest.Authorizer, error) {
82+
credsConfig := azureAuth.NewClientCredentialsConfig(creds.ClientID, creds.ClientSecret, creds.TenantID)
83+
return credsConfig.Authorizer()
84+
}

0 commit comments

Comments
 (0)