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