Skip to content

Commit

Permalink
Merge pull request #971 from jananivMS/clean-suitetest
Browse files Browse the repository at this point in the history
[Test cleanup] Cleanup suite_test and use Ensure functions
  • Loading branch information
jananivMS authored Apr 21, 2020
2 parents 0cb0fbf + 1fa843c commit 9e6909c
Show file tree
Hide file tree
Showing 25 changed files with 322 additions and 900 deletions.
3 changes: 1 addition & 2 deletions api/v1alpha1/storageaccount_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -92,7 +92,6 @@ type StorageAccountAdditionalResources struct {
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// StorageAccountList contains a list of Storage
type StorageAccountList struct {
Expand Down
26 changes: 2 additions & 24 deletions controllers/apimgmt_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
25 changes: 2 additions & 23 deletions controllers/appinsights_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
4 changes: 2 additions & 2 deletions controllers/azuredatalakegen2filesystem_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()
Expand Down
16 changes: 4 additions & 12 deletions controllers/azuresqlaction_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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)
}
45 changes: 4 additions & 41 deletions controllers/azuresqldatabase_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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)

}
24 changes: 2 additions & 22 deletions controllers/azuresqlfailovergroup_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
28 changes: 2 additions & 26 deletions controllers/azuresqlfirewallrule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)

}
Loading

0 comments on commit 9e6909c

Please sign in to comment.