github.com/wallyworld/juju@v0.0.0-20161013125918-6cf1bc9d917a/cmd/juju/model/configcommand_test.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  package model_test
     4  
     5  import (
     6  	"github.com/juju/cmd"
     7  	jc "github.com/juju/testing/checkers"
     8  	gc "gopkg.in/check.v1"
     9  
    10  	"github.com/juju/juju/apiserver/common"
    11  	"github.com/juju/juju/cmd/juju/model"
    12  	"github.com/juju/juju/testing"
    13  )
    14  
    15  type ConfigCommandSuite struct {
    16  	fakeEnvSuite
    17  }
    18  
    19  var _ = gc.Suite(&ConfigCommandSuite{})
    20  
    21  func (s *ConfigCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    22  	command := model.NewConfigCommandForTest(s.fake)
    23  	return testing.RunCommand(c, command, args...)
    24  }
    25  
    26  func (s *ConfigCommandSuite) TestInit(c *gc.C) {
    27  	for i, test := range []struct {
    28  		desc       string
    29  		args       []string
    30  		errorMatch string
    31  		nilErr     bool
    32  	}{
    33  		{ // Test set
    34  			desc:       "keys cannot be duplicates",
    35  			args:       []string{"special=extra", "special=other"},
    36  			errorMatch: `key "special" specified more than once`,
    37  		}, {
    38  			desc:       "agent-version cannot be set",
    39  			args:       []string{"agent-version=2.0.0"},
    40  			errorMatch: `agent-version must be set via "upgrade-juju"`,
    41  		}, {
    42  			// Test reset
    43  			desc:       "reset requires arg",
    44  			args:       []string{"--reset"},
    45  			errorMatch: "flag needs an argument: --reset",
    46  		}, {
    47  			desc:       "cannot set and retrieve at the same time",
    48  			args:       []string{"--reset", "something", "weird"},
    49  			errorMatch: "cannot set and retrieve model values simultaneously",
    50  		}, {
    51  			desc:       "agent-version cannot be reset",
    52  			args:       []string{"--reset", "agent-version"},
    53  			errorMatch: `"agent-version" cannot be reset`,
    54  		}, {
    55  			desc:       "set and reset cannot have duplicate keys",
    56  			args:       []string{"--reset", "special", "special=extra"},
    57  			errorMatch: `key "special" cannot be both set and reset in the same command`,
    58  		}, {
    59  			desc:       "reset cannot have k=v pairs",
    60  			args:       []string{"--reset", "a,b,c=d,e"},
    61  			errorMatch: `--reset accepts a comma delimited set of keys "a,b,c", received: "c=d"`,
    62  		}, {
    63  			// Test get
    64  			desc:   "get all succeds",
    65  			args:   nil,
    66  			nilErr: true,
    67  		}, {
    68  			desc:   "get one succeeds",
    69  			args:   []string{"one"},
    70  			nilErr: true,
    71  		}, {
    72  			desc:       "get multiple fails",
    73  			args:       []string{"one", "two"},
    74  			errorMatch: "can only retrieve a single value, or all values",
    75  		}, {
    76  			// test variations
    77  			desc:   "test reset interspersed",
    78  			args:   []string{"--reset", "one", "special=foo", "--reset", "two"},
    79  			nilErr: true,
    80  		},
    81  	} {
    82  		c.Logf("test %d: %s", i, test.desc)
    83  		cmd := model.NewConfigCommandForTest(s.fake)
    84  		err := testing.InitCommand(cmd, test.args)
    85  		if test.nilErr {
    86  			c.Check(err, jc.ErrorIsNil)
    87  			continue
    88  		}
    89  		c.Check(err, gc.ErrorMatches, test.errorMatch)
    90  	}
    91  }
    92  
    93  func (s *ConfigCommandSuite) TestSingleValue(c *gc.C) {
    94  	context, err := s.run(c, "special")
    95  	c.Assert(err, jc.ErrorIsNil)
    96  
    97  	output := testing.Stdout(context)
    98  	c.Assert(output, gc.Equals, "special value\n")
    99  }
   100  
   101  func (s *ConfigCommandSuite) TestGetUnknownValue(c *gc.C) {
   102  	context, err := s.run(c, "unknown")
   103  	c.Assert(err, gc.ErrorMatches, `key "unknown" not found in {<nil> ""} model.`)
   104  
   105  	output := testing.Stdout(context)
   106  	c.Assert(output, gc.Equals, "")
   107  }
   108  
   109  func (s *ConfigCommandSuite) TestSingleValueJSON(c *gc.C) {
   110  	context, err := s.run(c, "--format=json", "special")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	want := "{\"special\":{\"Value\":\"special value\",\"Source\":\"model\"}}\n"
   114  	output := testing.Stdout(context)
   115  	c.Assert(output, gc.Equals, want)
   116  }
   117  
   118  func (s *ConfigCommandSuite) TestSingleValueYAML(c *gc.C) {
   119  	context, err := s.run(c, "--format=yaml", "special")
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	want := "" +
   123  		"special:\n" +
   124  		"  value: special value\n" +
   125  		"  source: model\n"
   126  
   127  	output := testing.Stdout(context)
   128  	c.Assert(output, gc.Equals, want)
   129  }
   130  
   131  func (s *ConfigCommandSuite) TestAllValuesYAML(c *gc.C) {
   132  	context, err := s.run(c, "--format=yaml")
   133  	c.Assert(err, jc.ErrorIsNil)
   134  
   135  	output := testing.Stdout(context)
   136  	expected := "" +
   137  		"running:\n" +
   138  		"  value: true\n" +
   139  		"  source: model\n" +
   140  		"special:\n" +
   141  		"  value: special value\n" +
   142  		"  source: model\n"
   143  	c.Assert(output, gc.Equals, expected)
   144  }
   145  
   146  func (s *ConfigCommandSuite) TestAllValuesJSON(c *gc.C) {
   147  	context, err := s.run(c, "--format=json")
   148  	c.Assert(err, jc.ErrorIsNil)
   149  
   150  	output := testing.Stdout(context)
   151  	expected := `{"running":{"Value":true,"Source":"model"},"special":{"Value":"special value","Source":"model"}}` + "\n"
   152  	c.Assert(output, gc.Equals, expected)
   153  }
   154  
   155  func (s *ConfigCommandSuite) TestAllValuesTabular(c *gc.C) {
   156  	context, err := s.run(c)
   157  	c.Assert(err, jc.ErrorIsNil)
   158  
   159  	output := testing.Stdout(context)
   160  	expected := "" +
   161  		"Attribute  From   Value\n" +
   162  		"running    model  true\n" +
   163  		"special    model  special value\n" +
   164  		"\n"
   165  	c.Assert(output, gc.Equals, expected)
   166  }
   167  
   168  func (s *ConfigCommandSuite) TestPassesValues(c *gc.C) {
   169  	_, err := s.run(c, "special=extra", "unknown=foo")
   170  	c.Assert(err, jc.ErrorIsNil)
   171  	expected := map[string]interface{}{
   172  		"special": "extra",
   173  		"unknown": "foo",
   174  	}
   175  	c.Assert(s.fake.values, jc.DeepEquals, expected)
   176  }
   177  
   178  func (s *ConfigCommandSuite) TestSettingKnownValue(c *gc.C) {
   179  	_, err := s.run(c, "special=extra", "unknown=foo")
   180  	c.Assert(err, jc.ErrorIsNil)
   181  	// Command succeeds, but warning logged.
   182  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   183  	c.Check(c.GetTestLog(), jc.Contains, expected)
   184  }
   185  
   186  func (s *ConfigCommandSuite) TestBlockedError(c *gc.C) {
   187  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   188  	_, err := s.run(c, "special=extra")
   189  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   190  }
   191  
   192  func (s *ConfigCommandSuite) TestResetPassesValues(c *gc.C) {
   193  	_, err := s.run(c, "--reset", "special,running")
   194  	c.Assert(err, jc.ErrorIsNil)
   195  	c.Assert(s.fake.resetKeys, jc.DeepEquals, []string{"special", "running"})
   196  }
   197  
   198  func (s *ConfigCommandSuite) TestResettingUnKnownValue(c *gc.C) {
   199  	_, err := s.run(c, "--reset", "unknown")
   200  	c.Assert(err, jc.ErrorIsNil)
   201  	c.Assert(s.fake.resetKeys, jc.DeepEquals, []string{"unknown"})
   202  	// Command succeeds, but warning logged.
   203  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   204  	c.Check(c.GetTestLog(), jc.Contains, expected)
   205  }
   206  
   207  func (s *ConfigCommandSuite) TestResetBlockedError(c *gc.C) {
   208  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   209  	_, err := s.run(c, "--reset", "special")
   210  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   211  }