-
Notifications
You must be signed in to change notification settings - Fork 4
/
serializable_distro_test.go
132 lines (111 loc) · 4.12 KB
/
serializable_distro_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
package database_test
import (
"context"
"testing"
"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/distros/database"
"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/distros/distro"
"github.com/canonical/ubuntu-pro-for-windows/windows-agent/internal/testutils"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
)
func TestSerializableDistroMarshallUnmarshall(t *testing.T) {
t.Parallel()
testCases := map[string]database.SerializableDistro{
"Normal case": {
Name: "Ubuntu",
GUID: "{12345678-1234-1234-1234-123456789abc}",
Properties: distro.Properties{
DistroID: "Ubuntu",
VersionID: "98.04",
PrettyName: "Ubuntu 98.04.0 LTS",
ProAttached: true,
},
},
"Escaped characters": {
Name: "Ubuntu",
GUID: "{12345678-1234-1234-1234-123456789abc}",
Properties: distro.Properties{
DistroID: "Ubuntu",
VersionID: "122.04",
PrettyName: `Ubuntu '122.04.0 LTS "Jammiest Jellifish"`,
ProAttached: true,
},
},
"Control characters": {
Name: "Ubuntu",
GUID: "{12345678-1234-1234-1234-123456789abc}",
Properties: distro.Properties{
DistroID: "Ubuntu",
VersionID: "122.04",
PrettyName: `Ubuntu 122.04.0 LTS\t (Evil\x00 character eðition)`,
ProAttached: true,
},
},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
marshalled, err := yaml.Marshal(tc)
require.NoError(t, err, "A serializableDistro should always succeed in marshalling")
// We don't really care what the text representation is, so long as the original
// object can be recovered. We log it here for informational purposes.
t.Logf("%s", marshalled)
var got database.SerializableDistro
err = yaml.Unmarshal(marshalled, &got)
require.NoError(t, err, "serializableDistro should be successfully unmarshalled")
require.Equal(t, tc, got, "A Marshalled-then-Unmarshalled serializableDistro should be identical to its original version")
})
}
}
//nolint:tparallel // Subtests are parallel but the test itself is not due to the calls to RegisterDistro.
func TestSerializableDistroNewDistro(t *testing.T) {
registeredDistro, registeredGUID := testutils.RegisterDistro(t, false)
unregisteredDistro, fakeGUID := testutils.NonRegisteredDistro(t)
illFormedGUID := "{this string is not a valid GUID}"
testCases := map[string]struct {
distro string
guid string
wantErr bool
}{
"Deserialize registered distro with matching GUID": {distro: registeredDistro, guid: registeredGUID},
"Error with registered distro with non-matching GUID": {distro: registeredDistro, guid: fakeGUID, wantErr: true},
"Error on registered distro with ill-formed GUID": {distro: registeredDistro, guid: illFormedGUID, wantErr: true},
"Error on non-registered distro with a registered GUID": {distro: unregisteredDistro, guid: registeredGUID, wantErr: true},
"Error on non-registered distro with a non-registered GUID": {distro: unregisteredDistro, guid: fakeGUID, wantErr: true},
}
for name, tc := range testCases {
tc := tc
t.Run(name, func(t *testing.T) {
t.Parallel()
s := database.SerializableDistro{
Name: tc.distro,
GUID: tc.guid,
}
d, err := s.NewDistro(t.TempDir())
if err == nil {
defer d.Cleanup(context.Background())
}
if tc.wantErr {
require.Error(t, err, "serializableDistro.New() should fail with the provided serializableDistro object")
return
}
require.NoError(t, err, "serializableDistro.New() should succeed when the provided serializableDistro is valid")
})
}
}
func TestNewSerializableDistro(t *testing.T) {
registeredDistro, registeredGUID := testutils.RegisterDistro(t, false)
props := distro.Properties{
DistroID: "ubuntu",
VersionID: "-5.04",
PrettyName: "Ubuntu -5.04 (Invented Idea)",
ProAttached: true,
}
d, err := distro.New(registeredDistro, props, t.TempDir())
require.NoError(t, err, "Setup: distro New() should return no error")
s := database.NewSerializableDistro(d)
require.Equal(t, registeredDistro, s.Name)
require.Equal(t, registeredGUID, s.GUID)
require.Equal(t, props, s.Properties)
}