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-78065: [mongocli] atlas logs download doesn’t work if --out is not provided #541

Merged
merged 2 commits into from
Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
1 change: 1 addition & 0 deletions e2e/atlas/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const (
ldapEntity = "ldap"
awsEntity = "aws"
customDNSEntity = "customDns"
logsEntity = "logs"
)

func getHostnameAndPort() (string, error) {
Expand Down
157 changes: 51 additions & 106 deletions e2e/atlas/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ import (
)

func TestLogs(t *testing.T) {
const logsEntity = "logs"

clusterName, err := deployCluster()
if err != nil {
t.Fatalf("failed to deploy a cluster: %v", err)
Expand All @@ -47,125 +45,72 @@ func TestLogs(t *testing.T) {
t.Fatalf("unexpected error: %v", err)
}
t.Run("Download mongodb.gz", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
logFile := "mongodb.gz"
filepath := dir + logFile

cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
"--out",
filepath,
)

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

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

if _, err := os.Stat(filepath); err != nil {
t.Fatalf("%v has not been downloaded", filepath)
}
downloadLogTmpPath(t, cliPath, hostname, "mongodb.gz")
})

t.Run("Download mongos.gz", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}

logFile := "mongos.gz"
filepath := dir + logFile

cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
"--out",
filepath,
)

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

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

if _, err := os.Stat(filepath); err != nil {
t.Fatalf("%v has not been downloaded", filepath)
}
downloadLogTmpPath(t, cliPath, hostname, "mongos.gz")
})

t.Run("Download mongodb-audit-log.gz", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
downloadLogTmpPath(t, cliPath, hostname, "mongodb-audit-log.gz")
})

logFile := "mongodb-audit-log.gz"
filepath := dir + logFile
t.Run("Download mongos-audit-log.gz", func(t *testing.T) {
downloadLogTmpPath(t, cliPath, hostname, "mongos-audit-log.gz")
})

cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
"--out",
filepath,
)
t.Run("Download mongodb.gz no output path", func(t *testing.T) {
downloadLog(t, cliPath, hostname, "mongodb.gz")
})
}

cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()
func downloadLogTmpPath(t *testing.T, cliPath, hostname, logFile string) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
filepath := dir + logFile

if err != nil {
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
}
cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
"--out",
filepath,
)

if _, err := os.Stat(filepath); err != nil {
t.Fatalf("%v has not been downloaded", filepath)
}
})
cmd.Env = os.Environ()
resp, err := cmd.CombinedOutput()

t.Run("Download mongos-audit-log.gz", func(t *testing.T) {
dir, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
if err != nil {
t.Fatalf("unexpected error: %v, resp: %v", err, string(resp))
}

logFile := "mongos-audit-log.gz"
filepath := dir + logFile
if _, err := os.Stat(filepath); err != nil {
t.Fatalf("%v has not been downloaded", filepath)
}
}

cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
"--out",
filepath,
)
func downloadLog(t *testing.T, cliPath, hostname, logFile string) {
cmd := exec.Command(cliPath,
atlasEntity,
logsEntity,
"download",
hostname,
logFile,
)

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

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

if _, err := os.Stat(filepath); err != nil {
t.Fatalf("%v has not been downloaded", filepath)
}
})
if _, err := os.Stat(logFile); err != nil {
t.Fatalf("%v has not been downloaded", logFile)
}
}
10 changes: 5 additions & 5 deletions internal/cli/atlas/logs/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ func (opts *DownloadOpts) Run() error {
return f.Close()
}

func (opts *DownloadOpts) initDefaultOut() {
func (opts *DownloadOpts) initDefaultOut() error {
if opts.Out == "" {
opts.Out = strings.ReplaceAll(opts.name, ".gz", ".log.gz")
}
return nil
}
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved

func (opts *DownloadOpts) newDateRangeOpts() *atlas.DateRangetOptions {
Expand All @@ -89,12 +90,11 @@ func DownloadBuilder() *cobra.Command {
Long: downloadLong,
Args: require.ExactArgs(argsN),
PreRunE: func(cmd *cobra.Command, args []string) error {
opts.initDefaultOut()
return opts.PreRunE(opts.ValidateProjectID, opts.initStore)
},
RunE: func(cmd *cobra.Command, args []string) error {
opts.host = args[0]
opts.name = args[1]
return opts.PreRunE(opts.ValidateProjectID, opts.initStore, opts.initDefaultOut)
},
RunE: func(cmd *cobra.Command, args []string) error {
andreaangiolillo marked this conversation as resolved.
Show resolved Hide resolved
if !search.StringInSlice(cmd.ValidArgs, opts.name) {
return fmt.Errorf("<logname> must be one of %s", cmd.ValidArgs)
}
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/atlas/logs/download_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,9 @@ func TestDownloadOpts_initDefaultOut(t *testing.T) {
name: logName,
}
opts.Out = out
opts.initDefaultOut()
assert.Equal(t, opts.Out, want)
a := assert.New(t)
a.NoError(opts.initDefaultOut())
a.Equal(opts.Out, want)
})
}
}