~gz/juju-core/trunk

« back to all changes in this revision

Viewing changes to cmd/juju/constraints_test.go

[r=rogpeppe] all: standardise gocheck imports, A-D

This is an automated change to use "gc" for gocheck
imports throughout. To avoid clogging Rietveld, I've
split it up into three parts - this is the first.

https://codereview.appspot.com/12940044/

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 
6
6
import (
7
7
        "bytes"
8
 
        . "launchpad.net/gocheck"
 
8
        gc "launchpad.net/gocheck"
9
9
        "launchpad.net/juju-core/cmd"
10
10
        "launchpad.net/juju-core/constraints"
11
11
        "launchpad.net/juju-core/juju/testing"
16
16
        testing.JujuConnSuite
17
17
}
18
18
 
19
 
var _ = Suite(&ConstraintsCommandsSuite{})
 
19
var _ = gc.Suite(&ConstraintsCommandsSuite{})
20
20
 
21
 
func runCmdLine(c *C, com cmd.Command, args ...string) (code int, stdout, stderr string) {
 
21
func runCmdLine(c *gc.C, com cmd.Command, args ...string) (code int, stdout, stderr string) {
22
22
        ctx := coretesting.Context(c)
23
23
        code = cmd.Main(com, ctx, args)
24
24
        stdout = ctx.Stdout.(*bytes.Buffer).String()
31
31
        return &val
32
32
}
33
33
 
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, "")
39
39
}
40
40
 
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()
45
 
        c.Assert(err, IsNil)
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),
49
49
        })
51
51
        // Clear constraints.
52
52
        assertSet(c)
53
53
        cons, err = s.State.EnvironConstraints()
54
 
        c.Assert(err, IsNil)
55
 
        c.Assert(cons, DeepEquals, constraints.Value{})
 
54
        c.Assert(err, gc.IsNil)
 
55
        c.Assert(cons, gc.DeepEquals, constraints.Value{})
56
56
}
57
57
 
58
 
func (s *ConstraintsCommandsSuite) TestSetService(c *C) {
 
58
func (s *ConstraintsCommandsSuite) TestSetService(c *gc.C) {
59
59
        svc, err := s.State.AddService("svc", s.AddTestingCharm(c, "dummy"))
60
 
        c.Assert(err, IsNil)
 
60
        c.Assert(err, gc.IsNil)
61
61
 
62
62
        // Set constraints.
63
63
        assertSet(c, "-s", "svc", "mem=4G", "cpu-power=250")
64
64
        cons, err := svc.Constraints()
65
 
        c.Assert(err, IsNil)
66
 
        c.Assert(cons, DeepEquals, constraints.Value{
 
65
        c.Assert(err, gc.IsNil)
 
66
        c.Assert(cons, gc.DeepEquals, constraints.Value{
67
67
                CpuPower: uint64p(250),
68
68
                Mem:      uint64p(4096),
69
69
        })
71
71
        // Clear constraints.
72
72
        assertSet(c, "-s", "svc")
73
73
        cons, err = svc.Constraints()
74
 
        c.Assert(err, IsNil)
75
 
        c.Assert(cons, DeepEquals, constraints.Value{})
 
74
        c.Assert(err, gc.IsNil)
 
75
        c.Assert(cons, gc.DeepEquals, constraints.Value{})
76
76
}
77
77
 
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")
83
83
}
84
84
 
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")
90
90
}
91
91
 
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, "")
97
97
}
98
98
 
99
 
func (s *ConstraintsCommandsSuite) TestGetEnvironEmpty(c *C) {
 
99
func (s *ConstraintsCommandsSuite) TestGetEnvironEmpty(c *gc.C) {
100
100
        assertGet(c, "")
101
101
}
102
102
 
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, IsNil)
 
106
        c.Assert(err, gc.IsNil)
107
107
        assertGet(c, "cpu-cores=64\n")
108
108
}
109
109
 
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, IsNil)
 
112
        c.Assert(err, gc.IsNil)
113
113
        assertGet(c, "", "svc")
114
114
}
115
115
 
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, IsNil)
 
118
        c.Assert(err, gc.IsNil)
119
119
        err = svc.SetConstraints(constraints.Value{CpuCores: uint64p(64)})
120
 
        c.Assert(err, IsNil)
 
120
        c.Assert(err, gc.IsNil)
121
121
        assertGet(c, "cpu-cores=64\n", "svc")
122
122
}
123
123
 
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, IsNil)
 
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")
131
131
}
132
132
 
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")
138
138
}
139
139
 
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")