github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_disconnect_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2016 Canonical Ltd 5 * 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License version 3 as 8 * published by the Free Software Foundation. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program. If not, see <http://www.gnu.org/licenses/>. 17 * 18 */ 19 20 package main_test 21 22 import ( 23 "fmt" 24 "net/http" 25 "os" 26 27 "github.com/jessevdk/go-flags" 28 . "gopkg.in/check.v1" 29 30 . "github.com/snapcore/snapd/cmd/snap" 31 ) 32 33 func (s *SnapSuite) TestDisconnectHelp(c *C) { 34 msg := `Usage: 35 snap.test disconnect [disconnect-OPTIONS] [<snap>:<plug>] [<snap>:<slot>] 36 37 The disconnect command disconnects a plug from a slot. 38 It may be called in the following ways: 39 40 $ snap disconnect <snap>:<plug> <snap>:<slot> 41 42 Disconnects the specific plug from the specific slot. 43 44 $ snap disconnect <snap>:<slot or plug> 45 46 Disconnects everything from the provided plug or slot. 47 The snap name may be omitted for the core snap. 48 49 [disconnect command options] 50 --no-wait Do not wait for the operation to finish but just print 51 the change id. 52 ` 53 s.testSubCommandHelp(c, "disconnect", msg) 54 } 55 56 func (s *SnapSuite) TestDisconnectExplicitEverything(c *C) { 57 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 58 switch r.URL.Path { 59 case "/v2/interfaces": 60 c.Check(r.Method, Equals, "POST") 61 c.Check(DecodedRequestBody(c, r), DeepEquals, map[string]interface{}{ 62 "action": "disconnect", 63 "plugs": []interface{}{ 64 map[string]interface{}{ 65 "snap": "producer", 66 "plug": "plug", 67 }, 68 }, 69 "slots": []interface{}{ 70 map[string]interface{}{ 71 "snap": "consumer", 72 "slot": "slot", 73 }, 74 }, 75 }) 76 w.WriteHeader(202) 77 fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`) 78 case "/v2/changes/zzz": 79 c.Check(r.Method, Equals, "GET") 80 fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done"}}`) 81 default: 82 c.Fatalf("unexpected path %q", r.URL.Path) 83 } 84 }) 85 rest, err := Parser(Client()).ParseArgs([]string{"disconnect", "producer:plug", "consumer:slot"}) 86 c.Assert(err, IsNil) 87 c.Assert(rest, DeepEquals, []string{}) 88 c.Assert(s.Stdout(), Equals, "") 89 c.Assert(s.Stderr(), Equals, "") 90 } 91 92 func (s *SnapSuite) TestDisconnectEverythingFromSpecificSlot(c *C) { 93 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 94 switch r.URL.Path { 95 case "/v2/interfaces": 96 c.Check(r.Method, Equals, "POST") 97 c.Check(DecodedRequestBody(c, r), DeepEquals, map[string]interface{}{ 98 "action": "disconnect", 99 "plugs": []interface{}{ 100 map[string]interface{}{ 101 "snap": "", 102 "plug": "", 103 }, 104 }, 105 "slots": []interface{}{ 106 map[string]interface{}{ 107 "snap": "consumer", 108 "slot": "slot", 109 }, 110 }, 111 }) 112 w.WriteHeader(202) 113 fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`) 114 case "/v2/changes/zzz": 115 c.Check(r.Method, Equals, "GET") 116 fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done"}}`) 117 default: 118 c.Fatalf("unexpected path %q", r.URL.Path) 119 } 120 }) 121 rest, err := Parser(Client()).ParseArgs([]string{"disconnect", "consumer:slot"}) 122 c.Assert(err, IsNil) 123 c.Assert(rest, DeepEquals, []string{}) 124 c.Assert(s.Stdout(), Equals, "") 125 c.Assert(s.Stderr(), Equals, "") 126 } 127 128 func (s *SnapSuite) TestDisconnectEverythingFromSpecificSnapPlugOrSlot(c *C) { 129 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 130 switch r.URL.Path { 131 case "/v2/interfaces": 132 c.Check(r.Method, Equals, "POST") 133 c.Check(DecodedRequestBody(c, r), DeepEquals, map[string]interface{}{ 134 "action": "disconnect", 135 "plugs": []interface{}{ 136 map[string]interface{}{ 137 "snap": "", 138 "plug": "", 139 }, 140 }, 141 "slots": []interface{}{ 142 map[string]interface{}{ 143 "snap": "consumer", 144 "slot": "plug-or-slot", 145 }, 146 }, 147 }) 148 w.WriteHeader(202) 149 fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`) 150 case "/v2/changes/zzz": 151 c.Check(r.Method, Equals, "GET") 152 fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done"}}`) 153 default: 154 c.Fatalf("unexpected path %q", r.URL.Path) 155 } 156 }) 157 rest, err := Parser(Client()).ParseArgs([]string{"disconnect", "consumer:plug-or-slot"}) 158 c.Assert(err, IsNil) 159 c.Assert(rest, DeepEquals, []string{}) 160 c.Assert(s.Stdout(), Equals, "") 161 c.Assert(s.Stderr(), Equals, "") 162 } 163 164 func (s *SnapSuite) TestDisconnectEverythingFromSpecificSnap(c *C) { 165 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 166 c.Fatalf("expected nothing to reach the server") 167 }) 168 rest, err := Parser(Client()).ParseArgs([]string{"disconnect", "consumer"}) 169 c.Assert(err, ErrorMatches, `please provide the plug or slot name to disconnect from snap "consumer"`) 170 c.Assert(rest, DeepEquals, []string{"consumer"}) 171 c.Assert(s.Stdout(), Equals, "") 172 c.Assert(s.Stderr(), Equals, "") 173 } 174 175 func (s *SnapSuite) TestDisconnectCompletion(c *C) { 176 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 177 switch r.URL.Path { 178 case "/v2/connections": 179 c.Assert(r.Method, Equals, "GET") 180 EncodeResponseBody(c, w, map[string]interface{}{ 181 "type": "sync", 182 "result": fortestingConnectionList, 183 }) 184 default: 185 c.Fatalf("unexpected path %q", r.URL.Path) 186 } 187 }) 188 os.Setenv("GO_FLAGS_COMPLETION", "verbose") 189 defer os.Unsetenv("GO_FLAGS_COMPLETION") 190 191 expected := []flags.Completion{} 192 parser := Parser(Client()) 193 parser.CompletionHandler = func(obtained []flags.Completion) { 194 c.Check(obtained, DeepEquals, expected) 195 } 196 197 expected = []flags.Completion{{Item: "canonical-pi2:"}, {Item: "core:"}, {Item: "keyboard-lights:"}} 198 _, err := parser.ParseArgs([]string{"disconnect", ""}) 199 c.Assert(err, IsNil) 200 201 expected = []flags.Completion{{Item: "canonical-pi2:pin-13", Description: "slot"}} 202 _, err = parser.ParseArgs([]string{"disconnect", "ca"}) 203 c.Assert(err, IsNil) 204 205 expected = []flags.Completion{{Item: ":core-support", Description: "slot"}, {Item: ":core-support-plug", Description: "plug"}} 206 _, err = parser.ParseArgs([]string{"disconnect", ":"}) 207 c.Assert(err, IsNil) 208 209 expected = []flags.Completion{{Item: "keyboard-lights:capslock-led", Description: "plug"}} 210 _, err = parser.ParseArgs([]string{"disconnect", "k"}) 211 c.Assert(err, IsNil) 212 213 expected = []flags.Completion{{Item: "canonical-pi2:"}, {Item: "core:"}} 214 _, err = parser.ParseArgs([]string{"disconnect", "keyboard-lights:capslock-led", ""}) 215 c.Assert(err, IsNil) 216 217 expected = []flags.Completion{{Item: "canonical-pi2:pin-13", Description: "slot"}} 218 _, err = parser.ParseArgs([]string{"disconnect", "keyboard-lights:capslock-led", "ca"}) 219 c.Assert(err, IsNil) 220 221 expected = []flags.Completion{{Item: ":core-support", Description: "slot"}} 222 _, err = parser.ParseArgs([]string{"disconnect", ":core-support-plug", ":"}) 223 c.Assert(err, IsNil) 224 225 c.Assert(s.Stdout(), Equals, "") 226 c.Assert(s.Stderr(), Equals, "") 227 }