-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscan_ami_image_internal_test.go
More file actions
131 lines (119 loc) · 2.66 KB
/
scan_ami_image_internal_test.go
File metadata and controls
131 lines (119 loc) · 2.66 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
// 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 mockAMIClient struct {
mockImages []types.Image
mockImageErr error
}
func (m *mockAMIClient) DescribeImages(
_ context.Context,
_ *ec2.DescribeImagesInput,
_ ...func(*ec2.Options),
) (*ec2.DescribeImagesOutput, error) {
return &ec2.DescribeImagesOutput{Images: m.mockImages}, m.mockImageErr
}
var _ amiClient = (*mockAMIClient)(nil)
func Test_amiImageScan_scan(t *testing.T) {
t.Parallel()
withTimeout, _ := context.WithTimeout(t.Context(), -time.Minute) //nolint:govet
tests := []struct {
name string
ctx context.Context //nolint:containedctx
client amiClient
region string
target string
want []Result
wantErr bool
}{
{
name: "should fail when ctx is cancelled",
ctx: withTimeout,
client: &mockAMIClient{
mockImages: nil,
mockImageErr: nil,
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: true,
},
{
name: "should fail when api returns error",
ctx: t.Context(),
client: &mockAMIClient{
mockImages: nil,
mockImageErr: errors.New("some error"),
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: true,
},
{
name: "should succeed with zero images when no images found",
ctx: t.Context(),
client: &mockAMIClient{
mockImages: nil,
mockImageErr: nil,
},
region: "eu-west-1",
target: "self",
want: nil,
wantErr: false,
},
{
name: "should succeed with one AMI",
ctx: t.Context(),
client: &mockAMIClient{
mockImages: []types.Image{
{
CreationDate: aws.String("properly formatted date"),
ImageId: aws.String("test-image-id"),
},
},
mockImageErr: nil,
},
region: "eu-west-1",
target: "self",
want: []Result{
{
CreationDate: "properly formatted date",
Identifier: "test-image-id",
Region: "eu-west-1",
RType: ImageAMI,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
s := &AMIScan{
baseRunner: baseRunner{
region: tt.region,
runnerType: ImageAMI,
},
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)
}
})
}
}