github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/cmd/juju/application/series_selector_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package application
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	"github.com/juju/utils/series"
     9  	gc "gopkg.in/check.v1"
    10  )
    11  
    12  type SeriesSelectorSuite struct{}
    13  
    14  var _ = gc.Suite(&SeriesSelectorSuite{})
    15  
    16  func (s *SeriesSelectorSuite) TestCharmSeries(c *gc.C) {
    17  	deploySeriesTests := []struct {
    18  		title string
    19  
    20  		seriesSelector
    21  
    22  		ltsSeries      string
    23  		expectedSeries string
    24  		message        string
    25  		err            string
    26  	}{{
    27  		title: "use charm default, e.g. juju deploy ubuntu",
    28  		seriesSelector: seriesSelector{
    29  			supportedSeries: []string{"trusty", "precise"},
    30  			conf:            defaultSeries{"wily", true},
    31  		},
    32  		ltsSeries:      "precise",
    33  		expectedSeries: "trusty",
    34  		message:        "with the default charm metadata series %q",
    35  	}, {
    36  		title: "use supported requested, e.g. juju deploy ubuntu --series trusty",
    37  		seriesSelector: seriesSelector{
    38  			seriesFlag:      "trusty",
    39  			supportedSeries: []string{"trusty"},
    40  			conf:            defaultSeries{},
    41  		},
    42  		expectedSeries: "trusty",
    43  		message:        "with the user specified series %q",
    44  	}, {
    45  		title: "unsupported requested, e.g. juju deploy ubuntu --series quantal",
    46  		seriesSelector: seriesSelector{
    47  			seriesFlag:      "quantal",
    48  			supportedSeries: []string{"trusty", "precise"},
    49  			conf:            defaultSeries{},
    50  		},
    51  		err: `series "quantal" not supported by charm, supported series are: trusty,precise`,
    52  	}, {
    53  		title: "charm without series specified or requested, with --force",
    54  		seriesSelector: seriesSelector{
    55  			force: true,
    56  			conf:  defaultSeries{},
    57  		},
    58  		ltsSeries:      "quantal",
    59  		expectedSeries: "quantal",
    60  		message:        "with the latest LTS series %q",
    61  	}, {
    62  		title: "charm without series specified or requested, without --force",
    63  		seriesSelector: seriesSelector{
    64  			conf: defaultSeries{},
    65  		},
    66  		ltsSeries:      "quantal",
    67  		expectedSeries: "quantal",
    68  		err:            `series "quantal" not supported by charm, supported series are: <none defined>`,
    69  	}, {
    70  		title: "charm without series specified, series requested, without --force",
    71  		seriesSelector: seriesSelector{
    72  			seriesFlag: "xenial",
    73  			conf:       defaultSeries{},
    74  		},
    75  		ltsSeries:      "quantal",
    76  		expectedSeries: "quantal",
    77  		err:            `series "xenial" not supported by charm, supported series are: <none defined>`,
    78  	}, {
    79  		title: "charm without series specified, series requested, with --force",
    80  		seriesSelector: seriesSelector{
    81  			seriesFlag: "xenial",
    82  			conf:       defaultSeries{},
    83  			force:      true,
    84  		},
    85  		ltsSeries:      "quantal",
    86  		expectedSeries: "xenial",
    87  		message:        "with the user specified series %q",
    88  	}, {
    89  		title: "no requested series, default to model series if supported",
    90  		seriesSelector: seriesSelector{
    91  			conf:            defaultSeries{"xenial", true},
    92  			supportedSeries: []string{"precise", "xenial"},
    93  		},
    94  		expectedSeries: "xenial",
    95  		message:        "with the configured model default series %q",
    96  	}, {
    97  		title: "juju deploy --force --series=wily for unsupported series",
    98  		seriesSelector: seriesSelector{
    99  			seriesFlag:      "wily",
   100  			supportedSeries: []string{"trusty"},
   101  			force:           true,
   102  			conf:            defaultSeries{},
   103  		},
   104  		expectedSeries: "wily",
   105  		message:        "with the user specified series %q",
   106  	}, {
   107  		title: "juju deploy --series=precise for non-default but supported series",
   108  		seriesSelector: seriesSelector{
   109  			seriesFlag:      "precise",
   110  			supportedSeries: []string{"trusty", "precise"},
   111  			conf:            defaultSeries{},
   112  		},
   113  		expectedSeries: "precise",
   114  		message:        "with the user specified series %q",
   115  	}, {
   116  		title: "juju deploy precise/ubuntu for non-default but supported series",
   117  		seriesSelector: seriesSelector{
   118  			charmURLSeries:  "precise",
   119  			supportedSeries: []string{"trusty", "precise"},
   120  			conf:            defaultSeries{},
   121  		},
   122  		expectedSeries: "precise",
   123  		message:        "with the user specified series %q",
   124  	}, {
   125  		title: "juju deploy precise/ubuntu --series=wily for non-default but supported series",
   126  		seriesSelector: seriesSelector{
   127  			charmURLSeries:  "precise",
   128  			seriesFlag:      "wily",
   129  			supportedSeries: []string{"trusty", "wily"},
   130  			conf:            defaultSeries{},
   131  		},
   132  		expectedSeries: "wily",
   133  		message:        "with the user specified series %q",
   134  	}, {
   135  		title: "juju deploy precise/ubuntu --series=quantal for usupported series",
   136  		seriesSelector: seriesSelector{
   137  			charmURLSeries:  "precise",
   138  			seriesFlag:      "quantal",
   139  			supportedSeries: []string{"trusty", "precise"},
   140  			conf:            defaultSeries{},
   141  		},
   142  		err: `series "quantal" not supported by charm, supported series are: trusty,precise`,
   143  	}}
   144  
   145  	for i, test := range deploySeriesTests {
   146  
   147  		func() {
   148  			c.Logf("test %d [%s]", i, test.title)
   149  			if test.ltsSeries != "" {
   150  				previous := series.SetLatestLtsForTesting(test.ltsSeries)
   151  				defer series.SetLatestLtsForTesting(previous)
   152  			}
   153  			series, err := test.seriesSelector.charmSeries()
   154  			if test.err != "" {
   155  				c.Check(err, gc.ErrorMatches, test.err)
   156  				return
   157  			}
   158  			c.Check(err, jc.ErrorIsNil)
   159  			c.Check(series, gc.Equals, test.expectedSeries)
   160  		}()
   161  	}
   162  }
   163  
   164  type defaultSeries struct {
   165  	series   string
   166  	explicit bool
   167  }
   168  
   169  func (d defaultSeries) DefaultSeries() (string, bool) {
   170  	return d.series, d.explicit
   171  }