github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/tools/list_test.go (about) 1 // Copyright 2013 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package tools_test 5 6 import ( 7 gc "launchpad.net/gocheck" 8 9 "launchpad.net/juju-core/tools" 10 "launchpad.net/juju-core/version" 11 ) 12 13 type ListSuite struct{} 14 15 var _ = gc.Suite(&ListSuite{}) 16 17 func mustParseTools(name string) *tools.Tools { 18 return &tools.Tools{ 19 Version: version.MustParseBinary(name), 20 URL: "http://testing.invalid/" + name, 21 } 22 } 23 24 func extend(lists ...tools.List) tools.List { 25 var result tools.List 26 for _, list := range lists { 27 result = append(result, list...) 28 } 29 return result 30 } 31 32 var ( 33 t100precise = mustParseTools("1.0.0-precise-amd64") 34 t100precise32 = mustParseTools("1.0.0-precise-i386") 35 t100quantal = mustParseTools("1.0.0-quantal-amd64") 36 t100quantal32 = mustParseTools("1.0.0-quantal-i386") 37 t100all = tools.List{ 38 t100precise, t100precise32, t100quantal, t100quantal32, 39 } 40 t190precise = mustParseTools("1.9.0-precise-amd64") 41 t190precise32 = mustParseTools("1.9.0-precise-i386") 42 t190quantal = mustParseTools("1.9.0-quantal-amd64") 43 t190all = tools.List{ 44 t190precise, t190precise32, t190quantal, 45 } 46 t200precise = mustParseTools("2.0.0-precise-amd64") 47 t200quantal32 = mustParseTools("2.0.0-quantal-i386") 48 t200all = tools.List{ 49 t200precise, t200quantal32, 50 } 51 t2001precise = mustParseTools("2.0.0.1-precise-amd64") 52 tAllBefore210 = extend(t100all, t190all, append(t200all, t2001precise)) 53 t210precise = mustParseTools("2.1.0-precise-amd64") 54 t211precise = mustParseTools("2.1.1-precise-amd64") 55 t215precise = mustParseTools("2.1.5-precise-amd64") 56 t2152precise = mustParseTools("2.1.5.2-precise-amd64") 57 t210all = tools.List{t210precise, t211precise, t215precise, t2152precise} 58 ) 59 60 type stringsTest struct { 61 src tools.List 62 expect []string 63 } 64 65 var seriesTests = []stringsTest{{ 66 src: tools.List{t100precise}, 67 expect: []string{"precise"}, 68 }, { 69 src: tools.List{t100precise, t100precise32, t200precise}, 70 expect: []string{"precise"}, 71 }, { 72 src: tAllBefore210, 73 expect: []string{"precise", "quantal"}, 74 }} 75 76 func (s *ListSuite) TestSeries(c *gc.C) { 77 for i, test := range seriesTests { 78 c.Logf("test %d", i) 79 c.Check(test.src.AllSeries(), gc.DeepEquals, test.expect) 80 if len(test.expect) == 1 { 81 c.Check(test.src.OneSeries(), gc.Equals, test.expect[0]) 82 } 83 } 84 } 85 86 var archesTests = []stringsTest{{ 87 src: tools.List{t100precise}, 88 expect: []string{"amd64"}, 89 }, { 90 src: tools.List{t100precise, t100quantal, t200precise}, 91 expect: []string{"amd64"}, 92 }, { 93 src: tAllBefore210, 94 expect: []string{"amd64", "i386"}, 95 }} 96 97 func (s *ListSuite) TestArches(c *gc.C) { 98 for i, test := range archesTests { 99 c.Logf("test %d", i) 100 c.Check(test.src.Arches(), gc.DeepEquals, test.expect) 101 } 102 } 103 104 func (s *ListSuite) TestURLs(c *gc.C) { 105 empty := tools.List{} 106 c.Check(empty.URLs(), gc.DeepEquals, map[version.Binary]string{}) 107 108 full := tools.List{t100precise, t190quantal, t2001precise} 109 c.Check(full.URLs(), gc.DeepEquals, map[version.Binary]string{ 110 t100precise.Version: t100precise.URL, 111 t190quantal.Version: t190quantal.URL, 112 t2001precise.Version: t2001precise.URL, 113 }) 114 } 115 116 var newestTests = []struct { 117 src tools.List 118 expect tools.List 119 number version.Number 120 }{{ 121 src: nil, 122 expect: nil, 123 number: version.Zero, 124 }, { 125 src: tools.List{t100precise}, 126 expect: tools.List{t100precise}, 127 number: version.MustParse("1.0.0"), 128 }, { 129 src: t100all, 130 expect: t100all, 131 number: version.MustParse("1.0.0"), 132 }, { 133 src: extend(t100all, t190all, t200all), 134 expect: t200all, 135 number: version.MustParse("2.0.0"), 136 }, { 137 src: tAllBefore210, 138 expect: tools.List{t2001precise}, 139 number: version.MustParse("2.0.0.1"), 140 }} 141 142 func (s *ListSuite) TestNewest(c *gc.C) { 143 for i, test := range newestTests { 144 c.Logf("test %d", i) 145 number, actual := test.src.Newest() 146 c.Check(number, gc.DeepEquals, test.number) 147 c.Check(actual, gc.DeepEquals, test.expect) 148 } 149 } 150 151 var newestCompatibleTests = []struct { 152 src tools.List 153 base version.Number 154 expect version.Number 155 found bool 156 }{{ 157 src: nil, 158 base: version.Zero, 159 expect: version.Zero, 160 found: false, 161 }, { 162 src: tools.List{t100precise}, 163 base: version.Zero, 164 expect: version.Zero, 165 found: false, 166 }, { 167 src: t100all, 168 base: version.MustParse("1.0.0"), 169 expect: version.MustParse("1.0.0"), 170 found: true, 171 }, { 172 src: tAllBefore210, 173 base: version.MustParse("2.0.0"), 174 expect: version.MustParse("2.0.0.1"), 175 found: true, 176 }, { 177 src: tAllBefore210, 178 base: version.MustParse("1.9.0"), 179 expect: version.MustParse("1.9.0"), 180 found: true, 181 }, { 182 src: t210all, 183 base: version.MustParse("2.1.1"), 184 expect: version.MustParse("2.1.5.2"), 185 found: true, 186 }} 187 188 func (s *ListSuite) TestNewestCompatible(c *gc.C) { 189 for i, test := range newestCompatibleTests { 190 c.Logf("test %d", i) 191 actual, found := test.src.NewestCompatible(test.base) 192 c.Check(actual, gc.DeepEquals, test.expect) 193 c.Check(found, gc.Equals, test.found) 194 } 195 } 196 197 var excludeTests = []struct { 198 src tools.List 199 arg tools.List 200 expect tools.List 201 }{{ 202 nil, tools.List{t100precise}, nil, 203 }, { 204 tools.List{t100precise}, nil, tools.List{t100precise}, 205 }, { 206 tools.List{t100precise}, tools.List{t100precise}, nil, 207 }, { 208 nil, tAllBefore210, nil, 209 }, { 210 tAllBefore210, nil, tAllBefore210, 211 }, { 212 tAllBefore210, tAllBefore210, nil, 213 }, { 214 t100all, 215 tools.List{t100precise}, 216 tools.List{t100precise32, t100quantal, t100quantal32}, 217 }, { 218 t100all, 219 tools.List{t100precise32, t100quantal, t100quantal32}, 220 tools.List{t100precise}, 221 }, { 222 t100all, t190all, t100all, 223 }, { 224 t190all, t100all, t190all, 225 }, { 226 extend(t100all, t190all), 227 t190all, 228 t100all, 229 }} 230 231 func (s *ListSuite) TestExclude(c *gc.C) { 232 for i, test := range excludeTests { 233 c.Logf("test %d", i) 234 c.Check(test.src.Exclude(test.arg), gc.DeepEquals, test.expect) 235 } 236 } 237 238 var matchTests = []struct { 239 src tools.List 240 filter tools.Filter 241 expect tools.List 242 }{{ 243 tools.List{t100precise}, 244 tools.Filter{}, 245 tools.List{t100precise}, 246 }, { 247 tAllBefore210, 248 tools.Filter{}, 249 tAllBefore210, 250 }, { 251 tAllBefore210, 252 tools.Filter{Released: true}, 253 extend(t100all, t200all), 254 }, { 255 t190all, 256 tools.Filter{Released: true}, 257 nil, 258 }, { 259 tAllBefore210, 260 tools.Filter{Number: version.MustParse("1.9.0")}, 261 t190all, 262 }, { 263 tAllBefore210, 264 tools.Filter{Number: version.MustParse("1.9.0.1")}, 265 nil, 266 }, { 267 tAllBefore210, 268 tools.Filter{Series: "quantal"}, 269 tools.List{t100quantal, t100quantal32, t190quantal, t200quantal32}, 270 }, { 271 tAllBefore210, 272 tools.Filter{Series: "raring"}, 273 nil, 274 }, { 275 tAllBefore210, 276 tools.Filter{Arch: "i386"}, 277 tools.List{t100precise32, t100quantal32, t190precise32, t200quantal32}, 278 }, { 279 tAllBefore210, 280 tools.Filter{Arch: "arm"}, 281 nil, 282 }, { 283 tAllBefore210, 284 tools.Filter{ 285 Released: true, 286 Number: version.MustParse("2.0.0"), 287 Series: "quantal", 288 Arch: "i386", 289 }, 290 tools.List{t200quantal32}, 291 }} 292 293 func (s *ListSuite) TestMatch(c *gc.C) { 294 for i, test := range matchTests { 295 c.Logf("test %d", i) 296 actual, err := test.src.Match(test.filter) 297 c.Check(actual, gc.DeepEquals, test.expect) 298 if len(test.expect) > 0 { 299 c.Check(err, gc.IsNil) 300 } else { 301 c.Check(err, gc.Equals, tools.ErrNoMatches) 302 } 303 } 304 }