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