forked from Bjorn248/terraform_cashier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
204 lines (184 loc) · 5.51 KB
/
main_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package main
import (
"encoding/json"
"testing"
)
func TestCalculateInfraCost(t *testing.T) {
mockResponseData := `{
"data": {
"r3_xlarge_Dedicated": [
{
"PricePerUnit": "0.408",
"Unit": "Hrs",
"Currency": "USD"
}
],
"r3_xlarge_Shared": [
{
"PricePerUnit": "0.371",
"Unit": "Hrs",
"Currency": "USD"
}
],
"m4_large_Shared": [
{
"PricePerUnit": "0.126",
"Unit": "Hrs",
"Currency": "USD"
}
],
"r4_xlarge_Shared": [
{
"PricePerUnit": "0.2964",
"Unit": "Hrs",
"Currency": "USD"
}
],
"m4_xlarge_Shared": [
{
"PricePerUnit": "0.251",
"Unit": "Hrs",
"Currency": "USD"
}
],
"db_t2_large_mysql_Single_AZ": [
{
"PricePerUnit": "0.178",
"Unit": "Hrs",
"Currency": "USD"
}
],
"db_t2_large_mysql_Multi_AZ": [
{
"PricePerUnit": "0.356",
"Unit": "Hrs",
"Currency": "USD"
}
]
}
}`
var mockResponse graphQLHTTPResponseBody
unmarshalErr := json.Unmarshal([]byte(mockResponseData), &mockResponse)
if unmarshalErr != nil {
t.Error("Error Unmarshalling Mock Response Body", unmarshalErr)
}
mockTerraformResources := resourceMap{
Resources: map[string]map[string]int{
"aws_instance": {
"r3.xlarge,Shared": 3,
"r3.xlarge,Dedicated": 1,
"m4.large,Shared": 1,
"r4.xlarge,Shared": 3,
"m4.xlarge,Shared": 1,
},
"aws_db_instance": {
"db.t2.large,mysql,Single-AZ": 2,
"db.t2.large,mysql,Multi-AZ": 1,
},
},
}
var resourceCostMapArray []resourceCostMap
var err error
resourceCostMapArray, err = calculateInfraCost(mockResponse, mockTerraformResources)
switch resourceCostMapArray[0].Name {
case "aws_instance":
if resourceCostMapArray[0].Total != 4.7872 {
t.Error("Expected 4.7872, got ", resourceCostMapArray[0].Total)
}
case "aws_db_instance":
if resourceCostMapArray[0].Total != 0.712 {
t.Error("Expected 0.712, got ", resourceCostMapArray[0].Total)
}
}
if err != nil {
t.Error("Something went wrong", err)
}
}
func TestProcessTerraformFile(t *testing.T) {
var err error
mockTerraformResources := resourceMap{
Resources: map[string]map[string]int{
"aws_instance": {
"r4.xlarge,Shared": 0,
},
"aws_db_instance": {
"db.t2.large,mysql,Single-AZ": 0,
},
},
}
mockTerraformResources, err = processTerraformPlan(mockTerraformResources, "test/test.plan")
if err != nil {
t.Error("error processing test/test.plan", err)
}
mockTerraformResources, err = processTerraformPlan(mockTerraformResources, "test/test.plan.bad")
if err.Error() != "zip: not a valid zip file" {
t.Error("expected: zip: not a valid zip file\ngot:", err)
}
mockTerraformResources, err = processTerraformPlan(mockTerraformResources, "test/does_not_exist.plan")
if err.Error() != "open test/does_not_exist.plan: no such file or directory" {
t.Error("Did not get expected error message", err)
}
if mockTerraformResources.Resources["aws_instance"]["r3.xlarge,Shared"] != 3 ||
mockTerraformResources.Resources["aws_instance"]["m4.large,Shared"] != 1 ||
mockTerraformResources.Resources["aws_instance"]["r4.xlarge,Shared"] != 3 ||
mockTerraformResources.Resources["aws_instance"]["m4.xlarge,Shared"] != 1 ||
mockTerraformResources.Resources["aws_instance"]["r3.xlarge,Dedicated"] != 1 ||
mockTerraformResources.Resources["aws_db_instance"]["db.t2.large,mysql,Multi-AZ"] != 1 ||
mockTerraformResources.Resources["aws_db_instance"]["db.t2.large,mysql,Single-AZ"] != 2 {
t.Error("Didn't not get expected results", mockTerraformResources)
}
}
func TestGenerateGraphQLQuery(t *testing.T) {
mockTerraformResources := resourceMap{
Resources: map[string]map[string]int{
"aws_instance": {
"r3.xlarge,Shared": 3,
"r3.xlarge,Dedicated": 1,
"m4.large,Shared": 1,
"r4.xlarge,Shared": 3,
"m4.xlarge,Shared": 1,
},
"aws_db_instance": {
"db.r4.xlarge,mysql,Single-AZ": 3,
"db.t2.large,mysql,Multi-AZ": 1,
},
},
}
graphQLQueryString, err := generateGraphQLQuery(mockTerraformResources)
if err != nil {
t.Error("Something went wrong generating the query string", err)
}
var requestBody graphQLHTTPRequestBody
unmarshalErr := json.Unmarshal([]byte(graphQLQueryString), &requestBody)
if unmarshalErr != nil {
t.Error("Did not generate expected query string", unmarshalErr)
}
}
func TestCountResource(t *testing.T) {
mockTerraformResources := resourceMap{
Resources: map[string]map[string]int{
"aws_instance": {
"r3.xlarge,Shared": 3,
"r3.xlarge,Dedicated": 1,
"m4.large,Shared": 1,
"r4.xlarge,Shared": 3,
"m4.xlarge,Shared": 1,
},
"aws_db_instance": {
"db.r4.xlarge,mysql,Single-AZ": 3,
"db.t2.large,mysql,Multi-AZ": 1,
},
},
}
mockTerraformResources = countResource(mockTerraformResources, "aws_instance", "t2.small,Shared")
mockTerraformResources = countResource(mockTerraformResources, "aws_instance", "t2.medium,Shared")
mockTerraformResources = countResource(mockTerraformResources, "aws_instance", "m4.xlarge,Shared")
if mockTerraformResources.Resources["aws_instance"]["r3.xlarge,Shared"] != 3 ||
mockTerraformResources.Resources["aws_instance"]["m4.large,Shared"] != 1 ||
mockTerraformResources.Resources["aws_instance"]["r4.xlarge,Shared"] != 3 ||
mockTerraformResources.Resources["aws_instance"]["t2.small,Shared"] != 1 ||
mockTerraformResources.Resources["aws_instance"]["t2.medium,Shared"] != 1 ||
mockTerraformResources.Resources["aws_instance"]["m4.xlarge,Shared"] != 2 {
t.Error("Did not get expected results", mockTerraformResources)
}
}