-
Notifications
You must be signed in to change notification settings - Fork 67
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Ensure resource cleanup and add integration test for invalid credentials #449
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,10 +17,14 @@ package infrastructure | |
import ( | ||
"context" | ||
"encoding/json" | ||
"errors" | ||
"flag" | ||
"path/filepath" | ||
"time" | ||
|
||
gardencorev1beta1 "github.com/gardener/gardener/pkg/apis/core/v1beta1" | ||
gardencorev1beta1helper "github.com/gardener/gardener/pkg/apis/core/v1beta1/helper" | ||
|
||
"github.com/aliyun/alibaba-cloud-sdk-go/services/ecs" | ||
"github.com/aliyun/alibaba-cloud-sdk-go/services/vpc" | ||
extensionsv1alpha1 "github.com/gardener/gardener/pkg/apis/extensions/v1alpha1" | ||
|
@@ -127,17 +131,15 @@ var _ = AfterSuite(func() { | |
mgrCancel() | ||
}() | ||
|
||
By("running cleanup actions") | ||
framework.RunCleanupActions() | ||
|
||
By("stopping test environment") | ||
Expect(testEnv.Stop()).To(Succeed()) | ||
}) | ||
|
||
var _ = Describe("Infrastructure tests", func() { | ||
|
||
Context("with infrastructure that requests new vpc (networks.vpc.cidr)", func() { | ||
AfterEach(func() { | ||
framework.RunCleanupActions() | ||
}) | ||
|
||
It("should successfully create and delete", func() { | ||
providerConfig := newProviderConfig(&alicloudv1alpha1.VPC{ | ||
CIDR: pointer.StringPtr(vpcCIDR), | ||
|
@@ -149,61 +151,143 @@ var _ = Describe("Infrastructure tests", func() { | |
}) | ||
|
||
Context("with infrastructure that requests existing vpc", func() { | ||
var ( | ||
identifiers infrastructureIdentifiers | ||
) | ||
It("should successfully create and delete", func() { | ||
identifiers := prepareVPC(ctx, clientFactory, *region, vpcCIDR, natGatewayCIDR) | ||
EmoinLanyu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
framework.AddCleanupAction(func() { | ||
cleanupVPC(ctx, clientFactory, identifiers) | ||
shaoyongfeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}) | ||
|
||
BeforeEach(func() { | ||
identifiers = prepareVPC(ctx, clientFactory, *region, vpcCIDR, natGatewayCIDR) | ||
}) | ||
providerConfig := newProviderConfig(&alicloudv1alpha1.VPC{ | ||
ID: identifiers.vpcID, | ||
}, availabilityZone) | ||
|
||
AfterEach(func() { | ||
framework.RunCleanupActions() | ||
cleanupVPC(ctx, clientFactory, identifiers) | ||
err := runTest(ctx, logger, c, providerConfig, decoder, clientFactory) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}) | ||
}) | ||
|
||
It("should successfully create and delete", func() { | ||
Context("with invalid credentials", func() { | ||
shaoyongfeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
It("should fail creation but succeed deletion", func() { | ||
providerConfig := newProviderConfig(&alicloudv1alpha1.VPC{ | ||
ID: identifiers.vpcID, | ||
CIDR: pointer.StringPtr(vpcCIDR), | ||
}, availabilityZone) | ||
|
||
err := runTest(ctx, logger, c, providerConfig, decoder, clientFactory) | ||
var ( | ||
namespace *corev1.Namespace | ||
cluster *extensionsv1alpha1.Cluster | ||
infra *extensionsv1alpha1.Infrastructure | ||
err error | ||
) | ||
|
||
framework.AddCleanupAction(func() { | ||
By("cleaning up namespace and cluster") | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, namespace))).To(Succeed()) | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, cluster))).To(Succeed()) | ||
}) | ||
|
||
defer func() { | ||
By("delete infrastructure") | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, infra))).To(Succeed()) | ||
|
||
By("wait until infrastructure is deleted") | ||
// deletion should succeed even though creation failed with invalid credentials (no-op) | ||
err := extensions.WaitUntilExtensionObjectDeleted( | ||
ctx, | ||
c, | ||
logger, | ||
infra, | ||
extensionsv1alpha1.InfrastructureResource, | ||
10*time.Second, | ||
30*time.Minute, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
}() | ||
|
||
By("create namespace for test execution") | ||
namespace = &corev1.Namespace{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "provider-alicloud-test-", | ||
}, | ||
} | ||
Expect(c.Create(ctx, namespace)).To(Succeed()) | ||
|
||
By("deploy invalid cloudprovider secret into namespace") | ||
secret := &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: secretName, | ||
Namespace: namespace.Name, | ||
}, | ||
Data: map[string][]byte{ | ||
alicloud.AccessKeyID: []byte("invalid"), | ||
alicloud.AccessKeySecret: []byte("fake"), | ||
}, | ||
} | ||
Expect(c.Create(ctx, secret)).To(Succeed()) | ||
|
||
By("create cluster which contains information of shoot info. It is used for encrypted image testing") | ||
cluster, err = newCluster(namespace.Name) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(c.Create(ctx, cluster)).To(Succeed()) | ||
|
||
By("create infrastructure") | ||
infra, err = newInfrastructure(namespace.Name, providerConfig) | ||
Expect(err).NotTo(HaveOccurred()) | ||
Expect(c.Create(ctx, infra)).To(Succeed()) | ||
|
||
By("wait until infrastructure creation has failed") | ||
err = extensions.WaitUntilExtensionObjectReady( | ||
ctx, | ||
c, | ||
logger, | ||
infra, | ||
extensionsv1alpha1.InfrastructureResource, | ||
10*time.Second, | ||
30*time.Second, | ||
5*time.Minute, | ||
nil, | ||
) | ||
Expect(err).To(MatchError(ContainSubstring("Specified access key is not found"))) | ||
var errorWithCode *gardencorev1beta1helper.ErrorWithCodes | ||
Expect(errors.As(err, &errorWithCode)).To(BeTrue()) | ||
Expect(errorWithCode.Codes()).To(ConsistOf(gardencorev1beta1.ErrorInfraUnauthenticated, gardencorev1beta1.ErrorConfigurationProblem)) | ||
}) | ||
}) | ||
}) | ||
|
||
func runTest(ctx context.Context, logger *logrus.Entry, c client.Client, providerConfig *alicloudv1alpha1.InfrastructureConfig, decoder runtime.Decoder, clientFactory alicloudclient.ClientFactory) error { | ||
var ( | ||
infra *extensionsv1alpha1.Infrastructure | ||
namespace *corev1.Namespace | ||
cluster *extensionsv1alpha1.Cluster | ||
infra *extensionsv1alpha1.Infrastructure | ||
infrastructureIdentifiers infrastructureIdentifiers | ||
err error | ||
) | ||
|
||
var cleanupHandle framework.CleanupActionHandle | ||
cleanupHandle = framework.AddCleanupAction(func() { | ||
framework.AddCleanupAction(func() { | ||
By("cleaning up namespace and cluster") | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, namespace))).To(Succeed()) | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, cluster))).To(Succeed()) | ||
}) | ||
|
||
defer func() { | ||
shaoyongfeng marked this conversation as resolved.
Show resolved
Hide resolved
|
||
By("delete infrastructure") | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, infra))).To(Succeed()) | ||
|
||
By("wait until infrastructure is deleted") | ||
err := extensions.WaitUntilExtensionObjectDeleted( | ||
ctx, c, logger, | ||
ctx, | ||
c, | ||
logger, | ||
infra, | ||
"Infrastructure", | ||
10*time.Second, 30*time.Minute, | ||
extensionsv1alpha1.InfrastructureResource, | ||
10*time.Second, | ||
30*time.Minute, | ||
) | ||
Expect(err).NotTo(HaveOccurred()) | ||
|
||
By("verify infrastructure deletion") | ||
verifyDeletion(ctx, clientFactory, infrastructureIdentifiers) | ||
|
||
Expect(client.IgnoreNotFound(c.Delete(ctx, namespace))).To(Succeed()) | ||
Expect(client.IgnoreNotFound(c.Delete(ctx, cluster))).To(Succeed()) | ||
|
||
framework.RemoveCleanupAction(cleanupHandle) | ||
}) | ||
}() | ||
|
||
By("create namespace for test execution") | ||
namespace = &corev1.Namespace{ | ||
|
@@ -259,10 +343,15 @@ func runTest(ctx context.Context, logger *logrus.Entry, c client.Client, provide | |
|
||
By("wait until infrastructure is created") | ||
if err := extensions.WaitUntilExtensionObjectReady( | ||
ctx, c, logger, | ||
ctx, | ||
c, | ||
logger, | ||
infra, | ||
"Infrastucture", | ||
10*time.Second, 30*time.Second, 16*time.Minute, nil, | ||
extensionsv1alpha1.InfrastructureResource, | ||
10*time.Second, | ||
30*time.Second, | ||
16*time.Minute, | ||
nil, | ||
); err != nil { | ||
return err | ||
} | ||
|
@@ -535,6 +624,7 @@ func prepareVPC(ctx context.Context, clientFactory alicloudclient.ClientFactory, | |
vpcClient, err := clientFactory.NewVPCClient(region, *accessKeyID, *accessKeySecret) | ||
Expect(err).NotTo(HaveOccurred()) | ||
createVpcReq := vpc.CreateCreateVpcRequest() | ||
createVpcReq.VpcName = "provider-alicloud-infra-test" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what will happen if there are several testing running in parallel? will this name cause any conflict? |
||
createVpcReq.CidrBlock = vpcCIDR | ||
createVpcReq.RegionId = region | ||
createVPCsResp, err := vpcClient.CreateVpc(createVpcReq) | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it mean all namespaces generated during testing are only be cleaned at the end of testing? Is it expected behavior?