Skip to content

Commit

Permalink
fix: Make audit log unit test pass
Browse files Browse the repository at this point in the history
  • Loading branch information
jarjee committed Mar 14, 2024
1 parent aea6992 commit 5d444b2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
26 changes: 21 additions & 5 deletions internal/cli/atlas/streams/instance/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package instance
import (
"context"
"fmt"
"io"
"slices"

"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
Expand All @@ -27,6 +28,8 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/usage"
"github.com/spf13/afero"
"github.com/spf13/cobra"

atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
)

var downloadMessage = "Download of %s completed.\n"
Expand All @@ -38,7 +41,6 @@ type DownloadOpts struct {
fileName string
start int64
end int64
decompress bool
store store.StreamsDownloader
}

Expand All @@ -58,13 +60,28 @@ func (opts *DownloadOpts) initDefaultOut() error {
}

func (opts *DownloadOpts) Run() error {
w, err := opts.NewWriteCloser()
params := atlasv2.DownloadStreamTenantAuditLogsApiParams{
GroupId: opts.ProjectID,
TenantName: opts.tenantName,
StartDate: &opts.start,
EndDate: &opts.end,
}

f, err := opts.store.DownloadAuditLog(&params)
if err != nil {
return err
}
defer w.Close()

return nil
defer f.Close()

out, err := opts.NewWriteCloser()
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, f)
return err
}

// DownloadBuilder
Expand Down Expand Up @@ -109,7 +126,6 @@ func DownloadBuilder() *cobra.Command {
cmd.Flags().Int64Var(&opts.start, flag.Start, 0, usage.LogStart)
cmd.Flags().Int64Var(&opts.end, flag.End, 0, usage.LogEnd)
cmd.Flags().BoolVar(&opts.Force, flag.Force, false, usage.ForceFile)
cmd.Flags().BoolVarP(&opts.decompress, flag.Decompress, flag.DecompressShort, false, usage.Decompress)

cmd.Flags().StringVar(&opts.ProjectID, flag.ProjectID, "", usage.ProjectID)

Expand Down
21 changes: 11 additions & 10 deletions internal/cli/atlas/streams/instance/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,19 @@ import (
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/cli"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/flag"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/mocks"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/store"
"github.com/mongodb/mongodb-atlas-cli/atlascli/internal/test"
"github.com/spf13/afero"
"github.com/stretchr/testify/require"
atlasv2 "go.mongodb.org/atlas-sdk/v20231115008/admin"
)

func TestDownloadOpts_Run(t *testing.T) {
ctrl := gomock.NewController(t)
mockStore := mocks.NewMockStreamsDownloader(ctrl)

const contents = "expected"
const fileName = "auditLogs.gz"
const projectID = "download-project-id"
const tenantName = "streams-tenant"

file, err := os.CreateTemp("", "")
if err != nil {
Expand All @@ -52,22 +53,22 @@ func TestDownloadOpts_Run(t *testing.T) {
downloadOpts := &DownloadOpts{
store: mockStore,
DownloaderOpts: cli.DownloaderOpts{
Out: fileName,
Out: "auditLogs.gz",
Fs: fs,
},
}

downloadOpts.ProjectID = "download-project-id"
downloadOpts.decompress = true
downloadOpts.fileName = fileName
downloadOpts.ProjectID = projectID
downloadOpts.tenantName = tenantName

endDate := int64(0)
startDate := int64(0)

downloadParams := new(store.DownloadStreamTenantAuditLogsApiParams)
downloadParams := new(atlasv2.DownloadStreamTenantAuditLogsApiParams)
downloadParams.EndDate = &endDate
downloadParams.StartDate = &startDate
downloadParams.TenantName = "streams-tenant"
downloadParams.GroupId = projectID
downloadParams.TenantName = tenantName

mockStore.
EXPECT().
Expand All @@ -79,7 +80,7 @@ func TestDownloadOpts_Run(t *testing.T) {
t.Fatalf("Run() unexpected error: %v", err)
}

of, _ := fs.Open(fileName)
of, _ := fs.Open("auditLogs.gz")
defer of.Close()
b, _ := io.ReadAll(of)
require.Equal(t, contents, string(b))
Expand All @@ -90,6 +91,6 @@ func TestDownloadBuilder(t *testing.T) {
t,
DownloadBuilder(),
0,
[]string{flag.Out, flag.Start, flag.End, flag.Force, flag.Decompress, flag.ProjectID},
[]string{flag.Out, flag.Start, flag.End, flag.Force, flag.ProjectID},
)
}

0 comments on commit 5d444b2

Please sign in to comment.