Skip to content

Commit

Permalink
Added Secret tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ross-p-smith committed Oct 11, 2023
1 parent 29fbcdb commit 0d5fda8
Show file tree
Hide file tree
Showing 2 changed files with 1,278 additions and 257 deletions.
78 changes: 70 additions & 8 deletions v2/internal/controllers/crd_apimanagement_20220801_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"testing"

. "github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
"sigs.k8s.io/controller-runtime/pkg/client"

apim "github.com/Azure/azure-service-operator/v2/api/apimanagement/v1api20220801"
Expand All @@ -21,15 +22,15 @@ func Test_ApiManagement_20220801_CRUD(t *testing.T) {
t.Parallel()

if *isLive {
t.Skip("skipping test in live mode as it takes a very long time to provision an APIM service (1+h)")
t.Skip("skipping test in live mode as it takes a very long time to provision an APIM service (1+h)")
}

tc := globalTestContext.ForTest(t)

// If you don't want to delete the resource group at the end of the test as APIM
// takes a long time to provision; comment out the delete lines at the end of the test.
rg := tc.NewTestResourceGroup()
tc.CreateResourceAndWait(rg)
tc.CreateResourceAndWaitWithoutCleanup(rg)

// There will be a New v2 SKU released 5/10/2023 which will have a much quicker start up
// time. Move to that when it's available (BasicV2 or StandardV2 SKU)
Expand All @@ -48,7 +49,7 @@ func Test_ApiManagement_20220801_CRUD(t *testing.T) {
PublisherEmail: to.Ptr("[email protected]"),
PublisherName: to.Ptr("ASOTesting"),
Sku: &sku,
// Restore: to.Ptr(true),
Restore: to.Ptr(true),
},
}

Expand Down Expand Up @@ -111,8 +112,8 @@ func Test_ApiManagement_20220801_CRUD(t *testing.T) {

// Whilst developing APIM tests, you can comment these two lines
// out to keep the APIM instance
tc.DeleteResourceAndWait(&service)
tc.DeleteResourceAndWait(rg)
// tc.DeleteResourceAndWait(&service)
// tc.DeleteResourceAndWait(rg)
}

func APIM_Subscription_CRUD(tc *testcommon.KubePerTestContext, service client.Object) {
Expand All @@ -128,9 +129,34 @@ func APIM_Subscription_CRUD(tc *testcommon.KubePerTestContext, service client.Ob

tc.T.Log("creating apim subscriptions")
tc.CreateResourceAndWait(&subscription)
defer tc.DeleteResourceAndWait(&subscription)

tc.Expect(subscription.Status).ToNot(BeNil())
tc.Expect(subscription.Status.Id).ToNot(BeNil())

// The subscription will have been created and it would have generated it's own
// PrimaryKey and SecondaryKey. Now let's see if we can set them from a Kubernetes secret.

// There should be no secrets at this point
secretList := &v1.SecretList{}
tc.ListResources(secretList, client.InNamespace(tc.Namespace))
tc.Expect(secretList.Items).To(HaveLen(0))

// Run sub-tests on subscription
tc.RunSubtests(
testcommon.Subtest{
Name: "SecretsWrittenToSameKubeSecret",
Test: func(tc *testcommon.KubePerTestContext) {
Subscription_SecretsWrittenToSameKubeSecret(tc, &subscription)
},
},
testcommon.Subtest{
Name: "SecretsWrittenToDifferentKubeSecrets",
Test: func(tc *testcommon.KubePerTestContext) {
Subscription_SecretsWrittenToDifferentKubeSecrets(tc, &subscription)
},
},
)

defer tc.DeleteResourceAndWait(&subscription)

tc.T.Log("cleaning up subscription")
}
Expand Down Expand Up @@ -309,4 +335,40 @@ func APIM_Api_CRUD(tc *testcommon.KubePerTestContext, service client.Object) {
tc.Expect(api.Status).ToNot(BeNil())

tc.T.Log("cleaning up api")
}
}

func Subscription_SecretsWrittenToSameKubeSecret(tc *testcommon.KubePerTestContext, subscription *apim.Subscription) {
old := subscription.DeepCopy()
subscriptionSecret := "storagekeys"
subscription.Spec.OperatorSpec = &apim.SubscriptionOperatorSpec{
Secrets: &apim.SubscriptionOperatorSecrets{
PrimaryKey: &genruntime.SecretDestination{Name: subscriptionSecret, Key: "primary"},
SecondaryKey: &genruntime.SecretDestination{Name: subscriptionSecret, Key: "secondary"},
},
}
tc.PatchResourceAndWait(old, subscription)

tc.ExpectSecretHasKeys(subscriptionSecret, "primary", "secondary")
}

func Subscription_SecretsWrittenToDifferentKubeSecrets(tc *testcommon.KubePerTestContext, subscription *apim.Subscription) {
old := subscription.DeepCopy()
primaryKeySecret := "secret1"
secondaryKeySecret := "secret2"

subscription.Spec.OperatorSpec = &apim.SubscriptionOperatorSpec{
Secrets: &apim.SubscriptionOperatorSecrets{
PrimaryKey: &genruntime.SecretDestination{
Name: primaryKeySecret,
Key: "primarymasterkey",
},
SecondaryKey: &genruntime.SecretDestination{
Name: secondaryKeySecret,
Key: "secondarymasterkey",
},
},
}
tc.PatchResourceAndWait(old, subscription)

tc.ExpectSecretHasKeys(primaryKeySecret, "primarymasterkey")
tc.ExpectSecretHasKeys(secondaryKeySecret, "secondarymasterkey")}
Loading

0 comments on commit 0d5fda8

Please sign in to comment.