github.com/juju/charm/v11@v11.2.0/channel_test.go (about) 1 // Copyright 2020 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package charm_test 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/testing" 9 jc "github.com/juju/testing/checkers" 10 gc "gopkg.in/check.v1" 11 12 "github.com/juju/charm/v11" 13 ) 14 15 type channelSuite struct { 16 testing.IsolationSuite 17 } 18 19 var _ = gc.Suite(&channelSuite{}) 20 21 func (s channelSuite) TestParseChannelNormalize(c *gc.C) { 22 // ParseChannelNormalize tests ParseChannel as well. 23 tests := []struct { 24 Name string 25 Value string 26 Expected charm.Channel 27 ExpectedErr string 28 }{{ 29 Name: "empty", 30 Value: "", 31 ExpectedErr: "empty channel not valid", 32 }, { 33 Name: "empty components", 34 Value: "//", 35 ExpectedErr: `risk in channel "//" not valid`, 36 }, { 37 Name: "too many components", 38 Value: "////", 39 ExpectedErr: `channel is malformed and has too many components "////"`, 40 }, { 41 Name: "invalid risk", 42 Value: "track/meshuggah", 43 ExpectedErr: `risk in channel "track/meshuggah" not valid`, 44 }, { 45 Name: "risk", 46 Value: "stable", 47 Expected: charm.Channel{ 48 Risk: "stable", 49 }, 50 }, { 51 Name: "track", 52 Value: "meshuggah", 53 Expected: charm.Channel{ 54 Track: "meshuggah", 55 Risk: "stable", 56 }, 57 }, { 58 Name: "risk and branch", 59 Value: "stable/foo", 60 Expected: charm.Channel{ 61 Risk: "stable", 62 Branch: "foo", 63 }, 64 }, { 65 Name: "track and risk", 66 Value: "foo/stable", 67 Expected: charm.Channel{ 68 Track: "foo", 69 Risk: "stable", 70 }, 71 }, { 72 Name: "track, risk and branch", 73 Value: "foo/stable/bar", 74 Expected: charm.Channel{ 75 Track: "foo", 76 Risk: "stable", 77 Branch: "bar", 78 }, 79 }} 80 for k, test := range tests { 81 c.Logf("test %q at %d", test.Name, k) 82 ch, err := charm.ParseChannelNormalize(test.Value) 83 if test.ExpectedErr != "" { 84 c.Assert(err, gc.ErrorMatches, test.ExpectedErr) 85 } else { 86 c.Assert(ch, gc.DeepEquals, test.Expected) 87 c.Assert(err, gc.IsNil) 88 } 89 } 90 } 91 92 func (s channelSuite) TestString(c *gc.C) { 93 tests := []struct { 94 Name string 95 Value string 96 Expected string 97 }{{ 98 Name: "risk", 99 Value: "stable", 100 Expected: "stable", 101 }, { 102 Name: "latest track", 103 Value: "latest/stable", 104 Expected: "latest/stable", 105 }, { 106 Name: "track", 107 Value: "1.0", 108 Expected: "1.0/stable", 109 }, { 110 Name: "track and risk", 111 Value: "1.0/edge", 112 Expected: "1.0/edge", 113 }, { 114 Name: "track, risk and branch", 115 Value: "1.0/edge/foo", 116 Expected: "1.0/edge/foo", 117 }, { 118 Name: "latest, risk and branch", 119 Value: "latest/edge/foo", 120 Expected: "latest/edge/foo", 121 }} 122 for k, test := range tests { 123 c.Logf("test %q at %d", test.Name, k) 124 ch, err := charm.ParseChannelNormalize(test.Value) 125 c.Assert(err, gc.IsNil) 126 c.Assert(ch.String(), gc.DeepEquals, test.Expected) 127 } 128 } 129 130 func (s channelSuite) TestMakeChannel(c *gc.C) { 131 tests := []struct { 132 Name string 133 Track string 134 Risk string 135 Branch string 136 Expected string 137 ErrorType func(err error) bool 138 }{{ 139 Name: "track, risk, branch not normalized", 140 Track: "latest", 141 Risk: "beta", 142 Branch: "bar", 143 Expected: "latest/beta/bar", 144 ErrorType: nil, 145 }, { 146 Name: "", 147 Track: "", 148 Risk: "testme", 149 Branch: "", 150 ErrorType: errors.IsNotValid, 151 }} 152 for k, test := range tests { 153 c.Logf("test %q at %d", test.Name, k) 154 ch, err := charm.MakeChannel(test.Track, test.Risk, test.Branch) 155 if test.ErrorType == nil { 156 c.Assert(err, jc.ErrorIsNil) 157 c.Assert(ch, gc.DeepEquals, charm.Channel{ 158 Track: test.Track, 159 Risk: charm.Risk(test.Risk), 160 Branch: test.Branch, 161 }) 162 } else { 163 c.Assert(err, jc.Satisfies, errors.IsNotValid) 164 } 165 } 166 } 167 168 func (s channelSuite) TestMakePermissiveChannelAndEmpty(c *gc.C) { 169 tests := []struct { 170 Name string 171 Track string 172 Risk string 173 Expected string 174 }{{ 175 Name: "latest track, risk", 176 Track: "latest", 177 Risk: "beta", 178 Expected: "latest/beta", 179 }, { 180 Name: "risk not valid", 181 Track: "", 182 Risk: "testme", 183 Expected: "testme", 184 }} 185 for k, test := range tests { 186 c.Logf("test %q at %d", test.Name, k) 187 ch := charm.MakePermissiveChannel(test.Track, test.Risk, "") 188 c.Assert(ch.String(), gc.Equals, test.Expected) 189 } 190 } 191 192 func (s channelSuite) TestEmpty(c *gc.C) { 193 c.Assert(charm.Channel{}.Empty(), jc.IsTrue) 194 }