forked from juju/description
-
Notifications
You must be signed in to change notification settings - Fork 0
/
charmorigin_test.go
177 lines (149 loc) · 4.54 KB
/
charmorigin_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
// Copyright 2020 Canonical Ltd.
// Licensed under the LGPLv3, see LICENCE file for details.
package description
import (
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"gopkg.in/yaml.v2"
)
type CharmOriginSerializationSuite struct {
SerializationSuite
}
var _ = gc.Suite(&CharmOriginSerializationSuite{})
func (s *CharmOriginSerializationSuite) SetUpTest(c *gc.C) {
s.importName = "charmOrigin"
s.importFunc = func(m map[string]interface{}) (interface{}, error) {
return importCharmOrigin(m)
}
}
func (s *CharmOriginSerializationSuite) TestNewCharmOrigin(c *gc.C) {
args := CharmOriginArgs{
Source: "local",
}
instance := newCharmOrigin(args)
c.Assert(instance.Source(), gc.Equals, args.Source)
}
func minimalCharmOriginMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"version": 2,
"source": "local",
"id": "",
"hash": "",
"revision": 0,
"channel": "",
"platform": "amd64/ubuntu/22.04/stable",
}
}
func minimalCharmOriginArgs() CharmOriginArgs {
return CharmOriginArgs{
Source: "local",
Platform: "amd64/ubuntu/22.04/stable",
}
}
func minimalCharmOrigin() *charmOrigin {
return newCharmOrigin(minimalCharmOriginArgs())
}
func maximalCharmOriginMap() map[interface{}]interface{} {
return map[interface{}]interface{}{
"version": 2,
"source": "charmhub",
"id": "random-id",
"hash": "c553eee8dc77f2cce29a1c7090d1e3c81e76c6e12346d09936048ed12305fd35",
"revision": 0,
"channel": "foo/stable",
"platform": "amd64/ubuntu/22.04/stable",
}
}
func maximalCharmOriginArgs() CharmOriginArgs {
return CharmOriginArgs{
Source: "charmhub",
ID: "random-id",
Hash: "c553eee8dc77f2cce29a1c7090d1e3c81e76c6e12346d09936048ed12305fd35",
Channel: "foo/stable",
Platform: "amd64/ubuntu/22.04/stable",
}
}
func maximalCharmOrigin() *charmOrigin {
return newCharmOrigin(maximalCharmOriginArgs())
}
func (s *CharmOriginSerializationSuite) TestMinimalMatches(c *gc.C) {
bytes, err := yaml.Marshal(minimalCharmOrigin())
c.Assert(err, jc.ErrorIsNil)
var source map[interface{}]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(source, jc.DeepEquals, minimalCharmOriginMap())
}
func (s *CharmOriginSerializationSuite) TestMaximalMatches(c *gc.C) {
bytes, err := yaml.Marshal(maximalCharmOrigin())
c.Assert(err, jc.ErrorIsNil)
var source map[interface{}]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(source, jc.DeepEquals, maximalCharmOriginMap())
}
func (s *CharmOriginSerializationSuite) TestParsingSerializedData(c *gc.C) {
initial := maximalCharmOrigin()
bytes, err := yaml.Marshal(initial)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
instance, err := importCharmOrigin(source)
c.Assert(err, jc.ErrorIsNil)
c.Assert(instance, jc.DeepEquals, initial)
}
func (s *CharmOriginSerializationSuite) exportImportVersion(c *gc.C, origin_ *charmOrigin, version int) *charmOrigin {
origin_.Version_ = version
bytes, err := yaml.Marshal(origin_)
c.Assert(err, jc.ErrorIsNil)
var source map[string]interface{}
err = yaml.Unmarshal(bytes, &source)
c.Assert(err, jc.ErrorIsNil)
origin, err := importCharmOrigin(source)
c.Assert(err, jc.ErrorIsNil)
return origin
}
func (s *CharmOriginSerializationSuite) TestV1ParsingReturnsLatest(c *gc.C) {
args := maximalCharmOriginArgs()
args.Platform = "amd64/ubuntu/focal"
originV1 := newCharmOrigin(args)
originLatest := *originV1
originLatest.Platform_ = "amd64/ubuntu/20.04/stable"
originResult := s.exportImportVersion(c, originV1, 1)
c.Assert(*originResult, jc.DeepEquals, originLatest)
}
func (s *CharmOriginSerializationSuite) TestPlatformFromSeriesErrorsOnEmpty(c *gc.C) {
rval, err := platformFromSeries("")
c.Assert(err, gc.NotNil)
c.Assert(rval, gc.Equals, "")
c.Assert(err.Error(), gc.Equals, "cannot convert empty series to a platform")
}
func (s *CharmOriginSerializationSuite) TestPlatformFromSeries(c *gc.C) {
tests := []struct {
expectedVal string
series string
}{
{
expectedVal: "unknown/ubuntu/20.04",
series: "focal",
},
{
expectedVal: "unknown/ubuntu/22.04",
series: "jammy",
},
{
expectedVal: "unknown/ubuntu/16.04",
series: "xenial",
},
{
expectedVal: "unknown/windows/win2008r2",
series: "win2008r2",
},
}
for _, test := range tests {
platform, err := platformFromSeries(test.series)
c.Assert(err, jc.ErrorIsNil)
c.Assert(platform, gc.Equals, test.expectedVal)
}
}