Skip to content

Commit

Permalink
test: stub AreCredsFromEnvVars in app init to speed tests by 70s (#2465)
Browse files Browse the repository at this point in the history
🤦 Previously, we were calling sessions.AreCredsFromEnvVars in our tests
which would timeout after 10s:
https://github.com/aws/copilot-cli/blob/f2f0ee317e01b9da85ab72ecd64481c10658cd05/internal/pkg/aws/sessions/sessions.go#L28

Since the test TestInitAppOpts_Ask has 7 testcases it would take 70s to
test the cli package. With this one weird trick™️  and replacing the
functional call with a fake, we speed up our tests by 70s.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.
  • Loading branch information
efekarakus authored Jun 15, 2021
1 parent 8236f9b commit a486bac
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 12 deletions.
24 changes: 12 additions & 12 deletions internal/pkg/cli/app_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@ type initAppVars struct {
type initAppOpts struct {
initAppVars

identity identityService
store applicationStore
route53 domainHostedZoneGetter
ws wsAppManager
cfn appDeployer
prompt prompter
prog progress
identity identityService
store applicationStore
route53 domainHostedZoneGetter
ws wsAppManager
cfn appDeployer
prompt prompter
prog progress
isSessionFromEnvVars func() (bool, error)

cachedHostedZoneID string
}
Expand Down Expand Up @@ -75,6 +76,9 @@ func newInitAppOpts(vars initAppVars) (*initAppOpts, error) {
cfn: cloudformation.New(sess),
prompt: prompt.New(),
prog: termprogress.NewSpinner(log.DiagnosticWriter),
isSessionFromEnvVars: func() (bool, error) {
return sessions.AreCredsFromEnvVars(sess)
},
}, nil
}

Expand All @@ -100,11 +104,7 @@ func (o *initAppOpts) Validate() error {

// Ask prompts the user for any required arguments that they didn't provide.
func (o *initAppOpts) Ask() error {
sess, err := sessions.NewProvider().Default()
if err != nil {
return fmt.Errorf("get default session: %w", err)
}
if ok, _ := sessions.AreCredsFromEnvVars(sess); ok { // Ignore the error, we do not want to crash for a warning.
if ok, _ := o.isSessionFromEnvVars(); ok { // Ignore the error, we do not want to crash for a warning.
log.Warningln(`Looks like you're creating an application using credentials set by environment variables.
Copilot will store your application metadata in this account.
We recommend using credentials from named profiles. To learn more:
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/cli/app_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,9 @@ func TestInitAppOpts_Ask(t *testing.T) {
store: mocks.NewMockstore(ctrl),
ws: mocks.NewMockwsAppManager(ctrl),
prompt: mocks.NewMockprompter(ctrl),
isSessionFromEnvVars: func() (bool, error) {
return false, nil
},
}
tc.expect(opts)

Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/cli/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ func newInitOpts(vars initVars) (*initOpts, error) {
identity: id,
cfn: deployer,
prog: spin,
isSessionFromEnvVars: func() (bool, error) {
return sessions.AreCredsFromEnvVars(defaultSess)
},
}
initEnvCmd := &initEnvOpts{
initEnvVars: initEnvVars{
Expand Down

0 comments on commit a486bac

Please sign in to comment.