Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CLOUDP-196384 e2e test for "atlas deployments setup --type LOCAL" #2229

Merged
merged 34 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
ee4698a
CLOUDP-196384 e2e test for "atlas deployments setup --type LOCAL"
fmenezes Sep 11, 2023
6e58313
fix evergreen file
fmenezes Sep 11, 2023
2584c6a
fix lint
fmenezes Sep 11, 2023
6a14ebc
move podman logs into stderr
fmenezes Sep 11, 2023
9074405
Merge branch 'master' into CLOUDP-196384
fmenezes Sep 11, 2023
c650926
fix license
fmenezes Sep 11, 2023
f4490ef
update podman
fmenezes Sep 11, 2023
51744ce
fix update podman
fmenezes Sep 12, 2023
9b552f3
install mongosh
fmenezes Sep 12, 2023
fd99459
fix install mongosh
fmenezes Sep 12, 2023
df6a3ab
fix permission
fmenezes Sep 12, 2023
0754101
Merge branch 'master' into CLOUDP-196384
fmenezes Sep 12, 2023
b1b3a87
wait for mongot
fmenezes Sep 12, 2023
2ab0101
fix lint
fmenezes Sep 12, 2023
994ecf2
named index
fmenezes Sep 12, 2023
a8ed715
extend timeout
fmenezes Sep 13, 2023
b5df95e
fix timeout
fmenezes Sep 13, 2023
16e18a3
wait for index creation
fmenezes Sep 13, 2023
d4d1224
add debug logs
fmenezes Sep 13, 2023
df86ad0
fix lint
fmenezes Sep 13, 2023
1e95a18
change instance size in evg
fmenezes Sep 13, 2023
bd50c44
Merge branch 'master' into CLOUDP-196384
fmenezes Sep 13, 2023
16683e3
dump diagnostics
fmenezes Sep 13, 2023
5008cf3
fix diagnostics
fmenezes Sep 13, 2023
d833e5a
get logs on linux
fmenezes Sep 13, 2023
2da5ddf
store errors
fmenezes Sep 13, 2023
ad6fa05
get container logs
fmenezes Sep 13, 2023
d1a58c6
Merge branch 'master' into CLOUDP-196384
fmenezes Sep 13, 2023
6be357f
remove mongosh and update podman settings
fmenezes Sep 13, 2023
1b005f6
Merge branch 'master' into CLOUDP-196384
fmenezes Sep 13, 2023
9c812cc
fix lint
fmenezes Sep 13, 2023
1673567
change from dns to ip
fmenezes Sep 14, 2023
6b23192
fix lint
fmenezes Sep 14, 2023
5e2947c
update to exec
fmenezes Sep 14, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions build/ci/evergreen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,19 @@ functions:
set -Eeou pipefail
export PATH="$ADD_PATH:$PATH"
curl -sfL https://github.com/gotestyourself/gotestsum/releases/download/v${gotestsum_ver}/gotestsum_${gotestsum_ver}_linux_amd64.tar.gz | tar zx
"update podman":
- command: shell.exec
type: setup
params:
<<: *go_options
working_dir: src/github.com/mongodb/mongodb-atlas-cli/bin
shell: bash
script: |
set -Eeou pipefail
export PATH="$ADD_PATH:$PATH"
sudo yum update -y podman
sudo yum install -y netavark
sudo /bin/sh -c "cat /usr/share/containers/containers.conf | sed 's|network_backend = \"cni\"|network_backend = \"netavark\"|' > /usr/share/containers/containers2.conf && mv /usr/share/containers/containers2.conf /usr/share/containers/containers.conf"
"install snyk":
- command: shell.exec
type: setup
Expand Down Expand Up @@ -1305,6 +1318,28 @@ tasks:
MCLI_SERVICE: cloudgov
E2E_TAGS: atlas,cleanup
E2E_PARALLEL: 16
- name: atlas_deployments_e2e
tags: ["e2e","deployments","atlas"]
must_have_test_results: true
exec_timeout_secs: 11400 # 3 hours 10 minutes
depends_on:
- name: compile
variant: "code_health"
patch_optional: true
commands:
- func: "install gotestsum"
- func: "update podman"
- func: "e2e test"
timeout_secs: 11400 # 3 hours 10 minutes
vars:
MCLI_ORG_ID: ${atlas_org_id}
MCLI_PROJECT_ID: ${atlas_project_id}
MCLI_PRIVATE_API_KEY: ${atlas_private_api_key}
MCLI_PUBLIC_API_KEY: ${atlas_public_api_key}
MCLI_OPS_MANAGER_URL: ${mcli_ops_manager_url}
MCLI_SERVICE: cloud
E2E_TAGS: atlas,deployments
E2E_TIMEOUT: 3h
buildvariants:
- name: code_health
display_name: "Code Health"
Expand Down Expand Up @@ -1443,5 +1478,13 @@ buildvariants:
<<: *go_linux_version
tasks:
- name: .snyk
- name: e2e_atlas_deployments
display_name: "E2E Atlas Deployments Tests"
run_on:
- rhel80-large
expansions:
<<: *go_linux_version
tasks:
- name: ".e2e .deployments .atlas"
include:
- filename: build/ci/release.yml
2 changes: 1 addition & 1 deletion internal/cli/atlas/atlas.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func Builder() *cobra.Command {
Short: "MongoDB Atlas operations.",
Long: deprecatedMessage,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.ErrOrStderr())
log.SetWriter(cmd.ErrOrStderr())

_, _ = log.Warning(deprecatedMessage)
if err := opts.InitFlow(config.Default())(); err != nil {
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/atlas/deployments/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,8 @@ func ConnectBuilder() *cobra.Command {
if len(args) == 1 {
opts.DeploymentName = args[0]
}
w := cmd.OutOrStdout()
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), w)
return opts.PreRunE(opts.InitOutput(w, ""), opts.InitStore(opts.podmanClient), opts.validateFlags)
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return opts.PreRunE(opts.InitOutput(cmd.OutOrStdout(), ""), opts.InitStore(opts.podmanClient), opts.validateFlags)
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run(cmd.Context())
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/atlas/deployments/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,8 @@ func DeleteBuilder() *cobra.Command {
if len(args) == 1 {
opts.DeploymentName = args[0]
}
w := cmd.OutOrStdout()
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), w)
return opts.PreRunE(opts.InitOutput(w, ""), opts.InitStore(opts.podmanClient))
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return opts.PreRunE(opts.InitOutput(cmd.OutOrStdout(), ""), opts.InitStore(opts.podmanClient))
},
RunE: func(cmd *cobra.Command, args []string) error {
return opts.Run(cmd.Context())
Expand Down
10 changes: 6 additions & 4 deletions internal/cli/atlas/deployments/diagnostics.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,13 @@ func (opts *diagnosticsOpts) Run(ctx context.Context) error {
opts.podmanDiag.Errors = append(opts.podmanDiag.Errors, fmt.Errorf("failed to get podman logs: %w", err).Error())
}

if opts.DeploymentName != "" && opts.podmanDiag.MachineInfo.State == podman.PodmanRunningState {
if opts.DeploymentName != "" {
_, _ = log.Warningf("Fetching logs for deployment %s\n", opts.DeploymentName)
// ignore error if container does not exist just capture log for that command
opts.mongotLogs, _ = opts.podmanClient.ContainerLogs(ctx, opts.LocalMongotHostname())
opts.mongodLogs, _ = opts.podmanClient.ContainerLogs(ctx, opts.LocalMongodHostname())
opts.mongotLogs, err = opts.podmanClient.ContainerLogs(ctx, opts.LocalMongotHostname())
opts.podmanDiag.Errors = append(opts.podmanDiag.Errors, fmt.Errorf("failed to get mongot logs: %w", err).Error())
opts.mongodLogs, err = opts.podmanClient.ContainerLogs(ctx, opts.LocalMongodHostname())
opts.podmanDiag.Errors = append(opts.podmanDiag.Errors, fmt.Errorf("failed to get mongod logs: %w", err).Error())
}

diagnosis := map[string]interface{}{
Expand All @@ -89,7 +91,7 @@ func DiagnosticsBuilder() *cobra.Command {
machineDiag: &machineDiagnostic{},
}
cmd := &cobra.Command{
Use: "diagnostics",
Use: "diagnostics [deploymentName]",
Short: "Fetch detailed information about all your deployments and system processes.",
Hidden: true, // always hidden
Args: require.MaximumNArgs(1),
Expand Down
5 changes: 2 additions & 3 deletions internal/cli/atlas/deployments/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/mongodb/mongodb-atlas-cli/internal/cli/require"
"github.com/mongodb/mongodb-atlas-cli/internal/config"
"github.com/mongodb/mongodb-atlas-cli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/internal/log"
"github.com/mongodb/mongodb-atlas-cli/internal/podman"
"github.com/mongodb/mongodb-atlas-cli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/internal/usage"
Expand All @@ -42,7 +43,6 @@ type ListOpts struct {
store store.ClusterLister
credStore store.CredentialsGetter
config setup.ProfileReader
debug bool
}

type Deployment struct {
Expand Down Expand Up @@ -201,7 +201,7 @@ func ListBuilder() *cobra.Command {

opts.defaultSetter.OutWriter = cmd.OutOrStdout()

opts.podmanClient = podman.NewClient(opts.debug, opts.OutWriter)
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return opts.podmanClient.Ready(cmd.Context())
},
RunE: func(cmd *cobra.Command, args []string) error {
Expand All @@ -210,7 +210,6 @@ func ListBuilder() *cobra.Command {
}

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)
cmd.Flags().BoolVarP(&opts.debug, flag.Debug, flag.DebugShort, false, usage.Debug)

return cmd
}
5 changes: 5 additions & 0 deletions internal/cli/atlas/deployments/options/deployment_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
const (
MongodHostnamePrefix = "mongod"
MongotHostnamePrefix = "mongot"
CheckHostnamePrefix = "check"
spinnerSpeed = 100 * time.Millisecond
// based on https://www.mongodb.com/docs/atlas/reference/api-resources-spec/v2/#tag/Clusters/operation/createCluster
clusterNamePattern = "^[a-zA-Z0-9][a-zA-Z0-9-]*$"
Expand Down Expand Up @@ -63,6 +64,10 @@ func (opts *DeploymentOpts) LocalMongotHostname() string {
return fmt.Sprintf("%s-%s", MongotHostnamePrefix, opts.DeploymentName)
}

func (opts *DeploymentOpts) LocalCheckHostname() string {
return fmt.Sprintf("%s-%s", CheckHostnamePrefix, opts.DeploymentName)
}

func (opts *DeploymentOpts) LocalNetworkName() string {
return fmt.Sprintf("mdb-local-%s", opts.DeploymentName)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ func (opts *DeploymentOpts) CheckIfDeploymentExists(ctx context.Context) error {
if err != nil {
return err
}

opts.updateFields(c)
return nil
}
Expand Down
78 changes: 63 additions & 15 deletions internal/cli/atlas/deployments/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const (
skipConnect = "skip"
spinnerSpeed = 100 * time.Millisecond
shortStepCount = 2
waitMongotTimeout = 5 * time.Minute
)

var (
Expand Down Expand Up @@ -104,15 +105,16 @@ type SetupOpts struct {
cli.GlobalOpts
podmanClient podman.Client
mongodbClient mongodbclient.MongoDBClient
debug bool
settings string
connectWith string
force bool
mongodIP string
mongotIP string
s *spinner.Spinner
}

func (opts *SetupOpts) initPodmanClient() error {
opts.podmanClient = podman.NewClient(opts.debug, opts.OutWriter)
opts.podmanClient = podman.NewClient(log.IsDebugLevel(), log.Writer())
return nil
}

Expand Down Expand Up @@ -174,13 +176,25 @@ func (opts *SetupOpts) startEnvironment(ctx context.Context, currentStep int, st
return errList
}

if err := opts.validateLocalDeploymentsSettings(containers); err != nil {
return opts.validateLocalDeploymentsSettings(containers)
}

func (opts *SetupOpts) internalIPs(ctx context.Context) error {
n, err := opts.podmanClient.Network(ctx, opts.LocalNetworkName())
if err != nil {
return err
}

if _, err := opts.podmanClient.CreateNetwork(ctx, opts.LocalNetworkName()); err != nil {
_, ipNet, err := net.ParseCIDR(n.Subnets[0].Subnet)
if err != nil {
return err
}

ipNet.IP[3] = 10
opts.mongodIP = ipNet.IP.String()
ipNet.IP[3] = 11
opts.mongotIP = ipNet.IP.String()

return nil
}

Expand Down Expand Up @@ -249,6 +263,15 @@ func (opts *SetupOpts) createLocalDeployment(ctx context.Context) error {
// create local deployment
opts.logStepStarted(fmt.Sprintf("Creating your cluster %s...", opts.DeploymentName), currentStep, steps)
defer opts.stop()

if _, err := opts.podmanClient.CreateNetwork(ctx, opts.LocalNetworkName()); err != nil {
return err
}

if err := opts.internalIPs(ctx); err != nil {
return err
}

keyFileContents := base64.URLEncoding.EncodeToString([]byte(uuid.NewString()))

if err := opts.configureMongod(ctx, keyFileContents); err != nil {
Expand All @@ -275,7 +298,7 @@ func (opts *SetupOpts) configureMongod(ctx context.Context, keyFileContents stri
"DBPATH": "/data/db",
"KEYFILE": "/data/configdb/keyfile",
"REPLSETNAME": replicaSetName,
"MONGOTHOST": fmt.Sprintf("%s:%d", opts.LocalMongotHostname(), internalMongotPort),
"MONGOTHOST": opts.internalMongotAddress(),
},
Volumes: map[string]string{
mongodDataVolume: "/data/db",
Expand All @@ -284,6 +307,7 @@ func (opts *SetupOpts) configureMongod(ctx context.Context, keyFileContents stri
opts.Port: internalMongodPort,
},
Network: opts.LocalNetworkName(),
IP: opts.mongodIP,
// wrap the entrypoint with a chain of commands that
// creates the keyfile in the container and sets the 400 permissions for it,
// then starts the entrypoint with the local dev config
Expand Down Expand Up @@ -334,6 +358,14 @@ func (opts *SetupOpts) initReplicaSet(ctx context.Context) error {
return err
}

func (opts *SetupOpts) internalMongodAddress() string {
return fmt.Sprintf("%s:%d", opts.mongodIP, internalMongodPort)
}

func (opts *SetupOpts) internalMongotAddress() string {
return fmt.Sprintf("%s:%d", opts.mongotIP, internalMongotPort)
}

func (opts *SetupOpts) configureMongot(ctx context.Context, keyFileContents string) error {
mongotDataVolume := opts.LocalMongotDataVolume()
if _, err := opts.podmanClient.CreateVolume(ctx, mongotDataVolume); err != nil {
Expand All @@ -345,14 +377,14 @@ func (opts *SetupOpts) configureMongot(ctx context.Context, keyFileContents stri
return err
}

_, err := opts.podmanClient.RunContainer(ctx, podman.RunContainerOpts{
if _, err := opts.podmanClient.RunContainer(ctx, podman.RunContainerOpts{
Detach: true,
Image: options.MongotDockerImageName,
Name: opts.LocalMongotHostname(),
Hostname: opts.LocalMongotHostname(),
Entrypoint: "/bin/sh",
EnvVars: map[string]string{
"MONGODHOST": fmt.Sprintf("%s:%d", opts.LocalMongodHostname(), internalMongodPort),
"MONGODHOST": opts.internalMongodAddress(),
"DATADIR": "/var/lib/mongot",
"KEYFILEPATH": "/var/lib/mongot/keyfile",
"KEYFILECONTENTS": keyFileContents,
Expand All @@ -363,9 +395,27 @@ func (opts *SetupOpts) configureMongot(ctx context.Context, keyFileContents stri
mongotMetricsVolume: "/var/lib/mongot/metrics",
},
Network: opts.LocalNetworkName(),
})
IP: opts.mongotIP,
}); err != nil {
return err
}

return err
return opts.waitForMongot(ctx)
}

func (opts *SetupOpts) waitForMongot(parentCtx context.Context) error {
ctx, cancel := context.WithTimeout(parentCtx, waitMongotTimeout)
defer cancel()
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
if err := opts.podmanClient.Exec(ctx, opts.LocalMongodHostname(), "/bin/sh", "-c", fmt.Sprintf("mongosh %s --eval \"db.adminCommand('ping')\"", opts.internalMongotAddress())); err == nil { // ping was successful
return nil
}
}
}
}

func (opts *SetupOpts) validateLocalDeploymentsSettings(containers []*podman.Container) error {
Expand Down Expand Up @@ -660,7 +710,7 @@ Port {{.Port}}
func (opts *SetupOpts) Run(ctx context.Context) error {
if err := opts.validateAndPrompt(); err != nil {
if errors.Is(err, errSkip) {
_, _ = fmt.Fprintf(opts.OutWriter, "%s\n", err)
_, _ = log.Warningln(err)
return nil
}

Expand All @@ -673,10 +723,9 @@ func (opts *SetupOpts) Run(ctx context.Context) error {

cs := fmt.Sprintf("mongodb://localhost:%d/?directConnection=true", opts.Port)

fmt.Fprintf(opts.OutWriter, `Cluster created!
Connection string: %s

`, cs)
_, _ = log.Warningln("Cluster created!")
_, _ = fmt.Fprintf(opts.OutWriter, "Connection string: %s\n", cs)
_, _ = log.Warningln("")

return opts.runConnectWith(cs)
}
Expand Down Expand Up @@ -713,7 +762,6 @@ func SetupBuilder() *cobra.Command {
cmd.Flags().StringVar(&opts.MdbVersion, flag.MDBVersion, "", usage.MDBVersion)
cmd.Flags().StringVar(&opts.connectWith, flag.ConnectWith, "", usage.ConnectWith)

cmd.Flags().BoolVarP(&opts.debug, flag.Debug, flag.DebugShort, false, usage.Debug)
cmd.Flags().BoolVar(&opts.force, flag.Force, false, usage.Force)

_ = cmd.RegisterFlagCompletionFunc(flag.MDBVersion, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/cloudmanager/cloud_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func Builder() *cobra.Command {
Aliases: []string{"cm"},
Short: "MongoDB Cloud Manager operations.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.ErrOrStderr())
log.SetWriter(cmd.ErrOrStderr())
if debugLevel {
log.SetLevel(log.DebugLevel)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func Builder() *cobra.Command {
cmd := &cobra.Command{
Use: "iam",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.ErrOrStderr())
log.SetWriter(cmd.ErrOrStderr())
if debugLevel {
log.SetLevel(log.DebugLevel)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/opsmanager/ops_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func Builder() *cobra.Command {
Aliases: []string{"om"},
Short: "MongoDB Ops Manager operations.",
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.ErrOrStderr())
log.SetWriter(cmd.ErrOrStderr())
if debugLevel {
log.SetLevel(log.DebugLevel)
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cli/root/atlas/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ Use the --help flag with any command for more info on that command.`,
"toc": "true",
},
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
log.SetOutput(cmd.ErrOrStderr())
log.SetWriter(cmd.ErrOrStderr())
if debugLevel {
log.SetLevel(log.DebugLevel)
}
Expand Down
Loading