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