mirror of
https://github.com/mattermost/mattermost.git
synced 2026-09-21 05:54:10 +08:00
* add mmctl compliance export download command and tests - Introduced `ComplianceExportDownloadCmd` to facilitate downloading compliance export files. - Implemented the `DownloadComplianceExport` method in the Client interface for handling file downloads. - Added unit tests for the download command, covering successful downloads, error handling for non-existent jobs, and retries on failure. - Included end-to-end tests to validate the command's functionality. - Updated documentation to include usage examples and options for the new command. * don't know why this was left out * PR comments * adjust test for new retry logic * refactored download fn for compliance_export and export * fix test due to fixed logic * docs
308 lines
8.1 KiB
Go
308 lines
8.1 KiB
Go
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
|
|
// See LICENSE.txt for license information.
|
|
|
|
package commands
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
gomock "github.com/golang/mock/gomock"
|
|
"github.com/mattermost/mattermost/server/public/model"
|
|
"github.com/mattermost/mattermost/server/v8/cmd/mmctl/printer"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
func (s *MmctlUnitTestSuite) TestComplianceExportListCmdF() {
|
|
s.Run("list default pagination", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
var mockJobs []*model.Job
|
|
|
|
// Test with default pagination
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 0, DefaultPageSize).
|
|
Return(mockJobs, &model.Response{}, nil).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportListCmdF(s.client, cmd, nil)
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal("No jobs found", printer.GetLines()[0])
|
|
|
|
// Test with 10 per page
|
|
printer.Clean()
|
|
cmd = makeCmd()
|
|
_ = cmd.Flags().Set("per-page", "10")
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 0, 10).
|
|
Return(mockJobs, &model.Response{}, nil).
|
|
Times(1)
|
|
|
|
err = complianceExportListCmdF(s.client, cmd, nil)
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal("No jobs found", printer.GetLines()[0])
|
|
|
|
// Test with all items
|
|
printer.Clean()
|
|
cmd = makeCmd()
|
|
_ = cmd.Flags().Set("all", "true")
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 0, DefaultPageSize).
|
|
Return(mockJobs, &model.Response{}, nil).
|
|
Times(1)
|
|
|
|
err = complianceExportListCmdF(s.client, cmd, nil)
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal("No jobs found", printer.GetLines()[0])
|
|
})
|
|
|
|
s.Run("list with paging", func() {
|
|
// Create 5 mock jobs
|
|
mockJobs := make([]*model.Job, 5)
|
|
for i := range 5 {
|
|
mockJobs[i] = &model.Job{
|
|
Id: model.NewId(),
|
|
CreateAt: model.GetMillis() - int64(i*1000),
|
|
}
|
|
}
|
|
|
|
// Test paging with 2 jobs per page
|
|
printer.Clean()
|
|
cmd := makeCmd()
|
|
_ = cmd.Flags().Set("all", "true")
|
|
_ = cmd.Flags().Set("per-page", "2")
|
|
|
|
// Expect 4 API calls (2 jobs each for first 2 pages, 1 job for last page, then a call with 0 jobs)
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 0, 2).
|
|
Return(mockJobs[0:2], &model.Response{}, nil).
|
|
Times(1)
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 1, 2).
|
|
Return(mockJobs[2:4], &model.Response{}, nil).
|
|
Times(1)
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 2, 2).
|
|
Return(mockJobs[4:5], &model.Response{}, nil).
|
|
Times(1)
|
|
s.client.
|
|
EXPECT().
|
|
GetJobs(context.TODO(), "message_export", "", 3, 2).
|
|
Return(mockJobs[5:], &model.Response{}, nil).
|
|
Times(1)
|
|
|
|
err := complianceExportListCmdF(s.client, cmd, nil)
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 5)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
|
|
// Verify jobs are printed in correct order
|
|
for i := range 5 {
|
|
s.Equal(mockJobs[i].Id, printer.GetLines()[i].(*model.Job).Id)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestComplianceExportShowCmdF() {
|
|
s.Run("show job successfully", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
mockJob := &model.Job{
|
|
Id: model.NewId(),
|
|
CreateAt: model.GetMillis(),
|
|
Type: model.JobTypeMessageExport,
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
GetJob(context.TODO(), mockJob.Id).
|
|
Return(mockJob, &model.Response{}, nil).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportShowCmdF(s.client, cmd, []string{mockJob.Id})
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal(mockJob, printer.GetLines()[0].(*model.Job))
|
|
})
|
|
|
|
s.Run("show job with error", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
mockError := &model.AppError{
|
|
Message: "failed to get job",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
GetJob(context.TODO(), "invalid-job-id").
|
|
Return(nil, &model.Response{}, mockError).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportShowCmdF(s.client, cmd, []string{"invalid-job-id"})
|
|
s.Require().NotNil(err)
|
|
s.EqualError(err, "failed to get compliance export job: failed to get job")
|
|
s.Len(printer.GetLines(), 0)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestComplianceExportCancelCmdF() {
|
|
s.Run("cancel job successfully", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
id := model.NewId()
|
|
|
|
s.client.
|
|
EXPECT().
|
|
CancelJob(context.TODO(), id).
|
|
Return(&model.Response{}, nil).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportCancelCmdF(s.client, cmd, []string{id})
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 0)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
})
|
|
|
|
s.Run("cancel job with get error", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
mockError := &model.AppError{
|
|
Message: "failed to get job",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
CancelJob(context.TODO(), "invalid-job-id").
|
|
Return(&model.Response{}, mockError).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportCancelCmdF(s.client, cmd, []string{"invalid-job-id"})
|
|
s.Require().NotNil(err)
|
|
s.EqualError(err, "failed to cancel compliance export job: failed to get job")
|
|
s.Len(printer.GetLines(), 0)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
})
|
|
|
|
s.Run("cancel job with cancel error", func() {
|
|
s.SetupTest() // Reset mocks before test
|
|
printer.Clean()
|
|
id := model.NewId()
|
|
|
|
mockError := &model.AppError{
|
|
Message: "failed to cancel job",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
CancelJob(context.TODO(), id).
|
|
Return(&model.Response{}, mockError).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
err := complianceExportCancelCmdF(s.client, cmd, []string{id})
|
|
s.Require().NotNil(err)
|
|
s.EqualError(err, "failed to cancel compliance export job: failed to cancel job")
|
|
s.Len(printer.GetLines(), 0)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
})
|
|
}
|
|
|
|
func (s *MmctlUnitTestSuite) TestComplianceExportDownloadCmdF() {
|
|
mockJob := &model.Job{
|
|
Id: model.NewId(),
|
|
CreateAt: model.GetMillis(),
|
|
Type: model.JobTypeMessageExport,
|
|
}
|
|
|
|
s.Run("download job file successfully", func() {
|
|
printer.Clean()
|
|
defer func() {
|
|
_ = os.Remove("suggested-filename.zip")
|
|
}()
|
|
|
|
s.client.
|
|
EXPECT().
|
|
DownloadComplianceExport(gomock.Any(), mockJob.Id, gomock.Any()).
|
|
Return("suggested-filename.zip", nil).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
cmd.Flags().Int("num-retries", 5, "")
|
|
err := complianceExportDownloadCmdF(s.client, cmd, []string{mockJob.Id})
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal(fmt.Sprintf("Compliance export file downloaded to %q", "suggested-filename.zip"), printer.GetLines()[0])
|
|
})
|
|
|
|
s.Run("download job file with explicit path", func() {
|
|
printer.Clean()
|
|
defer func() {
|
|
_ = os.Remove("custom-path.zip")
|
|
}()
|
|
|
|
s.client.
|
|
EXPECT().
|
|
DownloadComplianceExport(context.TODO(), mockJob.Id, gomock.Any()).
|
|
Return("", nil).
|
|
Times(1)
|
|
|
|
cmd := makeCmd()
|
|
cmd.Flags().Int("num-retries", 5, "")
|
|
err := complianceExportDownloadCmdF(s.client, cmd, []string{mockJob.Id, "custom-path.zip"})
|
|
s.Require().Nil(err)
|
|
s.Len(printer.GetLines(), 1)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
s.Equal(fmt.Sprintf("Compliance export file downloaded to %q", "custom-path.zip"), printer.GetLines()[0])
|
|
})
|
|
|
|
s.Run("download job with error", func() {
|
|
printer.Clean()
|
|
mockError := &model.AppError{
|
|
Message: "failed to download file",
|
|
}
|
|
|
|
s.client.
|
|
EXPECT().
|
|
DownloadComplianceExport(context.TODO(), mockJob.Id, gomock.Any()).
|
|
Return("", mockError).
|
|
Times(6) // Initial attempt + 5 retries
|
|
|
|
cmd := makeCmd()
|
|
cmd.Flags().Int("num-retries", 5, "")
|
|
err := complianceExportDownloadCmdF(s.client, cmd, []string{mockJob.Id})
|
|
s.Require().NotNil(err)
|
|
s.EqualError(err, "failed to download compliance export after 5 retries: failed to download file")
|
|
s.Len(printer.GetLines(), 0)
|
|
s.Len(printer.GetErrorLines(), 0)
|
|
})
|
|
}
|
|
|
|
func makeCmd() *cobra.Command {
|
|
cmd := &cobra.Command{}
|
|
cmd.Flags().Int("page", 0, "")
|
|
cmd.Flags().Int("per-page", DefaultPageSize, "")
|
|
cmd.Flags().Bool("all", false, "")
|
|
return cmd
|
|
}
|