-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathsnap_op_test.go
141 lines (120 loc) · 4.21 KB
/
snap_op_test.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2016 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package client_test
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"path/filepath"
"gopkg.in/check.v1"
"github.com/ubuntu-core/snappy/client"
)
var chanName = "achan"
var ops = []struct {
op func(*client.Client, string, *client.SnapOptions) (string, error)
action string
}{
{(*client.Client).InstallSnap, "install"},
{(*client.Client).RefreshSnap, "refresh"},
{(*client.Client).RemoveSnap, "remove"},
}
func (cs *clientSuite) TestClientOpSnapServerError(c *check.C) {
cs.err = errors.New("fail")
for _, s := range ops {
_, err := s.op(cs.cli, pkgName, nil)
c.Check(err, check.ErrorMatches, `.*fail`, check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpSnapResponseError(c *check.C) {
cs.rsp = `{"type": "error", "status": "potatoes"}`
for _, s := range ops {
_, err := s.op(cs.cli, pkgName, nil)
c.Check(err, check.ErrorMatches, `.*server error: "potatoes"`, check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpSnapBadType(c *check.C) {
cs.rsp = `{"type": "what"}`
for _, s := range ops {
_, err := s.op(cs.cli, pkgName, nil)
c.Check(err, check.ErrorMatches, `.*expected async response for "POST" on "/v2/snaps/`+pkgName+`", got "what"`, check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpSnapNotAccepted(c *check.C) {
cs.rsp = `{
"status-code": 200,
"type": "async"
}`
for _, s := range ops {
_, err := s.op(cs.cli, pkgName, nil)
c.Check(err, check.ErrorMatches, `.*operation not accepted`, check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpSnapNoChange(c *check.C) {
cs.rsp = `{
"status-code": 202,
"type": "async"
}`
for _, s := range ops {
_, err := s.op(cs.cli, pkgName, nil)
c.Assert(err, check.ErrorMatches, `.*response without change reference.*`, check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpSnap(c *check.C) {
cs.rsp = `{
"change": "d728",
"status-code": 202,
"type": "async"
}`
for _, s := range ops {
id, err := s.op(cs.cli, pkgName, &client.SnapOptions{Channel: chanName})
c.Assert(err, check.IsNil)
body, err := ioutil.ReadAll(cs.req.Body)
c.Assert(err, check.IsNil, check.Commentf(s.action))
jsonBody := make(map[string]string)
err = json.Unmarshal(body, &jsonBody)
c.Assert(err, check.IsNil, check.Commentf(s.action))
c.Check(jsonBody["action"], check.Equals, s.action, check.Commentf(s.action))
c.Check(jsonBody["name"], check.Equals, pkgName, check.Commentf(s.action))
c.Check(jsonBody["channel"], check.Equals, chanName, check.Commentf(s.action))
c.Check(jsonBody, check.HasLen, 3, check.Commentf(s.action))
c.Check(cs.req.Method, check.Equals, "POST", check.Commentf(s.action))
c.Check(cs.req.URL.Path, check.Equals, fmt.Sprintf("/v2/snaps/%s", pkgName), check.Commentf(s.action))
c.Check(id, check.Equals, "d728", check.Commentf(s.action))
}
}
func (cs *clientSuite) TestClientOpInstallPath(c *check.C) {
cs.rsp = `{
"change": "66b3",
"status-code": 202,
"type": "async"
}`
bodyData := []byte("snap-data")
snap := filepath.Join(c.MkDir(), "foo.snap")
err := ioutil.WriteFile(snap, bodyData, 0644)
c.Assert(err, check.IsNil)
id, err := cs.cli.InstallSnapPath(snap, nil)
c.Assert(err, check.IsNil)
body, err := ioutil.ReadAll(cs.req.Body)
c.Assert(err, check.IsNil)
c.Assert(string(body), check.Matches, "(?s).*\r\nsnap-data\r\n.*")
c.Assert(string(body), check.Matches, "(?s).*Content-Disposition: form-data; name=\"action\"\r\n\r\ninstall\r\n.*")
c.Check(cs.req.Method, check.Equals, "POST")
c.Check(cs.req.URL.Path, check.Equals, fmt.Sprintf("/v2/snaps"))
c.Check(id, check.Equals, "66b3")
}