Skip to content

Commit 1aa8ce3

Browse files
authored
Merge pull request #167 from arangodb/test/various-improvements
Various test improvements
2 parents a373283 + d2f6d48 commit 1aa8ce3

5 files changed

+38
-46
lines changed

scripts/kube_create_storage.sh

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ if [ "${REQUIRE_LOCAL_STORAGE}" = "1" ]; then
2020
else
2121
echo "No local storage needed for this cluster"
2222
fi
23+
echo "Found $(kubectl get pv | wc -l) PersistentVolumes"
24+
echo "Found $(kubectl get pv | grep Available | wc -l) available PersistentVolumes"

tests/cursor_test.go

+4-13
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import (
2929
"time"
3030

3131
"github.com/dchest/uniuri"
32-
"github.com/stretchr/testify/assert"
32+
"github.com/stretchr/testify/require"
3333

3434
driver "github.com/arangodb/go-driver"
3535
api "github.com/arangodb/kube-arangodb/pkg/apis/deployment/v1alpha"
@@ -71,10 +71,7 @@ func TestCursorSingle(t *testing.T) {
7171
}
7272

7373
// Check server role
74-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
75-
role, err := client.ServerRole(ctx)
76-
assert.NoError(t, err)
77-
assert.Equal(t, driver.ServerRoleSingle, role)
74+
require.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingle))
7875

7976
// Run cursor tests
8077
runCursorTests(t, client)
@@ -118,10 +115,7 @@ func TestCursorActiveFailover(t *testing.T) {
118115
}
119116

120117
// Check server role
121-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
122-
role, err := client.ServerRole(ctx)
123-
assert.NoError(t, err)
124-
assert.Equal(t, driver.ServerRoleSingleActive, role)
118+
require.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingleActive))
125119

126120
// Run cursor tests
127121
runCursorTests(t, client)
@@ -165,10 +159,7 @@ func TestCursorCluster(t *testing.T) {
165159
}
166160

167161
// Check server role
168-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
169-
role, err := client.ServerRole(ctx)
170-
assert.NoError(t, err)
171-
assert.Equal(t, driver.ServerRoleCoordinator, role)
162+
require.NoError(t, testServerRole(ctx, client, driver.ServerRoleCoordinator))
172163

173164
// Run cursor tests
174165
runCursorTests(t, client)

tests/service_account_test.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,7 @@ func TestServiceAccountSingle(t *testing.T) {
8484
checkMembersUsingServiceAccount(kubecli, ns, apiObject.Status.Members.Single, saName, t)
8585

8686
// Check server role
87-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
88-
role, err := client.ServerRole(ctx)
89-
assert.NoError(t, err)
90-
assert.Equal(t, driver.ServerRoleSingle, role)
87+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingle))
9188
}
9289

9390
// TestServiceAccountActiveFailover tests the creating of a ActiveFailover server deployment
@@ -137,10 +134,7 @@ func TestServiceAccountActiveFailover(t *testing.T) {
137134
checkMembersUsingServiceAccount(kubecli, ns, apiObject.Status.Members.Agents, saName, t)
138135

139136
// Check server role
140-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
141-
role, err := client.ServerRole(ctx)
142-
assert.NoError(t, err)
143-
assert.Equal(t, driver.ServerRoleSingleActive, role)
137+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingleActive))
144138
}
145139

146140
// TestServiceAccountCluster tests the creating of a cluster deployment
@@ -192,10 +186,7 @@ func TestServiceAccountCluster(t *testing.T) {
192186
checkMembersUsingServiceAccount(kubecli, ns, apiObject.Status.Members.DBServers, saName, t)
193187

194188
// Check server role
195-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
196-
role, err := client.ServerRole(ctx)
197-
assert.NoError(t, err)
198-
assert.Equal(t, driver.ServerRoleCoordinator, role)
189+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleCoordinator))
199190
}
200191

201192
// TestServiceAccountClusterWithSync tests the creating of a cluster deployment
@@ -262,10 +253,7 @@ func TestServiceAccountClusterWithSync(t *testing.T) {
262253
checkMembersUsingServiceAccount(kubecli, ns, apiObject.Status.Members.SyncWorkers, saName, t)
263254

264255
// Check server role
265-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
266-
role, err := client.ServerRole(ctx)
267-
assert.NoError(t, err)
268-
assert.Equal(t, driver.ServerRoleCoordinator, role)
256+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleCoordinator))
269257
}
270258

271259
// mustCreateServiceAccount creates an empty service account with random name and returns

tests/simple_test.go

+4-16
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,7 @@ func TestSimpleSingle(t *testing.T) {
7070
}
7171

7272
// Check server role
73-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
74-
role, err := client.ServerRole(ctx)
75-
assert.NoError(t, err)
76-
assert.Equal(t, driver.ServerRoleSingle, role)
73+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingle))
7774
}
7875

7976
// TestSimpleActiveFailover tests the creating of a ActiveFailover server deployment
@@ -111,10 +108,7 @@ func TestSimpleActiveFailover(t *testing.T) {
111108
}
112109

113110
// Check server role
114-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
115-
role, err := client.ServerRole(ctx)
116-
assert.NoError(t, err)
117-
assert.Equal(t, driver.ServerRoleSingleActive, role)
111+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleSingleActive))
118112
}
119113

120114
// TestSimpleCluster tests the creating of a cluster deployment
@@ -152,10 +146,7 @@ func TestSimpleCluster(t *testing.T) {
152146
}
153147

154148
// Check server role
155-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
156-
role, err := client.ServerRole(ctx)
157-
assert.NoError(t, err)
158-
assert.Equal(t, driver.ServerRoleCoordinator, role)
149+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleCoordinator))
159150
}
160151

161152
// TestSimpleClusterWithSync tests the creating of a cluster deployment
@@ -204,8 +195,5 @@ func TestSimpleClusterWithSync(t *testing.T) {
204195
}
205196

206197
// Check server role
207-
assert.NoError(t, client.SynchronizeEndpoints(ctx))
208-
role, err := client.ServerRole(ctx)
209-
assert.NoError(t, err)
210-
assert.Equal(t, driver.ServerRoleCoordinator, role)
198+
assert.NoError(t, testServerRole(ctx, client, driver.ServerRoleCoordinator))
211199
}

tests/test_util.go

+24-1
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,30 @@ func waitUntilArangoDeploymentHealthy(deployment *api.ArangoDeployment, DBClient
506506
}
507507
}
508508
default:
509-
return maskAny(fmt.Errorf("DeploymentMode %s is not supported!", mode))
509+
return maskAny(fmt.Errorf("DeploymentMode %s is not supported", mode))
510+
}
511+
return nil
512+
}
513+
514+
// testServerRole performs a synchronize endpoints and then requests the server role.
515+
// On success, the role is compared with the given expected role.
516+
// When the requests fail or the role is not equal to the expected role, an error is returned.
517+
func testServerRole(ctx context.Context, client driver.Client, expectedRole driver.ServerRole) error {
518+
op := func(ctx context.Context) error {
519+
if err := client.SynchronizeEndpoints(ctx); err != nil {
520+
return maskAny(err)
521+
}
522+
role, err := client.ServerRole(ctx)
523+
if err != nil {
524+
return maskAny(err)
525+
}
526+
if role != expectedRole {
527+
return retry.Permanent(fmt.Errorf("Unexpected server role: Expected '%s', got '%s'", expectedRole, role))
528+
}
529+
return nil
530+
}
531+
if err := retry.RetryWithContext(ctx, op, time.Second*20); err != nil {
532+
return maskAny(err)
510533
}
511534
return nil
512535
}

0 commit comments

Comments
 (0)