Skip to content

Commit

Permalink
adding mysql tests, refactored some password/username gen
Browse files Browse the repository at this point in the history
  • Loading branch information
frodopwns committed Apr 3, 2020
1 parent 7f55538 commit a4fe4d2
Show file tree
Hide file tree
Showing 7 changed files with 344 additions and 257 deletions.
24 changes: 23 additions & 1 deletion api/v1alpha1/mysqlserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type MySQLServerSpec struct {
Location string `json:"location"`
ResourceGroup string `json:"resourceGroup,omitempty"`
Sku PSQLSku `json:"sku,omitempty"`
Sku AzureDBsSQLSku `json:"sku,omitempty"`
ServerVersion ServerVersion `json:"serverVersion,omitempty"`
SSLEnforcement SslEnforcementEnum `json:"sslEnforcement,omitempty"`
KeyVaultToStoreSecrets string `json:"keyVaultToStoreSecrets,omitempty"`
Expand Down Expand Up @@ -44,3 +44,25 @@ type MySQLServerList struct {
func init() {
SchemeBuilder.Register(&MySQLServer{}, &MySQLServerList{})
}

func NewDefaultMySQLServer(name, resourceGroup, location string) *MySQLServer {
return &MySQLServer{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: "default",
},
Spec: MySQLServerSpec{
Location: location,
ResourceGroup: resourceGroup,
Sku: AzureDBsSQLSku{
Name: "B_Gen5_2",
Tier: SkuTier("Basic"),
Family: "Gen5",
Size: "51200",
Capacity: 2,
},
ServerVersion: ServerVersion("8.0"),
SSLEnforcement: SslEnforcementEnumEnabled,
},
}
}
4 changes: 2 additions & 2 deletions api/v1alpha1/postgresqlserver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import (
type PostgreSQLServerSpec struct {
Location string `json:"location"`
ResourceGroup string `json:"resourceGroup,omitempty"`
Sku PSQLSku `json:"sku,omitempty"`
Sku AzureDBsSQLSku `json:"sku,omitempty"`
ServerVersion ServerVersion `json:"serverVersion,omitempty"`
SSLEnforcement SslEnforcementEnum `json:"sslEnforcement,omitempty"`
KeyVaultToStoreSecrets string `json:"keyVaultToStoreSecrets,omitempty"`
}

type PSQLSku struct {
type AzureDBsSQLSku struct {
// Name - The name of the sku, typically, tier + family + cores, e.g. B_Gen4_1, GP_Gen5_8.
Name string `json:"name,omitempty"`
// Tier - The tier of the particular SKU, e.g. Basic. Possible values include: 'Basic', 'GeneralPurpose', 'MemoryOptimized'
Expand Down
65 changes: 65 additions & 0 deletions controllers/mysqlserver_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

// +build all mysql

package controllers

import (
"context"
"testing"

azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
)

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

// Add any setup steps that needs to be executed before each test
rgName := GenerateTestResourceNameWithRandom("mysqlsrv-rg", 10)
rgLocation := tc.resourceGroupLocation
mySQLServerName := GenerateTestResourceNameWithRandom("mysql-srv", 10)

// Create the mySQLServer object and expect the Reconcile to be created
mySQLServerInstance := azurev1alpha1.NewDefaultMySQLServer(mySQLServerName, rgName, rgLocation)

EnsureInstanceWithResult(ctx, t, tc, mySQLServerInstance, errhelp.ResourceGroupNotFoundErrorCode, false)
EnsureDelete(ctx, t, tc, mySQLServerInstance)
}

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

// Add any setup steps that needs to be executed before each test
rgLocation := GenerateTestResourceNameWithRandom("mysqlsrv-rg", 10)
rgName := tc.resourceGroupName
mySQLServerName := GenerateTestResourceNameWithRandom("mysql-srv", 10)

// Create the mySQLServer object and expect the Reconcile to be created
mySQLServerInstance := azurev1alpha1.NewDefaultMySQLServer(mySQLServerName, rgName, rgLocation)

EnsureInstanceWithResult(ctx, t, tc, mySQLServerInstance, errhelp.InvalidResourceLocation, false)
EnsureDelete(ctx, t, tc, mySQLServerInstance)
}

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

// Add any setup steps that needs to be executed before each test
rgLocation := "eastus2"
rgName := tc.resourceGroupName
mySQLServerName := GenerateTestResourceNameWithRandom("mysql-srv", 10)

// Create the mySQLServer object and expect the Reconcile to be created
mySQLServerInstance := azurev1alpha1.NewDefaultMySQLServer(mySQLServerName, rgName, rgLocation)

EnsureInstance(ctx, t, tc, mySQLServerInstance)
EnsureDelete(ctx, t, tc, mySQLServerInstance)
}
21 changes: 13 additions & 8 deletions pkg/helpers/stringhelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
const (
passwordLength = 16
passwordChars = lowerAlphaChars + upperAlphaChars + numberChars + specialChars
usernameChars = lowerAlphaChars + upperAlphaChars + numberChars
lowerAlphaChars = "abcdefghijklmnopqrstuvwxyz"
upperAlphaChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
numberChars = "0123456789"
Expand Down Expand Up @@ -90,16 +91,20 @@ func IsBeingDeleted(o metav1.Object) bool {
}

// GenerateRandomUsername - helper function to generate random username for sql server
func GenerateRandomUsername(n int, numOfDigits int) (string, error) {
func GenerateRandomUsername(n int) string {

// Generate a username that is n characters long, with n/2 digits and 0 symbols (not allowed),
// allowing only lower case letters (upper case not allowed), and disallowing repeat characters.
res, err := password.Generate(n, numOfDigits, 0, true, false)
if err != nil {
return "", err
}
b := make([]byte, n)

return res, nil
for i := 0; i < n; i++ {
b[i] = usernameChars[seededRand.Intn(len(usernameChars))]
}
// For good measure, shuffle the elements of the entire []byte so that
// the 0 character isn't predicatably lowercase, etc...
for i := range b {
j := seededRand.Intn(len(b))
b[i], b[j] = b[j], b[i]
}
return string(b)
}

// GenerateRandomPassword - helper function to generate random password for sql server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,11 +306,7 @@ func NewSecret(serverName string) (map[string][]byte, error) {

secret := map[string][]byte{}

randomUsername, err := helpers.GenerateRandomUsername(usernameLength, (usernameLength / 2))
if err != nil {
return secret, err
}

randomUsername := helpers.GenerateRandomUsername(usernameLength)
randomPassword := helpers.NewPassword()

secret["username"] = []byte(randomUsername)
Expand Down
Loading

0 comments on commit a4fe4d2

Please sign in to comment.