github.com/mattyw/juju@v0.0.0-20140610034352-732aecd63861/cmd/juju/environment_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package main
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "launchpad.net/gocheck"
    12  
    13  	"github.com/juju/juju/cmd/envcmd"
    14  	"github.com/juju/juju/environs/config"
    15  	jujutesting "github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/provider/dummy"
    17  	_ "github.com/juju/juju/provider/local"
    18  	"github.com/juju/juju/testing"
    19  )
    20  
    21  type GetEnvironmentSuite struct {
    22  	jujutesting.RepoSuite
    23  }
    24  
    25  var _ = gc.Suite(&GetEnvironmentSuite{})
    26  
    27  var singleValueTests = []struct {
    28  	key    string
    29  	output string
    30  	err    string
    31  }{
    32  	{
    33  		key:    "type",
    34  		output: "dummy",
    35  	}, {
    36  		key:    "name",
    37  		output: "dummyenv",
    38  	}, {
    39  		key:    "authorized-keys",
    40  		output: dummy.SampleConfig()["authorized-keys"].(string),
    41  	}, {
    42  		key: "unknown",
    43  		err: `Key "unknown" not found in "dummyenv" environment.`,
    44  	},
    45  }
    46  
    47  func (s *GetEnvironmentSuite) TestSingleValue(c *gc.C) {
    48  	for _, t := range singleValueTests {
    49  		context, err := testing.RunCommand(c, envcmd.Wrap(&GetEnvironmentCommand{}), t.key)
    50  		if t.err != "" {
    51  			c.Assert(err, gc.ErrorMatches, t.err)
    52  		} else {
    53  			output := strings.TrimSpace(testing.Stdout(context))
    54  			c.Assert(err, gc.IsNil)
    55  			c.Assert(output, gc.Equals, t.output)
    56  		}
    57  	}
    58  }
    59  
    60  func (s *GetEnvironmentSuite) TestTooManyArgs(c *gc.C) {
    61  	_, err := testing.RunCommand(c, envcmd.Wrap(&GetEnvironmentCommand{}), "name", "type")
    62  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["type"\]`)
    63  }
    64  
    65  func (s *GetEnvironmentSuite) TestAllValues(c *gc.C) {
    66  	context, _ := testing.RunCommand(c, envcmd.Wrap(&GetEnvironmentCommand{}))
    67  	output := strings.TrimSpace(testing.Stdout(context))
    68  
    69  	// Make sure that all the environment keys are there. The admin
    70  	// secret and CA private key are never pushed into the
    71  	// environment.
    72  	for key := range s.Conn.Environ.Config().AllAttrs() {
    73  		c.Logf("test for key %q", key)
    74  		any := `(.|\n)*`
    75  		pattern := fmt.Sprintf(`(?m)^%s:`, key)
    76  		c.Check(output, gc.Matches, any+pattern+any)
    77  	}
    78  }
    79  
    80  type SetEnvironmentSuite struct {
    81  	jujutesting.RepoSuite
    82  }
    83  
    84  var _ = gc.Suite(&SetEnvironmentSuite{})
    85  
    86  var setEnvInitTests = []struct {
    87  	args     []string
    88  	expected attributes
    89  	err      string
    90  }{
    91  	{
    92  		args: []string{},
    93  		err:  "No key, value pairs specified",
    94  	}, {
    95  		args: []string{"agent-version=1.2.3"},
    96  		err:  `agent-version must be set via upgrade-juju`,
    97  	}, {
    98  		args: []string{"missing"},
    99  		err:  `Missing "=" in arg 1: "missing"`,
   100  	}, {
   101  		args: []string{"key=value"},
   102  		expected: attributes{
   103  			"key": "value",
   104  		},
   105  	}, {
   106  		args: []string{"key=value", "key=other"},
   107  		err:  `Key "key" specified more than once`,
   108  	}, {
   109  		args: []string{"key=value", "other=embedded=equal"},
   110  		expected: attributes{
   111  			"key":   "value",
   112  			"other": "embedded=equal",
   113  		},
   114  	},
   115  }
   116  
   117  func (s *SetEnvironmentSuite) TestInit(c *gc.C) {
   118  	for _, t := range setEnvInitTests {
   119  		command := &SetEnvironmentCommand{}
   120  		testing.TestInit(c, envcmd.Wrap(command), t.args, t.err)
   121  		if t.expected != nil {
   122  			c.Assert(command.values, gc.DeepEquals, t.expected)
   123  		}
   124  	}
   125  }
   126  
   127  func (s *SetEnvironmentSuite) TestChangeDefaultSeries(c *gc.C) {
   128  	// default-series not set
   129  	stateConfig, err := s.State.EnvironConfig()
   130  	c.Assert(err, gc.IsNil)
   131  	series, ok := stateConfig.DefaultSeries()
   132  	c.Assert(ok, gc.Equals, true)
   133  	c.Assert(series, gc.Equals, "precise") // default-series set in RepoSuite.SetUpTest
   134  
   135  	_, err = testing.RunCommand(c, envcmd.Wrap(&SetEnvironmentCommand{}), "default-series=raring")
   136  	c.Assert(err, gc.IsNil)
   137  
   138  	stateConfig, err = s.State.EnvironConfig()
   139  	c.Assert(err, gc.IsNil)
   140  	series, ok = stateConfig.DefaultSeries()
   141  	c.Assert(ok, gc.Equals, true)
   142  	c.Assert(series, gc.Equals, "raring")
   143  	c.Assert(config.PreferredSeries(stateConfig), gc.Equals, "raring")
   144  }
   145  
   146  func (s *SetEnvironmentSuite) TestChangeBooleanAttribute(c *gc.C) {
   147  	_, err := testing.RunCommand(c, envcmd.Wrap(&SetEnvironmentCommand{}), "ssl-hostname-verification=false")
   148  	c.Assert(err, gc.IsNil)
   149  
   150  	stateConfig, err := s.State.EnvironConfig()
   151  	c.Assert(err, gc.IsNil)
   152  	c.Assert(stateConfig.SSLHostnameVerification(), gc.Equals, false)
   153  }
   154  
   155  func (s *SetEnvironmentSuite) TestChangeMultipleValues(c *gc.C) {
   156  	_, err := testing.RunCommand(c, envcmd.Wrap(&SetEnvironmentCommand{}), "default-series=spartan", "broken=nope", "secret=sekrit")
   157  	c.Assert(err, gc.IsNil)
   158  
   159  	stateConfig, err := s.State.EnvironConfig()
   160  	c.Assert(err, gc.IsNil)
   161  	attrs := stateConfig.AllAttrs()
   162  	c.Assert(attrs["default-series"].(string), gc.Equals, "spartan")
   163  	c.Assert(attrs["broken"].(string), gc.Equals, "nope")
   164  	c.Assert(attrs["secret"].(string), gc.Equals, "sekrit")
   165  }
   166  
   167  func (s *SetEnvironmentSuite) TestChangeAsCommandPair(c *gc.C) {
   168  	_, err := testing.RunCommand(c, envcmd.Wrap(&SetEnvironmentCommand{}), "default-series=raring")
   169  	c.Assert(err, gc.IsNil)
   170  
   171  	context, err := testing.RunCommand(c, envcmd.Wrap(&GetEnvironmentCommand{}), "default-series")
   172  	c.Assert(err, gc.IsNil)
   173  	output := strings.TrimSpace(testing.Stdout(context))
   174  
   175  	c.Assert(output, gc.Equals, "raring")
   176  }
   177  
   178  var immutableConfigTests = map[string]string{
   179  	"name":          "foo",
   180  	"type":          "local",
   181  	"firewall-mode": "global",
   182  	"state-port":    "1",
   183  	"api-port":      "666",
   184  }
   185  
   186  func (s *SetEnvironmentSuite) TestImmutableConfigValues(c *gc.C) {
   187  	for name, value := range immutableConfigTests {
   188  		param := fmt.Sprintf("%s=%s", name, value)
   189  		_, err := testing.RunCommand(c, envcmd.Wrap(&SetEnvironmentCommand{}), param)
   190  		errorPattern := fmt.Sprintf("cannot change %s from .* to [\"]?%v[\"]?", name, value)
   191  		c.Assert(err, gc.ErrorMatches, errorPattern)
   192  	}
   193  }
   194  
   195  type UnsetEnvironmentSuite struct {
   196  	jujutesting.RepoSuite
   197  }
   198  
   199  var _ = gc.Suite(&UnsetEnvironmentSuite{})
   200  
   201  var unsetEnvTests = []struct {
   202  	args       []string
   203  	err        string
   204  	expected   attributes
   205  	unexpected []string
   206  }{
   207  	{
   208  		args: []string{},
   209  		err:  "No keys specified",
   210  	}, {
   211  		args:       []string{"xyz", "xyz"},
   212  		unexpected: []string{"xyz"},
   213  	}, {
   214  		args: []string{"type", "xyz"},
   215  		err:  "type: expected string, got nothing",
   216  		expected: attributes{
   217  			"type": "dummy",
   218  			"xyz":  123,
   219  		},
   220  	}, {
   221  		args:       []string{"xyz2", "xyz"},
   222  		unexpected: []string{"xyz"},
   223  	},
   224  }
   225  
   226  func (s *UnsetEnvironmentSuite) initConfig(c *gc.C) {
   227  	err := s.State.UpdateEnvironConfig(map[string]interface{}{
   228  		"xyz": 123,
   229  	}, nil, nil)
   230  	c.Assert(err, gc.IsNil)
   231  }
   232  
   233  func (s *UnsetEnvironmentSuite) TestUnsetEnvironment(c *gc.C) {
   234  	for _, t := range unsetEnvTests {
   235  		c.Logf("testing unset-env %v", t.args)
   236  		s.initConfig(c)
   237  		_, err := testing.RunCommand(c, envcmd.Wrap(&UnsetEnvironmentCommand{}), t.args...)
   238  		if t.err != "" {
   239  			c.Assert(err, gc.ErrorMatches, t.err)
   240  		} else {
   241  			c.Assert(err, gc.IsNil)
   242  		}
   243  		if len(t.expected)+len(t.unexpected) != 0 {
   244  			stateConfig, err := s.State.EnvironConfig()
   245  			c.Assert(err, gc.IsNil)
   246  			for k, v := range t.expected {
   247  				vstate, ok := stateConfig.AllAttrs()[k]
   248  				c.Assert(ok, jc.IsTrue)
   249  				c.Assert(vstate, gc.Equals, v)
   250  			}
   251  			for _, k := range t.unexpected {
   252  				_, ok := stateConfig.AllAttrs()[k]
   253  				c.Assert(ok, jc.IsFalse)
   254  			}
   255  		}
   256  	}
   257  }