Skip to content

Commit 51f9bdd

Browse files
committed
APP-1823 - Support dry run in application CLI for create version
1 parent de92ba6 commit 51f9bdd

6 files changed

Lines changed: 84 additions & 19 deletions

File tree

apptrust/commands/flags.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ var commandFlags = map[string][]string{
120120
IncludeFilterFlag,
121121
ExcludeFilterFlag,
122122
SpecVarsFlag,
123+
DryRunFlag,
123124
},
124125
VersionPromote: {
125126
url,

apptrust/commands/version/create_app_version_cmd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ type createAppVersionCommand struct {
2727
serverDetails *coreConfig.ServerDetails
2828
requestPayload *model.CreateAppVersionRequest
2929
sync bool
30+
dryRun bool
3031
}
3132

3233
type createVersionSpec struct {
@@ -44,7 +45,7 @@ func (cv *createAppVersionCommand) Run() error {
4445
return err
4546
}
4647

47-
return cv.versionService.CreateAppVersion(ctx, cv.requestPayload, cv.sync)
48+
return cv.versionService.CreateAppVersion(ctx, cv.requestPayload, cv.sync, cv.dryRun)
4849
}
4950

5051
func (cv *createAppVersionCommand) ServerDetails() (*coreConfig.ServerDetails, error) {
@@ -69,6 +70,7 @@ func (cv *createAppVersionCommand) prepareAndRunCommand(ctx *components.Context)
6970
if errorutils.CheckError(err) != nil {
7071
return err
7172
}
73+
cv.dryRun = ctx.GetBoolFlagValue(commands.DryRunFlag)
7274
return commonCLiCommands.Exec(cv)
7375
}
7476

apptrust/commands/version/create_app_version_cmd_test.go

Lines changed: 28 additions & 8 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
}{
@@ -36,10 +37,28 @@ func TestCreateAppVersionCommand(t *testing.T) {
3637
}},
3738
},
3839
},
40+
dryRun: false,
41+
},
42+
{
43+
name: "success with dry-run",
44+
request: &model.CreateAppVersionRequest{
45+
ApplicationKey: "app-key",
46+
Version: "1.0.0",
47+
Sources: &model.CreateVersionSources{
48+
Packages: []model.CreateVersionPackage{{
49+
Type: "type",
50+
Name: "name",
51+
Version: "1.0.0",
52+
Repository: "repo",
53+
}},
54+
},
55+
},
56+
dryRun: true,
3957
},
4058
{
4159
name: "context error",
4260
request: &model.CreateAppVersionRequest{ApplicationKey: "app-key", Version: "1.0.0", Draft: false, Sources: &model.CreateVersionSources{Packages: []model.CreateVersionPackage{{Type: "type", Name: "name", Version: "1.0.0", Repository: "repo"}}}},
61+
dryRun: false,
4362
shouldError: true,
4463
errorMessage: "context error",
4564
},
@@ -57,10 +76,10 @@ func TestCreateAppVersionCommand(t *testing.T) {
5776

5877
mockVersionService := mockversions.NewMockVersionService(ctrl)
5978
if tt.shouldError {
60-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, true).
79+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, true, tt.dryRun).
6180
Return(errors.New(tt.errorMessage)).Times(1)
6281
} else {
63-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, true).
82+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), tt.request, true, tt.dryRun).
6483
Return(nil).Times(1)
6584
}
6685

@@ -69,6 +88,7 @@ func TestCreateAppVersionCommand(t *testing.T) {
6988
serverDetails: &config.ServerDetails{Url: "https://example.com"},
7089
requestPayload: tt.request,
7190
sync: true,
91+
dryRun: tt.dryRun,
7292
}
7393

7494
err := cmd.Run()
@@ -196,8 +216,8 @@ func TestCreateAppVersionCommand_FlagsSuite(t *testing.T) {
196216
var actualPayload *model.CreateAppVersionRequest
197217
mockVersionService := mockversions.NewMockVersionService(ctrl)
198218
if !tt.expectsError {
199-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any()).
200-
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, _ bool) error {
219+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
220+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, _ bool, _ bool) error {
201221
actualPayload = req
202222
return nil
203223
}).Times(1)
@@ -817,8 +837,8 @@ func TestCreateAppVersionCommand_SpecFileSuite(t *testing.T) {
817837
var capturedSync bool
818838
mockVersionService := mockversions.NewMockVersionService(ctrl)
819839
if !tt.expectsError {
820-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any()).
821-
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, sync bool) error {
840+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
841+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, sync, dryRun bool) error {
822842
actualPayload = req
823843
capturedSync = sync
824844
return nil
@@ -886,8 +906,8 @@ func TestCreateAppVersionCommand_SyncFlagSuite(t *testing.T) {
886906

887907
var capturedSync bool
888908
mockVersionService := mockversions.NewMockVersionService(ctrl)
889-
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any()).
890-
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, sync bool) error {
909+
mockVersionService.EXPECT().CreateAppVersion(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).
910+
DoAndReturn(func(_ interface{}, req *model.CreateAppVersionRequest, sync, dryRun bool) error {
891911
capturedSync = sync
892912
return nil
893913
}).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: 11 additions & 4 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, sync bool) error
17+
CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync, 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,24 +28,31 @@ func NewVersionService() VersionService {
2828
return &versionService{}
2929
}
3030

31-
func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync bool) error {
31+
func (vs *versionService) CreateAppVersion(ctx service.Context, request *model.CreateAppVersionRequest, sync, 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": strconv.FormatBool(!sync)})
33+
response, responseBody, err := ctx.GetHttpClient().Post(endpoint, request,
34+
map[string]string{"async": strconv.FormatBool(!sync), "dry_run": strconv.FormatBool(dryRun)})
3435
if err != nil {
3536
return err
3637
}
3738

3839
expectedStatusCode := http.StatusCreated
3940
if !sync {
4041
expectedStatusCode = http.StatusAccepted
42+
} else if dryRun {
43+
expectedStatusCode = http.StatusOK
4144
}
4245

4346
if response.StatusCode != expectedStatusCode {
4447
return fmt.Errorf("failed to create app version. Status code: %d. \n%s",
4548
response.StatusCode, responseBody)
4649
}
4750

48-
log.Info("Application version created successfully.")
51+
if dryRun {
52+
log.Info(fmt.Sprintf("Dry run successful for application version: %s:%s", request.ApplicationKey, request.Version))
53+
} else {
54+
log.Info(fmt.Sprintf("Application version created successfully: %s:%s", request.ApplicationKey, request.Version))
55+
}
4956
log.Output(string(responseBody))
5057
return nil
5158
}

apptrust/service/versions/version_service_test.go

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ func TestCreateAppVersion(t *testing.T) {
2424
name string
2525
request *model.CreateAppVersionRequest
2626
sync bool
27+
dryRun bool
2728
mockResponse *http.Response
2829
mockResponseBody string
2930
mockError error
@@ -33,6 +34,27 @@ func TestCreateAppVersion(t *testing.T) {
3334
name: "success",
3435
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
3536
sync: true,
37+
dryRun: false,
38+
mockResponse: &http.Response{StatusCode: 201},
39+
mockResponseBody: "{}",
40+
mockError: nil,
41+
expectedError: "",
42+
},
43+
{
44+
name: "success with dry-run (200 OK)",
45+
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
46+
sync: true,
47+
dryRun: true,
48+
mockResponse: &http.Response{StatusCode: 200},
49+
mockResponseBody: "{\"validation\": \"passed\"}",
50+
mockError: nil,
51+
expectedError: "",
52+
},
53+
{
54+
name: "success with dry-run (201 Created)",
55+
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
56+
sync: true,
57+
dryRun: true,
3658
mockResponse: &http.Response{StatusCode: 201},
3759
mockResponseBody: "{}",
3860
mockError: nil,
@@ -42,6 +64,17 @@ func TestCreateAppVersion(t *testing.T) {
4264
name: "success with sync=false",
4365
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
4466
sync: false,
67+
dryRun: false,
68+
mockResponse: &http.Response{StatusCode: 202},
69+
mockResponseBody: "{}",
70+
mockError: nil,
71+
expectedError: "",
72+
},
73+
{
74+
name: "success with sync=false & dryRun=true",
75+
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
76+
sync: false,
77+
dryRun: true,
4578
mockResponse: &http.Response{StatusCode: 202},
4679
mockResponseBody: "{}",
4780
mockError: nil,
@@ -51,6 +84,7 @@ func TestCreateAppVersion(t *testing.T) {
5184
name: "failure",
5285
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
5386
sync: true,
87+
dryRun: false,
5488
mockResponse: &http.Response{StatusCode: 400},
5589
mockResponseBody: "error",
5690
mockError: nil,
@@ -60,6 +94,7 @@ func TestCreateAppVersion(t *testing.T) {
6094
name: "http client error",
6195
request: &model.CreateAppVersionRequest{ApplicationKey: "test-app", Version: "1.0.0"},
6296
sync: true,
97+
dryRun: false,
6398
mockResponse: nil,
6499
mockResponseBody: "",
65100
mockError: errors.New("http client error"),
@@ -70,13 +105,13 @@ func TestCreateAppVersion(t *testing.T) {
70105
for _, tt := range tests {
71106
t.Run(tt.name, func(t *testing.T) {
72107
mockHttpClient := mockhttp.NewMockApptrustHttpClient(ctrl)
73-
mockHttpClient.EXPECT().Post("/v1/applications/test-app/versions/", tt.request, map[string]string{"async": strconv.FormatBool(!tt.sync)}).
108+
mockHttpClient.EXPECT().Post("/v1/applications/test-app/versions/", tt.request, map[string]string{"async": strconv.FormatBool(!tt.sync), "dry_run": strconv.FormatBool(tt.dryRun)}).
74109
Return(tt.mockResponse, []byte(tt.mockResponseBody), tt.mockError).Times(1)
75110

76111
mockCtx := mockservice.NewMockContext(ctrl)
77112
mockCtx.EXPECT().GetHttpClient().Return(mockHttpClient).Times(1)
78113

79-
err := service.CreateAppVersion(mockCtx, tt.request, tt.sync)
114+
err := service.CreateAppVersion(mockCtx, tt.request, tt.sync, tt.dryRun)
80115
if tt.expectedError == "" {
81116
assert.NoError(t, err)
82117
} else {

0 commit comments

Comments
 (0)