34
func assertSet(c *C, args ...string) {
34
func assertSet(c *gc.C, args ...string) {
35
35
rcode, rstdout, rstderr := runCmdLine(c, &SetConstraintsCommand{}, args...)
36
c.Assert(rcode, Equals, 0)
37
c.Assert(rstdout, Equals, "")
38
c.Assert(rstderr, Equals, "")
36
c.Assert(rcode, gc.Equals, 0)
37
c.Assert(rstdout, gc.Equals, "")
38
c.Assert(rstderr, gc.Equals, "")
41
func (s *ConstraintsCommandsSuite) TestSetEnviron(c *C) {
41
func (s *ConstraintsCommandsSuite) TestSetEnviron(c *gc.C) {
42
42
// Set constraints.
43
43
assertSet(c, "mem=4G", "cpu-power=250")
44
44
cons, err := s.State.EnvironConstraints()
46
c.Assert(cons, DeepEquals, constraints.Value{
45
c.Assert(err, gc.IsNil)
46
c.Assert(cons, gc.DeepEquals, constraints.Value{
47
47
CpuPower: uint64p(250),
48
48
Mem: uint64p(4096),
71
71
// Clear constraints.
72
72
assertSet(c, "-s", "svc")
73
73
cons, err = svc.Constraints()
75
c.Assert(cons, DeepEquals, constraints.Value{})
74
c.Assert(err, gc.IsNil)
75
c.Assert(cons, gc.DeepEquals, constraints.Value{})
78
func assertSetError(c *C, code int, stderr string, args ...string) {
78
func assertSetError(c *gc.C, code int, stderr string, args ...string) {
79
79
rcode, rstdout, rstderr := runCmdLine(c, &SetConstraintsCommand{}, args...)
80
c.Assert(rcode, Equals, code)
81
c.Assert(rstdout, Equals, "")
82
c.Assert(rstderr, Matches, "error: "+stderr+"\n")
80
c.Assert(rcode, gc.Equals, code)
81
c.Assert(rstdout, gc.Equals, "")
82
c.Assert(rstderr, gc.Matches, "error: "+stderr+"\n")
85
func (s *ConstraintsCommandsSuite) TestSetErrors(c *C) {
85
func (s *ConstraintsCommandsSuite) TestSetErrors(c *gc.C) {
86
86
assertSetError(c, 2, `invalid service name "badname-0"`, "-s", "badname-0")
87
87
assertSetError(c, 2, `malformed constraint "="`, "=")
88
88
assertSetError(c, 2, `malformed constraint "="`, "-s", "s", "=")
89
89
assertSetError(c, 1, `service "missing" not found`, "-s", "missing")
92
func assertGet(c *C, stdout string, args ...string) {
92
func assertGet(c *gc.C, stdout string, args ...string) {
93
93
rcode, rstdout, rstderr := runCmdLine(c, &GetConstraintsCommand{}, args...)
94
c.Assert(rcode, Equals, 0)
95
c.Assert(rstdout, Equals, stdout)
96
c.Assert(rstderr, Equals, "")
94
c.Assert(rcode, gc.Equals, 0)
95
c.Assert(rstdout, gc.Equals, stdout)
96
c.Assert(rstderr, gc.Equals, "")
99
func (s *ConstraintsCommandsSuite) TestGetEnvironEmpty(c *C) {
99
func (s *ConstraintsCommandsSuite) TestGetEnvironEmpty(c *gc.C) {
103
func (s *ConstraintsCommandsSuite) TestGetEnvironValues(c *C) {
103
func (s *ConstraintsCommandsSuite) TestGetEnvironValues(c *gc.C) {
104
104
cons := constraints.Value{CpuCores: uint64p(64)}
105
105
err := s.State.SetEnvironConstraints(cons)
106
c.Assert(err, gc.IsNil)
107
107
assertGet(c, "cpu-cores=64\n")
110
func (s *ConstraintsCommandsSuite) TestGetServiceEmpty(c *C) {
110
func (s *ConstraintsCommandsSuite) TestGetServiceEmpty(c *gc.C) {
111
111
_, err := s.State.AddService("svc", s.AddTestingCharm(c, "dummy"))
112
c.Assert(err, gc.IsNil)
113
113
assertGet(c, "", "svc")
116
func (s *ConstraintsCommandsSuite) TestGetServiceValues(c *C) {
116
func (s *ConstraintsCommandsSuite) TestGetServiceValues(c *gc.C) {
117
117
svc, err := s.State.AddService("svc", s.AddTestingCharm(c, "dummy"))
118
c.Assert(err, gc.IsNil)
119
119
err = svc.SetConstraints(constraints.Value{CpuCores: uint64p(64)})
120
c.Assert(err, gc.IsNil)
121
121
assertGet(c, "cpu-cores=64\n", "svc")
124
func (s *ConstraintsCommandsSuite) TestGetFormats(c *C) {
124
func (s *ConstraintsCommandsSuite) TestGetFormats(c *gc.C) {
125
125
cons := constraints.Value{CpuCores: uint64p(64), CpuPower: uint64p(0)}
126
126
err := s.State.SetEnvironConstraints(cons)
127
c.Assert(err, gc.IsNil)
128
128
assertGet(c, "cpu-cores=64 cpu-power=\n", "--format", "constraints")
129
129
assertGet(c, "cpu-cores: 64\ncpu-power: 0\n", "--format", "yaml")
130
130
assertGet(c, `{"cpu-cores":64,"cpu-power":0}`+"\n", "--format", "json")
133
func assertGetError(c *C, code int, stderr string, args ...string) {
133
func assertGetError(c *gc.C, code int, stderr string, args ...string) {
134
134
rcode, rstdout, rstderr := runCmdLine(c, &GetConstraintsCommand{}, args...)
135
c.Assert(rcode, Equals, code)
136
c.Assert(rstdout, Equals, "")
137
c.Assert(rstderr, Matches, "error: "+stderr+"\n")
135
c.Assert(rcode, gc.Equals, code)
136
c.Assert(rstdout, gc.Equals, "")
137
c.Assert(rstderr, gc.Matches, "error: "+stderr+"\n")
140
func (s *ConstraintsCommandsSuite) TestGetErrors(c *C) {
140
func (s *ConstraintsCommandsSuite) TestGetErrors(c *gc.C) {
141
141
assertGetError(c, 2, `invalid service name "badname-0"`, "badname-0")
142
142
assertGetError(c, 2, `unrecognized args: \["blether"\]`, "goodname", "blether")
143
143
assertGetError(c, 1, `service "missing" not found`, "missing")