github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/cmd/juju/constraints_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  	"bytes"
     8  	"strings"
     9  
    10  	"github.com/juju/cmd"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	"github.com/juju/juju/cmd/envcmd"
    15  	"github.com/juju/juju/constraints"
    16  	"github.com/juju/juju/juju/testing"
    17  	coretesting "github.com/juju/juju/testing"
    18  )
    19  
    20  type ConstraintsCommandsSuite struct {
    21  	testing.JujuConnSuite
    22  }
    23  
    24  var _ = gc.Suite(&ConstraintsCommandsSuite{})
    25  
    26  func runCmdLine(c *gc.C, com cmd.Command, args ...string) (code int, stdout, stderr string) {
    27  	ctx := coretesting.Context(c)
    28  	code = cmd.Main(com, ctx, args)
    29  	stdout = ctx.Stdout.(*bytes.Buffer).String()
    30  	stderr = ctx.Stderr.(*bytes.Buffer).String()
    31  	c.Logf("args:   %#v\ncode:   %d\nstdout: %q\nstderr: %q", args, code, stdout, stderr)
    32  	return
    33  }
    34  
    35  func uint64p(val uint64) *uint64 {
    36  	return &val
    37  }
    38  
    39  func assertSet(c *gc.C, args ...string) {
    40  	rcode, rstdout, rstderr := runCmdLine(c, envcmd.Wrap(&SetConstraintsCommand{}), args...)
    41  	c.Assert(rcode, gc.Equals, 0)
    42  	c.Assert(rstdout, gc.Equals, "")
    43  	c.Assert(rstderr, gc.Equals, "")
    44  }
    45  
    46  func assertSetBlocked(c *gc.C, args ...string) {
    47  	rcode, _, _ := runCmdLine(c, envcmd.Wrap(&SetConstraintsCommand{}), args...)
    48  	c.Assert(rcode, gc.Equals, 1)
    49  
    50  	// msg is logged
    51  	stripped := strings.Replace(c.GetTestLog(), "\n", "", -1)
    52  	c.Check(stripped, gc.Matches, ".*To unblock changes.*")
    53  }
    54  
    55  func (s *ConstraintsCommandsSuite) TestSetEnviron(c *gc.C) {
    56  	// Set constraints.
    57  	assertSet(c, "mem=4G", "cpu-power=250")
    58  	cons, err := s.State.EnvironConstraints()
    59  	c.Assert(err, jc.ErrorIsNil)
    60  	c.Assert(cons, gc.DeepEquals, constraints.Value{
    61  		CpuPower: uint64p(250),
    62  		Mem:      uint64p(4096),
    63  	})
    64  
    65  	// Clear constraints.
    66  	assertSet(c)
    67  	cons, err = s.State.EnvironConstraints()
    68  	c.Assert(err, jc.ErrorIsNil)
    69  	c.Assert(&cons, jc.Satisfies, constraints.IsEmpty)
    70  }
    71  
    72  func (s *ConstraintsCommandsSuite) TestBlockSetEnviron(c *gc.C) {
    73  	// Block operation
    74  	s.AssertConfigParameterUpdated(c, "block-all-changes", true)
    75  	// Set constraints.
    76  	assertSetBlocked(c, "mem=4G", "cpu-power=250")
    77  }
    78  
    79  func (s *ConstraintsCommandsSuite) TestSetService(c *gc.C) {
    80  	svc := s.AddTestingService(c, "svc", s.AddTestingCharm(c, "dummy"))
    81  
    82  	// Set constraints.
    83  	assertSet(c, "-s", "svc", "mem=4G", "cpu-power=250")
    84  	cons, err := svc.Constraints()
    85  	c.Assert(err, jc.ErrorIsNil)
    86  	c.Assert(cons, gc.DeepEquals, constraints.Value{
    87  		CpuPower: uint64p(250),
    88  		Mem:      uint64p(4096),
    89  	})
    90  
    91  	// Clear constraints.
    92  	assertSet(c, "-s", "svc")
    93  	cons, err = svc.Constraints()
    94  	c.Assert(err, jc.ErrorIsNil)
    95  	c.Assert(&cons, jc.Satisfies, constraints.IsEmpty)
    96  }
    97  
    98  func (s *ConstraintsCommandsSuite) TestBlockSetService(c *gc.C) {
    99  	s.AddTestingService(c, "svc", s.AddTestingCharm(c, "dummy"))
   100  
   101  	// Block operation
   102  	s.AssertConfigParameterUpdated(c, "block-all-changes", true)
   103  	// Set constraints.
   104  	assertSetBlocked(c, "-s", "svc", "mem=4G", "cpu-power=250")
   105  }
   106  
   107  func assertSetError(c *gc.C, code int, stderr string, args ...string) {
   108  	rcode, rstdout, rstderr := runCmdLine(c, envcmd.Wrap(&SetConstraintsCommand{}), args...)
   109  	c.Assert(rcode, gc.Equals, code)
   110  	c.Assert(rstdout, gc.Equals, "")
   111  	c.Assert(rstderr, gc.Matches, "error: "+stderr+"\n")
   112  }
   113  
   114  func (s *ConstraintsCommandsSuite) TestSetErrors(c *gc.C) {
   115  	assertSetError(c, 2, `invalid service name "badname-0"`, "-s", "badname-0")
   116  	assertSetError(c, 2, `malformed constraint "="`, "=")
   117  	assertSetError(c, 2, `malformed constraint "="`, "-s", "s", "=")
   118  	assertSetError(c, 1, `service "missing" not found`, "-s", "missing")
   119  }
   120  
   121  func assertGet(c *gc.C, stdout string, args ...string) {
   122  	rcode, rstdout, rstderr := runCmdLine(c, envcmd.Wrap(&GetConstraintsCommand{}), args...)
   123  	c.Assert(rcode, gc.Equals, 0)
   124  	c.Assert(rstdout, gc.Equals, stdout)
   125  	c.Assert(rstderr, gc.Equals, "")
   126  }
   127  
   128  func (s *ConstraintsCommandsSuite) TestGetEnvironEmpty(c *gc.C) {
   129  	assertGet(c, "")
   130  }
   131  
   132  func (s *ConstraintsCommandsSuite) TestGetEnvironValues(c *gc.C) {
   133  	cons := constraints.Value{CpuCores: uint64p(64)}
   134  	err := s.State.SetEnvironConstraints(cons)
   135  	c.Assert(err, jc.ErrorIsNil)
   136  	assertGet(c, "cpu-cores=64\n")
   137  }
   138  
   139  func (s *ConstraintsCommandsSuite) TestGetServiceEmpty(c *gc.C) {
   140  	s.AddTestingService(c, "svc", s.AddTestingCharm(c, "dummy"))
   141  	assertGet(c, "", "svc")
   142  }
   143  
   144  func (s *ConstraintsCommandsSuite) TestGetServiceValues(c *gc.C) {
   145  	svc := s.AddTestingService(c, "svc", s.AddTestingCharm(c, "dummy"))
   146  	err := svc.SetConstraints(constraints.Value{CpuCores: uint64p(64)})
   147  	c.Assert(err, jc.ErrorIsNil)
   148  	assertGet(c, "cpu-cores=64\n", "svc")
   149  }
   150  
   151  func (s *ConstraintsCommandsSuite) TestGetFormats(c *gc.C) {
   152  	cons := constraints.Value{CpuCores: uint64p(64), CpuPower: uint64p(0)}
   153  	err := s.State.SetEnvironConstraints(cons)
   154  	c.Assert(err, jc.ErrorIsNil)
   155  	assertGet(c, "cpu-cores=64 cpu-power=\n", "--format", "constraints")
   156  	assertGet(c, "cpu-cores: 64\ncpu-power: 0\n", "--format", "yaml")
   157  	assertGet(c, `{"cpu-cores":64,"cpu-power":0}`+"\n", "--format", "json")
   158  }
   159  
   160  func assertGetError(c *gc.C, code int, stderr string, args ...string) {
   161  	rcode, rstdout, rstderr := runCmdLine(c, envcmd.Wrap(&GetConstraintsCommand{}), args...)
   162  	c.Assert(rcode, gc.Equals, code)
   163  	c.Assert(rstdout, gc.Equals, "")
   164  	c.Assert(rstderr, gc.Matches, "error: "+stderr+"\n")
   165  }
   166  
   167  func (s *ConstraintsCommandsSuite) TestGetErrors(c *gc.C) {
   168  	assertGetError(c, 2, `invalid service name "badname-0"`, "badname-0")
   169  	assertGetError(c, 2, `unrecognized args: \["blether"\]`, "goodname", "blether")
   170  	assertGetError(c, 1, `service "missing" not found`, "missing")
   171  }