-
Notifications
You must be signed in to change notification settings - Fork 59
/
channel_test.go
194 lines (184 loc) · 4.28 KB
/
channel_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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package charm_test
import (
"github.com/juju/errors"
"github.com/juju/testing"
jc "github.com/juju/testing/checkers"
gc "gopkg.in/check.v1"
"github.com/juju/charm/v12"
)
type channelSuite struct {
testing.IsolationSuite
}
var _ = gc.Suite(&channelSuite{})
func (s channelSuite) TestParseChannelNormalize(c *gc.C) {
// ParseChannelNormalize tests ParseChannel as well.
tests := []struct {
Name string
Value string
Expected charm.Channel
ExpectedErr string
}{{
Name: "empty",
Value: "",
ExpectedErr: "empty channel not valid",
}, {
Name: "empty components",
Value: "//",
ExpectedErr: `risk in channel "//" not valid`,
}, {
Name: "too many components",
Value: "////",
ExpectedErr: `channel is malformed and has too many components "////"`,
}, {
Name: "invalid risk",
Value: "track/meshuggah",
ExpectedErr: `risk in channel "track/meshuggah" not valid`,
}, {
Name: "risk",
Value: "stable",
Expected: charm.Channel{
Risk: "stable",
},
}, {
Name: "track",
Value: "meshuggah",
Expected: charm.Channel{
Track: "meshuggah",
Risk: "stable",
},
}, {
Name: "risk and branch",
Value: "stable/foo",
Expected: charm.Channel{
Risk: "stable",
Branch: "foo",
},
}, {
Name: "track and risk",
Value: "foo/stable",
Expected: charm.Channel{
Track: "foo",
Risk: "stable",
},
}, {
Name: "track, risk and branch",
Value: "foo/stable/bar",
Expected: charm.Channel{
Track: "foo",
Risk: "stable",
Branch: "bar",
},
}}
for k, test := range tests {
c.Logf("test %q at %d", test.Name, k)
ch, err := charm.ParseChannelNormalize(test.Value)
if test.ExpectedErr != "" {
c.Assert(err, gc.ErrorMatches, test.ExpectedErr)
} else {
c.Assert(ch, gc.DeepEquals, test.Expected)
c.Assert(err, gc.IsNil)
}
}
}
func (s channelSuite) TestString(c *gc.C) {
tests := []struct {
Name string
Value string
Expected string
}{{
Name: "risk",
Value: "stable",
Expected: "stable",
}, {
Name: "latest track",
Value: "latest/stable",
Expected: "latest/stable",
}, {
Name: "track",
Value: "1.0",
Expected: "1.0/stable",
}, {
Name: "track and risk",
Value: "1.0/edge",
Expected: "1.0/edge",
}, {
Name: "track, risk and branch",
Value: "1.0/edge/foo",
Expected: "1.0/edge/foo",
}, {
Name: "latest, risk and branch",
Value: "latest/edge/foo",
Expected: "latest/edge/foo",
}}
for k, test := range tests {
c.Logf("test %q at %d", test.Name, k)
ch, err := charm.ParseChannelNormalize(test.Value)
c.Assert(err, gc.IsNil)
c.Assert(ch.String(), gc.DeepEquals, test.Expected)
}
}
func (s channelSuite) TestMakeChannel(c *gc.C) {
tests := []struct {
Name string
Track string
Risk string
Branch string
Expected string
ErrorType func(err error) bool
}{{
Name: "track, risk, branch not normalized",
Track: "latest",
Risk: "beta",
Branch: "bar",
Expected: "latest/beta/bar",
ErrorType: nil,
}, {
Name: "",
Track: "",
Risk: "testme",
Branch: "",
ErrorType: errors.IsNotValid,
}}
for k, test := range tests {
c.Logf("test %q at %d", test.Name, k)
ch, err := charm.MakeChannel(test.Track, test.Risk, test.Branch)
if test.ErrorType == nil {
c.Assert(err, jc.ErrorIsNil)
c.Assert(ch, gc.DeepEquals, charm.Channel{
Track: test.Track,
Risk: charm.Risk(test.Risk),
Branch: test.Branch,
})
} else {
c.Assert(err, jc.Satisfies, errors.IsNotValid)
}
}
}
func (s channelSuite) TestMakePermissiveChannelAndEmpty(c *gc.C) {
tests := []struct {
Name string
Track string
Risk string
Expected string
}{{
Name: "latest track, risk",
Track: "latest",
Risk: "beta",
Expected: "latest/beta",
}, {
Name: "risk not valid",
Track: "",
Risk: "testme",
Expected: "testme",
}}
for k, test := range tests {
c.Logf("test %q at %d", test.Name, k)
ch := charm.MakePermissiveChannel(test.Track, test.Risk, "")
c.Assert(ch.String(), gc.Equals, test.Expected)
}
}
func (s channelSuite) TestEmpty(c *gc.C) {
c.Assert(charm.Channel{}.Empty(), jc.IsTrue)
}