forked from Azure/azure-service-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcosmosdbsqldatabase_controller_test.go
196 lines (159 loc) · 6.01 KB
/
cosmosdbsqldatabase_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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
//go:build all || cosmos
// +build all cosmos
package controllers
import (
"context"
"strings"
"testing"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2021-03-15/documentdb"
"github.com/pkg/errors"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"github.com/Azure/azure-service-operator/api/v1alpha1"
"github.com/Azure/azure-service-operator/pkg/errhelp"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/config"
"github.com/Azure/azure-service-operator/pkg/resourcemanager/cosmosdb"
)
func CosmosDBSQLDatabaseHappyPath(t *testing.T, cosmosDBAccountName string) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
assert := require.New(t)
cosmosDBSQLDatabaseClient, err := cosmosdb.GetCosmosDBSQLDatabaseClient(config.GlobalCredentials())
assert.Equal(nil, err, "failed to get cosmos db SQL database client")
name := GenerateTestResourceNameWithRandom("cosmosdbsql", 8)
namespace := "default"
var throughPutRUs int32 = 400
sqlDBInstance := &v1alpha1.CosmosDBSQLDatabase{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1alpha1.CosmosDBSQLDatabaseSpec{
ResourceGroup: tc.resourceGroupName,
Account: cosmosDBAccountName,
Throughput: &throughPutRUs,
},
}
// Create the Cosmos SQL DB
EnsureInstance(ctx, t, tc, sqlDBInstance)
assert.Eventually(func() bool {
cosmosSQLDB, err := cosmosDBSQLDatabaseClient.GetSQLDatabase(ctx, tc.resourceGroupName, cosmosDBAccountName, name)
assert.Equal(nil, err, "err getting DB from Azure")
throughputMatches, err := doesThroughputMatch(
ctx,
cosmosDBSQLDatabaseClient,
tc.resourceGroupName,
cosmosDBAccountName,
name,
throughPutRUs)
assert.Equal(nil, err, "err ensuring throughput matches")
return cosmosSQLDB.Name != nil && *cosmosSQLDB.Name == name && throughputMatches
}, tc.timeout, tc.retry, "wait for cosmos SQL DB to exist in Azure")
// Get the updated Cosmos SQL DB from k8s
namespacedName := types.NamespacedName{Name: name, Namespace: namespace}
err = tc.k8sClient.Get(ctx, namespacedName, sqlDBInstance)
assert.Equal(nil, err, "getting cosmos SQL DB from k8s")
// Update the Cosmos SQL DB throughput
throughPutRUs = 1000
sqlDBInstance.Spec.Throughput = &throughPutRUs
err = tc.k8sClient.Update(ctx, sqlDBInstance)
assert.Equal(nil, err, "updating cosmos sql DB in k8s")
assert.Eventually(func() bool {
throughputMatches, err := doesThroughputMatch(
ctx,
cosmosDBSQLDatabaseClient,
tc.resourceGroupName,
cosmosDBAccountName,
name,
throughPutRUs)
assert.Equal(nil, err, "err ensuring throughput matches")
return throughputMatches
}, tc.timeout, tc.retry, "wait for cosmos SQL DB throughput to be updated in Azure")
EnsureDelete(ctx, t, tc, sqlDBInstance)
}
func CosmosDBSQLDatabase_FailedThroughputUpdate(t *testing.T, cosmosDBAccountName string) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
assert := require.New(t)
name := GenerateTestResourceNameWithRandom("cosmosdbsql", 8)
namespace := "default"
sqlDBInstance := &v1alpha1.CosmosDBSQLDatabase{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1alpha1.CosmosDBSQLDatabaseSpec{
ResourceGroup: tc.resourceGroupName,
Account: cosmosDBAccountName,
},
}
// Create the Cosmos SQL DB
EnsureInstance(ctx, t, tc, sqlDBInstance)
// Get the updated Cosmos SQL DB from k8s
namespacedName := types.NamespacedName{Name: name, Namespace: namespace}
err := tc.k8sClient.Get(ctx, namespacedName, sqlDBInstance)
assert.Equal(nil, err, "getting cosmos SQL DB from k8s")
// Update the Cosmos SQL DB throughput (we expect that this will be rejected by Cosmos DB)
var throughPutRUs int32 = 1000
sqlDBInstance.Spec.Throughput = &throughPutRUs
err = tc.k8sClient.Update(ctx, sqlDBInstance)
assert.Equal(nil, err, "updating cosmos sql DB in k8s")
// Ensure we get an error eventually
assert.Eventually(func() bool {
updatedInstance := &v1alpha1.CosmosDBSQLDatabase{}
err = tc.k8sClient.Get(ctx, namespacedName, updatedInstance)
assert.Equal(nil, err, "err getting Cosmos SQL DB from k8s")
return updatedInstance.Status.Provisioned == false &&
updatedInstance.Status.FailedProvisioning == true &&
strings.Contains(updatedInstance.Status.Message, "Throughput update is not supported for resources created without dedicated throughput")
}, tc.timeout, tc.retry, "wait for sql database to be updated in k8s")
EnsureDelete(ctx, t, tc, sqlDBInstance)
}
func doesThroughputMatch(
ctx context.Context,
client documentdb.SQLResourcesClient,
rgName string,
cosmosDBAccountName string,
dbName string,
expectedThroughput int32) (bool, error) {
throughputSettings, err := client.GetSQLDatabaseThroughput(ctx, rgName, cosmosDBAccountName, dbName)
if err != nil {
return false, errors.Wrap(err, "err getting cosmos sql DB throughput from Azure")
}
if throughputSettings.ThroughputSettingsGetProperties == nil {
return false, nil
}
if throughputSettings.ThroughputSettingsGetProperties.Resource == nil {
return false, nil
}
if throughputSettings.ThroughputSettingsGetProperties.Resource.Throughput == nil {
return false, nil
}
return *throughputSettings.ThroughputSettingsGetProperties.Resource.Throughput == expectedThroughput, nil
}
func TestCosmosDBSQLDatabaseNoCosmosDBAccount(t *testing.T) {
t.Parallel()
defer PanicRecover(t)
ctx := context.Background()
//wrong resource group name
resourceGroupName := "gone"
name := GenerateTestResourceNameWithRandom("cosmosdbsql", 8)
namespace := "default"
sqlDBInstance := &v1alpha1.CosmosDBSQLDatabase{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: v1alpha1.CosmosDBSQLDatabaseSpec{
ResourceGroup: resourceGroupName,
Account: name,
},
}
EnsureInstanceWithResult(ctx, t, tc, sqlDBInstance, errhelp.ResourceGroupNotFoundErrorCode, false)
EnsureDelete(ctx, t, tc, sqlDBInstance)
}