Skip to content

Commit

Permalink
CLOUDP-74585: Allow to stop monitoring a host (#483)
Browse files Browse the repository at this point in the history
  • Loading branch information
gssbzn committed Oct 8, 2020
1 parent f818f30 commit 803ff28
Show file tree
Hide file tree
Showing 9 changed files with 338 additions and 39 deletions.
81 changes: 44 additions & 37 deletions e2e/cloud_manager/deploy_replica_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func TestDeployReplicaSet(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}

const clustersEntity = "clusters"
const testFile = "om-new-cluster.json"

n, err := e2e.RandInt(1000)
Expand All @@ -54,31 +53,17 @@ func TestDeployReplicaSet(t *testing.T) {
entity,
clustersEntity,
"apply",
"-f="+testFile,
"-f",
testFile,
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
if resp, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
}
})

t.Run("Watch", func(t *testing.T) {
cmd := exec.Command(cliPath,
entity,
"automation",
"watch",
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
}
})
t.Run("Watch", watch(cliPath))

t.Run("List", func(t *testing.T) {
cmd := exec.Command(cliPath,
Expand All @@ -96,7 +81,6 @@ func TestDeployReplicaSet(t *testing.T) {
}
var clusters []*convert.ClusterConfig
if err := json.Unmarshal(resp, &clusters); err != nil {
t.Log(string(resp))
t.Fatalf("unexpected error: %v", err)
}

Expand Down Expand Up @@ -140,42 +124,65 @@ func TestDeployReplicaSet(t *testing.T) {
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
if resp, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
}
})

t.Run("Watch", func(t *testing.T) {
t.Run("Watch", watch(cliPath))

t.Run("Delete", func(t *testing.T) {
cmd := exec.Command(cliPath,
entity,
"automation",
"watch",
clustersEntity,
"rm",
clusterName,
"--force",
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
if resp, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
}
})

t.Run("Watch", watch(cliPath))

t.Run("Stop Monitoring", func(t *testing.T) {
hostIDs, err := hostIDs(cliPath)
if err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
t.Fatalf("unexpected error: %v\n", err)
}
for _, h := range hostIDs {
cmd := exec.Command(cliPath,
entity,
monitoringEntity,
"rm",
h,
"--force",
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
t.Errorf("unexpected error: %v, resp: %v\n", err, string(resp))
}
}
})
}

t.Run("Delete", func(t *testing.T) {
func watch(cliPath string) func(t *testing.T) {
return func(t *testing.T) {
cmd := exec.Command(cliPath,
entity,
clustersEntity,
"rm",
clusterName,
"--force",
"automation",
"watch",
)

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

if err != nil {
if resp, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("unexpected error: %v, resp: %v\n", err, string(resp))
}
})
}
}
25 changes: 25 additions & 0 deletions e2e/cloud_manager/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ const (
serversEntity = "servers"
iamEntity = "iam"
projectsEntity = "projects"
clustersEntity = "clusters"
maintenanceEntity = "maintenanceWindows"
monitoringEntity = "monitoring"
processesEntity = "processes"
)

// automationServerHostname tries to list available server running the automation agent
Expand All @@ -56,6 +59,28 @@ func automationServerHostname(cliPath string) (string, error) {
return servers.Results[0].Hostname, nil
}

func hostIDs(cliPath string) ([]string, error) {
cmd := exec.Command(cliPath, entity, processesEntity, "list", "-o=json")
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
if err != nil {
return nil, err
}

var servers *opsmngr.Hosts
if err := json.Unmarshal(resp, &servers); err != nil {
return nil, err
}
if servers.TotalCount == 0 {
return nil, errors.New("no hosts available")
}
result := make([]string, len(servers.Results))
for i, h := range servers.Results {
result[i] = h.ID
}
return result, nil
}

func generateConfig(filename, hostname, clusterName, version, fcVersion string) error {
feedFile, err := os.Create(filename)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/cli/opsmanager/monitoring/enable.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func Builder() *cobra.Command {

cmd.AddCommand(
EnableBuilder(),
StopBuilder(),
)
return cmd
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/opsmanager/monitoring/enable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestBuilder(t *testing.T) {
cli.CmdValidator(
t,
Builder(),
1,
2,
[]string{},
)
}
Expand Down
68 changes: 68 additions & 0 deletions internal/cli/opsmanager/monitoring/stop.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package monitoring

import (
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/config"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/store"
"github.com/mongodb/mongocli/internal/usage"
"github.com/spf13/cobra"
)

type StopOpts struct {
cli.GlobalOpts
*cli.DeleteOpts
store store.MonitoringStopper
}

func (opts *StopOpts) initStore() error {
var err error
opts.store, err = store.New(config.Default())
return err
}

func (opts *StopOpts) Run() error {
return opts.Delete(opts.store.StopMonitoring, opts.ConfigProjectID())
}

// mongocli ops-manager monitoring stop <ID> --force
func StopBuilder() *cobra.Command {
opts := &StopOpts{
DeleteOpts: cli.NewDeleteOpts("Stopped monitoring '%s'\n", "Monitoring did not stop"),
}
cmd := &cobra.Command{
Use: "stop <ID>",
Aliases: []string{"rm"},
Short: "Stops monitoring the MongoDB process specified",
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
if err := opts.PreRunE(opts.initStore); err != nil {
return err
}
opts.Entry = args[0]
return opts.Prompt()
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run()
},
}

cmd.Flags().BoolVar(&opts.Confirm, flag.Force, false, usage.Force)
cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)

return cmd
}
57 changes: 57 additions & 0 deletions internal/cli/opsmanager/monitoring/stop_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright 2020 MongoDB Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package monitoring

import (
"testing"

"github.com/golang/mock/gomock"
"github.com/mongodb/mongocli/internal/cli"
"github.com/mongodb/mongocli/internal/flag"
"github.com/mongodb/mongocli/internal/mocks"
)

func TestStopBuilder(t *testing.T) {
cli.CmdValidator(
t,
StopBuilder(),
0,
[]string{flag.ProjectID, flag.Force},
)
}

func TestStopOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockMonitoringStopper(ctrl)
defer ctrl.Finish()

opts := &StopOpts{
store: mockStore,
DeleteOpts: &cli.DeleteOpts{
Confirm: true,
Entry: "test",
},
}

mockStore.
EXPECT().
StopMonitoring(opts.ProjectID, opts.Entry).
Return(nil).
Times(1)

if err := opts.Run(); err != nil {
t.Fatalf("Run() unexpected error: %v", err)
}
}
2 changes: 1 addition & 1 deletion internal/cli/opsmanager/processes/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func DescribeBuilder() *cobra.Command {
cmd := &cobra.Command{
Use: "describe <ID>",
Short: describeProcesses,
Aliases: []string{"d"},
Aliases: []string{"get"},
Args: cobra.ExactArgs(1),
PreRunE: func(cmd *cobra.Command, args []string) error {
return opts.PreRunE(
Expand Down
Loading

0 comments on commit 803ff28

Please sign in to comment.