github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/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  	"io/ioutil"
     7  	"path/filepath"
     8  
     9  	"github.com/juju/cmd"
    10  	"github.com/juju/cmd/cmdtesting"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/apiserver/common"
    15  	"github.com/juju/juju/cmd/juju/model"
    16  	"github.com/juju/juju/testing"
    17  )
    18  
    19  type ConfigCommandSuite struct {
    20  	fakeEnvSuite
    21  }
    22  
    23  var _ = gc.Suite(&ConfigCommandSuite{})
    24  
    25  func (s *ConfigCommandSuite) run(c *gc.C, args ...string) (*cmd.Context, error) {
    26  	command := model.NewConfigCommandForTest(s.fake)
    27  	return cmdtesting.RunCommand(c, command, args...)
    28  }
    29  
    30  func (s *ConfigCommandSuite) TestInit(c *gc.C) {
    31  	for i, test := range []struct {
    32  		desc       string
    33  		args       []string
    34  		errorMatch string
    35  		nilErr     bool
    36  	}{
    37  		{
    38  			// Test reset
    39  			desc:       "reset requires arg",
    40  			args:       []string{"--reset"},
    41  			errorMatch: "flag needs an argument: --reset",
    42  		}, {
    43  			desc:       "cannot set and retrieve at the same time",
    44  			args:       []string{"--reset", "something", "weird"},
    45  			errorMatch: "cannot set and retrieve model values simultaneously",
    46  		}, {
    47  			desc:       "agent-version cannot be reset",
    48  			args:       []string{"--reset", "agent-version"},
    49  			errorMatch: `"agent-version" cannot be reset`,
    50  		}, {
    51  			desc:       "reset cannot have k=v pairs",
    52  			args:       []string{"--reset", "a,b,c=d,e"},
    53  			errorMatch: `--reset accepts a comma delimited set of keys "a,b,c", received: "c=d"`,
    54  		}, {
    55  			// Test get
    56  			desc:   "get all succeeds",
    57  			args:   nil,
    58  			nilErr: true,
    59  		}, {
    60  			desc:   "get one succeeds",
    61  			args:   []string{"one"},
    62  			nilErr: true,
    63  		}, {
    64  			desc:       "get multiple fails",
    65  			args:       []string{"one", "two"},
    66  			errorMatch: "can only retrieve a single value, or all values",
    67  		}, {
    68  			// test variations
    69  			desc:   "test reset interspersed",
    70  			args:   []string{"--reset", "one", "special=foo", "--reset", "two"},
    71  			nilErr: true,
    72  		},
    73  	} {
    74  		c.Logf("test %d: %s", i, test.desc)
    75  		cmd := model.NewConfigCommandForTest(s.fake)
    76  		err := cmdtesting.InitCommand(cmd, test.args)
    77  		if test.nilErr {
    78  			c.Check(err, jc.ErrorIsNil)
    79  			continue
    80  		}
    81  		c.Check(err, gc.ErrorMatches, test.errorMatch)
    82  	}
    83  }
    84  
    85  func (s *ConfigCommandSuite) TestSingleValue(c *gc.C) {
    86  	s.fake.values["special"] = "multi\nline"
    87  
    88  	context, err := s.run(c, "special")
    89  	c.Assert(err, jc.ErrorIsNil)
    90  
    91  	output := cmdtesting.Stdout(context)
    92  	c.Assert(output, gc.Equals, "multi\nline\n")
    93  }
    94  
    95  func (s *ConfigCommandSuite) TestSingleValueOutputFile(c *gc.C) {
    96  	s.fake.values["special"] = "multi\nline"
    97  
    98  	outpath := filepath.Join(c.MkDir(), "out")
    99  	_, err := s.run(c, "--output", outpath, "special")
   100  	c.Assert(err, jc.ErrorIsNil)
   101  
   102  	output, err := ioutil.ReadFile(outpath)
   103  	c.Assert(err, jc.ErrorIsNil)
   104  	c.Assert(string(output), gc.Equals, "multi\nline\n")
   105  }
   106  
   107  func (s *ConfigCommandSuite) TestGetUnknownValue(c *gc.C) {
   108  	context, err := s.run(c, "unknown")
   109  	c.Assert(err, gc.ErrorMatches, `key "unknown" not found in {<nil> ""} model.`)
   110  
   111  	output := cmdtesting.Stdout(context)
   112  	c.Assert(output, gc.Equals, "")
   113  }
   114  
   115  func (s *ConfigCommandSuite) TestSingleValueJSON(c *gc.C) {
   116  	context, err := s.run(c, "--format=json", "special")
   117  	c.Assert(err, jc.ErrorIsNil)
   118  
   119  	want := "{\"special\":{\"Value\":\"special value\",\"Source\":\"model\"}}\n"
   120  	output := cmdtesting.Stdout(context)
   121  	c.Assert(output, gc.Equals, want)
   122  }
   123  
   124  func (s *ConfigCommandSuite) TestSingleValueYAML(c *gc.C) {
   125  	context, err := s.run(c, "--format=yaml", "special")
   126  	c.Assert(err, jc.ErrorIsNil)
   127  
   128  	want := "" +
   129  		"special:\n" +
   130  		"  value: special value\n" +
   131  		"  source: model\n"
   132  
   133  	output := cmdtesting.Stdout(context)
   134  	c.Assert(output, gc.Equals, want)
   135  }
   136  
   137  func (s *ConfigCommandSuite) TestAllValuesYAML(c *gc.C) {
   138  	context, err := s.run(c, "--format=yaml")
   139  	c.Assert(err, jc.ErrorIsNil)
   140  
   141  	output := cmdtesting.Stdout(context)
   142  	expected := "" +
   143  		"running:\n" +
   144  		"  value: true\n" +
   145  		"  source: model\n" +
   146  		"special:\n" +
   147  		"  value: special value\n" +
   148  		"  source: model\n"
   149  	c.Assert(output, gc.Equals, expected)
   150  }
   151  
   152  func (s *ConfigCommandSuite) TestAllValuesJSON(c *gc.C) {
   153  	context, err := s.run(c, "--format=json")
   154  	c.Assert(err, jc.ErrorIsNil)
   155  
   156  	output := cmdtesting.Stdout(context)
   157  	expected := `{"running":{"Value":true,"Source":"model"},"special":{"Value":"special value","Source":"model"}}` + "\n"
   158  	c.Assert(output, gc.Equals, expected)
   159  }
   160  
   161  func (s *ConfigCommandSuite) TestAllValuesTabular(c *gc.C) {
   162  	context, err := s.run(c)
   163  	c.Assert(err, jc.ErrorIsNil)
   164  
   165  	output := cmdtesting.Stdout(context)
   166  	expected := "" +
   167  		"Attribute  From   Value\n" +
   168  		"running    model  true\n" +
   169  		"special    model  special value\n" +
   170  		"\n"
   171  	c.Assert(output, gc.Equals, expected)
   172  }
   173  
   174  func (s *ConfigCommandSuite) TestSetAgentVersion(c *gc.C) {
   175  	_, err := s.run(c, "agent-version=2.0.0")
   176  	c.Assert(err, gc.ErrorMatches, `"agent-version"" must be set via "upgrade-model"`)
   177  }
   178  
   179  func (s *ConfigCommandSuite) TestSetAndReset(c *gc.C) {
   180  	_, err := s.run(c, "--reset", "special", "special=bar")
   181  	c.Assert(err, gc.ErrorMatches, `key "special" cannot be both set and reset in the same command`)
   182  }
   183  
   184  func (s *ConfigCommandSuite) TestSetFromFile(c *gc.C) {
   185  	tmpdir := c.MkDir()
   186  	configFile := filepath.Join(tmpdir, "config.yaml")
   187  	err := ioutil.WriteFile(configFile, []byte("special: extra\n"), 0644)
   188  	c.Assert(err, jc.ErrorIsNil)
   189  
   190  	_, err = s.run(c, configFile)
   191  	c.Assert(err, jc.ErrorIsNil)
   192  	expected := map[string]interface{}{
   193  		"special": "extra",
   194  	}
   195  	c.Assert(s.fake.values, jc.DeepEquals, expected)
   196  }
   197  
   198  func (s *ConfigCommandSuite) TestSetFromFileCombined(c *gc.C) {
   199  	tmpdir := c.MkDir()
   200  	configFile := filepath.Join(tmpdir, "config.yaml")
   201  	err := ioutil.WriteFile(configFile, []byte("special: extra\n"), 0644)
   202  	c.Assert(err, jc.ErrorIsNil)
   203  
   204  	_, err = s.run(c, configFile, "unknown=foo")
   205  	c.Assert(err, jc.ErrorIsNil)
   206  	expected := map[string]interface{}{
   207  		"special": "extra",
   208  		"unknown": "foo",
   209  	}
   210  	c.Assert(s.fake.values, jc.DeepEquals, expected)
   211  }
   212  
   213  func (s *ConfigCommandSuite) TestPassesValues(c *gc.C) {
   214  	_, err := s.run(c, "special=extra", "unknown=foo")
   215  	c.Assert(err, jc.ErrorIsNil)
   216  	expected := map[string]interface{}{
   217  		"special": "extra",
   218  		"unknown": "foo",
   219  	}
   220  	c.Assert(s.fake.values, jc.DeepEquals, expected)
   221  }
   222  
   223  func (s *ConfigCommandSuite) TestPassesCloudInitUserDataLong(c *gc.C) {
   224  	modelCfg, err := s.fake.ModelGet()
   225  	modelCfg["cloudinit-userdata"] = "test data"
   226  	err = s.fake.ModelSet(modelCfg)
   227  	c.Assert(err, jc.ErrorIsNil)
   228  
   229  	context, err := s.run(c, "cloudinit-userdata")
   230  	c.Assert(err, jc.ErrorIsNil)
   231  	output := cmdtesting.Stdout(context)
   232  	c.Assert(output, gc.Equals, "test data\n")
   233  
   234  	context2, err := s.run(c)
   235  	c.Assert(err, jc.ErrorIsNil)
   236  	output2 := cmdtesting.Stdout(context2)
   237  	expected2 := "" +
   238  		"Attribute           From   Value\n" +
   239  		"cloudinit-userdata  model  <value set, see juju model-config cloudinit-userdata>\n" +
   240  		"running             model  true\n" +
   241  		"special             model  special value\n" +
   242  		"\n"
   243  	c.Assert(output2, gc.Equals, expected2)
   244  }
   245  
   246  func (s *ConfigCommandSuite) TestPassesCloudInitUserDataShort(c *gc.C) {
   247  	modelCfg, err := s.fake.ModelGet()
   248  	modelCfg["cloudinit-userdata"] = ""
   249  	err = s.fake.ModelSet(modelCfg)
   250  	c.Assert(err, jc.ErrorIsNil)
   251  
   252  	context, err := s.run(c)
   253  	c.Assert(err, jc.ErrorIsNil)
   254  	output := cmdtesting.Stdout(context)
   255  	expected := "" +
   256  		"Attribute           From   Value\n" +
   257  		"cloudinit-userdata  model  \"\"\n" +
   258  		"running             model  true\n" +
   259  		"special             model  special value\n" +
   260  		"\n"
   261  	c.Assert(output, gc.Equals, expected)
   262  }
   263  
   264  func (s *ConfigCommandSuite) TestSettingUnknownValue(c *gc.C) {
   265  	_, err := s.run(c, "special=extra", "unknown=foo")
   266  	c.Assert(err, jc.ErrorIsNil)
   267  	// Command succeeds, but warning logged.
   268  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   269  	c.Check(c.GetTestLog(), jc.Contains, expected)
   270  }
   271  
   272  func (s *ConfigCommandSuite) TestBlockedError(c *gc.C) {
   273  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   274  	_, err := s.run(c, "special=extra")
   275  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   276  }
   277  
   278  func (s *ConfigCommandSuite) TestResetPassesValues(c *gc.C) {
   279  	_, err := s.run(c, "--reset", "special,running")
   280  	c.Assert(err, jc.ErrorIsNil)
   281  	c.Assert(s.fake.resetKeys, jc.DeepEquals, []string{"special", "running"})
   282  }
   283  
   284  func (s *ConfigCommandSuite) TestResettingUnKnownValue(c *gc.C) {
   285  	_, err := s.run(c, "--reset", "unknown")
   286  	c.Assert(err, jc.ErrorIsNil)
   287  	c.Assert(s.fake.resetKeys, jc.DeepEquals, []string{"unknown"})
   288  	// Command succeeds, but warning logged.
   289  	expected := `key "unknown" is not defined in the current model configuration: possible misspelling`
   290  	c.Check(c.GetTestLog(), jc.Contains, expected)
   291  }
   292  
   293  func (s *ConfigCommandSuite) TestResetBlockedError(c *gc.C) {
   294  	s.fake.err = common.OperationBlockedError("TestBlockedError")
   295  	_, err := s.run(c, "--reset", "special")
   296  	testing.AssertOperationWasBlocked(c, err, ".*TestBlockedError.*")
   297  }