|
| 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 cloudmanager,generic |
| 15 | + |
| 16 | +package cloud_manager_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "os" |
| 22 | + "os/exec" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/mongodb/mongocli/e2e" |
| 26 | + "go.mongodb.org/ops-manager/opsmngr" |
| 27 | +) |
| 28 | + |
| 29 | +func TestFeaturePolicies(t *testing.T) { |
| 30 | + const policyExternallyManagedLock = "EXTERNALLY_MANAGED_LOCK" |
| 31 | + const policyDisableUserManagement = "DISABLE_USER_MANAGEMENT" |
| 32 | + |
| 33 | + n, err := e2e.RandInt(255) |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("unexpected error: %v", err) |
| 36 | + } |
| 37 | + |
| 38 | + projectName := fmt.Sprintf("e2e-maintenance-proj-%v", n) |
| 39 | + projectID, err := createProject(projectName) |
| 40 | + if err != nil { |
| 41 | + t.Fatalf("unexpected error: %v", err) |
| 42 | + } |
| 43 | + |
| 44 | + cliPath, err := e2e.Bin() |
| 45 | + if err != nil { |
| 46 | + t.Fatalf("unexpected error: %v", err) |
| 47 | + } |
| 48 | + |
| 49 | + defer func() { |
| 50 | + if e := deleteProject(projectID); e != nil { |
| 51 | + t.Errorf("error deleting project: %v", e) |
| 52 | + } |
| 53 | + }() |
| 54 | + |
| 55 | + t.Run("Update", func(t *testing.T) { |
| 56 | + cmd := exec.Command(cliPath, |
| 57 | + entity, |
| 58 | + featurePolicies, |
| 59 | + "update", |
| 60 | + "--name", |
| 61 | + "test", |
| 62 | + "--policy", |
| 63 | + policyExternallyManagedLock, |
| 64 | + "--policy", |
| 65 | + policyDisableUserManagement, |
| 66 | + "-o=json", |
| 67 | + "--projectId", |
| 68 | + projectID, |
| 69 | + ) |
| 70 | + |
| 71 | + cmd.Env = os.Environ() |
| 72 | + resp, err := cmd.CombinedOutput() |
| 73 | + |
| 74 | + if err != nil { |
| 75 | + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) |
| 76 | + } |
| 77 | + |
| 78 | + var policy *opsmngr.FeaturePolicy |
| 79 | + if err := json.Unmarshal(resp, &policy); err != nil { |
| 80 | + t.Fatalf("unexpected error: %v", err) |
| 81 | + } |
| 82 | + |
| 83 | + if len(policy.Policies) != 2 { |
| 84 | + t.Error("Policy count mismatch") |
| 85 | + } |
| 86 | + |
| 87 | + foundExternalManagedLock := false |
| 88 | + foundDisableUserManagement := false |
| 89 | + for _, p := range policy.Policies { |
| 90 | + if p.Policy == policyExternallyManagedLock { |
| 91 | + foundExternalManagedLock = true |
| 92 | + } else if p.Policy == policyDisableUserManagement { |
| 93 | + foundDisableUserManagement = true |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if !foundExternalManagedLock { |
| 98 | + t.Errorf("policy %s not found", policyExternallyManagedLock) |
| 99 | + } |
| 100 | + |
| 101 | + if !foundDisableUserManagement { |
| 102 | + t.Errorf("policy %s not found", policyDisableUserManagement) |
| 103 | + } |
| 104 | + }) |
| 105 | + |
| 106 | + t.Run("List", func(t *testing.T) { |
| 107 | + cmd := exec.Command(cliPath, |
| 108 | + entity, |
| 109 | + featurePolicies, |
| 110 | + "list", |
| 111 | + "-o=json", |
| 112 | + "--projectId", |
| 113 | + projectID, |
| 114 | + ) |
| 115 | + |
| 116 | + cmd.Env = os.Environ() |
| 117 | + resp, err := cmd.CombinedOutput() |
| 118 | + |
| 119 | + if err != nil { |
| 120 | + t.Fatalf("unexpected error: %v, resp: %v", err, string(resp)) |
| 121 | + } |
| 122 | + |
| 123 | + var policy *opsmngr.FeaturePolicy |
| 124 | + if err := json.Unmarshal(resp, &policy); err != nil { |
| 125 | + t.Fatalf("unexpected error: %v", err) |
| 126 | + } |
| 127 | + |
| 128 | + if len(policy.Policies) == 0 { |
| 129 | + t.Error("No policies found") |
| 130 | + } |
| 131 | + }) |
| 132 | +} |
0 commit comments