Skip to content

Commit 082122a

Browse files
CLOUDP-63103: Implementing e2e test for atlas metric processes (#181)
1 parent 07e3763 commit 082122a

File tree

4 files changed

+167
-1
lines changed

4 files changed

+167
-1
lines changed

.evergreen.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
exec_timeout_secs: 3600 # automatically fail any task if it takes longer than 60 minutes to finish.
12
stepback: true
23
command_type: system
34
pre_error_fails_task: true

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ build: ## Generate a binary in ./bin
9595
e2e-test: build ## Run E2E tests
9696
@echo "==> Running E2E tests..."
9797
# the target assumes the MCLI-* environment variables are exported
98-
$(E2E_CMD) -v -p 1 -parallel 1 -tags="$(E2E_TAGS)" ./e2e...
98+
$(E2E_CMD) -v -p 1 -parallel 1 -timeout 0 -tags="$(E2E_TAGS)" ./e2e...
9999

100100
.PHONY: install
101101
install: ## Install a binary in $GOPATH/bin

e2e/atlas/clusters_test.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"os"
2323
"os/exec"
2424
"path/filepath"
25+
"strings"
2526
"testing"
2627
"time"
2728

@@ -73,6 +74,24 @@ func TestAtlasClusters(t *testing.T) {
7374
ensureCluster(t, cluster, clusterName, "4.0", 10)
7475
})
7576

77+
t.Run("Watch", func(t *testing.T) {
78+
cmd := exec.Command(cliPath,
79+
atlasEntity,
80+
clustersEntity,
81+
"watch",
82+
clusterName)
83+
cmd.Env = os.Environ()
84+
resp, err := cmd.CombinedOutput()
85+
86+
if err != nil {
87+
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
88+
}
89+
90+
if !strings.Contains(string(resp), "Cluster available at:") {
91+
t.Errorf("got=%#v\nwant=%#v\n", string(resp), "Cluster available at:")
92+
}
93+
})
94+
7695
t.Run("List", func(t *testing.T) {
7796
cmd := exec.Command(cliPath, atlasEntity, clustersEntity, "ls")
7897
cmd.Env = os.Environ()

e2e/atlas/metrics_processes_test.go

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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

Comments
 (0)