|
| 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,atlas |
| 15 | + |
| 16 | +package atlas_test |
| 17 | + |
| 18 | +import ( |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "math/rand" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + "path/filepath" |
| 25 | + "strconv" |
| 26 | + "testing" |
| 27 | + "time" |
| 28 | + |
| 29 | + "github.com/mongodb/go-client-mongodb-atlas/mongodbatlas" |
| 30 | +) |
| 31 | + |
| 32 | +func TestAtlasMetrics(t *testing.T) { |
| 33 | + cliPath, err := filepath.Abs("../../bin/mongocli") |
| 34 | + if err != nil { |
| 35 | + t.Fatalf("unexpected error: %v", err) |
| 36 | + } |
| 37 | + _, err = os.Stat(cliPath) |
| 38 | + |
| 39 | + if err != nil { |
| 40 | + t.Fatalf("unexpected error: %v", err) |
| 41 | + } |
| 42 | + |
| 43 | + atlasEntity := "atlas" |
| 44 | + metricsEntity := "metrics" |
| 45 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 46 | + clusterName := fmt.Sprintf("e2e-cluster-%v", r.Uint32()) |
| 47 | + |
| 48 | + err = deployCluster(cliPath, atlasEntity, clusterName) |
| 49 | + if err != nil { |
| 50 | + t.Fatalf("unexpected error: %v", err) |
| 51 | + } |
| 52 | + |
| 53 | + hostname, err := getHostnameAndPort(cliPath, atlasEntity) |
| 54 | + if err != nil { |
| 55 | + t.Fatalf("unexpected error: %v", err) |
| 56 | + } |
| 57 | + |
| 58 | + t.Run("processes", func(t *testing.T) { |
| 59 | + cmd := exec.Command(cliPath, |
| 60 | + atlasEntity, |
| 61 | + metricsEntity, |
| 62 | + "processes", |
| 63 | + hostname, |
| 64 | + "--granularity=PT30M", |
| 65 | + "--period=P1DT12H") |
| 66 | + |
| 67 | + cmd.Env = os.Environ() |
| 68 | + resp, err := cmd.CombinedOutput() |
| 69 | + |
| 70 | + metrics := &mongodbatlas.ProcessMeasurements{} |
| 71 | + err = json.Unmarshal(resp, &metrics) |
| 72 | + |
| 73 | + if err != nil { |
| 74 | + t.Fatalf("unexpected error: %v", err) |
| 75 | + } |
| 76 | + |
| 77 | + if metrics.Measurements == nil { |
| 78 | + t.Errorf("there are no measurements") |
| 79 | + } |
| 80 | + |
| 81 | + if len(metrics.Measurements) == 0 { |
| 82 | + t.Errorf("got=%#v\nwant=%#v\n", 0, "len(metrics.Measurements) > 0") |
| 83 | + } |
| 84 | + }) |
| 85 | + |
| 86 | + deleteCluster(cliPath, atlasEntity, clusterName) |
| 87 | +} |
| 88 | + |
| 89 | +func getHostnameAndPort(cliPath, atlasEntity string) (string, error) { |
| 90 | + cmd := exec.Command(cliPath, |
| 91 | + atlasEntity, |
| 92 | + "processes", |
| 93 | + "list") |
| 94 | + |
| 95 | + cmd.Env = os.Environ() |
| 96 | + resp, err := cmd.CombinedOutput() |
| 97 | + |
| 98 | + var processes []*mongodbatlas.Process |
| 99 | + err = json.Unmarshal(resp, &processes) |
| 100 | + |
| 101 | + if err != nil { |
| 102 | + return "", err |
| 103 | + } |
| 104 | + |
| 105 | + if len(processes) == 0 { |
| 106 | + return "", fmt.Errorf("got=%#v\nwant=%#v\n", 0, "len(processes) > 0") |
| 107 | + } |
| 108 | + |
| 109 | + // The first element may not be the created cluster but that is fine since |
| 110 | + // we just need one cluster up and running |
| 111 | + return processes[0].Hostname + ":" + strconv.Itoa(processes[0].Port), nil |
| 112 | +} |
| 113 | + |
| 114 | +func deployCluster(cliPath, atlasEntity, clusterName string) error { |
| 115 | + cmd := exec.Command(cliPath, |
| 116 | + atlasEntity, |
| 117 | + "clusters", |
| 118 | + "create", |
| 119 | + clusterName, |
| 120 | + "--region=US_EAST_1", |
| 121 | + "--members=3", |
| 122 | + "--tier=M10", |
| 123 | + "--provider=AWS", |
| 124 | + "--mdbVersion=4.0", |
| 125 | + "--diskSizeGB=10") |
| 126 | + cmd.Env = os.Environ() |
| 127 | + err := cmd.Run() |
| 128 | + |
| 129 | + if err != nil { |
| 130 | + return err |
| 131 | + } |
| 132 | + |
| 133 | + cmd = exec.Command(cliPath, |
| 134 | + "atlas", |
| 135 | + "clusters", |
| 136 | + "watch", |
| 137 | + clusterName) |
| 138 | + cmd.Env = os.Environ() |
| 139 | + return cmd.Run() |
| 140 | +} |
| 141 | + |
| 142 | +func deleteCluster(cliPath, atlasEntity, clusterName string) error { |
| 143 | + cmd := exec.Command(cliPath, atlasEntity, "clusters", "delete", clusterName, "--force") |
| 144 | + cmd.Env = os.Environ() |
| 145 | + return cmd.Run() |
| 146 | +} |
0 commit comments