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