summaryrefslogtreecommitdiff
diff options
authorMichael Vogt <mvo@ubuntu.com>2017-03-09 07:55:37 +0100
committerMichael Vogt <mvo@ubuntu.com>2017-03-09 08:16:41 +0100
commitcf96362a9c0de00481191e94dd92acab591e9ef8 (patch)
tree8c29bd9255bac9af500a52ae67c5ff50de53db9b
parent59b512409bd89e7ad863b0ec0e6195593a81d3c1 (diff)
Remove client.EnsureStateSoon() and replace with client.Debug()hidden-ensure-command
This way we do not give away internal implementation details and do not incentivize normal API users to use the client.Debug() interface. Thanks to Gustavo for his review feedback.
-rw-r--r--client/client.go17
-rw-r--r--client/client_test.go19
-rw-r--r--cmd/snap/cmd_ensure_state_soon.go2
-rw-r--r--daemon/api_test.go7
4 files changed, 35 insertions, 10 deletions
diff --git a/client/client.go b/client/client.go
index 57193d9930..9e695eac20 100644
--- a/client/client.go
+++ b/client/client.go
@@ -518,18 +518,21 @@ func (client *Client) Users() ([]*User, error) {
}
type debugAction struct {
- Action string `json:"action"`
+ Action string `json:"action"`
+ Params interface{} `json:"params,omitempty"`
}
-// EnsureStateSoon is only useful when writing test code, it will trigger
-// the internal "ensure" loop of the state engine to be run as soon as
-// possible.
-func (client *Client) EnsureStateSoon() error {
- body, err := json.Marshal(debugAction{Action: "ensure-state-soon"})
+// Debug is only useful when writing test code, it will trigger
+// an internal action with the given parameters.
+func (client *Client) Debug(action string, params interface{}, result interface{}) error {
+ body, err := json.Marshal(debugAction{
+ Action: action,
+ Params: params,
+ })
if err != nil {
return err
}
- _, err = client.doSync("POST", "/v2/debug", nil, nil, bytes.NewReader(body), nil)
+ _, err = client.doSync("POST", "/v2/debug", nil, nil, bytes.NewReader(body), result)
return err
}
diff --git a/client/client_test.go b/client/client_test.go
index d3d9e8230d..bd6969d7f5 100644
--- a/client/client_test.go
+++ b/client/client_test.go
@@ -441,9 +441,9 @@ func (cs *clientSuite) TestUsers(c *C) {
})
}
-func (cs *clientSuite) TestEnsureStateSoon(c *C) {
+func (cs *clientSuite) TestDebugEnsureStateSoon(c *C) {
cs.rsp = `{"type": "sync", "result":true}`
- err := cs.cli.EnsureStateSoon()
+ err := cs.cli.Debug("ensure-state-soon", nil, nil)
c.Check(err, IsNil)
c.Check(cs.reqs, HasLen, 1)
c.Check(cs.reqs[0].Method, Equals, "POST")
@@ -452,3 +452,18 @@ func (cs *clientSuite) TestEnsureStateSoon(c *C) {
c.Assert(err, IsNil)
c.Check(data, DeepEquals, []byte(`{"action":"ensure-state-soon"}`))
}
+
+func (cs *clientSuite) TestDebugGeneric(c *C) {
+ cs.rsp = `{"type": "sync", "result":["res1","res2"]}`
+
+ var result []string
+ err := cs.cli.Debug("do-something", []string{"param1", "param2"}, &result)
+ c.Check(err, IsNil)
+ c.Check(result, DeepEquals, []string{"res1", "res2"})
+ c.Check(cs.reqs, HasLen, 1)
+ c.Check(cs.reqs[0].Method, Equals, "POST")
+ c.Check(cs.reqs[0].URL.Path, Equals, "/v2/debug")
+ data, err := ioutil.ReadAll(cs.reqs[0].Body)
+ c.Assert(err, IsNil)
+ c.Check(string(data), DeepEquals, `{"action":"do-something","params":["param1","param2"]}`)
+}
diff --git a/cmd/snap/cmd_ensure_state_soon.go b/cmd/snap/cmd_ensure_state_soon.go
index 98aa926ea6..d3077058b2 100644
--- a/cmd/snap/cmd_ensure_state_soon.go
+++ b/cmd/snap/cmd_ensure_state_soon.go
@@ -40,5 +40,5 @@ func (x *cmdEnsureStateSoon) Execute(args []string) error {
return ErrExtraArgs
}
- return Client().EnsureStateSoon()
+ return Client().Debug("ensure-state-soon", nil, nil)
}
diff --git a/daemon/api_test.go b/daemon/api_test.go
index 8f7cfb21ca..9906d3110d 100644
--- a/daemon/api_test.go
+++ b/daemon/api_test.go
@@ -5036,6 +5036,12 @@ func (s *postDebugSuite) TestPostDebugEnsureStateSoon(c *check.C) {
d.overlord.Loop()
defer d.overlord.Stop()
+ soon := 0
+ ensureStateSoon = func(st *state.State) {
+ soon++
+ ensureStateSoonImpl(st)
+ }
+
buf := bytes.NewBufferString(`{"action": "ensure-state-soon"}`)
req, err := http.NewRequest("POST", "/v2/debug", buf)
c.Assert(err, check.IsNil)
@@ -5044,4 +5050,5 @@ func (s *postDebugSuite) TestPostDebugEnsureStateSoon(c *check.C) {
c.Check(rsp.Type, check.Equals, ResponseTypeSync)
c.Check(rsp.Result, check.Equals, true)
+ c.Check(soon, check.Equals, 1)
}