github.com/Pankov404/juju@v0.0.0-20150703034450-be266991dceb/cmd/juju/commands/environment_test.go (about)

     1  // Copyright 2013 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package commands
     5  
     6  import (
     7  	"strings"
     8  
     9  	"github.com/juju/cmd"
    10  	jc "github.com/juju/testing/checkers"
    11  	gc "gopkg.in/check.v1"
    12  
    13  	"github.com/juju/juju/constraints"
    14  	"github.com/juju/juju/feature"
    15  	jujutesting "github.com/juju/juju/juju/testing"
    16  	"github.com/juju/juju/state"
    17  	"github.com/juju/juju/testing"
    18  	"github.com/juju/juju/testing/factory"
    19  )
    20  
    21  // EnvironmentSuite tests the connectivity of all the environment subcommands.
    22  // These tests go from the command line, api client, api server, db. The db
    23  // changes are then checked.  Only one test for each command is done here to
    24  // check connectivity.  Exhaustive unit tests are at each layer.
    25  type EnvironmentSuite struct {
    26  	jujutesting.JujuConnSuite
    27  }
    28  
    29  var _ = gc.Suite(&EnvironmentSuite{})
    30  
    31  func (s *EnvironmentSuite) assertEnvValue(c *gc.C, key string, expected interface{}) {
    32  	envConfig, err := s.State.EnvironConfig()
    33  	c.Assert(err, jc.ErrorIsNil)
    34  	value, found := envConfig.AllAttrs()[key]
    35  	c.Assert(found, jc.IsTrue)
    36  	c.Assert(value, gc.Equals, expected)
    37  }
    38  
    39  func (s *EnvironmentSuite) assertEnvValueMissing(c *gc.C, key string) {
    40  	envConfig, err := s.State.EnvironConfig()
    41  	c.Assert(err, jc.ErrorIsNil)
    42  	_, found := envConfig.AllAttrs()[key]
    43  	c.Assert(found, jc.IsFalse)
    44  }
    45  
    46  func (s *EnvironmentSuite) RunEnvironmentCommand(c *gc.C, commands ...string) (*cmd.Context, error) {
    47  	args := []string{"environment"}
    48  	args = append(args, commands...)
    49  	context := testing.Context(c)
    50  	juju := NewJujuCommand(context)
    51  	if err := testing.InitCommand(juju, args); err != nil {
    52  		return context, err
    53  	}
    54  	return context, juju.Run(context)
    55  }
    56  
    57  func (s *EnvironmentSuite) TestGet(c *gc.C) {
    58  	err := s.State.UpdateEnvironConfig(map[string]interface{}{"special": "known"}, nil, nil)
    59  	c.Assert(err, jc.ErrorIsNil)
    60  
    61  	context, err := s.RunEnvironmentCommand(c, "get", "special")
    62  	c.Assert(err, jc.ErrorIsNil)
    63  	c.Assert(testing.Stdout(context), gc.Equals, "known\n")
    64  }
    65  
    66  func (s *EnvironmentSuite) TestSet(c *gc.C) {
    67  	_, err := s.RunEnvironmentCommand(c, "set", "special=known")
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	s.assertEnvValue(c, "special", "known")
    70  }
    71  
    72  func (s *EnvironmentSuite) TestUnset(c *gc.C) {
    73  	err := s.State.UpdateEnvironConfig(map[string]interface{}{"special": "known"}, nil, nil)
    74  	c.Assert(err, jc.ErrorIsNil)
    75  
    76  	_, err = s.RunEnvironmentCommand(c, "unset", "special")
    77  	c.Assert(err, jc.ErrorIsNil)
    78  	s.assertEnvValueMissing(c, "special")
    79  }
    80  
    81  func (s *EnvironmentSuite) TestRetryProvisioning(c *gc.C) {
    82  	s.Factory.MakeMachine(c, &factory.MachineParams{
    83  		Jobs: []state.MachineJob{state.JobManageEnviron},
    84  	})
    85  	ctx, err := s.RunEnvironmentCommand(c, "retry-provisioning", "0")
    86  	c.Assert(err, jc.ErrorIsNil)
    87  
    88  	output := testing.Stderr(ctx)
    89  	stripped := strings.Replace(output, "\n", "", -1)
    90  	c.Check(stripped, gc.Equals, `machine 0 is not in an error state`)
    91  }
    92  
    93  func (s *EnvironmentSuite) TestCreate(c *gc.C) {
    94  	s.SetFeatureFlags(feature.JES)
    95  	// The JujuConnSuite doesn't set up an ssh key in the fake home dir,
    96  	// so fake one on the command line.  The dummy provider also expects
    97  	// a config value for 'state-server'.
    98  	context, err := s.RunEnvironmentCommand(c, "create", "new-env", "authorized-keys=fake-key", "state-server=false")
    99  	c.Check(err, jc.ErrorIsNil)
   100  	c.Check(testing.Stdout(context), gc.Equals, "")
   101  	c.Check(testing.Stderr(context), gc.Equals, "")
   102  }
   103  
   104  func uint64p(val uint64) *uint64 {
   105  	return &val
   106  }
   107  
   108  func (s *EnvironmentSuite) TestGetConstraints(c *gc.C) {
   109  	cons := constraints.Value{CpuPower: uint64p(250)}
   110  	err := s.State.SetEnvironConstraints(cons)
   111  	c.Assert(err, jc.ErrorIsNil)
   112  
   113  	ctx, err := s.RunEnvironmentCommand(c, "get-constraints")
   114  	c.Assert(err, jc.ErrorIsNil)
   115  	c.Check(testing.Stdout(ctx), gc.Equals, "cpu-power=250\n")
   116  }
   117  
   118  func (s *EnvironmentSuite) TestSetConstraints(c *gc.C) {
   119  	_, err := s.RunEnvironmentCommand(c, "set-constraints", "mem=4G", "cpu-power=250")
   120  	c.Assert(err, jc.ErrorIsNil)
   121  
   122  	cons, err := s.State.EnvironConstraints()
   123  	c.Assert(err, jc.ErrorIsNil)
   124  	c.Assert(cons, gc.DeepEquals, constraints.Value{
   125  		CpuPower: uint64p(250),
   126  		Mem:      uint64p(4096),
   127  	})
   128  }