Skip to content

Commit

Permalink
Merge branch 'master' into issue#858
Browse files Browse the repository at this point in the history
  • Loading branch information
frodopwns authored Apr 16, 2020
2 parents 7327240 + fabc83b commit 7371d2d
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 71 deletions.
69 changes: 68 additions & 1 deletion controllers/cosmosdb_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestCosmosDBHappyPath(t *testing.T) {
Namespace: cosmosDBNamespace,
},
Spec: v1alpha1.CosmosDBSpec{
Location: "westus",
Location: tc.resourceGroupLocation,
ResourceGroup: tc.resourceGroupName,
Kind: v1alpha1.CosmosDBKindGlobalDocumentDB,
Properties: v1alpha1.CosmosDBProperties{
Expand All @@ -42,3 +42,70 @@ func TestCosmosDBHappyPath(t *testing.T) {
EnsureDelete(ctx, t, tc, dbInstance)

}

func TestCosmosDBControllerNoResourceGroup(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()

rgLocation := tc.resourceGroupLocation
//wrong resource group name
resourceGroupName := "gone"

cosmosDBAccountName := GenerateTestResourceNameWithRandom("cosmosdb", 8)
cosmosDBNamespace := "default"

dbInstance1 := &v1alpha1.CosmosDB{
ObjectMeta: metav1.ObjectMeta{
Name: cosmosDBAccountName,
Namespace: cosmosDBNamespace,
},
Spec: v1alpha1.CosmosDBSpec{
Location: rgLocation,
ResourceGroup: resourceGroupName,
Kind: v1alpha1.CosmosDBKindGlobalDocumentDB,
Properties: v1alpha1.CosmosDBProperties{
DatabaseAccountOfferType: v1alpha1.CosmosDBDatabaseAccountOfferTypeStandard,
},
},
}
//the expected error meessage to be shown
errMessage := "Waiting for resource group '" + resourceGroupName + "' to be available"

EnsureInstanceWithResult(ctx, t, tc, dbInstance1, errMessage, false)
EnsureDelete(ctx, t, tc, dbInstance1)
}

func TestCosmosDBControllerInvalidLocation(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()

resourceGroupName := tc.resourceGroupName
//rglocation doesnot exist
rgLocation := GenerateTestResourceNameWithRandom("cosmos-lo", 10)

cosmosDBAccountName := GenerateTestResourceNameWithRandom("cosmos-db", 8)
cosmosDBNamespace := "default"

dbInstance2 := &v1alpha1.CosmosDB{
ObjectMeta: metav1.ObjectMeta{
Name: cosmosDBAccountName,
Namespace: cosmosDBNamespace,
},
Spec: v1alpha1.CosmosDBSpec{
Location: rgLocation,
ResourceGroup: resourceGroupName,
Kind: v1alpha1.CosmosDBKindGlobalDocumentDB,
Properties: v1alpha1.CosmosDBProperties{
DatabaseAccountOfferType: v1alpha1.CosmosDBDatabaseAccountOfferTypeStandard,
},
},
}

//error meessage to be expected
errMessage := "The specified location '" + rgLocation + "' is invalid"

EnsureInstanceWithResult(ctx, t, tc, dbInstance2, errMessage, false)
EnsureDelete(ctx, t, tc, dbInstance2)
}
9 changes: 9 additions & 0 deletions controllers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,15 @@ func EnsureInstanceWithResult(ctx context.Context, t *testing.T, tc TestContext,
statused := ConvertToStatus(instance)
// if we expect this resource to end up with provisioned == true then failedProvisioning == true is unrecoverable
if provisioned && statused.Status.FailedProvisioning {
if strings.Contains(statused.Status.Message, "already exists") || strings.Contains(statused.Status.Message, "AlreadyExists") {
t.Log("")
t.Log("-------")
t.Log("unexpected failed provisioning encountered")
t.Logf("%+v\n", statused.Status)
t.Logf("current time %v\n", time.Now())
t.Log("-------")
t.Log("")
}
return helpers.NewStop(fmt.Errorf("Failed provisioning: %s", statused.Status.Message))
}
if !strings.Contains(statused.Status.Message, message) || statused.Status.Provisioned != provisioned {
Expand Down
75 changes: 6 additions & 69 deletions controllers/postgresql_combined_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,18 @@ import (
"testing"

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 TestPSQLDatabaseController(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
assert := assert.New(t)

var rgName string
var rgLocation string
var postgreSQLServerName string
var postgreSQLServerInstance *azurev1alpha1.PostgreSQLServer
var postgreSQLServerNamespacedName types.NamespacedName
var err error

// Add any setup steps that needs to be executed before each test
rgName = tc.resourceGroupName
Expand Down Expand Up @@ -56,20 +50,7 @@ func TestPSQLDatabaseController(t *testing.T) {
},
}

err = tc.k8sClient.Create(ctx, postgreSQLServerInstance)
assert.Equal(nil, err, "create postgreSQLServerInstance in k8s")

postgreSQLServerNamespacedName = types.NamespacedName{Name: postgreSQLServerName, Namespace: "default"}

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLServerNamespacedName, postgreSQLServerInstance)
return HasFinalizer(postgreSQLServerInstance, finalizerName)
}, tc.timeout, tc.retry, "wait for postgreSQLserver to have finlizer")

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLServerNamespacedName, postgreSQLServerInstance)
return postgreSQLServerInstance.Status.Provisioned
}, tc.timeout, tc.retry, "wait for postgreSQLserver to be provisioned")
EnsureInstance(ctx, t, tc, postgreSQLServerInstance)

postgreSQLDatabaseName := GenerateTestResourceNameWithRandom("psql-db", 10)

Expand All @@ -85,28 +66,9 @@ func TestPSQLDatabaseController(t *testing.T) {
},
}

err = tc.k8sClient.Create(ctx, postgreSQLDatabaseInstance)
assert.Equal(nil, err, "create postgreSQLDatabaseInstance in k8s")

postgreSQLDatabaseNamespacedName := types.NamespacedName{Name: postgreSQLDatabaseName, Namespace: "default"}

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLDatabaseNamespacedName, postgreSQLDatabaseInstance)
return HasFinalizer(postgreSQLDatabaseInstance, finalizerName)
}, tc.timeout, tc.retry, "wait for postgreSQLDBInstance to have finalizer")

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLDatabaseNamespacedName, postgreSQLDatabaseInstance)
return postgreSQLDatabaseInstance.Status.Provisioned
}, tc.timeout, tc.retry, "wait for postgreSQLDBInstance to be provisioned")
EnsureInstance(ctx, t, tc, postgreSQLDatabaseInstance)

err = tc.k8sClient.Delete(ctx, postgreSQLDatabaseInstance)
assert.Equal(nil, err, "delete postgreSQLDatabaseInstance in k8s")

assert.Eventually(func() bool {
err = tc.k8sClient.Get(ctx, postgreSQLDatabaseNamespacedName, postgreSQLDatabaseInstance)
return apierrors.IsNotFound(err)
}, tc.timeout, tc.retry, "wait for postgreSQLDBInstance to be gone from k8s")
EnsureDelete(ctx, t, tc, postgreSQLDatabaseInstance)

// Test firewall rule -------------------------------

Expand All @@ -126,36 +88,11 @@ func TestPSQLDatabaseController(t *testing.T) {
},
}

err = tc.k8sClient.Create(ctx, postgreSQLFirewallRuleInstance)
assert.Equal(nil, err, "create postgreSQLFirewallRuleInstance in k8s")

postgreSQLFirewallRuleNamespacedName := types.NamespacedName{Name: postgreSQLFirewallRuleName, Namespace: "default"}

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLFirewallRuleNamespacedName, postgreSQLFirewallRuleInstance)
return HasFinalizer(postgreSQLFirewallRuleInstance, finalizerName)
}, tc.timeout, tc.retry, "wait for postgreSQLFirewallRuleInstance to have finalizer")
EnsureInstance(ctx, t, tc, postgreSQLFirewallRuleInstance)

assert.Eventually(func() bool {
_ = tc.k8sClient.Get(ctx, postgreSQLFirewallRuleNamespacedName, postgreSQLFirewallRuleInstance)
return postgreSQLFirewallRuleInstance.Status.Provisioned
}, tc.timeout, tc.retry, "wait for postgreSQLFirewallRuleInstance to be provisioned")

err = tc.k8sClient.Delete(ctx, postgreSQLFirewallRuleInstance)
assert.Equal(nil, err, "delete postgreSQLFirewallRuleInstance in k8s")

assert.Eventually(func() bool {
err = tc.k8sClient.Get(ctx, postgreSQLFirewallRuleNamespacedName, postgreSQLFirewallRuleInstance)
return apierrors.IsNotFound(err)
}, tc.timeout, tc.retry, "wait for postgreSQLFirewallRuleInstance to be gone from k8s")
EnsureDelete(ctx, t, tc, postgreSQLFirewallRuleInstance)

// Add any teardown steps that needs to be executed after each test
err = tc.k8sClient.Delete(ctx, postgreSQLServerInstance)
assert.Equal(nil, err, "delete postgreSQLServerInstance in k8s")

assert.Eventually(func() bool {
err = tc.k8sClient.Get(ctx, postgreSQLServerNamespacedName, postgreSQLServerInstance)
return apierrors.IsNotFound(err)
}, tc.timeout, tc.retry, "wait for postgreSQLServerInstance to be gone from k8s")
EnsureDelete(ctx, t, tc, postgreSQLServerInstance)

}
2 changes: 1 addition & 1 deletion pkg/resourcemanager/config/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func ParseEnvironment() error {
authorizationServerURL = azureEnv.ActiveDirectoryEndpoint
baseURI = azureEnv.ResourceManagerEndpoint // BaseURI()

locationDefault = envy.Get("AZURE_LOCATION_DEFAULT", "southcentralus") // DefaultLocation()
locationDefault = envy.Get("AZURE_LOCATION_DEFAULT", "westus2") // DefaultLocation()
useDeviceFlow = ParseBoolFromEnvironment("AZURE_USE_DEVICEFLOW") // UseDeviceFlow()
useMI = ParseBoolFromEnvironment("AZURE_USE_MI") // UseMI()
keepResources = ParseBoolFromEnvironment("AZURE_SAMPLES_KEEP_RESOURCES") // KeepResources()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ func (bc *AzureBlobContainerManager) GetParents(obj runtime.Object) ([]resourcem
}

return []resourcemanager.KubeParent{
{
Key: types.NamespacedName{
Name: instance.Spec.AccountName,
Namespace: instance.Namespace,
},
Target: &azurev1alpha1.StorageAccount{},
},
{
Key: types.NamespacedName{
Name: instance.Spec.ResourceGroup,
Expand All @@ -133,6 +140,7 @@ func (bc *AzureBlobContainerManager) GetParents(obj runtime.Object) ([]resourcem
Target: &azurev1alpha1.ResourceGroup{},
},
}, nil

}

func (bc *AzureBlobContainerManager) GetStatus(obj runtime.Object) (*azurev1alpha1.ASOStatus, error) {
Expand Down

0 comments on commit 7371d2d

Please sign in to comment.