|
| 1 | +// Copyright 2020 MongoDB Inc |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// +build e2e |
| 15 | + |
| 16 | +package cloud_manager_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strings" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" |
| 30 | +) |
| 31 | + |
| 32 | +const ( |
| 33 | + roleReadWrite = "readWrite" |
| 34 | +) |
| 35 | + |
| 36 | +func TestCloudManagerDBUsers(t *testing.T) { |
| 37 | + cliPath, err := filepath.Abs("../../bin/mongocli") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("unexpected error: %v", err) |
| 40 | + } |
| 41 | + _, err = os.Stat(cliPath) |
| 42 | + |
| 43 | + if err != nil { |
| 44 | + t.Fatalf("unexpected error: %v", err) |
| 45 | + } |
| 46 | + |
| 47 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 48 | + |
| 49 | + entity := "cloud-manager" |
| 50 | + dbusersEntity := "dbusers" |
| 51 | + username := fmt.Sprintf("user-%v", r.Uint32()) |
| 52 | + |
| 53 | + t.Run("Create", func(t *testing.T) { |
| 54 | + cmd := exec.Command(cliPath, |
| 55 | + entity, |
| 56 | + dbusersEntity, |
| 57 | + "create", |
| 58 | + "--username="+username, |
| 59 | + "--password=passW0rd", |
| 60 | + "--role=readWriteAnyDatabase", |
| 61 | + "--mechanisms=SCRAM-SHA-256 ") |
| 62 | + cmd.Env = os.Environ() |
| 63 | + resp, err := cmd.CombinedOutput() |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) |
| 67 | + } |
| 68 | + |
| 69 | + if !strings.Contains(string(resp), "Changes are being applied") { |
| 70 | + t.Errorf("got=%#v\nwant=%#v\n", string(resp), "Changes are being applied") |
| 71 | + } |
| 72 | + }) |
| 73 | + |
| 74 | + t.Run("List", func(t *testing.T) { |
| 75 | + cmd := exec.Command(cliPath, entity, dbusersEntity, "ls") |
| 76 | + cmd.Env = os.Environ() |
| 77 | + resp, err := cmd.CombinedOutput() |
| 78 | + |
| 79 | + if err != nil { |
| 80 | + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) |
| 81 | + } |
| 82 | + |
| 83 | + users := []mongodbatlas.DatabaseUser{} |
| 84 | + err = json.Unmarshal(resp, &users) |
| 85 | + |
| 86 | + if len(users) == 0 { |
| 87 | + t.Fatalf("expected len(users) > 0, got 0") |
| 88 | + } |
| 89 | + |
| 90 | + }) |
| 91 | + |
| 92 | + t.Run("Delete", func(t *testing.T) { |
| 93 | + cmd := exec.Command(cliPath, entity, dbusersEntity, "delete", username, "--force", "--authDB", "admin") |
| 94 | + cmd.Env = os.Environ() |
| 95 | + resp, err := cmd.CombinedOutput() |
| 96 | + |
| 97 | + if err != nil { |
| 98 | + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) |
| 99 | + } |
| 100 | + |
| 101 | + if !strings.Contains(string(resp), "Changes are being applied") { |
| 102 | + t.Errorf("got=%#v\nwant=%#v\n", string(resp), "Changes are being applied") |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | +} |
0 commit comments