Skip to content

Commit e490be8

Browse files
committed
APP-1823 - Support dry run in application CLI
1 parent 3657239 commit e490be8

6 files changed

Lines changed: 74 additions & 20 deletions

File tree

apptrust/commands/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ var commandFlags = map[string][]string{
116116
IncludeFilterFlag,
117117
ExcludeFilterFlag,
118118
SpecVarsFlag,
119+
DryRunFlag,
119120
},
120121
VersionPromote: {
121122
url,

apptrust/commands/version/create_app_version_cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ type createAppVersionCommand struct {
2626
versionService versions.VersionService
2727
serverDetails *coreConfig.ServerDetails
2828
requestPayload *model.CreateAppVersionRequest
29+
dryRun bool
2930
}
3031

3132
type createVersionSpec struct {
@@ -43,7 +44,7 @@ func (cv *createAppVersionCommand) Run() error {
4344
return err
4445
}
4546

46-
return cv.versionService.CreateAppVersion(ctx, cv.requestPayload)
47+
return cv.versionService.CreateAppVersion(ctx, cv.requestPayload, cv.dryRun)
4748
}
4849

4950
func (cv *createAppVersionCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
@@ -67,6 +68,7 @@ func (cv *createAppVersionCommand) prepareAndRunCommand(ctx *components.Context)
6768
if errorutils.CheckError(err) != nil {
6869
return err
6970
}
71+
cv.dryRun = ctx.GetBoolFlagValue(commands.DryRunFlag)
7072
return commonCLiCommands.Exec(cv)
7173
}
7274

apptrust/commands/version/create_app_version_cmd_test.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ func TestCreateAppVersionCommand(t *testing.T) {
1818
tests := []struct {
1919
name string
2020
request *model.CreateAppVersionRequest
21+
dryRun bool
2122
shouldError bool
2223
errorMessage string
2324
}{
@@ -35,10 +36,28 @@ func TestCreateAppVersionCommand(t *testing.T) {
3536
}},
3637
},
3738
},
39+
dryRun: false,
40+
},
41+
{
42+
name: "success with dry-run",
43+
request: &model.CreateAppVersionRequest{
44+
ApplicationKey: "app-key",
45+
Version: "1.0.0",
46+
Sources: &model.CreateVersionSources{
47+
Packages: []model.CreateVersionPackage{{
48+
Type: "type",
49+
Name: "name",
50+
Version: "1.0.0",
51+
Repository: "repo",
52+
}},
53+
},
54+
},
55+
dryRun: true,
3856
},
3957
{
4058
name: "context error",
4159
request: &model.CreateAppVersionRequest{ApplicationKey: "app-key", Version: "1.0.0", Sources: &model.CreateVersionSources{Packages: []model.CreateVersionPackage{{Type: "type", Name: "name", Version: "1.0.0", Repository: "repo"}}}},
60+
dryRun: false,
4261
shouldError: true,
4362
errorMessage: "context error",
4463
},
@@ -56,17 +75,18 @@ func TestCreateAppVersionCommand(t *testing.T) {
5675

5776
mockVersionService := mockversions.NewMockVersionService(ctrl)
5877
if tt.shouldError {
59-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request).
78+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, tt.dryRun).
6079
Return(errors.New(tt.errorMessage)).Times(1)
6180
} else {
62-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request).
81+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, tt.dryRun).
6382
Return(nil).Times(1)
6483
}
6584

6685
cmd := &createAppVersionCommand{
6786
versionService: mockVersionService,
6887
serverDetails: &config.ServerDetails{Url: "https://example.com"},
6988
requestPayload: tt.request,
89+
dryRun: tt.dryRun,
7090
}
7191

7292
err := cmd.Run()
@@ -192,8 +212,8 @@ func TestCreateAppVersionCommand_FlagsSuite(t *testing.T) {
192212
var actualPayload *model.CreateAppVersionRequest
193213
mockVersionService := mockversions.NewMockVersionService(ctrl)
194214
if !tt.expectsError {
195-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any()).
196-
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest) error {
215+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any()).
216+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, _ bool) error {
197217
actualPayload = req
198218
return nil
199219
}).Times(1)
@@ -705,8 +725,8 @@ func TestCreateAppVersionCommand_SpecFileSuite(t *testing.T) {
705725
var actualPayload *model.CreateAppVersionRequest
706726
mockVersionService := mockversions.NewMockVersionService(ctrl)
707727
if !tt.expectsError {
708-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any()).
709-
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest) error {
728+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any()).
729+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, _ bool) error {
710730
actualPayload = req
711731
return nil
712732
}).Times(1)

apptrust/service/versions/mocks/version_service_mock.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apptrust/service/versions/version_service.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
type VersionService interface {
17-
CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest) error
17+
CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, dryRun bool) error
1818
PromoteAppVersion(ctx service.Context, applicationKey string, version string, payload *model.PromoteAppVersionRequest, sync bool) error
1919
ReleaseAppVersion(ctx service.Context, applicationKey string, version string, request *model.ReleaseAppVersionRequest, sync bool) error
2020
RollbackAppVersion(ctx service.Context, applicationKey string, version string, request *model.RollbackAppVersionRequest, sync bool) error
@@ -28,19 +28,26 @@ func NewVersionService() VersionService {
2828
return &versionService{}
2929
}
3030

31-
func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest) error {
31+
func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, dryRun bool) error {
3232
endpoint := fmt.Sprintf("/v1/applications/%s/versions/", request.ApplicationKey)
33-
response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, map[string]string{"async": "false"})
33+
response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request, map[string]string{
34+
"async": "false",
35+
"dry_run": strconv.FormatBool(dryRun),
36+
})
3437
if err != nil {
3538
return err
3639
}
37-
38-
if response.StatusCode != http.StatusCreated {
40+
// Dry run returns http.StatusOK
41+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
3942
return fmt.Errorf("failed to create app version. Status code: %d. \n%s",
4043
response.StatusCode, responseBody)
4144
}
4245

43-
log.Info("Application version created successfully.")
46+
if dryRun {
47+
log.Info(fmt.Sprintf("Dry run successful for application version: %s:%s", request.ApplicationKey, request.Version))
48+
} else {
49+
log.Info(fmt.Sprintf("Application version created successfully: %s:%s", request.ApplicationKey, request.Version))
50+
}
4451
log.Output(string(responseBody))
4552
return nil
4653
}

apptrust/service/versions/version_service_test.go

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ func TestCreateAppVersion(t *testing.T) {
2323
tests := []struct {
2424
name string
2525
request *model.CreateAppVersionRequest
26+
dryRun bool
2627
mockResponse *http.Response
2728
mockResponseBody string
2829
mockError error
@@ -31,6 +32,25 @@ func TestCreateAppVersion(t *testing.T) {
3132
{
3233
name: "success",
3334
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
35+
dryRun: false,
36+
mockResponse: &http.Response{StatusCode: 201},
37+
mockResponseBody: "{}",
38+
mockError: nil,
39+
expectedError: "",
40+
},
41+
{
42+
name: "success with dry-run (200 OK)",
43+
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
44+
dryRun: true,
45+
mockResponse: &http.Response{StatusCode: 200},
46+
mockResponseBody: "{\"validation\": \"passed\"}",
47+
mockError: nil,
48+
expectedError: "",
49+
},
50+
{
51+
name: "success with dry-run (201 Created)",
52+
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
53+
dryRun: true,
3454
mockResponse: &http.Response{StatusCode: 201},
3555
mockResponseBody: "{}",
3656
mockError: nil,
@@ -39,6 +59,7 @@ func TestCreateAppVersion(t *testing.T) {
3959
{
4060
name: "failure",
4161
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
62+
dryRun: false,
4263
mockResponse: &http.Response{StatusCode: 400},
4364
mockResponseBody: "error",
4465
mockError: nil,
@@ -47,6 +68,7 @@ func TestCreateAppVersion(t *testing.T) {
4768
{
4869
name: "http client error",
4970
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
71+
dryRun: false,
5072
mockResponse: nil,
5173
mockResponseBody: "",
5274
mockError: errors.New("http client error"),
@@ -57,13 +79,15 @@ func TestCreateAppVersion(t *testing.T) {
5779
for _, tt := range tests {
5880
t.Run(tt.name, func(t *testing.T) {
5981
mockHttpClient := mockhttp.NewMockApptrustHttpClient(ctrl)
60-
mockHttpClient.EXPECT().Post("/v1/applications/test-app/versions/", tt.request, map[string]string{"async": "false"}).
61-
Return(tt.mockResponse, []byte(tt.mockResponseBody), tt.mockError).Times(1)
82+
mockHttpClient.EXPECT().Post("/v1/applications/test-app/versions/", tt.request, map[string]string{
83+
"async": "false",
84+
"dry_run": strconv.FormatBool(tt.dryRun),
85+
}).Return(tt.mockResponse, []byte(tt.mockResponseBody), tt.mockError).Times(1)
6286

6387
mockCtx := mockservice.NewMockContext(ctrl)
6488
mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1)
6589

66-
err := service.CreateAppVersion(mockCtx, tt.request)
90+
err := service.CreateAppVersion(mockCtx, tt.request, tt.dryRun)
6791
if tt.expectedError == "" {
6892
assert.NoError(t, err)
6993
} else {

0 commit comments

Comments
 (0)