forked from Azure/azure-service-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathazuresqluser_controller_test.go
127 lines (102 loc) · 3.45 KB
/
azuresqluser_controller_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//go:build all || azuresqlserver || azuresqluser
// +build all azuresqlserver azuresqluser
package controllers
import (
"context"
"testing"
azurev1alpha1 "github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/helpers"
"github.com/Azure/azure-service-operator/pkg/secrets"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestAzureSQLUserControllerNoAdminSecret(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
var sqlUser *azurev1alpha1.AzureSQLUser
sqlServerName := GenerateTestResourceNameWithRandom("sqlusr-test", 10)
sqlDatabaseName := GenerateTestResourceNameWithRandom("sqldb-test", 10)
resourceGroup := GenerateTestResourceNameWithRandom("myrg", 10)
username := "sql-test-user" + helpers.RandomString(10)
roles := []string{"db_owner"}
sqlUser = &azurev1alpha1.AzureSQLUser{
ObjectMeta: metav1.ObjectMeta{
Name: username,
Namespace: "default",
},
Spec: azurev1alpha1.AzureSQLUserSpec{
ResourceGroup: resourceGroup,
Server: sqlServerName,
DbName: sqlDatabaseName,
AdminSecret: "",
Roles: roles,
},
}
EnsureInstanceWithResult(ctx, t, tc, sqlUser, "admin secret", false)
EnsureDelete(ctx, t, tc, sqlUser)
}
func TestAzureSQLUserControllerNoResourceGroup(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
assert := assert.New(t)
var err error
var sqlUser *azurev1alpha1.AzureSQLUser
sqlServerName := GenerateTestResourceNameWithRandom("sqlusr-test", 10)
sqlDatabaseName := GenerateTestResourceNameWithRandom("sqldb-test", 10)
username := "sql-test-user" + helpers.RandomString(10)
roles := []string{"db_owner"}
adminSecretKey := secrets.SecretKey{Name: sqlServerName, Namespace: "default", Kind: "AzureSQLServer"}
data := map[string][]byte{
"username": []byte("username"),
"password": []byte("password"),
}
err = tc.secretClient.Upsert(ctx, adminSecretKey, data)
assert.NoError(err)
sqlUser = &azurev1alpha1.AzureSQLUser{
ObjectMeta: metav1.ObjectMeta{
Name: username,
Namespace: "default",
},
Spec: azurev1alpha1.AzureSQLUserSpec{
Server: sqlServerName,
DbName: sqlDatabaseName,
AdminSecret: "",
Roles: roles,
ResourceGroup: "fakerg" + helpers.RandomString(10),
},
}
EnsureInstanceWithResult(ctx, t, tc, sqlUser, errhelp.ResourceGroupNotFoundErrorCode, false)
EnsureDelete(ctx, t, tc, sqlUser)
}
func TestAzureSQLUserValidatesDatabaseName(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
var sqlUser *azurev1alpha1.AzureSQLUser
sqlServerName := GenerateTestResourceNameWithRandom("sqlusr-test", 10)
sqlDatabaseName := "master"
username := "sql-test-user" + helpers.RandomString(10)
roles := []string{"db_owner"}
sqlUser = &azurev1alpha1.AzureSQLUser{
ObjectMeta: metav1.ObjectMeta{
Name: username,
Namespace: "default",
},
Spec: azurev1alpha1.AzureSQLUserSpec{
Server: sqlServerName,
DbName: sqlDatabaseName,
AdminSecret: "",
Roles: roles,
ResourceGroup: "fakerg" + helpers.RandomString(10),
},
}
assert := assert.New(t)
err := tc.k8sClient.Create(ctx, sqlUser)
assert.Error(err)
assert.Contains(err.Error(), "'master' is a reserved database name and cannot be used")
}