forked from RedHatInsights/yggdrasil
-
Notifications
You must be signed in to change notification settings - Fork 7
/
canonical_facts_test.go
156 lines (148 loc) · 5.44 KB
/
canonical_facts_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
package yggdrasil
import (
"encoding/json"
"testing"
"github.com/google/go-cmp/cmp"
)
func TestCanonicalFactsFromMap(t *testing.T) {
tests := []struct {
description string
input map[string]interface{}
want *CanonicalFacts
wantError error
}{
{
description: "valid",
input: map[string]interface{}{
"insights_id": "bb69cd34-263f-444c-9278-5935b61d7f60",
"machine_id": "acc046d0-0add-4550-ac7c-5a833b1b6470",
"bios_uuid": "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
"subscription_manager_id": "bc452b83-c4ee-4b80-91d8-98ff816b2440",
"ip_addresses": []string{"1.2.3.4", "5.6.7.8"},
"fqdn": "foo.bar.com",
"mac_addresses": []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
want: &CanonicalFacts{
InsightsID: "bb69cd34-263f-444c-9278-5935b61d7f60",
MachineID: "acc046d0-0add-4550-ac7c-5a833b1b6470",
BIOSUUID: "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
SubscriptionManagerID: "bc452b83-c4ee-4b80-91d8-98ff816b2440",
IPAddresses: []string{"1.2.3.4", "5.6.7.8"},
FQDN: "foo.bar.com",
MACAddresses: []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
},
{
description: "error",
input: map[string]interface{}{
"insights_id": 1,
"machine_id": "acc046d0-0add-4550-ac7c-5a833b1b6470",
"bios_uuid": "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
"subscription_manager_id": "bc452b83-c4ee-4b80-91d8-98ff816b2440",
"ip_addresses": []string{"1.2.3.4", "5.6.7.8"},
"fqdn": "foo.bar.com",
"mac_addresses": []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
wantError: &InvalidValueTypeError{key: "insights_id", val: 1},
},
{
description: "valid with absent insights_id",
input: map[string]interface{}{
"machine_id": "acc046d0-0add-4550-ac7c-5a833b1b6470",
"bios_uuid": "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
"subscription_manager_id": "bc452b83-c4ee-4b80-91d8-98ff816b2440",
"ip_addresses": []string{"1.2.3.4", "5.6.7.8"},
"fqdn": "foo.bar.com",
"mac_addresses": []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
want: &CanonicalFacts{
InsightsID: "",
MachineID: "acc046d0-0add-4550-ac7c-5a833b1b6470",
BIOSUUID: "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
SubscriptionManagerID: "bc452b83-c4ee-4b80-91d8-98ff816b2440",
IPAddresses: []string{"1.2.3.4", "5.6.7.8"},
FQDN: "foo.bar.com",
MACAddresses: []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
got, err := CanonicalFactsFromMap(test.input)
if test.wantError != nil {
if !cmp.Equal(err, test.wantError, cmp.AllowUnexported(InvalidValueTypeError{})) {
t.Errorf("%v != %v", err, test.wantError)
}
} else {
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, test.want) {
t.Errorf("diff got test.want\n--- got\n+++ test.want\n%v", cmp.Diff(got, test.want))
}
}
})
}
}
func TestCanonicalFactsUnmarshalJSON(t *testing.T) {
tests := []struct {
description string
input []byte
want CanonicalFacts
wantError error
}{
{
description: "empty",
input: []byte(`{}`),
want: CanonicalFacts{},
},
{
description: "valid",
input: []byte(`{"insights_id":"bb69cd34-263f-444c-9278-5935b61d7f60","machine_id":"acc046d0-0add-4550-ac7c-5a833b1b6470","bios_uuid":"d8ec3cd5-a6bc-4742-bd2f-32940da182b0","subscription_manager_id":"bc452b83-c4ee-4b80-91d8-98ff816b2440","ip_addresses":["1.2.3.4", "5.6.7.8"],"fqdn":"foo.bar.com","mac_addresses":["CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"]}`),
want: CanonicalFacts{
InsightsID: "bb69cd34-263f-444c-9278-5935b61d7f60",
MachineID: "acc046d0-0add-4550-ac7c-5a833b1b6470",
BIOSUUID: "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
SubscriptionManagerID: "bc452b83-c4ee-4b80-91d8-98ff816b2440",
IPAddresses: []string{"1.2.3.4", "5.6.7.8"},
FQDN: "foo.bar.com",
MACAddresses: []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
},
},
}
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
var got CanonicalFacts
err := json.Unmarshal(test.input, &got)
if test.wantError != nil {
if !cmp.Equal(err, test.wantError) {
t.Errorf("%v != %v", err, test.wantError)
}
} else {
if err != nil {
t.Fatal(err)
}
if !cmp.Equal(got, test.want) {
t.Errorf("%v != %v", got, test.want)
}
}
})
}
}
func BenchmarkCanonicalFactsFromMap(b *testing.B) {
input := map[string]interface{}{
"insights_id": "bb69cd34-263f-444c-9278-5935b61d7f60",
"machine_id": "acc046d0-0add-4550-ac7c-5a833b1b6470",
"bios_uuid": "d8ec3cd5-a6bc-4742-bd2f-32940da182b0",
"subscription_manager_id": "bc452b83-c4ee-4b80-91d8-98ff816b2440",
"ip_addresses": []string{"1.2.3.4", "5.6.7.8"},
"fqdn": "foo.bar.com",
"mac_addresses": []string{"CC:D1:7A:44:6D:1B", "A7:03:90:D0:05:A7"},
}
for i := 0; i < b.N; i++ {
_, err := CanonicalFactsFromMap(input)
if err != nil {
b.Error(err)
}
}
}