-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_ebs_snapshot_internal_test.go
More file actions
132 lines (120 loc) · 2.8 KB
/
scan_ebs_snapshot_internal_test.go
File metadata and controls
132 lines (120 loc) · 2.8 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
// Copyright 2025 variHQ OÜ
// SPDX-License-Identifier: BSD-3-Clause
package spark
import (
"context"
"errors"
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
)
type mockEBSSnapshotClient struct {
mockSnapshot []types.Snapshot
mockSnapshotErr error
}
func (m *mockEBSSnapshotClient) DescribeSnapshots(
_ context.Context,
_ *ec2.DescribeSnapshotsInput,
_ ...func(*ec2.Options),
) (*ec2.DescribeSnapshotsOutput, error) {
return &ec2.DescribeSnapshotsOutput{Snapshots: m.mockSnapshot}, m.mockSnapshotErr
}
var _ ebsSnapshotClient = (*mockEBSSnapshotClient)(nil)
func Test_ebsSnapshotScan_scan(t *testing.T) {
t.Parallel()
withTimeout, _ := context.WithTimeout(t.Context(), -time.Minute) //nolint:govet
now := time.Now()
tests := []struct {
name string
ctx context.Context //nolint:containedctx
client ebsSnapshotClient
region string
target string
want []Result
wantErr bool
}{
{
name: "should fail when ctx is cancelled",
ctx: withTimeout,
client: &mockEBSSnapshotClient{
mockSnapshot: nil,
mockSnapshotErr: nil,
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: true,
},
{
name: "should fail when api returns error",
ctx: t.Context(),
client: &mockEBSSnapshotClient{
mockSnapshot: nil,
mockSnapshotErr: errors.New("some error"),
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: true,
},
{
name: "should succeed with zero snapshots when no snapshots found",
ctx: t.Context(),
client: &mockEBSSnapshotClient{
mockSnapshot: nil,
mockSnapshotErr: nil,
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: false,
},
{
name: "should succeed with one snapshot",
ctx: t.Context(),
client: &mockEBSSnapshotClient{
mockSnapshot: []types.Snapshot{
{
CompletionTime: &now,
SnapshotId: aws.String("test-snapshot-id"),
},
},
mockSnapshotErr: nil,
},
region: "eu-west-1",
target: "self",
want: []Result{
{
CreationDate: now.Format(time.RFC3339),
Identifier: "test-snapshot-id",
Region: "eu-west-1",
RType: SnapshotEBS,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := &EBSSnapshotScan{
baseRunner: baseRunner{
region: tt.region,
runnerType: SnapshotEBS,
},
client: tt.client,
}
got, err := s.Scan(tt.ctx, tt.target)
if (err != nil) != tt.wantErr {
t.Errorf("scan() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("scan() got = %v, want %v", got, tt.want)
}
})
}
}