-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcost_test.go
115 lines (108 loc) · 3.09 KB
/
cost_test.go
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
package acos
import (
"context"
"reflect"
"testing"
"time"
"github.com/aws/aws-sdk-go-v2/service/costexplorer"
"github.com/aws/aws-sdk-go-v2/service/costexplorer/types"
)
func TestGetCosts(t *testing.T) {
type args struct {
ctx context.Context
accounts Accounts
opt AcosGetCostsOption
}
tests := []struct {
name string
args args
want Costs
wantErr bool
}{
{
name: "error when empty accounts",
args: args{
ctx: context.Background(),
accounts: Accounts{},
opt: NewGetCostsOption(time.Now().UTC()),
},
want: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetCosts(tt.args.ctx, tt.args.accounts, tt.args.opt)
if (err != nil) != tt.wantErr {
t.Errorf("GetCosts() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCosts() = %v, want %v", got, tt.want)
}
})
}
}
type mockGetCostAndUsageAPI func(ctx context.Context, params *costexplorer.GetCostAndUsageInput, optFns ...func(*costexplorer.Options)) (*costexplorer.GetCostAndUsageOutput, error)
func (m mockGetCostAndUsageAPI) GetCostAndUsage(ctx context.Context, params *costexplorer.GetCostAndUsageInput, optFns ...func(*costexplorer.Options)) (*costexplorer.GetCostAndUsageOutput, error) {
return m(ctx, params, optFns...)
}
func TestWithMock_GetCosts(t *testing.T) {
ceClient = mockGetCostAndUsageAPI(func(ctx context.Context, params *costexplorer.GetCostAndUsageInput, optFns ...func(*costexplorer.Options)) (*costexplorer.GetCostAndUsageOutput, error) {
t.Helper()
out := &costexplorer.GetCostAndUsageOutput{
ResultsByTime: []types.ResultByTime{},
GroupDefinitions: []types.GroupDefinition{},
DimensionValueAttributes: []types.DimensionValuesWithAttributes{},
}
return out, nil
})
type args struct {
ctx context.Context
accounts Accounts
opt AcosGetCostsOption
}
tests := []struct {
name string
args args
want Costs
wantErr bool
}{
{
name: "selected account must exists in result", // even GetCostAndUsage API returns no result. This is because newly created account has no billed cost in most cases.
args: args{
ctx: context.Background(),
accounts: Accounts{
"123456789012": Account{
Id: toPointer("123456789012"),
Name: toPointer("test"),
},
},
opt: NewGetCostsOption(time.Now().UTC()),
},
want: Costs{
"123456789012": Cost{
AccountID: "123456789012",
AccountName: "test",
LatestDailyCostIncrease: 0,
LatestWeeklyCostIncrease: 0,
AmountLastMonth: 0,
AmountThisMonth: 0,
},
},
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := GetCosts(tt.args.ctx, tt.args.accounts, tt.args.opt)
if (err != nil) != tt.wantErr {
t.Errorf("GetCosts() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("GetCosts() = %v, want %v", got, tt.want)
}
})
}
}