github.com/axw/juju@v0.0.0-20161005053422-4bd6544d08d4/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  		args       []string
    29  		errorMatch string
    30  		nilErr     bool
    31  	}{
    32  		{ // Test set
    33  			// 0
    34  			args:       []string{"special=extra", "special=other"},
    35  			errorMatch: `key "special" specified more than once`,
    36  		}, {
    37  			// 1
    38  			args:       []string{"agent-version=2.0.0"},
    39  			errorMatch: `agent-version must be set via "upgrade-juju"`,
    40  		}, {
    41  			// Test reset
    42  			// 2
    43  			args:       []string{"--reset"},
    44  			errorMatch: "no keys specified",
    45  		}, {
    46  			// 3
    47  			args:   []string{"--reset", "something", "weird"},
    48  			nilErr: true,
    49  		}, {
    50  			// 4
    51  			args:       []string{"--reset", "agent-version"},
    52  			errorMatch: "agent-version cannot be reset",
    53  		}, {
    54  			// Test get
    55  			// 5
    56  			args:   nil,
    57  			nilErr: true,
    58  		}, {
    59  			// 6
    60  			args:   []string{"one"},
    61  			nilErr: true,
    62  		}, {
    63  			// 7
    64  			args:       []string{"one", "two"},
    65  			errorMatch: "can only retrieve a single value, or all values",
    66  		},
    67  	} {
    68  		c.Logf("test %d", i)
    69  		cmd := model.NewConfigCommandForTest(s.fake)
    70  		err := testing.InitCommand(cmd, test.args)
    71  		if test.nilErr {
    72  			c.Check(err, jc.ErrorIsNil)
    73  			continue
    74  		}
    75  		c.Check(err, gc.ErrorMatches, test.errorMatch)
    76  	}
    77  }
    78  
    79  func (s *ConfigCommandSuite) TestSingleValue(c *gc.C) {
    80  	context, err := s.run(c, "special")
    81  	c.Assert(err, jc.ErrorIsNil)
    82  
    83  	output := testing.Stdout(context)
    84  	c.Assert(output, gc.Equals, "special value\n")
    85  }
    86  
    87  func (s *ConfigCommandSuite) TestSingleValueJSON(c *gc.C) {
    88  	context, err := s.run(c, "--format=json", "special")
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	want := "{\"special\":{\"Value\":\"special value\",\"Source\":\"model\"}}\n"
    92  	output := testing.Stdout(context)
    93  	c.Assert(output, gc.Equals, want)
    94  }
    95  
    96  func (s *ConfigCommandSuite) TestSingleValueYAML(c *gc.C) {
    97  	context, err := s.run(c, "--format=yaml", "special")
    98  	c.Assert(err, jc.ErrorIsNil)
    99  
   100  	want := "" +
   101  		"special:\n" +
   102  		"  value: special value\n" +
   103  		"  source: model\n"
   104  
   105  	output := testing.Stdout(context)
   106  	c.Assert(output, gc.Equals, want)
   107  }
   108  
   109  func (s *ConfigCommandSuite) TestAllValuesYAML(c *gc.C) {
   110  	context, err := s.run(c, "--format=yaml")
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	output := testing.Stdout(context)
   114  	expected := "" +
   115  		"running:\n" +
   116  		"  value: true\n" +
   117  		"  source: model\n" +
   118  		"special:\n" +
   119  		"  value: special value\n" +
   120  		"  source: model\n"
   121  	c.Assert(output, gc.Equals, expected)
   122  }
   123  
   124  func (s *ConfigCommandSuite) TestAllValuesJSON(c *gc.C) {
   125  	context, err := s.run(c, "--format=json")
   126  	c.Assert(err, jc.ErrorIsNil)
   127  
   128  	output := testing.Stdout(context)
   129  	expected := `{"running":{"Value":true,"Source":"model"},"special":{"Value":"special value","Source":"model"}}` + "\n"
   130  	c.Assert(output, gc.Equals, expected)
   131  }
   132  
   133  func (s *ConfigCommandSuite) TestAllValuesTabular(c *gc.C) {
   134  	context, err := s.run(c)
   135  	c.Assert(err, jc.ErrorIsNil)
   136  
   137  	output := testing.Stdout(context)
   138  	expected := "" +
   139  		"ATTRIBUTE  FROM   VALUE\n" +
   140  		"running    model  true\n" +
   141  		"special    model  special value\n" +
   142  		"\n"
   143  	c.Assert(output, gc.Equals, expected)
   144  }
   145  
   146  func (s *ConfigCommandSuite) TestPassesValues(c *gc.C) {
   147  	_, err := s.run(c, "special=extra", "unknown=foo")
   148  	c.Assert(err, jc.ErrorIsNil)
   149  	expected := map[string]interface{}{
   150  		"special": "extra",
   151  		"unknown": "foo",
   152  	}
   153  	c.Assert(s.fake.values, jc.DeepEquals, expected)
   154  }
   155  
   156  func (s *ConfigCommandSuite) TestSettingKnownValue(c *gc.C) {
   157  	_, err := s.run(c, "special=extra", "unknown=foo")
   158  	c.Assert(err, jc.ErrorIsNil)
   159  	// Command succeeds, but warning logged.
   160  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   161  	c.Check(c.GetTestLog(), jc.Contains, expected)
   162  }
   163  
   164  func (s *ConfigCommandSuite) TestBlockedError(c *gc.C) {
   165  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   166  	_, err := s.run(c, "special=extra")
   167  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   168  }
   169  
   170  func (s *ConfigCommandSuite) TestResetPassesValues(c *gc.C) {
   171  	_, err := s.run(c, "--reset", "special", "running")
   172  	c.Assert(err, jc.ErrorIsNil)
   173  	c.Assert(s.fake.keys, jc.DeepEquals, []string{"special", "running"})
   174  }
   175  
   176  func (s *ConfigCommandSuite) TestResettingKnownValue(c *gc.C) {
   177  	_, err := s.run(c, "--reset", "unknown")
   178  	c.Assert(err, jc.ErrorIsNil)
   179  	c.Assert(s.fake.keys, jc.DeepEquals, []string{"unknown"})
   180  	// Command succeeds, but warning logged.
   181  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   182  	c.Check(c.GetTestLog(), jc.Contains, expected)
   183  }
   184  
   185  func (s *ConfigCommandSuite) TestResetBlockedError(c *gc.C) {
   186  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   187  	_, err := s.run(c, "--reset", "special")
   188  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   189  }