func (*suite) TestParseBinary(c *gc.C) { parseBinaryTests := []struct { v string err string expect version.Binary }{{ v: "1.2.3-a-b", expect: binaryVersion(1, 2, 3, 0, "", "a", "b"), }, { v: "1.2.3.4-a-b", expect: binaryVersion(1, 2, 3, 4, "", "a", "b"), }, { v: "1.2-alpha3-a-b", expect: binaryVersion(1, 2, 3, 0, "alpha", "a", "b"), }, { v: "1.2-alpha3.4-a-b", expect: binaryVersion(1, 2, 3, 4, "alpha", "a", "b"), }, { v: "1.2.3", err: "invalid binary version.*", }, { v: "1.2-beta1", err: "invalid binary version.*", }, { v: "1.2.3--b", err: "invalid binary version.*", }, { v: "1.2.3-a-", err: "invalid binary version.*", }} for i, test := range parseBinaryTests { c.Logf("test 1: %d", i) got, err := version.ParseBinary(test.v) if test.err != "" { c.Assert(err, gc.ErrorMatches, test.err) } else { c.Assert(err, gc.IsNil) c.Assert(got, gc.Equals, test.expect) } } for i, test := range parseTests { c.Logf("test 2: %d", i) v := test.v + "-a-b" got, err := version.ParseBinary(v) expect := version.Binary{ Number: test.expect, Series: "a", Arch: "b", } if test.err != "" { c.Assert(err, gc.ErrorMatches, strings.Replace(test.err, "version", "binary version", 1)) } else { c.Assert(err, gc.IsNil) c.Assert(got, gc.Equals, expect) c.Check(got.IsDev(), gc.Equals, test.dev) } } }
// ReadList returns a List of the tools in store with the given major.minor version. // If minorVersion = -1, then only majorVersion is considered. // If store contains no such tools, it returns ErrNoMatches. func ReadList(stor storage.StorageReader, majorVersion, minorVersion int) (coretools.List, error) { if minorVersion >= 0 { logger.Debugf("reading v%d.%d tools", majorVersion, minorVersion) } else { logger.Debugf("reading v%d.* tools", majorVersion) } names, err := storage.List(stor, toolPrefix) if err != nil { return nil, err } var list coretools.List var foundAnyTools bool for _, name := range names { if !strings.HasPrefix(name, toolPrefix) || !strings.HasSuffix(name, toolSuffix) { continue } var t coretools.Tools vers := name[len(toolPrefix) : len(name)-len(toolSuffix)] if t.Version, err = version.ParseBinary(vers); err != nil { logger.Debugf("failed to parse version %q: %v", vers, err) continue } foundAnyTools = true // Major version must match specified value. if t.Version.Major != majorVersion { continue } // If specified minor version value supplied, minor version must match. if minorVersion >= 0 && t.Version.Minor != minorVersion { continue } logger.Debugf("found %s", vers) if t.URL, err = stor.URL(name); err != nil { return nil, err } list = append(list, &t) } if len(list) == 0 { if foundAnyTools { return nil, coretools.ErrNoMatches } return nil, ErrNoTools } return list, nil }