-
Notifications
You must be signed in to change notification settings - Fork 59
/
base_test.go
177 lines (166 loc) · 5.14 KB
/
base_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 charm_test
import (
"encoding/json"
"strings"
"github.com/juju/os/v2"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
"github.com/juju/utils/v3/arch"
gc "gopkg.in/check.v1"
"github.com/juju/charm/v12"
)
type baseSuite struct {
testing.CleanupSuite
}
var _ = gc.Suite(&baseSuite{})
func (s *baseSuite) TestParseBase(c *gc.C) {
tests := []struct {
str string
parsedBase charm.Base
err string
}{
{
str: "ubuntu",
parsedBase: charm.Base{},
err: `base string must contain exactly one @. "ubuntu" not valid`,
}, {
str: "windows",
parsedBase: charm.Base{},
err: `base string must contain exactly one @. "windows" not valid`,
}, {
str: "mythicalos@channel",
parsedBase: charm.Base{},
err: `invalid base string "mythicalos@channel": os "mythicalos" not valid`,
}, {
str: "ubuntu@20.04/stable",
parsedBase: charm.Base{Name: strings.ToLower(os.Ubuntu.String()), Channel: mustParseChannel("20.04/stable")},
}, {
str: "windows@win10/stable",
parsedBase: charm.Base{Name: strings.ToLower(os.Windows.String()), Channel: mustParseChannel("win10/stable")},
}, {
str: "ubuntu@20.04/edge",
parsedBase: charm.Base{Name: strings.ToLower(os.Ubuntu.String()), Channel: mustParseChannel("20.04/edge")},
},
}
for i, v := range tests {
comment := gc.Commentf("test %d", i)
s, err := charm.ParseBase(v.str)
if v.err != "" {
c.Check(err, gc.ErrorMatches, v.err, comment)
} else {
c.Check(err, jc.ErrorIsNil, comment)
}
c.Check(s, jc.DeepEquals, v.parsedBase, comment)
}
}
func (s *baseSuite) TestParseBaseWithArchitectures(c *gc.C) {
tests := []struct {
str string
baseString string
archs []string
parsedBase charm.Base
err string
}{
{
baseString: "ubuntu@",
str: "ubuntu on amd64",
archs: []string{"amd64"},
parsedBase: charm.Base{},
err: `invalid base string "ubuntu@" with architectures "amd64": channel not valid`,
}, {
baseString: "windows@",
str: "windows",
parsedBase: charm.Base{},
err: `invalid base string "windows@": channel not valid`,
}, {
baseString: "mythicalos@channel",
str: "mythicalos",
parsedBase: charm.Base{},
err: `invalid base string "mythicalos@channel": os "mythicalos" not valid`,
}, {
baseString: "ubuntu@20.04/stable",
archs: []string{arch.AMD64, "ppc64"},
str: "ubuntu@20.04/stable on amd64, ppc64el",
parsedBase: charm.Base{
Name: strings.ToLower(os.Ubuntu.String()),
Channel: mustParseChannel("20.04/stable"),
Architectures: []string{arch.AMD64, arch.PPC64EL}},
}, {
baseString: "windows@win10/stable",
archs: []string{"testme"},
str: "windows@win10/stable",
parsedBase: charm.Base{},
err: `invalid base string "windows@win10/stable" with architectures "testme": architecture "testme" not valid`,
},
}
for i, v := range tests {
comment := gc.Commentf("test %d", i)
s, err := charm.ParseBase(v.baseString, v.archs...)
if v.err != "" {
c.Check(err, gc.ErrorMatches, v.err, comment)
} else {
c.Check(err, jc.ErrorIsNil, comment)
}
c.Check(s, jc.DeepEquals, v.parsedBase, comment)
}
}
func (s *baseSuite) TestStringifyBase(c *gc.C) {
tests := []struct {
base charm.Base
str string
}{
{
base: charm.Base{Name: strings.ToLower(os.Ubuntu.String()), Channel: mustParseChannel("20.04/stable")},
str: "ubuntu@20.04/stable",
}, {
base: charm.Base{Name: strings.ToLower(os.Windows.String()), Channel: mustParseChannel("win10/stable")},
str: "windows@win10/stable",
}, {
base: charm.Base{Name: strings.ToLower(os.Ubuntu.String()), Channel: mustParseChannel("20.04/edge")},
str: "ubuntu@20.04/edge",
}, {
base: charm.Base{
Name: strings.ToLower(os.Ubuntu.String()),
Channel: mustParseChannel("20.04/stable"),
Architectures: []string{arch.AMD64},
},
str: "ubuntu@20.04/stable on amd64",
}, {
base: charm.Base{
Name: strings.ToLower(os.Ubuntu.String()),
Channel: mustParseChannel("20.04/stable"),
Architectures: []string{arch.AMD64, arch.PPC64EL},
},
str: "ubuntu@20.04/stable on amd64, ppc64el",
},
}
for i, v := range tests {
comment := gc.Commentf("test %d", i)
c.Assert(v.base.Validate(), jc.ErrorIsNil)
c.Assert(v.base.String(), gc.Equals, v.str, comment)
}
}
func (s *baseSuite) TestJSONEncoding(c *gc.C) {
sys := charm.Base{
Name: "ubuntu",
Channel: mustParseChannel("20.04/stable"),
}
bytes, err := json.Marshal(sys)
c.Assert(err, jc.ErrorIsNil)
c.Assert(string(bytes), gc.Equals, `{"name":"ubuntu","channel":{"track":"20.04","risk":"stable"}}`)
sys2 := charm.Base{}
err = json.Unmarshal(bytes, &sys2)
c.Assert(err, jc.ErrorIsNil)
c.Assert(sys2, jc.DeepEquals, sys)
}
// MustParseChannel parses a given string or returns a panic.
// Used for unit tests.
func mustParseChannel(s string) charm.Channel {
c, err := charm.ParseChannelNormalize(s)
if err != nil {
panic(err)
}
return c
}