Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
34 changes: 18 additions & 16 deletions internal/pkg/cli/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,27 +71,29 @@ func newDeployOpts(vars deployWkldVars) (*deployOpts, error) {
o.deployWkld = &deployJobOpts{
deployWkldVars: o.deployWkldVars,

store: o.store,
ws: o.ws,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(o.prompt, o.store, o.ws),
prompt: o.prompt,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
store: o.store,
ws: o.ws,
newInterpolator: newManifestInterpolator,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(o.prompt, o.store, o.ws),
prompt: o.prompt,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
}
case contains(workloadType, manifest.ServiceTypes):
opts := &deploySvcOpts{
deployWkldVars: o.deployWkldVars,

store: o.store,
ws: o.ws,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(o.prompt, o.store, o.ws),
prompt: o.prompt,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
store: o.store,
ws: o.ws,
newInterpolator: newManifestInterpolator,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(o.prompt, o.store, o.ws),
prompt: o.prompt,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
newAppVersionGetter: func(appName string) (versionGetter, error) {
return describe.NewAppDescriber(appName)
},
Expand Down
36 changes: 19 additions & 17 deletions internal/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,15 +162,16 @@ func newInitOpts(vars initVars) (*initOpts, error) {
appName: vars.appName,
},

store: ssm,
prompt: prompt,
ws: ws,
unmarshal: manifest.UnmarshalWorkload,
sel: sel,
spinner: spin,
cmd: exec.NewCmd(),
sessProvider: sessProvider,
snsTopicGetter: deployStore,
store: ssm,
prompt: prompt,
ws: ws,
newInterpolator: newManifestInterpolator,
unmarshal: manifest.UnmarshalWorkload,
sel: sel,
spinner: spin,
cmd: exec.NewCmd(),
sessProvider: sessProvider,
snsTopicGetter: deployStore,

newAppVersionGetter: func(appName string) (versionGetter, error) {
return describe.NewAppDescriber(appName)
Expand All @@ -182,14 +183,15 @@ func newInitOpts(vars initVars) (*initOpts, error) {
imageTag: vars.imageTag,
appName: vars.appName,
},
store: ssm,
prompt: prompt,
ws: ws,
unmarshal: manifest.UnmarshalWorkload,
sel: sel,
spinner: spin,
cmd: exec.NewCmd(),
sessProvider: sessProvider,
store: ssm,
prompt: prompt,
ws: ws,
newInterpolator: newManifestInterpolator,
unmarshal: manifest.UnmarshalWorkload,
sel: sel,
spinner: spin,
cmd: exec.NewCmd(),
sessProvider: sessProvider,
}
fs := &afero.Afero{Fs: afero.NewOsFs()}
cmd := exec.NewCmd()
Expand Down
4 changes: 4 additions & 0 deletions internal/pkg/cli/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -623,3 +623,7 @@ type timeoutError interface {
error
Timeout() bool
}

type interpolator interface {
Interpolate(s string) (string, error)
}
24 changes: 15 additions & 9 deletions internal/pkg/cli/job_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ type deployJobOpts struct {
store store
ws wsJobDirReader
unmarshal func(in []byte) (manifest.WorkloadManifest, error)
newInterpolator func(app, env string) interpolator
cmd runner
addons templater
appCFN appResourcesGetter
Expand Down Expand Up @@ -80,14 +81,15 @@ func newJobDeployOpts(vars deployWkldVars) (*deployJobOpts, error) {
return &deployJobOpts{
deployWkldVars: vars,

store: store,
ws: ws,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(prompter, store, ws),
prompt: prompter,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
store: store,
ws: ws,
unmarshal: manifest.UnmarshalWorkload,
spinner: termprogress.NewSpinner(log.DiagnosticWriter),
sel: selector.NewWorkspaceSelect(prompter, store, ws),
prompt: prompter,
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
newInterpolator: newManifestInterpolator,
}, nil
}

Expand Down Expand Up @@ -356,7 +358,11 @@ func (o *deployJobOpts) manifest() (interface{}, error) {
if err != nil {
return nil, fmt.Errorf("read job %s manifest: %w", o.name, err)
}
mft, err := o.unmarshal(raw)
interpolated, err := o.newInterpolator(o.appName, o.envName).Interpolate(string(raw))
if err != nil {
return nil, fmt.Errorf("interpolate environment variables for %s manifest: %w", o.name, err)
}
mft, err := o.unmarshal([]byte(interpolated))
if err != nil {
return nil, fmt.Errorf("unmarshal job %s manifest: %w", o.name, err)
}
Expand Down
22 changes: 22 additions & 0 deletions internal/pkg/cli/job_deploy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
type deployJobMocks struct {
mockWs *mocks.MockwsJobDirReader
mockimageBuilderPusher *mocks.MockimageBuilderPusher
mockInterpolator *mocks.Mockinterpolator
}

func TestJobDeployOpts_Validate(t *testing.T) {
Expand Down Expand Up @@ -240,11 +241,22 @@ on:
},
wantErr: fmt.Errorf("read job %s manifest: %w", "mailer", mockError),
},
"should return error if interpolation fail": {
inputSvc: "mailer",
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest(gomock.Any()).Return(mockManifest, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockManifest)).Return("", mockError),
)
},
wantErr: fmt.Errorf("interpolate environment variables for mailer manifest: %w", mockError),
},
"should return error if workspace methods fail": {
inputSvc: "mailer",
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest(gomock.Any()).Return(mockManifest, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockManifest)).Return(string(mockManifest), nil),
m.mockWs.EXPECT().CopilotDirPath().Return("", mockError),
)
},
Expand All @@ -255,6 +267,7 @@ on:
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest("mailer").Return(mockMftNoBuild, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockMftNoBuild)).Return(string(mockMftNoBuild), nil),
m.mockWs.EXPECT().CopilotDirPath().Times(0),
m.mockimageBuilderPusher.EXPECT().BuildAndPush(gomock.Any(), gomock.Any()).Times(0),
)
Expand All @@ -265,6 +278,7 @@ on:
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest("mailer").Return(mockManifest, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockManifest)).Return(string(mockManifest), nil),
m.mockWs.EXPECT().CopilotDirPath().Return("/ws/root/copilot", nil),
m.mockimageBuilderPusher.EXPECT().BuildAndPush(gomock.Any(), gomock.Any()).Return("", mockError),
)
Expand All @@ -276,6 +290,7 @@ on:
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest("mailer").Return(mockManifest, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockManifest)).Return(string(mockManifest), nil),
m.mockWs.EXPECT().CopilotDirPath().Return("/ws/root/copilot", nil),
m.mockimageBuilderPusher.EXPECT().BuildAndPush(gomock.Any(), &dockerengine.BuildArguments{
Dockerfile: filepath.Join("/ws", "root", "path", "to", "Dockerfile"),
Expand All @@ -290,6 +305,7 @@ on:
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest("mailer").Return(mockMftBuildString, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockMftBuildString)).Return(string(mockMftBuildString), nil),
m.mockWs.EXPECT().CopilotDirPath().Return("/ws/root/copilot", nil),
m.mockimageBuilderPusher.EXPECT().BuildAndPush(gomock.Any(), &dockerengine.BuildArguments{
Dockerfile: filepath.Join("/ws", "root", "path", "to", "Dockerfile"),
Expand All @@ -304,6 +320,7 @@ on:
setupMocks: func(m deployJobMocks) {
gomock.InOrder(
m.mockWs.EXPECT().ReadJobManifest("mailer").Return(mockMftNoContext, nil),
m.mockInterpolator.EXPECT().Interpolate(string(mockMftNoContext)).Return(string(mockMftNoContext), nil),
m.mockWs.EXPECT().CopilotDirPath().Return("/ws/root/copilot", nil),
m.mockimageBuilderPusher.EXPECT().BuildAndPush(gomock.Any(), &dockerengine.BuildArguments{
Dockerfile: filepath.Join("/ws", "root", "path", "to", "Dockerfile"),
Expand All @@ -322,9 +339,11 @@ on:

mockWorkspace := mocks.NewMockwsJobDirReader(ctrl)
mockimageBuilderPusher := mocks.NewMockimageBuilderPusher(ctrl)
mockInterpolator := mocks.NewMockinterpolator(ctrl)
mocks := deployJobMocks{
mockWs: mockWorkspace,
mockimageBuilderPusher: mockimageBuilderPusher,
mockInterpolator: mockInterpolator,
}
test.setupMocks(mocks)
opts := deployJobOpts{
Expand All @@ -334,6 +353,9 @@ on:
unmarshal: manifest.UnmarshalWorkload,
imageBuilderPusher: mockimageBuilderPusher,
ws: mockWorkspace,
newInterpolator: func(app, env string) interpolator {
return mockInterpolator
},
}

gotErr := opts.configureContainerImage()
Expand Down
38 changes: 38 additions & 0 deletions internal/pkg/cli/mocks/mock_interfaces.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 14 additions & 4 deletions internal/pkg/cli/svc_deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type deploySvcOpts struct {
ws wsSvcDirReader
imageBuilderPusher imageBuilderPusher
unmarshal func([]byte) (manifest.WorkloadManifest, error)
newInterpolator func(app, env string) interpolator
s3 artifactUploader
cmd runner
addons templater
Expand Down Expand Up @@ -139,14 +140,19 @@ func newSvcDeployOpts(vars deployWkldVars) (*deploySvcOpts, error) {
}
return d, nil
},
cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
snsTopicGetter: deployStore,
newInterpolator: newManifestInterpolator,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you think of just doing

Suggested change
newInterpolator: newManifestInterpolator,
newInterpolator: manifest.NewInterpolator,

and same in job_deploy.go

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

they return to different type :)

cmd: exec.NewCmd(),
sessProvider: sessions.NewProvider(),
snsTopicGetter: deployStore,
}
opts.uploadOpts = newUploadCustomResourcesOpts(opts)
return opts, err
}

func newManifestInterpolator(app, env string) interpolator {
return manifest.NewInterpolator(app, env)
}

// Validate returns an error if the user inputs are invalid.
func (o *deploySvcOpts) Validate() error {
if o.appName == "" {
Expand Down Expand Up @@ -451,7 +457,11 @@ func (o *deploySvcOpts) manifest() (interface{}, error) {
if err != nil {
return nil, fmt.Errorf("read service %s manifest file: %w", o.name, err)
}
mft, err := o.unmarshal(raw)
interpolated, err := o.newInterpolator(o.appName, o.envName).Interpolate(string(raw))
if err != nil {
return nil, fmt.Errorf("interpolate environment variables for %s manifest: %w", o.name, err)
}
mft, err := o.unmarshal([]byte(interpolated))
if err != nil {
return nil, fmt.Errorf("unmarshal service %s manifest: %w", o.name, err)
}
Expand Down
Loading