From e2be8a0aa134691cec928fe649612152876b11b2 Mon Sep 17 00:00:00 2001 From: Michel Vocks Date: Thu, 23 Jan 2020 16:42:05 +0100 Subject: [PATCH] Fix failing test --- builtin/logical/database/rotation_test.go | 32 ++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/builtin/logical/database/rotation_test.go b/builtin/logical/database/rotation_test.go index c1de9bbbe51a..684d89261093 100644 --- a/builtin/logical/database/rotation_test.go +++ b/builtin/logical/database/rotation_test.go @@ -2,6 +2,8 @@ package database import ( "context" + "go.mongodb.org/mongo-driver/mongo" + "go.mongodb.org/mongo-driver/mongo/options" "log" "strings" "testing" @@ -843,6 +845,11 @@ func TestBackend_StaticRole_Rotations_MongoDB(t *testing.T) { // configure backend, add item and confirm length cleanup, connURL := mongodb.PrepareTestContainerWithDatabase(t, "latest", "vaulttestdb") defer cleanup() + testCases := []string{"65", "130", "5400"} + // Create database users ahead + for _, tc := range testCases { + testCreateDBUser(t, connURL, "vaulttestdb", "statictestMongo" + tc, "test") + } // Configure a connection data := map[string]interface{}{ @@ -865,7 +872,6 @@ func TestBackend_StaticRole_Rotations_MongoDB(t *testing.T) { } // create three static roles with different rotation periods - testCases := []string{"65", "130", "5400"} for _, tc := range testCases { roleName := "plugin-static-role-" + tc data = map[string]interface{}{ @@ -956,6 +962,30 @@ func TestBackend_StaticRole_Rotations_MongoDB(t *testing.T) { } } +func testCreateDBUser(t testing.TB, connURL, db, username, password string) { + ctx, _ := context.WithTimeout(context.Background(), 10*time.Second) + client, err := mongo.Connect(ctx, options.Client().ApplyURI(connURL)) + if err != nil { + t.Fatal(err) + } + + createUserCmd := &createUserCommand{ + Username: username, + Password: password, + Roles: []interface{}{}, + } + result := client.Database(db).RunCommand(ctx, createUserCmd, nil) + if result.Err() != nil { + t.Fatal(result.Err()) + } +} + +type createUserCommand struct { + Username string `bson:"createUser"` + Password string `bson:"pwd"` + Roles []interface{} `bson:"roles"` +} + // Demonstrates a bug fix for the credential rotation not releasing locks func TestBackend_StaticRole_LockRegression(t *testing.T) { cluster, sys := getCluster(t)