-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_internal_test.go
More file actions
135 lines (121 loc) · 2.45 KB
/
app_internal_test.go
File metadata and controls
135 lines (121 loc) · 2.45 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
// Copyright 2025 variHQ OÜ
// SPDX-License-Identifier: BSD-3-Clause
package spark
import (
"context"
"errors"
"reflect"
"testing"
"time"
)
func TestApp_Run(t *testing.T) {
t.Parallel()
withTimeout, _ := context.WithTimeout(t.Context(), -time.Minute) //nolint:govet
mockRunners := []Runner{
&EBSSnapshotScan{
baseRunner: baseRunner{
region: "eu-west-1",
runnerType: SnapshotEBS,
},
client: &mockEBSSnapshotClient{
mockSnapshot: nil,
mockSnapshotErr: nil,
},
},
}
tests := []struct {
name string
ctx context.Context //nolint:containedctx
runners []Runner
target string
want []Result
wantErr bool
}{
{
name: "successful run",
ctx: t.Context(),
runners: mockRunners,
target: "42",
want: nil,
wantErr: false,
},
{
name: "fail with timeout",
ctx: withTimeout,
runners: mockRunners,
target: "self",
want: nil,
wantErr: true,
},
{
name: "error during the scan",
ctx: t.Context(),
runners: mockRunners,
target: "",
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
a := &App{
Runners: tt.runners,
workerLimit: 1,
}
got, err := a.Run(tt.ctx, tt.target)
if (err != nil) != tt.wantErr {
t.Errorf("Run() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("Run() got = %v, want %v", got, tt.want)
}
})
}
}
func TestApp_GetAccountID(t *testing.T) {
t.Parallel()
tests := []struct {
name string
stsClient stsClient
wantErr bool
}{
{
name: "fail with error",
stsClient: &mockSTSClient{
mockAccountID: "",
mockGetCallerIdentityErr: errors.New("some error"),
},
wantErr: true,
},
{
name: "fail with empty account id",
stsClient: &mockSTSClient{
mockAccountID: "",
mockGetCallerIdentityErr: nil,
},
wantErr: true,
},
{
name: "obtain account ID without any errors",
stsClient: &mockSTSClient{
mockAccountID: "42",
mockGetCallerIdentityErr: nil,
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
a := &App{
stsClient: tt.stsClient,
}
err := a.GetAccountID(t.Context())
if (err != nil) != tt.wantErr {
t.Errorf("getAccountID() error = %v, wantErr %v", err, tt.wantErr)
}
})
}
}