From d0aff6c8a1f5fd17b57148c9cff5276599e40ece Mon Sep 17 00:00:00 2001 From: jananivMS Date: Mon, 20 Apr 2020 23:29:23 -0600 Subject: [PATCH 1/4] test cleanup --- api/v1alpha1/storageaccount_types.go | 3 +- controllers/apimgmt_controller_test.go | 26 +-- controllers/appinsights_controller_test.go | 25 +-- ...edatalakegen2filesystem_controller_test.go | 4 +- controllers/azuresqlaction_controller_test.go | 16 +- .../azuresqldatabase_controller_test.go | 45 +--- .../azuresqlfailovergroup_controller_test.go | 24 +- .../azuresqlfirewallrule_controller_test.go | 28 +-- controllers/azuresqlserver_controller_test.go | 29 +-- controllers/azuresqluser_controller_test.go | 49 +---- controllers/azurevnetrule_controller_test.go | 33 +-- controllers/blobcontainer_controller_test.go | 116 ++-------- controllers/consumergroup_controller_test.go | 84 ------- ...namespace_consumergroup_controller_test.go | 104 +++++++++ ...venthub_storageaccount_controller_test.go} | 205 ++++++++---------- .../eventhubnamespace_controller_test.go | 124 ++--------- controllers/helpers.go | 61 +++++- controllers/keyvault_controller_test.go | 61 +----- controllers/rediscache_controller_test.go | 39 +--- controllers/resourcegroup_controller_test.go | 11 +- controllers/storageaccount_controller_test.go | 43 ---- controllers/suite_test.go | 126 +++++------ .../storages/blobcontainer/blob_container.go | 4 - .../blobcontainer/blob_container_reconcile.go | 1 + .../storageaccount_reconcile.go | 3 + 25 files changed, 392 insertions(+), 872 deletions(-) delete mode 100644 controllers/consumergroup_controller_test.go create mode 100644 controllers/eventhub_namespace_consumergroup_controller_test.go rename controllers/{eventhub_controller_test.go => eventhub_storageaccount_controller_test.go} (57%) delete mode 100644 controllers/storageaccount_controller_test.go diff --git a/api/v1alpha1/storageaccount_types.go b/api/v1alpha1/storageaccount_types.go index 41ba60b368c..99696d8b3fe 100644 --- a/api/v1alpha1/storageaccount_types.go +++ b/api/v1alpha1/storageaccount_types.go @@ -51,7 +51,7 @@ type StorageAccountSkuName string // Only one of the following kinds may be specified. // If none of the following kinds is specified, the default one // is StorageV2. -// +kubebuilder:validation:Enum=BlobStorage;BlockBlobStorage;FileStorage;StorageAccount;StorageV2 +// +kubebuilder:validation:Enum=BlobStorage;BlockBlobStorage;FileStorage;Storage;StorageV2 type StorageAccountKind string // StorageAccountAccessTier enumerates the values for access tier. @@ -90,7 +90,6 @@ type StorageAccountAdditionalResources struct { } // +kubebuilder:object:root=true -// +kubebuilder:subresource:status // StorageAccountList contains a list of Storage type StorageAccountList struct { diff --git a/controllers/apimgmt_controller_test.go b/controllers/apimgmt_controller_test.go index d9781435e1f..21d9410b2f6 100644 --- a/controllers/apimgmt_controller_test.go +++ b/controllers/apimgmt_controller_test.go @@ -7,21 +7,17 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" "github.com/Azure/azure-service-operator/pkg/helpers" - "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAPIMgmtController(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // rgName := tc.resourceGroupName rgLocation := "southcentralus" @@ -50,25 +46,7 @@ func TestAPIMgmtController(t *testing.T) { }, }, } + EnsureInstance(ctx, t, tc, apiMgmtInstance) - err := tc.k8sClient.Create(ctx, apiMgmtInstance) - assert.Equal(nil, err, "create APIMgmtAPI record in k8s") - - APIMgmtNamespacedName := types.NamespacedName{Name: apiMgmtName, Namespace: "default"} - - // Wait for the APIMgmtAPI instance to be written to k8s - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, APIMgmtNamespacedName, apiMgmtInstance) - return strings.Contains(apiMgmtInstance.Status.Message, successMsg) - }, tc.timeout, tc.retry, "awaiting APIMgmt instance creation") - - // Delete the service - err = tc.k8sClient.Delete(ctx, apiMgmtInstance) - assert.Equal(nil, err, "deleting APIMgmt in k8s") - - // Wait for the APIMgmtAPI instance to be deleted - assert.Eventually(func() bool { - err := tc.k8sClient.Get(ctx, APIMgmtNamespacedName, apiMgmtInstance) - return err != nil - }, tc.timeout, tc.retry, "awaiting APIMgmtInstance deletion") + EnsureDelete(ctx, t, tc, apiMgmtInstance) } diff --git a/controllers/appinsights_controller_test.go b/controllers/appinsights_controller_test.go index aa4c19f15f9..6d9498fd571 100644 --- a/controllers/appinsights_controller_test.go +++ b/controllers/appinsights_controller_test.go @@ -7,20 +7,16 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAppInsightsController(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) rgName := tc.resourceGroupName rgLocation := tc.resourceGroupLocation @@ -40,24 +36,7 @@ func TestAppInsightsController(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, appInsightsInstance) - assert.Equal(nil, err, "create appinsights record in k8s") + EnsureInstance(ctx, t, tc, appInsightsInstance) - appInsightsNamespacedName := types.NamespacedName{Name: appInsightsName, Namespace: "default"} - - // Wait for the AppInsights instance to be provisioned - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, appInsightsNamespacedName, appInsightsInstance) - return strings.Contains(appInsightsInstance.Status.Message, successMsg) - }, tc.timeout, tc.retry, "awaiting appinsights instance creation") - - // Delete the service - err = tc.k8sClient.Delete(ctx, appInsightsInstance) - assert.Equal(nil, err, "deleting appinsights in k8s") - - // Wait for the AppInsights instance to be deleted - assert.Eventually(func() bool { - err := tc.k8sClient.Get(ctx, appInsightsNamespacedName, appInsightsInstance) - return err != nil - }, tc.timeout, tc.retry, "awaiting appInsightsInstance deletion") + EnsureDelete(ctx, t, tc, appInsightsInstance) } diff --git a/controllers/azuredatalakegen2filesystem_controller_test.go b/controllers/azuredatalakegen2filesystem_controller_test.go index f3b2fdc978d..0eb87b83118 100644 --- a/controllers/azuredatalakegen2filesystem_controller_test.go +++ b/controllers/azuredatalakegen2filesystem_controller_test.go @@ -18,7 +18,7 @@ import ( "k8s.io/apimachinery/pkg/types" ) -func TestADLSFilesystemControllerNoResourceGroup(t *testing.T) { +/*func TestADLSFilesystemControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) assert := assert.New(t) @@ -67,7 +67,7 @@ func TestADLSFilesystemControllerNoResourceGroup(t *testing.T) { return apierrors.IsNotFound(err) }, tc.timeout, tc.retry, "wait for filesystem to be gone") -} +}*/ func TestADLSFilesystemControllerNoStorageAccount(t *testing.T) { t.Parallel() diff --git a/controllers/azuresqlaction_controller_test.go b/controllers/azuresqlaction_controller_test.go index 544a5a08e6d..efaa06c3a77 100644 --- a/controllers/azuresqlaction_controller_test.go +++ b/controllers/azuresqlaction_controller_test.go @@ -51,22 +51,12 @@ func RunSQLActionHappy(t *testing.T, server string) { }, } - err := tc.k8sClient.Create(ctx, sqlActionInstance) - assert.Equal(nil, err, "create sqlaction in k8s") - - sqlActionInstanceNamespacedName := types.NamespacedName{Name: sqlActionName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlActionInstanceNamespacedName, sqlActionInstance) - return sqlActionInstance.Status.Provisioned - }, tc.timeout, tc.retry, "wait for sql action to be submitted") - - // TODO Check SQL Database credentials + EnsureInstance(ctx, t, tc, sqlActionInstance) // makre sure credentials are not the same as previous secretAfter := &v1.Secret{} assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, types.NamespacedName{Name: server, Namespace: "default"}, secretAfter) + err := tc.k8sClient.Get(ctx, types.NamespacedName{Name: server, Namespace: "default"}, secretAfter) if err != nil { return false } @@ -75,4 +65,6 @@ func RunSQLActionHappy(t *testing.T, server string) { assert.Equal(secret.Data["username"], secretAfter.Data["username"], "username should still be the same") assert.NotEqual(string(secret.Data["password"]), string(secretAfter.Data["password"]), "password should have changed") + + EnsureDelete(ctx, t, tc, sqlActionInstance) } diff --git a/controllers/azuresqldatabase_controller_test.go b/controllers/azuresqldatabase_controller_test.go index 5150b4efeca..7601267edb3 100644 --- a/controllers/azuresqldatabase_controller_test.go +++ b/controllers/azuresqldatabase_controller_test.go @@ -7,23 +7,18 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" "github.com/Azure/azure-service-operator/pkg/errhelp" - "github.com/stretchr/testify/assert" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSqlDatabaseControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add any setup steps that needs to be executed before each test rgLocation := tc.resourceGroupLocation @@ -43,32 +38,15 @@ func TestAzureSqlDatabaseControllerNoResourceGroup(t *testing.T) { Edition: 0, }, } + EnsureInstanceWithResult(ctx, t, tc, sqlDatabaseInstance, errhelp.ResourceGroupNotFoundErrorCode, false) - err := tc.k8sClient.Create(ctx, sqlDatabaseInstance) - assert.Equal(nil, err, "create db in k8s") - - sqlDatabaseNamespacedName := types.NamespacedName{Name: sqlDatabaseName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlDatabaseNamespacedName, sqlDatabaseInstance) - return strings.Contains(sqlDatabaseInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for rg not found error") - - err = tc.k8sClient.Delete(ctx, sqlDatabaseInstance) - assert.Equal(nil, err, "delete db in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlDatabaseNamespacedName, sqlDatabaseInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for resource not found error") - + EnsureDelete(ctx, t, tc, sqlDatabaseInstance) } func TestAzureSqlDatabaseControllerNoServer(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add any setup steps that needs to be executed before each test rgName := tc.resourceGroupName @@ -90,23 +68,8 @@ func TestAzureSqlDatabaseControllerNoServer(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, sqlDatabaseInstance) - assert.Equal(false, apierrors.IsInvalid(err), "create db resource") - assert.Equal(nil, err, "create db in k8s") - - sqlDatabaseNamespacedName := types.NamespacedName{Name: sqlDatabaseName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlDatabaseNamespacedName, sqlDatabaseInstance) - return strings.Contains(sqlDatabaseInstance.Status.Message, errhelp.ParentNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for rg not found error") - - err = tc.k8sClient.Delete(ctx, sqlDatabaseInstance) - assert.Equal(nil, err, "delete db in k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlDatabaseInstance, errhelp.ParentNotFoundErrorCode, false) - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlDatabaseNamespacedName, sqlDatabaseInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for resource not found error") + EnsureDelete(ctx, t, tc, sqlDatabaseInstance) } diff --git a/controllers/azuresqlfailovergroup_controller_test.go b/controllers/azuresqlfailovergroup_controller_test.go index b20a26a6dd2..23248104150 100644 --- a/controllers/azuresqlfailovergroup_controller_test.go +++ b/controllers/azuresqlfailovergroup_controller_test.go @@ -7,30 +7,24 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" "github.com/Azure/azure-service-operator/pkg/errhelp" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSqlFailoverGroupControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) var rgName string var rgLocation1 string var sqlServerOneName string var sqlServerTwoName string var sqlDatabaseName string - var err error // Add any setup steps that needs to be executed before each test rgName = tc.resourceGroupName @@ -58,21 +52,7 @@ func TestAzureSqlFailoverGroupControllerNoResourceGroup(t *testing.T) { }, } - err = tc.k8sClient.Create(ctx, sqlFailoverGroupInstance) - assert.Equal(nil, err, "create failovergroup in k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlFailoverGroupInstance, errhelp.ResourceGroupNotFoundErrorCode, false) - sqlFailoverGroupNamespacedName := types.NamespacedName{Name: sqlFailoverGroupName, Namespace: "default"} - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlFailoverGroupNamespacedName, sqlFailoverGroupInstance) - return strings.Contains(sqlFailoverGroupInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for rg not found error to clear") - - err = tc.k8sClient.Delete(ctx, sqlFailoverGroupInstance) - assert.Equal(nil, err, "delete failovergroup in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlFailoverGroupNamespacedName, sqlFailoverGroupInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for failovergroup to be gone from k8s") + EnsureDelete(ctx, t, tc, sqlFailoverGroupInstance) } diff --git a/controllers/azuresqlfirewallrule_controller_test.go b/controllers/azuresqlfirewallrule_controller_test.go index a1b54a99a1b..eebbe717721 100644 --- a/controllers/azuresqlfirewallrule_controller_test.go +++ b/controllers/azuresqlfirewallrule_controller_test.go @@ -7,23 +7,18 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" "github.com/Azure/azure-service-operator/pkg/errhelp" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSqlFirewallRuleControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add any setup steps that needs to be executed before each test //rgName := tc.resourceGroupName @@ -44,27 +39,8 @@ func TestAzureSqlFirewallRuleControllerNoResourceGroup(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, sqlFirewallRuleInstance) - assert.Equal(nil, err, "create sqlfirewallrule in k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlFirewallRuleInstance, errhelp.ResourceGroupNotFoundErrorCode, false) - sqlFirewallRuleNamespacedName := types.NamespacedName{Name: sqlFirewallRuleName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlFirewallRuleNamespacedName, sqlFirewallRuleInstance) - return HasFinalizer(sqlFirewallRuleInstance, finalizerName) - }, tc.timeout, tc.retry, "wait for firewallrule to have finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlFirewallRuleNamespacedName, sqlFirewallRuleInstance) - return strings.Contains(sqlFirewallRuleInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for firewallrule to have rg not found error") - - err = tc.k8sClient.Delete(ctx, sqlFirewallRuleInstance) - assert.Equal(nil, err, "delete sqlfirewallrule in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlFirewallRuleNamespacedName, sqlFirewallRuleInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for firewallrule to be gone from k8s") + EnsureDelete(ctx, t, tc, sqlFirewallRuleInstance) } diff --git a/controllers/azuresqlserver_controller_test.go b/controllers/azuresqlserver_controller_test.go index 9eb84b99949..ecedcbe46a0 100644 --- a/controllers/azuresqlserver_controller_test.go +++ b/controllers/azuresqlserver_controller_test.go @@ -7,16 +7,11 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" - "github.com/Azure/azure-service-operator/pkg/errhelp" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSqlServerControllerNoResourceGroup(t *testing.T) { @@ -39,27 +34,7 @@ func TestAzureSqlServerControllerNoResourceGroup(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, sqlServerInstance) - assert.Equal(nil, err, "create sql server in k8s") - - sqlServerNamespacedName := types.NamespacedName{Name: sqlServerName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlServerNamespacedName, sqlServerInstance) - return HasFinalizer(sqlServerInstance, finalizerName) - }, tc.timeout, tc.retry, "wait for finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlServerNamespacedName, sqlServerInstance) - return strings.Contains(sqlServerInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for rg error") - - err = tc.k8sClient.Delete(ctx, sqlServerInstance) - assert.Equal(nil, err, "delete sql server in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlServerNamespacedName, sqlServerInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for server to be gone") + EnsureInstanceWithResult(ctx, t, tc, sqlServerInstance, errhelp.ResourceGroupNotFoundErrorCode, false) + EnsureDelete(ctx, t, tc, sqlServerInstance) } diff --git a/controllers/azuresqluser_controller_test.go b/controllers/azuresqluser_controller_test.go index ba58fb5ac12..2c5b0f99558 100644 --- a/controllers/azuresqluser_controller_test.go +++ b/controllers/azuresqluser_controller_test.go @@ -48,30 +48,9 @@ func TestAzureSQLUserControllerNoAdminSecret(t *testing.T) { }, } - // Create the sqlUser - err = tc.k8sClient.Create(ctx, sqlUser) - assert.Equal(nil, err, "create db user in k8s") - - sqlUserNamespacedName := types.NamespacedName{Name: username, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return HasFinalizer(sqlUser, finalizerName) - }, tc.timeout, tc.retry, "wait for finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return strings.Contains(sqlUser.Status.Message, "admin secret") - }, tc.timeout, tc.retry, "wait for missing admin secret message") - - err = tc.k8sClient.Delete(ctx, sqlUser) - assert.Equal(nil, err, "delete db user in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for user to be gone from k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlUser, "admin secret", false) + EnsureDelete(ctx, t, tc, sqlUser) } func TestAzureSQLUserControllerNoResourceGroup(t *testing.T) { @@ -120,28 +99,8 @@ func TestAzureSQLUserControllerNoResourceGroup(t *testing.T) { }, } - // Create the sqlUser - err = tc.k8sClient.Create(ctx, sqlUser) - assert.Equal(nil, err, "create db user in k8s") - - sqlUserNamespacedName := types.NamespacedName{Name: username, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return HasFinalizer(sqlUser, finalizerName) - }, tc.timeout, tc.retry, "wait for finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return strings.Contains(sqlUser.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for rg fail message") - - err = tc.k8sClient.Delete(ctx, sqlUser) - assert.Equal(nil, err, "delete db user in k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlUser, errhelp.ResourceGroupNotFoundErrorCode, false) - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlUserNamespacedName, sqlUser) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for user to be gone from k8s") + EnsureDelete(ctx, t, tc, sqlUser) } diff --git a/controllers/azurevnetrule_controller_test.go b/controllers/azurevnetrule_controller_test.go index e4f7509b63e..a32f4007d9e 100644 --- a/controllers/azurevnetrule_controller_test.go +++ b/controllers/azurevnetrule_controller_test.go @@ -45,36 +45,9 @@ func TestAzureSqlVNetRuleControllerNoResourceGroup(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, sqlVNetRuleInstance) - assert.Equal(nil, err, "create sqlvnetrule in k8s") - - sqlVNETRuleNamespacedName := types.NamespacedName{Name: sqlVNetRuleName, Namespace: "default"} - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlVNETRuleNamespacedName, sqlVNetRuleInstance) - if err == nil { - return HasFinalizer(sqlVNetRuleInstance, finalizerName) - } else { - return false - } - }, tc.timeout, tc.retry, "wait for sqlvnetrule to have finalizer") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlVNETRuleNamespacedName, sqlVNetRuleInstance) - if err == nil { - return strings.Contains(sqlVNetRuleInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - } else { - return false - } - }, tc.timeout, tc.retry, "wait for sqlvnetrule to have rg not found error") - - err = tc.k8sClient.Delete(ctx, sqlVNetRuleInstance) - assert.Equal(nil, err, "delete sqlvnetrule in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, sqlVNETRuleNamespacedName, sqlVNetRuleInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for sqlvnetrule to be gone from k8s") + EnsureInstanceWithResult(ctx, t, tc, sqlVNetRuleInstance, errhelp.ResourceGroupNotFoundErrorCode, false) + + EnsureDelete(ctx, t, tc, sqlVNetRuleInstance) } func RunAzureSqlVNetRuleHappyPath(t *testing.T, sqlServerName string, rgLocation string) { diff --git a/controllers/blobcontainer_controller_test.go b/controllers/blobcontainer_controller_test.go index b73a56455a5..7289729ea30 100644 --- a/controllers/blobcontainer_controller_test.go +++ b/controllers/blobcontainer_controller_test.go @@ -7,27 +7,22 @@ package controllers import ( "context" - "strings" "testing" s "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" + "github.com/Azure/go-autorest/autorest/to" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" "github.com/Azure/azure-service-operator/pkg/errhelp" helpers "github.com/Azure/azure-service-operator/pkg/helpers" - "github.com/stretchr/testify/assert" - - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestBlobContainerControlleNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) var rgLocation string var saName string @@ -35,71 +30,30 @@ func TestBlobContainerControlleNoResourceGroup(t *testing.T) { // Add any setup steps that needs to be executed before each test rgLocation = tc.resourceGroupLocation - saName = tc.storageAccountName + saName = GenerateAlphaNumTestResourceName("blobsa") containerAccessLevel = s.PublicAccessContainer - // Add Tests for OpenAPI validation (or additonal CRD features) specified in - // your API definition. - // Avoid adding tests for vanilla CRUD operations because they would - // test Kubernetes API server, which isn't the goal here. - blobContainerName := GenerateTestResourceNameWithRandom("bc", 10) resourceGroupName := GenerateTestResourceNameWithRandom("rg", 10) - var err error - - blobContainerInstance := &azurev1alpha1.BlobContainer{ + // Create Storage account + saInstance := &azurev1alpha1.StorageAccount{ ObjectMeta: metav1.ObjectMeta{ - Name: blobContainerName, + Name: saName, Namespace: "default", }, - Spec: azurev1alpha1.BlobContainerSpec{ - Location: rgLocation, - ResourceGroup: resourceGroupName, - AccountName: saName, - AccessLevel: containerAccessLevel, + Spec: azurev1alpha1.StorageAccountSpec{ + Location: tc.resourceGroupLocation, + ResourceGroup: tc.resourceGroupName, + Sku: azurev1alpha1.StorageAccountSku{ + Name: "Standard_RAGRS", + }, + Kind: "StorageV2", + AccessTier: "Hot", + EnableHTTPSTrafficOnly: to.BoolPtr(true), }, } - - err = tc.k8sClient.Create(ctx, blobContainerInstance) - assert.Equal(nil, err, "create blobcontainer in k8s") - - blobContainerNamespacedName := types.NamespacedName{Name: blobContainerName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, blobContainerNamespacedName, blobContainerInstance) - return strings.Contains(blobContainerInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for blob to have rg not found error") - - err = tc.k8sClient.Delete(ctx, blobContainerInstance) - assert.Equal(nil, err, "delete blobcontainer in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, blobContainerNamespacedName, blobContainerInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for blob to be not found") - -} - -func TestTestBlobContainerControllerNoStorageAccount(t *testing.T) { - t.Parallel() - defer PanicRecover(t) - ctx := context.Background() - assert := assert.New(t) - - var rgLocation string - var rgName string - var containerAccessLevel s.PublicAccess - - // Add any setup steps that needs to be executed before each test - rgLocation = tc.resourceGroupLocation - rgName = tc.resourceGroupName - containerAccessLevel = s.PublicAccessContainer - - blobContainerName := GenerateTestResourceNameWithRandom("bc", 10) - storageAccountName := helpers.FillWithRandom(GenerateAlphaNumTestResourceName("sa"), 24) - - var err error + EnsureInstance(ctx, t, tc, saInstance) blobContainerInstance := &azurev1alpha1.BlobContainer{ ObjectMeta: metav1.ObjectMeta{ @@ -108,54 +62,32 @@ func TestTestBlobContainerControllerNoStorageAccount(t *testing.T) { }, Spec: azurev1alpha1.BlobContainerSpec{ Location: rgLocation, - ResourceGroup: rgName, - AccountName: storageAccountName, + ResourceGroup: resourceGroupName, + AccountName: saName, AccessLevel: containerAccessLevel, }, } + EnsureInstanceWithResult(ctx, t, tc, blobContainerInstance, errhelp.ResourceGroupNotFoundErrorCode, false) - err = tc.k8sClient.Create(ctx, blobContainerInstance) - assert.Equal(nil, err, "create blob container in k8s") - - blobContainerNamespacedName := types.NamespacedName{Name: blobContainerName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, blobContainerNamespacedName, blobContainerInstance) - return strings.Contains(blobContainerInstance.Status.Message, errhelp.ParentNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for blob to have parent not found error") - - err = tc.k8sClient.Delete(ctx, blobContainerInstance) - assert.Equal(nil, err, "delete blob container in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, blobContainerNamespacedName, blobContainerInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for blob to be not found") - + EnsureDelete(ctx, t, tc, blobContainerInstance) } -func TestTestBlobContainerControllerHappyPath(t *testing.T) { +func TestTestBlobContainerControllerNoStorageAccount(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() var rgLocation string var rgName string - var saName string var containerAccessLevel s.PublicAccess // Add any setup steps that needs to be executed before each test rgLocation = tc.resourceGroupLocation rgName = tc.resourceGroupName - saName = tc.storageAccountName containerAccessLevel = s.PublicAccessContainer - // Add Tests for OpenAPI validation (or additonal CRD features) specified in - // your API definition. - // Avoid adding tests for vanilla CRUD operations because they would - // test Kubernetes API server, which isn't the goal here. - blobContainerName := GenerateTestResourceNameWithRandom("bc", 10) + storageAccountName := helpers.FillWithRandom(GenerateAlphaNumTestResourceName("sa"), 24) blobContainerInstance := &azurev1alpha1.BlobContainer{ ObjectMeta: metav1.ObjectMeta{ @@ -165,13 +97,11 @@ func TestTestBlobContainerControllerHappyPath(t *testing.T) { Spec: azurev1alpha1.BlobContainerSpec{ Location: rgLocation, ResourceGroup: rgName, - AccountName: saName, + AccountName: storageAccountName, AccessLevel: containerAccessLevel, }, } - - EnsureInstance(ctx, t, tc, blobContainerInstance) + EnsureInstanceWithResult(ctx, t, tc, blobContainerInstance, errhelp.ParentNotFoundErrorCode, false) EnsureDelete(ctx, t, tc, blobContainerInstance) - } diff --git a/controllers/consumergroup_controller_test.go b/controllers/consumergroup_controller_test.go deleted file mode 100644 index 8d701c9bb88..00000000000 --- a/controllers/consumergroup_controller_test.go +++ /dev/null @@ -1,84 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -// +build all consumergroup - -package controllers - -import ( - "context" - "net/http" - "testing" - - azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" - - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" -) - -func TestConsumerGroup(t *testing.T) { - t.Parallel() - assert := assert.New(t) - - var rgName string = tc.resourceGroupName - var ehnName string = tc.eventhubNamespaceName - var ehName string = tc.eventhubName - var ctx = context.Background() - defer PanicRecover(t) - - consumerGroupName := GenerateTestResourceNameWithRandom("cg", 10) - azureConsumerGroupName := consumerGroupName + "-azure" - - var err error - - // Create the consumer group object and expect the Reconcile to be created - consumerGroupInstance := &azurev1alpha1.ConsumerGroup{ - ObjectMeta: metav1.ObjectMeta{ - Name: consumerGroupName, - Namespace: "default", - }, - Spec: azurev1alpha1.ConsumerGroupSpec{ - Namespace: ehnName, - ResourceGroup: rgName, - Eventhub: ehName, - ConsumerGroupName: azureConsumerGroupName, - }, - } - - err = tc.k8sClient.Create(ctx, consumerGroupInstance) - assert.Equal(nil, err, "create consumergroup in k8s") - - consumerGroupNamespacedName := types.NamespacedName{Name: consumerGroupName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, consumerGroupNamespacedName, consumerGroupInstance) - return HasFinalizer(consumerGroupInstance, finalizerName) - }, tc.timeout, tc.retry, "wait for finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, consumerGroupNamespacedName, consumerGroupInstance) - return consumerGroupInstance.Status.Provisioned - }, tc.timeout, tc.retry, "wait for provision") - - assert.Eventually(func() bool { - cg, _ := tc.consumerGroupClient.GetConsumerGroup(ctx, rgName, ehnName, ehName, azureConsumerGroupName) - return cg.Name != nil && *cg.Name == azureConsumerGroupName && cg.Response.StatusCode == http.StatusOK - }, tc.timeout, tc.retry, "wait for consumergroup to exist in Azure") - - err = tc.k8sClient.Delete(ctx, consumerGroupInstance) - assert.Equal(nil, err, "delete consumergroup in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, consumerGroupNamespacedName, consumerGroupInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for consumergroup to be gone from k8s") - - assert.Eventually(func() bool { - cg, _ := tc.consumerGroupClient.GetConsumerGroup(ctx, rgName, ehnName, ehName, azureConsumerGroupName) - return cg.Response.StatusCode != http.StatusOK - }, tc.timeout, tc.retry, "wait for consumergroup to be gone from azure") - -} diff --git a/controllers/eventhub_namespace_consumergroup_controller_test.go b/controllers/eventhub_namespace_consumergroup_controller_test.go new file mode 100644 index 00000000000..7e50b7a5d84 --- /dev/null +++ b/controllers/eventhub_namespace_consumergroup_controller_test.go @@ -0,0 +1,104 @@ +// Copyright (c) Microsoft Corporation. +// Licensed under the MIT License. + +// +build all consumergroup eventhub eventhubnamespace + +package controllers + +import ( + "context" + "net/http" + "testing" + + azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" + "github.com/stretchr/testify/assert" + + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" +) + +func TestConsumerGroupEventHubAndNamespaceControllerHappy(t *testing.T) { + t.Parallel() + defer PanicRecover(t) + ctx := context.Background() + assert := assert.New(t) + + var rgName string = tc.resourceGroupName + var rgLocation string = tc.resourceGroupLocation + eventhubNamespaceName := GenerateTestResourceNameWithRandom("ns-dev-eh", 10) + + consumerGroupName := GenerateTestResourceNameWithRandom("cg", 10) + azureConsumerGroupName := consumerGroupName + "-azure" + + // Create the Eventhub namespace object and expect the Reconcile to be created + eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventhubNamespaceName, + Namespace: "default", + }, + Spec: azurev1alpha1.EventhubNamespaceSpec{ + Location: rgLocation, + ResourceGroup: rgName, + }, + } + + EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) + + eventhubName := GenerateTestResourceNameWithRandom("eh-cd", 10) + + // Create the EventHub object and expect the Reconcile to be created + eventhubInstance := &azurev1alpha1.Eventhub{ + ObjectMeta: metav1.ObjectMeta{ + Name: eventhubName, + Namespace: "default", + }, + Spec: azurev1alpha1.EventhubSpec{ + Location: rgLocation, + Namespace: eventhubNamespaceName, + ResourceGroup: rgName, + Properties: azurev1alpha1.EventhubProperties{ + MessageRetentionInDays: 7, + PartitionCount: 2, + }, + AuthorizationRule: azurev1alpha1.EventhubAuthorizationRule{ + Name: "RootManageSharedAccessKey", + Rights: []string{"Listen"}, + }, + }, + } + + // verify eventhub is created successfully + EnsureInstance(ctx, t, tc, eventhubInstance) + + // Create a consumer group instance and verify + consumerGroupInstance := &azurev1alpha1.ConsumerGroup{ + ObjectMeta: metav1.ObjectMeta{ + Name: consumerGroupName, + Namespace: "default", + }, + Spec: azurev1alpha1.ConsumerGroupSpec{ + Namespace: eventhubNamespaceName, + ResourceGroup: rgName, + Eventhub: eventhubName, + ConsumerGroupName: azureConsumerGroupName, + }, + } + EnsureInstance(ctx, t, tc, consumerGroupInstance) + + assert.Eventually(func() bool { + cg, _ := tc.consumerGroupClient.GetConsumerGroup(ctx, rgName, eventhubNamespaceName, eventhubName, azureConsumerGroupName) + return cg.Name != nil && *cg.Name == azureConsumerGroupName && cg.Response.StatusCode == http.StatusOK + }, tc.timeout, tc.retry, "wait for consumergroup to exist in Azure") + + EnsureDelete(ctx, t, tc, consumerGroupInstance) + + assert.Eventually(func() bool { + cg, _ := tc.consumerGroupClient.GetConsumerGroup(ctx, rgName, eventhubNamespaceName, eventhubName, azureConsumerGroupName) + return cg.Response.StatusCode != http.StatusOK + }, tc.timeout, tc.retry, "wait for consumergroup to be gone from azure") + + // verify eventhub is deleted + EnsureDelete(ctx, t, tc, eventhubInstance) + + // verify eventhubnamespace gets deleted + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) +} diff --git a/controllers/eventhub_controller_test.go b/controllers/eventhub_storageaccount_controller_test.go similarity index 57% rename from controllers/eventhub_controller_test.go rename to controllers/eventhub_storageaccount_controller_test.go index 3e2597eee1b..9fdbbf6bd66 100644 --- a/controllers/eventhub_controller_test.go +++ b/controllers/eventhub_storageaccount_controller_test.go @@ -1,33 +1,31 @@ // Copyright (c) Microsoft Corporation. // Licensed under the MIT License. -// +build all eventhub onlyeventhub +// +build all eventhub storage blobcontainer package controllers import ( "context" - "strings" + "fmt" "testing" + s "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/Azure/azure-service-operator/pkg/errhelp" - kvhelper "github.com/Azure/azure-service-operator/pkg/resourcemanager/keyvaults" - kvsecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault" + //"github.com/Azure/azure-service-operator/pkg/errhelp" + + //kvhelper "github.com/Azure/azure-service-operator/pkg/resourcemanager/keyvaults" + //kvsecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - - apierrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" ) func TestEventHubControllerNoNamespace(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add Tests for OpenAPI validation (or additonal CRD features) specified in // your API definition. @@ -53,96 +51,36 @@ func TestEventHubControllerNoNamespace(t *testing.T) { }, } - err := tc.k8sClient.Create(ctx, eventhubInstance) - assert.Equal(nil, err, "create eventhub in k8s") - - eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return strings.Contains(eventhubInstance.Status.Message, errhelp.ParentNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for eventhub to provision") - - err = tc.k8sClient.Delete(ctx, eventhubInstance) - assert.Equal(nil, err, "delete eventhub in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for eventHubInstance to be gone from k8s") + EnsureInstanceWithResult(ctx, t, tc, eventhubInstance, errhelp.ParentNotFoundErrorCode, false) + EnsureDelete(ctx, t, tc, eventhubInstance) } -func TestEventHubControllerCreateAndDelete(t *testing.T) { +func TestEventHubControllerCreateAndDeleteCustomSecret(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add any setup steps that needs to be executed before each test rgName := tc.resourceGroupName - ehnName := tc.eventhubNamespaceName - eventhubName := GenerateTestResourceNameWithRandom("eh-cd", 10) + rgLocation := tc.resourceGroupLocation + ehnName := GenerateTestResourceNameWithRandom("eh-ns", 10) + eventhubName := GenerateTestResourceNameWithRandom("eh-customsec", 10) + secretName := "secret-" + eventhubName - // Create the EventHub object and expect the Reconcile to be created - eventhubInstance := &azurev1alpha1.Eventhub{ + // Create EventhubNamespace instance as prereq + eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ ObjectMeta: metav1.ObjectMeta{ - Name: eventhubName, + Name: ehnName, Namespace: "default", }, - Spec: azurev1alpha1.EventhubSpec{ - Location: "westus", - Namespace: ehnName, + Spec: azurev1alpha1.EventhubNamespaceSpec{ + Location: rgLocation, ResourceGroup: rgName, - Properties: azurev1alpha1.EventhubProperties{ - MessageRetentionInDays: 7, - PartitionCount: 2, - }, - AuthorizationRule: azurev1alpha1.EventhubAuthorizationRule{ - Name: "RootManageSharedAccessKey", - Rights: []string{"Listen"}, - }, }, } - err := tc.k8sClient.Create(ctx, eventhubInstance) - assert.Equal(nil, err, "create eventhub in k8s") - - eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return eventhubInstance.HasFinalizer(finalizerName) - }, tc.timeout, tc.retry, "wait for eventhub to have finalizer") - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return strings.Contains(eventhubInstance.Status.Message, successMsg) - }, tc.timeout, tc.retry, "wait for eventhub to provision") - - err = tc.k8sClient.Delete(ctx, eventhubInstance) - assert.Equal(nil, err, "delete eventhub in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for eventHubInstance to be gone from k8s") - -} - -func TestEventHubControllerCreateAndDeleteCustomSecret(t *testing.T) { - t.Parallel() - defer PanicRecover(t) - ctx := context.Background() - assert := assert.New(t) - var err error - - // Add any setup steps that needs to be executed before each test - rgName := tc.resourceGroupName - rgLocation := tc.resourceGroupLocation - ehnName := tc.eventhubNamespaceName - eventhubName := GenerateTestResourceNameWithRandom("eh-customsec", 10) - secretName := "secret-" + eventhubName + EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) // Create the EventHub object and expect the Reconcile to be created eventhubInstance := &azurev1alpha1.Eventhub{ @@ -168,15 +106,11 @@ func TestEventHubControllerCreateAndDeleteCustomSecret(t *testing.T) { EnsureInstance(ctx, t, tc, eventhubInstance) - eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"} + EnsureSecretsWithValue(ctx, t, tc, eventhubInstance, tc.secretClient, secretName, eventhubInstance.Namespace, "eventhubName", eventhubName) - err = tc.k8sClient.Delete(ctx, eventhubInstance) - assert.Equal(nil, err, "delete eventhub in k8s") + EnsureDelete(ctx, t, tc, eventhubInstance) - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for eventHubInstance to be gone from k8s") + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) } func TestEventHubControllerCreateAndDeleteCustomKeyVault(t *testing.T) { @@ -188,7 +122,7 @@ func TestEventHubControllerCreateAndDeleteCustomKeyVault(t *testing.T) { // Add any setup steps that needs to be executed before each test rgName := tc.resourceGroupName rgLocation := tc.resourceGroupLocation - ehnName := tc.eventhubNamespaceName + ehnName := GenerateTestResourceNameWithRandom("eh-ns", 10) eventhubName := GenerateTestResourceNameWithRandom("ev", 10) keyVaultNameForSecrets := tc.keyvaultName @@ -196,6 +130,20 @@ func TestEventHubControllerCreateAndDeleteCustomKeyVault(t *testing.T) { _, err := kvhelper.AzureKeyVaultManager.GetVault(ctx, rgName, keyVaultNameForSecrets) assert.Equal(nil, err, "wait for keyvault to be available") + // Create EventhubNamespace instance as prereq + eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: ehnName, + Namespace: "default", + }, + Spec: azurev1alpha1.EventhubNamespaceSpec{ + Location: rgLocation, + ResourceGroup: rgName, + }, + } + + EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) + // Create the EventHub object and expect the Reconcile to be created eventhubInstance := &azurev1alpha1.Eventhub{ ObjectMeta: metav1.ObjectMeta{ @@ -219,35 +167,76 @@ func TestEventHubControllerCreateAndDeleteCustomKeyVault(t *testing.T) { }, } - eventhubNamespacedName := types.NamespacedName{Name: eventhubName, Namespace: "default"} EnsureInstance(ctx, t, tc, eventhubInstance) // Check that the secret is added to KeyVault keyvaultSecretClient := kvsecrets.New(keyVaultNameForSecrets) - assert.Eventually(func() bool { - _, err = keyvaultSecretClient.Get(ctx, eventhubNamespacedName) - return err == nil - }, tc.timeout, tc.retry, "wait for secret to exist in keyvault") + EnsureSecrets(ctx, t, tc, eventhubInstance, keyvaultSecretClient, eventhubName, eventhubInstance.Namespace) EnsureDelete(ctx, t, tc, eventhubInstance) + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) } - -func TestEventHubControllerCreateAndDeleteCapture(t *testing.T) { +func TestEventHubCapture_StorageAccountAndBlob_Controllers(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() assert := assert.New(t) - var err error // Add any setup steps that needs to be executed before each test rgName := tc.resourceGroupName rgLocation := tc.resourceGroupLocation - ehnName := tc.eventhubNamespaceName - saName := tc.storageAccountName - bcName := tc.blobContainerName + ehnName := GenerateTestResourceNameWithRandom("eh-ns", 10) + saName := GenerateAlphaNumTestResourceName("ehsa") + bcName := GenerateTestResourceNameWithRandom("ehblob", 10) eventHubName := GenerateTestResourceNameWithRandom("eh-capture", 10) + containerAccessLevel := s.PublicAccessContainer + + // Create Eventhub namespace + eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ + ObjectMeta: metav1.ObjectMeta{ + Name: ehnName, + Namespace: "default", + }, + Spec: azurev1alpha1.EventhubNamespaceSpec{ + Location: rgLocation, + ResourceGroup: rgName, + }, + } + EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) + + // Create Storage account + saInstance := &azurev1alpha1.StorageAccount{ + ObjectMeta: metav1.ObjectMeta{ + Name: saName, + Namespace: "default", + }, + Spec: azurev1alpha1.StorageAccountSpec{ + Location: rgLocation, + ResourceGroup: rgName, + Sku: azurev1alpha1.StorageAccountSku{ + Name: "Standard_LRS", + }, + Kind: "Storage", + }, + } + EnsureInstance(ctx, t, tc, saInstance) + + // Create blob container + blobContainerInstance := &azurev1alpha1.BlobContainer{ + ObjectMeta: metav1.ObjectMeta{ + Name: bcName, + Namespace: "default", + }, + Spec: azurev1alpha1.BlobContainerSpec{ + Location: rgLocation, + ResourceGroup: rgName, + AccountName: saName, + AccessLevel: containerAccessLevel, + }, + } + EnsureInstance(ctx, t, tc, blobContainerInstance) // Create the EventHub object and expect the Reconcile to be created eventhubInstance := &azurev1alpha1.Eventhub{ @@ -290,8 +279,6 @@ func TestEventHubControllerCreateAndDeleteCapture(t *testing.T) { EnsureInstance(ctx, t, tc, eventhubInstance) - eventHubNamespacedName := types.NamespacedName{Name: eventHubName, Namespace: "default"} - assert.Eventually(func() bool { hub, _ := tc.eventhubClient.GetHub(ctx, rgName, ehnName, eventHubName) if hub.Properties == nil || hub.CaptureDescription == nil || hub.CaptureDescription.Enabled == nil { @@ -300,12 +287,8 @@ func TestEventHubControllerCreateAndDeleteCapture(t *testing.T) { return *hub.CaptureDescription.Enabled }, tc.timeout, tc.retry, "wait for eventhub capture check") - err = tc.k8sClient.Delete(ctx, eventhubInstance) - assert.Equal(nil, err, "delete eventhub in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, eventHubNamespacedName, eventhubInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for eventHubInstance to be gone from k8s") - + EnsureDelete(ctx, t, tc, eventhubInstance) + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) + EnsureDelete(ctx, t, tc, blobContainerInstance) + EnsureDelete(ctx, t, tc, saInstance) } diff --git a/controllers/eventhubnamespace_controller_test.go b/controllers/eventhubnamespace_controller_test.go index 1a0ad692757..ffc3817d710 100644 --- a/controllers/eventhubnamespace_controller_test.go +++ b/controllers/eventhubnamespace_controller_test.go @@ -12,11 +12,8 @@ import ( azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" "github.com/Azure/azure-service-operator/pkg/errhelp" config "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" - "github.com/stretchr/testify/assert" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestEventHubNamespaceControllerNoResourceGroup(t *testing.T) { @@ -26,12 +23,6 @@ func TestEventHubNamespaceControllerNoResourceGroup(t *testing.T) { var rgLocation string rgLocation = tc.resourceGroupLocation - - // Add Tests for OpenAPI validation (or additonal CRD features) specified in - // your API definition. - // Avoid adding tests for vanilla CRUD operations because they would - // test Kubernetes API server, which isn't the goal here. - // setting this rg name tells the mocks to set a proper error resourceGroupName := "gone" eventhubNamespaceName := GenerateTestResourceNameWithRandom("ns-dev-eh", 10) @@ -51,10 +42,9 @@ func TestEventHubNamespaceControllerNoResourceGroup(t *testing.T) { EnsureInstanceWithResult(ctx, t, tc, eventhubNamespaceInstance, errhelp.ResourceGroupNotFoundErrorCode, false) EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) - } -func TestEventHubNamespaceControllerBasicTierNetworkRule(t *testing.T) { +func TestEventHubNamespaceControllerNetworkRules(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() @@ -70,7 +60,7 @@ func TestEventHubNamespaceControllerBasicTierNetworkRule(t *testing.T) { SubnetAddressPrefix: "110.1.0.0/16", } - // Create a VNET + // Create a VNET as prereq VNetInstance := &azurev1alpha1.VirtualNetwork{ ObjectMeta: metav1.ObjectMeta{ Name: VNetName, @@ -86,7 +76,7 @@ func TestEventHubNamespaceControllerBasicTierNetworkRule(t *testing.T) { EnsureInstance(ctx, t, tc, VNetInstance) - // Create EventhubNamespace network rule for this namespace and expect success + // Create EventhubNamespace network rule using the above VNET subnetID := "/subscriptions/" + config.SubscriptionID() + "/resourceGroups/" + rgName + "/providers/Microsoft.Network/virtualNetworks/" + VNetName + "/subnets/" + subnetName vnetRules := []azurev1alpha1.VirtualNetworkRules{ { @@ -103,8 +93,8 @@ func TestEventHubNamespaceControllerBasicTierNetworkRule(t *testing.T) { eventhubNamespaceName := GenerateTestResourceNameWithRandom("ns-dev-eh", 10) - // Create the Eventhub namespace object as prereq - eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ + // Add this to the Eventhubnamespace that is Basic Tier and expect to fail + eventhubNamespaceInstance1 := &azurev1alpha1.EventhubNamespace{ ObjectMeta: metav1.ObjectMeta{ Name: eventhubNamespaceName, Namespace: "default", @@ -125,99 +115,12 @@ func TestEventHubNamespaceControllerBasicTierNetworkRule(t *testing.T) { }, } - EnsureInstanceWithResult(ctx, t, tc, eventhubNamespaceInstance, errhelp.BadRequest, false) - - // Delete the namespace - EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) - -} - -func TestEventHubNamespaceControllerHappy(t *testing.T) { - t.Parallel() - defer PanicRecover(t) - ctx := context.Background() - assert := assert.New(t) - var err error - - var rgName string = tc.resourceGroupName - var rgLocation string = tc.resourceGroupLocation - eventhubNamespaceName := GenerateTestResourceNameWithRandom("ns-dev-eh", 10) - - // Create the Eventhub namespace object and expect the Reconcile to be created - eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ - ObjectMeta: metav1.ObjectMeta{ - Name: eventhubNamespaceName, - Namespace: "default", - }, - Spec: azurev1alpha1.EventhubNamespaceSpec{ - Location: rgLocation, - ResourceGroup: rgName, - }, - } - - EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) - - eventhubNamespacedName := types.NamespacedName{Name: eventhubNamespaceName, Namespace: "default"} - - err = tc.k8sClient.Delete(ctx, eventhubNamespaceInstance) - assert.Equal(nil, err, "delete eventhubns in k8s") - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, eventhubNamespacedName, eventhubNamespaceInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for eventHubnamespaceInstance to be gone from k8s") -} - -func TestEventHubNamespaceControllerHappyWithNetworkRule(t *testing.T) { - t.Parallel() - defer PanicRecover(t) - ctx := context.Background() - - var rgName string = tc.resourceGroupName - var rgLocation string = tc.resourceGroupLocation - // Create a VNET as prereq for the test - VNetName := GenerateTestResourceNameWithRandom("vnet", 10) - subnetName := "subnet-test" - VNetSubNetInstance := azurev1alpha1.VNetSubnets{ - SubnetName: subnetName, - SubnetAddressPrefix: "110.1.0.0/16", - } - - // Create a VNET - VNetInstance := &azurev1alpha1.VirtualNetwork{ - ObjectMeta: metav1.ObjectMeta{ - Name: VNetName, - Namespace: "default", - }, - Spec: azurev1alpha1.VirtualNetworkSpec{ - Location: rgLocation, - ResourceGroup: rgName, - AddressSpace: "110.0.0.0/8", - Subnets: []azurev1alpha1.VNetSubnets{VNetSubNetInstance}, - }, - } - - EnsureInstance(ctx, t, tc, VNetInstance) + EnsureInstanceWithResult(ctx, t, tc, eventhubNamespaceInstance1, errhelp.BadRequest, false) - // Create EventhubNamespace network rule for this namespace and expect success - subnetID := "/subscriptions/" + config.SubscriptionID() + "/resourceGroups/" + rgName + "/providers/Microsoft.Network/virtualNetworks/" + VNetName + "/subnets/" + subnetName - vnetRules := []azurev1alpha1.VirtualNetworkRules{ - { - SubnetID: subnetID, - IgnoreMissingServiceEndpoint: true, - }, - } - ipmask := "1.1.1.1" - ipRules := []azurev1alpha1.IPRules{ - { - IPMask: &ipmask, - }, - } - - eventhubNamespaceName := GenerateTestResourceNameWithRandom("ns-dev-eh", 10) + eventhubNamespaceName = GenerateTestResourceNameWithRandom("ns-dev-eh", 10) - // Create the Eventhub namespace object as prereq - eventhubNamespaceInstance := &azurev1alpha1.EventhubNamespace{ + // Add the network rule to a namespace that is Standard SKU and expect to pass + eventhubNamespaceInstance2 := &azurev1alpha1.EventhubNamespace{ ObjectMeta: metav1.ObjectMeta{ Name: eventhubNamespaceName, Namespace: "default", @@ -238,9 +141,14 @@ func TestEventHubNamespaceControllerHappyWithNetworkRule(t *testing.T) { }, } - EnsureInstance(ctx, t, tc, eventhubNamespaceInstance) + EnsureInstance(ctx, t, tc, eventhubNamespaceInstance2) // Delete the namespace - EnsureDelete(ctx, t, tc, eventhubNamespaceInstance) + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance2) + + // Delete the namespace + EnsureDelete(ctx, t, tc, eventhubNamespaceInstance1) + + EnsureDelete(ctx, t, tc, VNetInstance) } diff --git a/controllers/helpers.go b/controllers/helpers.go index 22153b36a2a..a542b41c9a0 100644 --- a/controllers/helpers.go +++ b/controllers/helpers.go @@ -43,16 +43,16 @@ import ( ) type TestContext struct { - k8sClient client.Client - secretClient secrets.SecretClient - resourceNamePrefix string - resourceGroupName string - resourceGroupLocation string - eventhubNamespaceName string - eventhubName string - namespaceLocation string - storageAccountName string - blobContainerName string + k8sClient client.Client + secretClient secrets.SecretClient + resourceNamePrefix string + resourceGroupName string + resourceGroupLocation string + //eventhubNamespaceName string + //eventhubName string + //namespaceLocation string + //storageAccountName string + //blobContainerName string keyvaultName string resourceGroupManager resourcegroupsresourcemanager.ResourceGroupManager redisCacheManager resourcemanagerrediscaches.RedisCacheManager @@ -299,6 +299,47 @@ func EnsureDelete(ctx context.Context, t *testing.T, tc TestContext, instance ru } +func EnsureSecrets(ctx context.Context, t *testing.T, tc TestContext, instance runtime.Object, secretclient secrets.SecretClient, secretname string, secretnamespace string) { + assert := assert.New(t) + typeOf := fmt.Sprintf("%T", instance) + + key := types.NamespacedName{Name: secretname, Namespace: secretnamespace} + + // Wait for secret + err := helpers.Retry(tc.timeoutFast, tc.retry, func() error { + + _, err := secretclient.Get(ctx, key) + if err != nil { + return fmt.Errorf("secret with name %s does not exist", key.String()) + } + return nil + }) + assert.Nil(err, "error waiting for %s to have secret", typeOf) + +} +func EnsureSecretsWithValue(ctx context.Context, t *testing.T, tc TestContext, instance runtime.Object, secretclient secrets.SecretClient, secretname string, secretnamespace string, secretkey string, secretvalue string) { + assert := assert.New(t) + typeOf := fmt.Sprintf("%T", instance) + + key := types.NamespacedName{Name: secretname, Namespace: secretnamespace} + + // Wait for secret + err := helpers.Retry(tc.timeoutFast, tc.retry, func() error { + + secrets, err := secretclient.Get(ctx, key) + if err != nil { + return err + } + if !strings.Contains(string(secrets[secretkey]), secretvalue) { + return fmt.Errorf("secret with key %s not equal to %s", secretname, secretvalue) + } + + return nil + }) + assert.Nil(err, "error waiting for %s to have correct secret", typeOf) + +} + func RequireInstance(ctx context.Context, t *testing.T, tc TestContext, instance runtime.Object) { RequireInstanceWithResult(ctx, t, tc, instance, successMsg, true) } diff --git a/controllers/keyvault_controller_test.go b/controllers/keyvault_controller_test.go index 1620b901c88..2398bf6ba87 100644 --- a/controllers/keyvault_controller_test.go +++ b/controllers/keyvault_controller_test.go @@ -254,36 +254,9 @@ func TestKeyvaultControllerInvalidName(t *testing.T) { }, } - // Create the Keyvault object and expect the Reconcile to be created - err := tc.k8sClient.Create(ctx, keyVaultInstance) - assert.Equal(nil, err, "create keyvault in k8s") - - // Prep query for get - keyVaultNamespacedName := types.NamespacedName{Name: keyVaultName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return HasFinalizer(keyVaultInstance, finalizerName) - }, tc.timeout, tc.retry, "wait for keyvault to have finalizer") - - // Verify you get the invalid name error - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return strings.Contains(keyVaultInstance.Status.Message, errhelp.AccountNameInvalid) - }, tc.timeout, tc.retry, "wait for invalid account name error") - - // delete key vault - err = tc.k8sClient.Delete(ctx, keyVaultInstance) - assert.Equal(nil, err, "delete keyvault in k8s") - - // verify key vault is gone from kubernetes - - assert.Eventually(func() bool { - err := tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for keyVaultInstance to be gone from k8s") + EnsureInstanceWithResult(ctx, t, tc, keyVaultInstance, errhelp.AccountNameInvalid, false) + EnsureDelete(ctx, t, tc, keyVaultInstance) } func TestKeyvaultControllerNoResourceGroup(t *testing.T) { @@ -308,35 +281,9 @@ func TestKeyvaultControllerNoResourceGroup(t *testing.T) { }, } - // Create the Keyvault object and expect the Reconcile to be created - err := tc.k8sClient.Create(ctx, keyVaultInstance) - assert.Equal(nil, err, "create keyvault in k8s") - - // Prep query for get - keyVaultNamespacedName := types.NamespacedName{Name: keyVaultName, Namespace: "default"} - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return HasFinalizer(keyVaultInstance, finalizerName) - }, tc.timeout, tc.retry, "wait for keyvault to have finalizer") - - // Verify you get the resource group not found error + EnsureInstanceWithResult(ctx, t, tc, keyVaultInstance, errhelp.ResourceGroupNotFoundErrorCode, false) - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return strings.Contains(keyVaultInstance.Status.Message, errhelp.ResourceGroupNotFoundErrorCode) - }, tc.timeout, tc.retry, "wait for ResourceGroupNotFound error") - - // delete key vault - err = tc.k8sClient.Delete(ctx, keyVaultInstance) - assert.Equal(nil, err, "delete keyvault in k8s") - - // verify key vault is gone from kubernetes - - assert.Eventually(func() bool { - err := tc.k8sClient.Get(ctx, keyVaultNamespacedName, keyVaultInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for keyVaultInstance to be gone from k8s") + EnsureDelete(ctx, t, tc, keyVaultInstance) } diff --git a/controllers/rediscache_controller_test.go b/controllers/rediscache_controller_test.go index 3946a682a66..1be7f6b5e0c 100644 --- a/controllers/rediscache_controller_test.go +++ b/controllers/rediscache_controller_test.go @@ -61,42 +61,11 @@ func TestRedisCacheControllerHappyPath(t *testing.T) { } // create rc - //EnsureInstance(ctx, t, tc, redisCacheInstance) + EnsureInstance(ctx, t, tc, redisCacheInstance) - err = tc.k8sClient.Create(ctx, redisCacheInstance) - assert.Equal(nil, err, "create redis cache in k8s") - - names := types.NamespacedName{Name: redisCacheInstance.GetName(), Namespace: redisCacheInstance.GetNamespace()} - - // Wait for first sql server to resolve - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, names, redisCacheInstance) - return HasFinalizer(redisCacheInstance, finalizerName) - }, tc.timeoutFast, tc.retry, fmt.Sprintf("wait for %s to have finalizer", "rediscache")) - - assert.Eventually(func() bool { - _ = tc.k8sClient.Get(ctx, names, redisCacheInstance) - return strings.Contains(redisCacheInstance.Status.Message, successMsg) && redisCacheInstance.Status.Provisioned == true - }, longRunningTimeout, tc.retry, fmt.Sprintf("wait for %s to provision", "rediscache")) - - //verify secret exists in k8s for rc - secret := &v1.Secret{} - assert.Eventually(func() bool { - log.Println("get secret") - err = tc.k8sClient.Get(ctx, types.NamespacedName{Name: redisCacheInstance.Name, Namespace: redisCacheInstance.Namespace}, secret) - if err == nil { - return true - } - return false - }, 45*time.Second, tc.retry, "wait for rc to have secret") + // verify secret exists in secretclient + EnsureSecrets(ctx, t, tc, redisCacheInstance, tc.SecretClient, redisCacheInstance.Name, redisCacheInstance.Namespace) // delete rc - err = tc.k8sClient.Delete(ctx, redisCacheInstance) - assert.Equal(nil, err, fmt.Sprintf("delete %s in k8s", "rediscache")) - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, names, redisCacheInstance) - return apierrors.IsNotFound(err) - }, longRunningTimeout, tc.retry, fmt.Sprintf("wait for %s to be gone from k8s", "rediscache")) - + EnsureDelete(ctx, t, tc, redisCacheInstance) } diff --git a/controllers/resourcegroup_controller_test.go b/controllers/resourcegroup_controller_test.go index 17bd5f84664..436b1f96a9c 100644 --- a/controllers/resourcegroup_controller_test.go +++ b/controllers/resourcegroup_controller_test.go @@ -45,22 +45,13 @@ func TestResourceGroupControllerHappyPath(t *testing.T) { resourceGroupNamespacedName := types.NamespacedName{Name: resourceGroupName, Namespace: "default"} // verify rg exists in azure - assert.Eventually(func() bool { _, err := tc.resourceGroupManager.CheckExistence(ctx, resourceGroupName) return err == nil }, tc.timeout, tc.retry, "wait for resourceGroupInstance to exist in azure") // delete rg - err = tc.k8sClient.Delete(ctx, resourceGroupInstance) - assert.Equal(nil, err, "delete rg in k8s") - - // verify rg is being deleted - - assert.Eventually(func() bool { - err = tc.k8sClient.Get(ctx, resourceGroupNamespacedName, resourceGroupInstance) - return apierrors.IsNotFound(err) - }, tc.timeout, tc.retry, "wait for resourceGroupInstance to be gone from k8s") + EnsureDelete(ctx, t, tc, resourceGroupInstance) assert.Eventually(func() bool { result, _ := tc.resourceGroupManager.CheckExistence(ctx, resourceGroupName) diff --git a/controllers/storageaccount_controller_test.go b/controllers/storageaccount_controller_test.go deleted file mode 100644 index 6b82b83af33..00000000000 --- a/controllers/storageaccount_controller_test.go +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) Microsoft Corporation. -// Licensed under the MIT License. - -// +build all storage - -package controllers - -import ( - "context" - "testing" - - azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/Azure/go-autorest/autorest/to" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -func TestStorageControllerHappyPathWithoutNetworkRule(t *testing.T) { - t.Parallel() - defer PanicRecover(t) - ctx := context.Background() - StorageAccountName := GenerateAlphaNumTestResourceName("sadev") - // Create the ResourceGroup object and expect the Reconcile to be created - saInstance := &azurev1alpha1.StorageAccount{ - ObjectMeta: metav1.ObjectMeta{ - Name: StorageAccountName, - Namespace: "default", - }, - Spec: azurev1alpha1.StorageAccountSpec{ - Location: tc.resourceGroupLocation, - ResourceGroup: tc.resourceGroupName, - Sku: azurev1alpha1.StorageAccountSku{ - Name: "Standard_RAGRS", - }, - Kind: "StorageV2", - AccessTier: "Hot", - EnableHTTPSTrafficOnly: to.BoolPtr(true), - }, - } - // create rg - EnsureInstance(ctx, t, tc, saInstance) - // delete rg - EnsureDelete(ctx, t, tc, saInstance) -} diff --git a/controllers/suite_test.go b/controllers/suite_test.go index f3fb0123e48..25926076599 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -15,7 +15,6 @@ import ( "testing" "time" - s "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" k8sSecrets "github.com/Azure/azure-service-operator/pkg/secrets/kube" "k8s.io/client-go/rest" @@ -74,13 +73,13 @@ func setup() error { resourceGroupName := GenerateTestResourceName("rg-prime") resourcegroupLocation := resourcemanagerconfig.DefaultLocation() - eventhubNamespaceName := GenerateTestResourceName("evns-prime") - eventhubName := GenerateTestResourceName("ev-prime") - namespaceLocation := resourcemanagerconfig.DefaultLocation() + //eventhubNamespaceName := GenerateTestResourceName("evns-prime") + //eventhubName := GenerateTestResourceName("ev-prime") + //namespaceLocation := resourcemanagerconfig.DefaultLocation() - storageAccountName := GenerateAlphaNumTestResourceName("saprime") - blobContainerName := GenerateTestResourceName("blob-prime") - containerAccessLevel := s.PublicAccessContainer + //storageAccountName := GenerateAlphaNumTestResourceName("saprime") + //blobContainerName := GenerateTestResourceName("blob-prime") + //containerAccessLevel := s.PublicAccessContainer keyvaultName := GenerateAlphaNumTestResourceName("kv-prime") @@ -684,25 +683,26 @@ func setup() error { _, _ = resourceGroupManager.CreateGroup(context.Background(), resourceGroupName, resourcegroupLocation) } - log.Println("Creating EHNS:", eventhubNamespaceName) - eventHubNSManager := eventHubManagers.EventHubNamespace + /* + log.Println("Creating EHNS:", eventhubNamespaceName) + eventHubNSManager := eventHubManagers.EventHubNamespace - // Create the Eventhub namespace resource - _, err = eventHubNSManager.CreateNamespaceAndWait(context.Background(), resourceGroupName, eventhubNamespaceName, namespaceLocation) - if err != nil { - return err - } + // Create the Eventhub namespace resource + _, err = eventHubNSManager.CreateNamespaceAndWait(context.Background(), resourceGroupName, eventhubNamespaceName, namespaceLocation) + if err != nil { + return err + }*/ tc = TestContext{ - k8sClient: k8sClient, - secretClient: secretClient, - resourceGroupName: resourceGroupName, - resourceGroupLocation: resourcegroupLocation, - eventhubNamespaceName: eventhubNamespaceName, - eventhubName: eventhubName, - namespaceLocation: namespaceLocation, - storageAccountName: storageAccountName, - blobContainerName: blobContainerName, + k8sClient: k8sClient, + secretClient: secretClient, + resourceGroupName: resourceGroupName, + resourceGroupLocation: resourcegroupLocation, + //eventhubNamespaceName: eventhubNamespaceName, + //eventhubName: eventhubName, + //namespaceLocation: namespaceLocation, + //storageAccountName: storageAccountName, + //blobContainerName: blobContainerName, keyvaultName: keyvaultName, eventHubManagers: eventHubManagers, eventhubClient: eventhubClient, @@ -720,60 +720,60 @@ func setup() error { retry: time.Second * 3, consumerGroupClient: consumerGroupClient, } + /* + var pstate *string + finish := time.Now().Add(tc.timeout) + for { + if finish.Before(time.Now()) { + return fmt.Errorf("time out waiting for eventhub namespace") + } - var pstate *string - finish := time.Now().Add(tc.timeout) - for { - if finish.Before(time.Now()) { - return fmt.Errorf("time out waiting for eventhub namespace") + namespace, _ := eventHubManagers.EventHubNamespace.GetNamespace(context.Background(), resourceGroupName, eventhubNamespaceName) + pstate = namespace.ProvisioningState + if pstate != nil && *pstate == "Succeeded" { + break + } + time.Sleep(tc.retry) } - namespace, _ := eventHubManagers.EventHubNamespace.GetNamespace(context.Background(), resourceGroupName, eventhubNamespaceName) - pstate = namespace.ProvisioningState - if pstate != nil && *pstate == "Succeeded" { - break + log.Println("Creating EH:", eventhubName) + // Create the Eventhub resource + _, err = eventHubManagers.EventHub.CreateHub(context.Background(), resourceGroupName, eventhubNamespaceName, eventhubName, int32(7), int32(2), nil) + if err != nil { + return err } - time.Sleep(tc.retry) - } - log.Println("Creating EH:", eventhubName) - // Create the Eventhub resource - _, err = eventHubManagers.EventHub.CreateHub(context.Background(), resourceGroupName, eventhubNamespaceName, eventhubName, int32(7), int32(2), nil) - if err != nil { - return err - } + log.Println("Creating SA:", storageAccountName) + // Create the Storage Account and Container + _, _, _ = storageAccountManager.CreateStorage(context.Background(), resourceGroupName, storageAccountName, resourcegroupLocation, azurev1alpha1.StorageAccountSku{ + Name: "Standard_LRS", + }, "Storage", map[string]*string{}, "", nil, nil, nil) - log.Println("Creating SA:", storageAccountName) - // Create the Storage Account and Container - _, _, _ = storageAccountManager.CreateStorage(context.Background(), resourceGroupName, storageAccountName, resourcegroupLocation, azurev1alpha1.StorageAccountSku{ - Name: "Standard_LRS", - }, "Storage", map[string]*string{}, "", nil, nil, nil) + // Storage account needs to be in "Suceeded" state + // for container create to succeed + finish = time.Now().Add(tc.timeout) + for { - // Storage account needs to be in "Suceeded" state - // for container create to succeed - finish = time.Now().Add(tc.timeout) - for { + if finish.Before(time.Now()) { + return fmt.Errorf("time out waiting for storage account") + } - if finish.Before(time.Now()) { - return fmt.Errorf("time out waiting for storage account") + result, _ := storageAccountManager.GetStorage(context.Background(), resourceGroupName, storageAccountName) + if result.ProvisioningState == s.Succeeded { + break + } + time.Sleep(tc.retry) } - result, _ := storageAccountManager.GetStorage(context.Background(), resourceGroupName, storageAccountName) - if result.ProvisioningState == s.Succeeded { - break - } - time.Sleep(tc.retry) - } - - _, err = storageManagers.BlobContainer.CreateBlobContainer(context.Background(), resourceGroupName, storageAccountName, blobContainerName, containerAccessLevel) - if err != nil { - return err - } + _, err = storageManagers.BlobContainer.CreateBlobContainer(context.Background(), resourceGroupName, storageAccountName, blobContainerName, containerAccessLevel) + if err != nil { + return err + }*/ log.Println("Creating KV:", keyvaultName) _, err = resourcemanagerkeyvaults.AzureKeyVaultManager.CreateVaultWithAccessPolicies(context.Background(), resourceGroupName, keyvaultName, resourcegroupLocation, resourcemanagerconfig.ClientID()) // Key Vault needs to be in "Suceeded" state - finish = time.Now().Add(tc.timeout) + finish := time.Now().Add(tc.timeout) for { if finish.Before(time.Now()) { return fmt.Errorf("time out waiting for keyvault") diff --git a/pkg/resourcemanager/storages/blobcontainer/blob_container.go b/pkg/resourcemanager/storages/blobcontainer/blob_container.go index 2b41cceb721..2096fcb3404 100644 --- a/pkg/resourcemanager/storages/blobcontainer/blob_container.go +++ b/pkg/resourcemanager/storages/blobcontainer/blob_container.go @@ -5,8 +5,6 @@ package blobcontainer import ( "context" - "fmt" - "log" s "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" "github.com/Azure/azure-service-operator/pkg/resourcemanager/config" @@ -68,8 +66,6 @@ func (bc *AzureBlobContainerManager) GetBlobContainer(ctx context.Context, resou // containerName - the name of the container func (bc *AzureBlobContainerManager) DeleteBlobContainer(ctx context.Context, resourceGroupName string, accountName string, containerName string) (result autorest.Response, err error) { containerClient := getContainerClient() - log.Println(fmt.Sprintf("Deleting blob container '%s' for resource group: %s", containerName, accountName)) - return containerClient.Delete(ctx, resourceGroupName, accountName, diff --git a/pkg/resourcemanager/storages/blobcontainer/blob_container_reconcile.go b/pkg/resourcemanager/storages/blobcontainer/blob_container_reconcile.go index ea7c46f44a6..619c6c453f5 100644 --- a/pkg/resourcemanager/storages/blobcontainer/blob_container_reconcile.go +++ b/pkg/resourcemanager/storages/blobcontainer/blob_container_reconcile.go @@ -106,6 +106,7 @@ func (bc *AzureBlobContainerManager) Delete(ctx context.Context, obj runtime.Obj catch = []string{ errhelp.ParentNotFoundErrorCode, errhelp.ResourceGroupNotFoundErrorCode, + errhelp.NotFoundErrorCode, } if helpers.ContainsString(catch, azerr.Type) { return false, nil diff --git a/pkg/resourcemanager/storages/storageaccount/storageaccount_reconcile.go b/pkg/resourcemanager/storages/storageaccount/storageaccount_reconcile.go index 45efda9e48c..f9ee1bd5235 100644 --- a/pkg/resourcemanager/storages/storageaccount/storageaccount_reconcile.go +++ b/pkg/resourcemanager/storages/storageaccount/storageaccount_reconcile.go @@ -168,11 +168,14 @@ func (sa *azureStorageManager) Delete(ctx context.Context, obj runtime.Object, o name := instance.ObjectMeta.Name groupName := instance.Spec.ResourceGroup + _, err = sa.DeleteStorage(ctx, groupName, name) if err != nil { catch := []string{ errhelp.ValidationError, + errhelp.ResourceGroupNotFoundErrorCode, } + err = errhelp.NewAzureError(err) if azerr, ok := err.(*errhelp.AzureError); ok { if helpers.ContainsString(catch, azerr.Type) { From f56f92b5e4fbc072e125838b242bc07c93ed72a8 Mon Sep 17 00:00:00 2001 From: jananivMS Date: Tue, 21 Apr 2020 10:42:47 -0600 Subject: [PATCH 2/4] fix compile issues --- controllers/azuresqlserver_controller_test.go | 1 - controllers/azuresqluser_controller_test.go | 6 +----- controllers/azurevnetrule_controller_test.go | 5 ----- controllers/eventhub_storageaccount_controller_test.go | 7 +++---- controllers/keyvault_controller_test.go | 4 ---- controllers/resourcegroup_controller_test.go | 6 ------ 6 files changed, 4 insertions(+), 25 deletions(-) diff --git a/controllers/azuresqlserver_controller_test.go b/controllers/azuresqlserver_controller_test.go index ecedcbe46a0..86e42e0e420 100644 --- a/controllers/azuresqlserver_controller_test.go +++ b/controllers/azuresqlserver_controller_test.go @@ -18,7 +18,6 @@ func TestAzureSqlServerControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) sqlServerName := GenerateTestResourceNameWithRandom("sqlserver-dev", 10) diff --git a/controllers/azuresqluser_controller_test.go b/controllers/azuresqluser_controller_test.go index 2c5b0f99558..9e18f595936 100644 --- a/controllers/azuresqluser_controller_test.go +++ b/controllers/azuresqluser_controller_test.go @@ -7,7 +7,6 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" @@ -15,17 +14,14 @@ import ( "github.com/Azure/azure-service-operator/pkg/helpers" "github.com/stretchr/testify/assert" v1 "k8s.io/api/core/v1" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSQLUserControllerNoAdminSecret(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) - var err error + var sqlServerName string var sqlDatabaseName string var sqlUser *azurev1alpha1.AzureSQLUser diff --git a/controllers/azurevnetrule_controller_test.go b/controllers/azurevnetrule_controller_test.go index a32f4007d9e..96cdffa198b 100644 --- a/controllers/azurevnetrule_controller_test.go +++ b/controllers/azurevnetrule_controller_test.go @@ -7,23 +7,18 @@ package controllers import ( "context" - "strings" "testing" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - "github.com/stretchr/testify/assert" "github.com/Azure/azure-service-operator/pkg/errhelp" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestAzureSqlVNetRuleControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) // Add any setup steps that needs to be executed before each test sqlServerName := GenerateTestResourceNameWithRandom("sqlvnetrule-test-srv", 10) diff --git a/controllers/eventhub_storageaccount_controller_test.go b/controllers/eventhub_storageaccount_controller_test.go index 9fdbbf6bd66..181c34ed80c 100644 --- a/controllers/eventhub_storageaccount_controller_test.go +++ b/controllers/eventhub_storageaccount_controller_test.go @@ -7,16 +7,15 @@ package controllers import ( "context" - "fmt" "testing" s "github.com/Azure/azure-sdk-for-go/services/storage/mgmt/2019-04-01/storage" azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" - //"github.com/Azure/azure-service-operator/pkg/errhelp" + "github.com/Azure/azure-service-operator/pkg/errhelp" - //kvhelper "github.com/Azure/azure-service-operator/pkg/resourcemanager/keyvaults" - //kvsecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault" + kvhelper "github.com/Azure/azure-service-operator/pkg/resourcemanager/keyvaults" + kvsecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault" "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" diff --git a/controllers/keyvault_controller_test.go b/controllers/keyvault_controller_test.go index 2398bf6ba87..a4552b05361 100644 --- a/controllers/keyvault_controller_test.go +++ b/controllers/keyvault_controller_test.go @@ -8,7 +8,6 @@ package controllers import ( "context" "net/http" - "strings" "testing" "time" @@ -19,7 +18,6 @@ import ( kvsecrets "github.com/Azure/azure-service-operator/pkg/secrets/keyvault" "github.com/stretchr/testify/assert" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/types" ) @@ -236,7 +234,6 @@ func TestKeyvaultControllerInvalidName(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) keyVaultName := "k" @@ -263,7 +260,6 @@ func TestKeyvaultControllerNoResourceGroup(t *testing.T) { t.Parallel() defer PanicRecover(t) ctx := context.Background() - assert := assert.New(t) keyVaultName := helpers.FillWithRandom(GenerateTestResourceName("kv"), 24) diff --git a/controllers/resourcegroup_controller_test.go b/controllers/resourcegroup_controller_test.go index 436b1f96a9c..08997d640e5 100644 --- a/controllers/resourcegroup_controller_test.go +++ b/controllers/resourcegroup_controller_test.go @@ -13,9 +13,7 @@ import ( azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1" "github.com/stretchr/testify/assert" - apierrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/types" ) func TestResourceGroupControllerHappyPath(t *testing.T) { @@ -26,8 +24,6 @@ func TestResourceGroupControllerHappyPath(t *testing.T) { resourceGroupName := GenerateTestResourceNameWithRandom("rg-dev", 10) - var err error - // Create the ResourceGroup object and expect the Reconcile to be created resourceGroupInstance := &azurev1alpha1.ResourceGroup{ ObjectMeta: metav1.ObjectMeta{ @@ -42,8 +38,6 @@ func TestResourceGroupControllerHappyPath(t *testing.T) { // create rg EnsureInstance(ctx, t, tc, resourceGroupInstance) - resourceGroupNamespacedName := types.NamespacedName{Name: resourceGroupName, Namespace: "default"} - // verify rg exists in azure assert.Eventually(func() bool { _, err := tc.resourceGroupManager.CheckExistence(ctx, resourceGroupName) From 41b90f689ee19b455e38477d430a4b743c41dd65 Mon Sep 17 00:00:00 2001 From: jananivMS Date: Tue, 21 Apr 2020 11:30:38 -0600 Subject: [PATCH 3/4] fix test --- .../eventhub_namespace_consumergroup_controller_test.go | 5 ----- 1 file changed, 5 deletions(-) diff --git a/controllers/eventhub_namespace_consumergroup_controller_test.go b/controllers/eventhub_namespace_consumergroup_controller_test.go index 7e50b7a5d84..123436bcdc4 100644 --- a/controllers/eventhub_namespace_consumergroup_controller_test.go +++ b/controllers/eventhub_namespace_consumergroup_controller_test.go @@ -91,11 +91,6 @@ func TestConsumerGroupEventHubAndNamespaceControllerHappy(t *testing.T) { EnsureDelete(ctx, t, tc, consumerGroupInstance) - assert.Eventually(func() bool { - cg, _ := tc.consumerGroupClient.GetConsumerGroup(ctx, rgName, eventhubNamespaceName, eventhubName, azureConsumerGroupName) - return cg.Response.StatusCode != http.StatusOK - }, tc.timeout, tc.retry, "wait for consumergroup to be gone from azure") - // verify eventhub is deleted EnsureDelete(ctx, t, tc, eventhubInstance) From 982f427bc80c3aa617381c1f5a0cf3b6dd15d49c Mon Sep 17 00:00:00 2001 From: jananivMS Date: Tue, 21 Apr 2020 14:28:24 -0600 Subject: [PATCH 4/4] remove comments --- controllers/suite_test.go | 80 ++------------------------------------- 1 file changed, 4 insertions(+), 76 deletions(-) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index 47cee1c297c..9018ad9cc14 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -73,14 +73,6 @@ func setup() error { resourceGroupName := GenerateTestResourceName("rg-prime") resourcegroupLocation := resourcemanagerconfig.DefaultLocation() - //eventhubNamespaceName := GenerateTestResourceName("evns-prime") - //eventhubName := GenerateTestResourceName("ev-prime") - //namespaceLocation := resourcemanagerconfig.DefaultLocation() - - //storageAccountName := GenerateAlphaNumTestResourceName("saprime") - //blobContainerName := GenerateTestResourceName("blob-prime") - //containerAccessLevel := s.PublicAccessContainer - keyvaultName := GenerateAlphaNumTestResourceName("kv-prime") var timeout time.Duration @@ -683,26 +675,11 @@ func setup() error { _, _ = resourceGroupManager.CreateGroup(context.Background(), resourceGroupName, resourcegroupLocation) } - /* - log.Println("Creating EHNS:", eventhubNamespaceName) - eventHubNSManager := eventHubManagers.EventHubNamespace - - // Create the Eventhub namespace resource - _, err = eventHubNSManager.CreateNamespaceAndWait(context.Background(), resourceGroupName, eventhubNamespaceName, namespaceLocation) - if err != nil { - return err - }*/ - tc = TestContext{ - k8sClient: k8sClient, - secretClient: secretClient, - resourceGroupName: resourceGroupName, - resourceGroupLocation: resourcegroupLocation, - //eventhubNamespaceName: eventhubNamespaceName, - //eventhubName: eventhubName, - //namespaceLocation: namespaceLocation, - //storageAccountName: storageAccountName, - //blobContainerName: blobContainerName, + k8sClient: k8sClient, + secretClient: secretClient, + resourceGroupName: resourceGroupName, + resourceGroupLocation: resourcegroupLocation, keyvaultName: keyvaultName, eventHubManagers: eventHubManagers, eventhubClient: eventhubClient, @@ -720,55 +697,6 @@ func setup() error { retry: time.Second * 3, consumerGroupClient: consumerGroupClient, } - /* - var pstate *string - finish := time.Now().Add(tc.timeout) - for { - if finish.Before(time.Now()) { - return fmt.Errorf("time out waiting for eventhub namespace") - } - - namespace, _ := eventHubManagers.EventHubNamespace.GetNamespace(context.Background(), resourceGroupName, eventhubNamespaceName) - pstate = namespace.ProvisioningState - if pstate != nil && *pstate == "Succeeded" { - break - } - time.Sleep(tc.retry) - } - - log.Println("Creating EH:", eventhubName) - // Create the Eventhub resource - _, err = eventHubManagers.EventHub.CreateHub(context.Background(), resourceGroupName, eventhubNamespaceName, eventhubName, int32(7), int32(2), nil) - if err != nil { - return err - } - - log.Println("Creating SA:", storageAccountName) - // Create the Storage Account and Container - _, _, _ = storageAccountManager.CreateStorage(context.Background(), resourceGroupName, storageAccountName, resourcegroupLocation, azurev1alpha1.StorageAccountSku{ - Name: "Standard_LRS", - }, "Storage", map[string]*string{}, "", nil, nil, nil) - - // Storage account needs to be in "Suceeded" state - // for container create to succeed - finish = time.Now().Add(tc.timeout) - for { - - if finish.Before(time.Now()) { - return fmt.Errorf("time out waiting for storage account") - } - - result, _ := storageAccountManager.GetStorage(context.Background(), resourceGroupName, storageAccountName) - if result.ProvisioningState == s.Succeeded { - break - } - time.Sleep(tc.retry) - } - - _, err = storageManagers.BlobContainer.CreateBlobContainer(context.Background(), resourceGroupName, storageAccountName, blobContainerName, containerAccessLevel) - if err != nil { - return err - }*/ log.Println("Creating KV:", keyvaultName) _, err = resourcemanagerkeyvaults.AzureKeyVaultManager.CreateVaultWithAccessPolicies(context.Background(), resourceGroupName, keyvaultName, resourcegroupLocation, resourcemanagerconfig.ClientID())