Skip to content

Commit

Permalink
test fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eschwartz committed Feb 26, 2020
1 parent db9ae00 commit e4a2a69
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 63 deletions.
21 changes: 19 additions & 2 deletions tests/integration/cli_test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,31 @@ type cliTest struct {
stdout *bytes.Buffer
injector injector
configFile string
t *testing.T
}

func (test *cliTest) WriteConfig(t *testing.T, config *configs.Root) {
test.configFile = writeTempConfig(t, config)
}

func (test *cliTest) Execute(args []string) error {
// Pass in config file path, if we have one
if test.configFile != "" {
var hasConfigFlag bool
for _, arg := range args {
if arg == "--config" {
hasConfigFlag = true
break
}
}

// Use a temporary config file, if none is otherwise set
if !hasConfigFlag {
// Write an empty config file, if none has been set
// we want to avoid accidentally using the system's ~/.dce/config.yml`,
// by default
if test.configFile == "" {
test.WriteConfig(test.t, &configs.Root{})
}

args = append(args, "--config", test.configFile)
}

Expand All @@ -54,6 +70,7 @@ func NewCLITest(t *testing.T) *cliTest {
cli := &cliTest{
MockPrompter: prompter,
stdout: &stdout,
t: t,
}

// Wrap the `PreRun` method, to inject the mock prompter,
Expand Down
93 changes: 32 additions & 61 deletions tests/integration/leases_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,38 @@ import (
"time"
)

func TestLeasesCreate(t *testing.T) {

t.Run("with --expires-on flag", func(t *testing.T) {
leasesCreateTest(t, &leaseCreateTestCase{
commandArgs: []string{
"leases", "create",
"-p", "test-user",
"-b", "100", "-c", "USD",
"-e", "[email protected]",
"--expires-on", "1h",
},
expectedCreateLeaseRequest: operations.PostLeasesBody{
BudgetAmount: aws.Float64(100),
BudgetCurrency: aws.String("USD"),
BudgetNotificationEmails: []string{"[email protected]"},
ExpiresOn: float64(time.Now().Add(time.Hour).Unix()),
PrincipalID: aws.String("test-user"),
},
mockAccountID: "123456789012",
expectedJSONOutput: map[string]interface{}{
"accountId": "123456789012",
"budgetAmount": float64(100),
"budgetCurrency": "USD",
"budgetNotificationEmails": []interface{}{"[email protected]"},
"principalId": "test-user",
"expiresOn": time.Now().Add(time.Hour).Unix(),
},
})
})

}

type leaseCreateTestCase struct {
// Args to pass to the dce-cli
commandArgs []string
Expand Down Expand Up @@ -113,64 +145,3 @@ func leasesCreateTest(t *testing.T, testCase *leaseCreateTestCase) {
api.AssertExpectations(t)
out.AssertNumberOfCalls(t, "Write", 1)
}

func TestLeasesCreate(t *testing.T) {

t.Run("with all required flags, only", func(t *testing.T) {
leasesCreateTest(t, &leaseCreateTestCase{
commandArgs: []string{
"leases", "create",
"-p", "test-user",
"-b", "100", "-c", "USD",
"-e", "[email protected]", "-e", "[email protected]",
},
expectedCreateLeaseRequest: operations.PostLeasesBody{
BudgetAmount: aws.Float64(100),
BudgetCurrency: aws.String("USD"),
BudgetNotificationEmails: []string{"[email protected]", "[email protected]"},
// Defaults to 7d
ExpiresOn: float64(time.Now().Add(time.Hour * 24 * 7).Unix()),
PrincipalID: aws.String("test-user"),
},
mockAccountID: "123456789012",
expectedJSONOutput: map[string]interface{}{
"accountId": "123456789012",
"budgetAmount": float64(100),
"budgetCurrency": "USD",
"budgetNotificationEmails": []interface{}{"[email protected]", "[email protected]"},
"principalId": "test-user",
// Defaults to 7d
"expiresOn": time.Now().Add(time.Hour * 24 * 7).Unix(),
},
})
})

t.Run("with --expires-on flag", func(t *testing.T) {
leasesCreateTest(t, &leaseCreateTestCase{
commandArgs: []string{
"leases", "create",
"-p", "test-user",
"-b", "100", "-c", "USD",
"-e", "[email protected]",
"--expires-on", "1h",
},
expectedCreateLeaseRequest: operations.PostLeasesBody{
BudgetAmount: aws.Float64(100),
BudgetCurrency: aws.String("USD"),
BudgetNotificationEmails: []string{"[email protected]"},
ExpiresOn: float64(time.Now().Add(time.Hour).Unix()),
PrincipalID: aws.String("test-user"),
},
mockAccountID: "123456789012",
expectedJSONOutput: map[string]interface{}{
"accountId": "123456789012",
"budgetAmount": float64(100),
"budgetCurrency": "USD",
"budgetNotificationEmails": []interface{}{"[email protected]"},
"principalId": "test-user",
"expiresOn": time.Now().Add(time.Hour).Unix(),
},
})
})

}

0 comments on commit e4a2a69

Please sign in to comment.