github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/core/lxdprofile/name_test.go (about)

     1  // Copyright 2018 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package lxdprofile_test
     5  
     6  import (
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/core/lxdprofile"
    11  	"github.com/juju/testing"
    12  )
    13  
    14  type LXDProfileNameSuite struct {
    15  	testing.IsolationSuite
    16  }
    17  
    18  var _ = gc.Suite(&LXDProfileNameSuite{})
    19  
    20  func (*LXDProfileNameSuite) TestProfileNames(c *gc.C) {
    21  	testCases := []struct {
    22  		input  []string
    23  		output []string
    24  	}{
    25  		{
    26  			input:  []string{},
    27  			output: []string{},
    28  		},
    29  		{
    30  			input:  []string{"default"},
    31  			output: []string{},
    32  		},
    33  		{
    34  			input: []string{
    35  				"default",
    36  				"juju-model",
    37  			},
    38  			output: []string{},
    39  		},
    40  		{
    41  			input: []string{
    42  				lxdprofile.Name("foo", "bar", 1),
    43  			},
    44  			output: []string{
    45  				lxdprofile.Name("foo", "bar", 1),
    46  			},
    47  		},
    48  		{
    49  			input: []string{
    50  				"default",
    51  				lxdprofile.Name("foo", "bar", 1),
    52  				lxdprofile.Name("foo", "bar", 1),
    53  				lxdprofile.Name("aaa", "bbb", 100),
    54  			},
    55  			output: []string{
    56  				lxdprofile.Name("foo", "bar", 1),
    57  				lxdprofile.Name("aaa", "bbb", 100),
    58  			},
    59  		},
    60  		{
    61  			input: []string{
    62  				"default",
    63  				lxdprofile.Name("foo", "bar", 1),
    64  				lxdprofile.Name("foo", "bar", 1),
    65  				"some-other-profile",
    66  				lxdprofile.Name("aaa", "bbb", 100),
    67  			},
    68  			output: []string{
    69  				lxdprofile.Name("foo", "bar", 1),
    70  				lxdprofile.Name("aaa", "bbb", 100),
    71  			},
    72  		},
    73  	}
    74  	for k, tc := range testCases {
    75  		c.Logf("running test %d with input %q", k, tc.input)
    76  		c.Assert(lxdprofile.LXDProfileNames(tc.input), gc.DeepEquals, tc.output)
    77  	}
    78  }
    79  
    80  func (*LXDProfileNameSuite) TestIsValidName(c *gc.C) {
    81  	testCases := []struct {
    82  		input  string
    83  		output bool
    84  	}{
    85  		{
    86  			input:  "",
    87  			output: false,
    88  		},
    89  		{
    90  			input:  "default",
    91  			output: false,
    92  		},
    93  		{
    94  			input:  "juju-model",
    95  			output: false,
    96  		},
    97  		{
    98  			input:  lxdprofile.Name("foo", "bar", 1),
    99  			output: true,
   100  		},
   101  		{
   102  			input:  lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 100),
   103  			output: true,
   104  		},
   105  	}
   106  	for k, tc := range testCases {
   107  		c.Logf("running test %d with input %q", k, tc.input)
   108  		c.Assert(lxdprofile.IsValidName(tc.input), gc.Equals, tc.output)
   109  	}
   110  }
   111  
   112  func (*LXDProfileNameSuite) TestProfileRevision(c *gc.C) {
   113  	testCases := []struct {
   114  		input  string
   115  		output int
   116  		err    string
   117  	}{
   118  		{
   119  			input: "",
   120  			err:   "not a juju profile name: \"\"",
   121  		},
   122  		{
   123  			input: "default",
   124  			err:   "not a juju profile name: \"default\"",
   125  		},
   126  		{
   127  			input: "juju-model",
   128  			err:   "not a juju profile name: \"juju-model\"",
   129  		},
   130  		{
   131  			input:  lxdprofile.Name("foo", "bar", 1),
   132  			output: 1,
   133  		},
   134  		{
   135  			input:  lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 100),
   136  			output: 100,
   137  		},
   138  	}
   139  	for k, tc := range testCases {
   140  		c.Logf("running test %d of %d with input %q", k, len(testCases), tc.input)
   141  		obtained, err := lxdprofile.ProfileRevision(tc.input)
   142  		if tc.err != "" {
   143  			c.Assert(err, gc.ErrorMatches, tc.err)
   144  			continue
   145  		}
   146  		c.Assert(err, jc.ErrorIsNil)
   147  		c.Assert(obtained, gc.Equals, tc.output)
   148  	}
   149  }
   150  
   151  func (*LXDProfileNameSuite) TestProfileReplaceRevision(c *gc.C) {
   152  	testCases := []struct {
   153  		input    string
   154  		inputRev int
   155  		output   string
   156  		err      string
   157  	}{
   158  		{
   159  			input: "",
   160  			err:   "not a juju profile name: \"\"",
   161  		},
   162  		{
   163  			input: "default",
   164  			err:   "not a juju profile name: \"default\"",
   165  		},
   166  		{
   167  			input: "juju-model",
   168  			err:   "not a juju profile name: \"juju-model\"",
   169  		},
   170  		{
   171  			input:    lxdprofile.Name("foo", "bar", 1),
   172  			inputRev: 4,
   173  			output:   lxdprofile.Name("foo", "bar", 4),
   174  		},
   175  		{
   176  			input:    lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 123),
   177  			inputRev: 312,
   178  			output:   lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 312),
   179  		},
   180  	}
   181  	for k, tc := range testCases {
   182  		c.Logf("running test %d of %d with input %q", k, len(testCases), tc.input)
   183  		obtained, err := lxdprofile.ProfileReplaceRevision(tc.input, tc.inputRev)
   184  		if tc.err != "" {
   185  			c.Assert(err, gc.ErrorMatches, tc.err)
   186  			continue
   187  		}
   188  		c.Assert(err, jc.ErrorIsNil)
   189  		c.Assert(obtained, gc.Equals, tc.output)
   190  	}
   191  }
   192  
   193  func (*LXDProfileNameSuite) TestMatchProfileNameByAppName(c *gc.C) {
   194  	testCases := []struct {
   195  		input    []string
   196  		inputApp string
   197  		output   string
   198  		err      string
   199  	}{
   200  		{
   201  			input:    []string{},
   202  			inputApp: "",
   203  			err:      "no application name specified",
   204  		},
   205  		{
   206  			input:    []string{"default"},
   207  			inputApp: "one",
   208  			output:   "",
   209  		},
   210  		{
   211  			input:    []string{"default", "juju-model"},
   212  			inputApp: "one",
   213  			output:   "",
   214  		},
   215  		{
   216  			input: []string{
   217  				"default",
   218  				"juju-model",
   219  				lxdprofile.Name("foo", "bar", 2),
   220  			},
   221  			inputApp: "bar",
   222  			output:   lxdprofile.Name("foo", "bar", 2),
   223  		},
   224  		{
   225  			input: []string{
   226  				"default",
   227  				"juju-model",
   228  				lxdprofile.Name("foo", "nonebar", 2),
   229  				lxdprofile.Name("foo", "bar", 2),
   230  			},
   231  			inputApp: "bar",
   232  			output:   lxdprofile.Name("foo", "bar", 2),
   233  		},
   234  		{
   235  			input: []string{
   236  				"default",
   237  				"juju-model",
   238  				lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 123),
   239  			},
   240  			inputApp: "b312--?123!!bb-x__xx-012-y123yy",
   241  			output:   lxdprofile.Name("aaa-zzz", "b312--?123!!bb-x__xx-012-y123yy", 123),
   242  		},
   243  	}
   244  	for k, tc := range testCases {
   245  		c.Logf("running test %d of %d with input %q", k, len(testCases), tc.input)
   246  		obtained, err := lxdprofile.MatchProfileNameByAppName(tc.input, tc.inputApp)
   247  		if tc.err != "" {
   248  			c.Assert(err, gc.ErrorMatches, tc.err)
   249  			continue
   250  		}
   251  		c.Assert(err, jc.ErrorIsNil)
   252  		c.Assert(obtained, gc.Equals, tc.output)
   253  	}
   254  }