-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaudit_test.go
More file actions
135 lines (122 loc) · 4.55 KB
/
Copy pathaudit_test.go
File metadata and controls
135 lines (122 loc) · 4.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package commands
import (
"context"
"errors"
"testing"
"github.com/loilo-inc/canarycage/v6/cli/cage/cageapp"
"github.com/loilo-inc/canarycage/v6/mocks/mock_types"
"github.com/loilo-inc/canarycage/v6/types"
"github.com/stretchr/testify/assert"
"github.com/urfave/cli/v3"
"go.uber.org/mock/gomock"
)
func TestAudit(t *testing.T) {
t.Run("returns error when region is missing", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit", "--region", ""})
assert.Error(t, err)
assert.Contains(t, err.Error(), "--region flag is required")
})
t.Run("return errors when too many arguments", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit", "--region", "us-east-1", "arg1", "arg2"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid number of arguments. expected at most 1")
})
t.Run("returns error when both directory and flags are missing", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit", "--region", "us-east-1"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "either directory argument or both --cluster and --service flags must be provided")
})
t.Run("returns error when only cluster flag is provided", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit", "--region", "us-east-1", "--cluster", "test-cluster"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "either directory argument or both --cluster and --service flags must be provided")
})
t.Run("returns error when only service flag is provided", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit", "--region", "us-east-1", "--service", "test-service"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "either directory argument or both --cluster and --service flags must be provided")
})
t.Run("returns error when diProvider fails", func(t *testing.T) {
expectedErr := errors.New("di provider error")
app := setupAuditApp(t, func(ctx context.Context, input *cageapp.AuditCmdInput) (types.Audit, error) {
assert.Equal(t, "us-east-1", input.Region)
return nil, expectedErr
})
err := app.Run(context.Background(), []string{
"cage", "audit", "--region", "us-east-1", "--cluster", "test-cluster", "--service", "test-service",
})
assert.Error(t, err)
assert.Equal(t, expectedErr, err)
})
setupBase := func(t *testing.T) (*cli.Command, *mock_types.MockAudit) {
t.Helper()
ctrl := gomock.NewController(t)
mockAudit := mock_types.NewMockAudit(ctrl)
app := setupAuditApp(t, func(ctx context.Context, input *cageapp.AuditCmdInput) (types.Audit, error) {
assert.Equal(t, "us-east-1", input.Region)
return mockAudit, nil
})
return app, mockAudit
}
t.Run("Succcess", func(t *testing.T) {
setup := func(t *testing.T) *cli.Command {
t.Helper()
app, mockAudit := setupBase(t)
mockAudit.EXPECT().
Run(gomock.Any()).
Return(nil)
return app
}
t.Run("executes scan with directory argument", func(t *testing.T) {
app := setup(t)
err := app.Run(context.Background(), []string{"cage", "audit",
"--region", "us-east-1", "../../../fixtures"})
assert.NoError(t, err)
})
t.Run("executes scan with flags", func(t *testing.T) {
app := setup(t)
err := app.Run(context.Background(), []string{"cage", "audit",
"--region", "us-east-1",
"--cluster", "cluster",
"--service", "service"})
assert.NoError(t, err)
})
})
t.Run("Error", func(t *testing.T) {
t.Run("error on scanner.Scan()", func(t *testing.T) {
app, mockAudit := setupBase(t)
mockAudit.EXPECT().
Run(gomock.Any()).
Return(errors.New("scan error"))
err := app.Run(context.Background(), []string{"cage", "audit",
"--region", "us-east-1",
"--cluster", "cluster",
"--service", "service"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "scan error")
})
t.Run("error on loading service definition", func(t *testing.T) {
app := setupAuditApp(t, nil)
err := app.Run(context.Background(), []string{"cage", "audit",
"--region", "us-east-1", "../../../fixtures/invalid-service"})
assert.Error(t, err)
assert.Contains(t, err.Error(), "no 'service.json' found")
})
})
}
func setupAuditApp(t *testing.T, provider cageapp.AuditCmdProvider) *cli.Command {
t.Helper()
conf := &cageapp.App{}
app := &cli.Command{
Name: "cage",
Commands: []*cli.Command{
Audit(conf, provider),
},
}
return app
}