github.com/cloudbase/juju-core@v0.0.0-20140504232958-a7271ac7912f/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  	gc "launchpad.net/gocheck"
    11  
    12  	jujutesting "launchpad.net/juju-core/juju/testing"
    13  	"launchpad.net/juju-core/provider/dummy"
    14  	"launchpad.net/juju-core/testing"
    15  )
    16  
    17  type GetEnvironmentSuite struct {
    18  	jujutesting.RepoSuite
    19  }
    20  
    21  var _ = gc.Suite(&GetEnvironmentSuite{})
    22  
    23  var singleValueTests = []struct {
    24  	key    string
    25  	output string
    26  	err    string
    27  }{
    28  	{
    29  		key:    "type",
    30  		output: "dummy",
    31  	}, {
    32  		key:    "name",
    33  		output: "dummyenv",
    34  	}, {
    35  		key:    "authorized-keys",
    36  		output: dummy.SampleConfig()["authorized-keys"].(string),
    37  	}, {
    38  		key: "unknown",
    39  		err: `Key "unknown" not found in "dummyenv" environment.`,
    40  	},
    41  }
    42  
    43  func (s *GetEnvironmentSuite) TestSingleValue(c *gc.C) {
    44  	for _, t := range singleValueTests {
    45  		context, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{t.key})
    46  		if t.err != "" {
    47  			c.Assert(err, gc.ErrorMatches, t.err)
    48  		} else {
    49  			output := strings.TrimSpace(testing.Stdout(context))
    50  			c.Assert(err, gc.IsNil)
    51  			c.Assert(output, gc.Equals, t.output)
    52  		}
    53  	}
    54  }
    55  
    56  func (s *GetEnvironmentSuite) TestTooManyArgs(c *gc.C) {
    57  	_, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{"name", "type"})
    58  	c.Assert(err, gc.ErrorMatches, `unrecognized args: \["type"\]`)
    59  }
    60  
    61  func (s *GetEnvironmentSuite) TestAllValues(c *gc.C) {
    62  	context, _ := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{})
    63  	output := strings.TrimSpace(testing.Stdout(context))
    64  
    65  	// Make sure that all the environment keys are there. The admin
    66  	// secret and CA private key are never pushed into the
    67  	// environment.
    68  	for key := range s.Conn.Environ.Config().AllAttrs() {
    69  		c.Logf("test for key %q", key)
    70  		any := `(.|\n)*`
    71  		pattern := fmt.Sprintf(`(?m)^%s:`, key)
    72  		c.Check(output, gc.Matches, any+pattern+any)
    73  	}
    74  }
    75  
    76  type SetEnvironmentSuite struct {
    77  	jujutesting.RepoSuite
    78  }
    79  
    80  var _ = gc.Suite(&SetEnvironmentSuite{})
    81  
    82  var setEnvInitTests = []struct {
    83  	args     []string
    84  	expected attributes
    85  	err      string
    86  }{
    87  	{
    88  		args: []string{},
    89  		err:  "No key, value pairs specified",
    90  	}, {
    91  		args: []string{"agent-version=1.2.3"},
    92  		err:  `agent-version must be set via upgrade-juju`,
    93  	}, {
    94  		args: []string{"missing"},
    95  		err:  `Missing "=" in arg 1: "missing"`,
    96  	}, {
    97  		args: []string{"key=value"},
    98  		expected: attributes{
    99  			"key": "value",
   100  		},
   101  	}, {
   102  		args: []string{"key=value", "key=other"},
   103  		err:  `Key "key" specified more than once`,
   104  	}, {
   105  		args: []string{"key=value", "other=embedded=equal"},
   106  		expected: attributes{
   107  			"key":   "value",
   108  			"other": "embedded=equal",
   109  		},
   110  	},
   111  }
   112  
   113  func (s *SetEnvironmentSuite) TestInit(c *gc.C) {
   114  	for _, t := range setEnvInitTests {
   115  		command := &SetEnvironmentCommand{}
   116  		testing.TestInit(c, command, t.args, t.err)
   117  		if t.expected != nil {
   118  			c.Assert(command.values, gc.DeepEquals, t.expected)
   119  		}
   120  	}
   121  }
   122  
   123  func (s *SetEnvironmentSuite) TestChangeDefaultSeries(c *gc.C) {
   124  	_, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"})
   125  	c.Assert(err, gc.IsNil)
   126  
   127  	stateConfig, err := s.State.EnvironConfig()
   128  	c.Assert(err, gc.IsNil)
   129  	c.Assert(stateConfig.DefaultSeries(), gc.Equals, "raring")
   130  }
   131  
   132  func (s *SetEnvironmentSuite) TestChangeBooleanAttribute(c *gc.C) {
   133  	_, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"ssl-hostname-verification=false"})
   134  	c.Assert(err, gc.IsNil)
   135  
   136  	stateConfig, err := s.State.EnvironConfig()
   137  	c.Assert(err, gc.IsNil)
   138  	c.Assert(stateConfig.SSLHostnameVerification(), gc.Equals, false)
   139  }
   140  
   141  func (s *SetEnvironmentSuite) TestChangeMultipleValues(c *gc.C) {
   142  	_, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=spartan", "broken=nope", "secret=sekrit"})
   143  	c.Assert(err, gc.IsNil)
   144  
   145  	stateConfig, err := s.State.EnvironConfig()
   146  	c.Assert(err, gc.IsNil)
   147  	attrs := stateConfig.AllAttrs()
   148  	c.Assert(attrs["default-series"].(string), gc.Equals, "spartan")
   149  	c.Assert(attrs["broken"].(string), gc.Equals, "nope")
   150  	c.Assert(attrs["secret"].(string), gc.Equals, "sekrit")
   151  }
   152  
   153  func (s *SetEnvironmentSuite) TestChangeAsCommandPair(c *gc.C) {
   154  	_, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{"default-series=raring"})
   155  	c.Assert(err, gc.IsNil)
   156  
   157  	context, err := testing.RunCommand(c, &GetEnvironmentCommand{}, []string{"default-series"})
   158  	c.Assert(err, gc.IsNil)
   159  	output := strings.TrimSpace(testing.Stdout(context))
   160  
   161  	c.Assert(output, gc.Equals, "raring")
   162  }
   163  
   164  var immutableConfigTests = map[string]string{
   165  	"name":          "foo",
   166  	"type":          "foo",
   167  	"firewall-mode": "global",
   168  	"state-port":    "1",
   169  	"api-port":      "666",
   170  }
   171  
   172  func (s *SetEnvironmentSuite) TestImmutableConfigValues(c *gc.C) {
   173  	for name, value := range immutableConfigTests {
   174  		param := fmt.Sprintf("%s=%s", name, value)
   175  		_, err := testing.RunCommand(c, &SetEnvironmentCommand{}, []string{param})
   176  		errorPattern := fmt.Sprintf("cannot change %s from .* to [\"]?%v[\"]?", name, value)
   177  		c.Assert(err, gc.ErrorMatches, errorPattern)
   178  	}
   179  }