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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
|
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2022 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 main_test
import (
"fmt"
"net/http"
snap "github.com/snapcore/snapd/cmd/snap"
"gopkg.in/check.v1"
. "gopkg.in/check.v1"
)
type MigrateHomeSuite struct {
BaseSnapSuite
}
var _ = check.Suite(&MigrateHomeSuite{})
// failRequest logs an error message, fails the test and returns a proper error
// to the client. Use this instead of panic() or c.Fatal() because those crash
// the server and leave the client hanging/retrying.
func failRequest(msg string, w http.ResponseWriter, c *C) {
c.Error(msg)
w.WriteHeader(400)
fmt.Fprintf(w, `{"type": "error", "status-code": 400, "result": {"message": %q}}`, msg)
}
func serverWithChange(chgRsp string, c *C) func(w http.ResponseWriter, r *http.Request) {
var n int
return func(w http.ResponseWriter, r *http.Request) {
switch n {
case 0:
c.Check(r.Method, check.Equals, "POST")
c.Check(r.URL.Path, check.Equals, "/v2/debug")
c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]any{
"action": "migrate-home",
"snaps": []any{"foo"},
})
w.WriteHeader(202)
fmt.Fprintln(w, `{"type": "async", "status-code": 202, "result": {}, "change": "12"}`)
case 1:
c.Check(r.Method, check.Equals, "GET")
c.Check(r.URL.Path, check.Equals, "/v2/changes/12")
fmt.Fprint(w, chgRsp)
default:
failRequest(fmt.Sprintf("server expected to get 2 requests, now on %d", n+1), w, c)
}
n++
}
}
func (s *MigrateHomeSuite) TestMigrateHome(c *C) {
rsp := serverWithChange(`{"type": "sync", "result": {"ready": true, "status": "Done", "data": {"snap-names": ["foo"]}}}\n`, c)
s.RedirectClientToTestServer(rsp)
rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home", "foo"})
c.Assert(err, check.IsNil)
c.Assert(rest, check.DeepEquals, []string{})
c.Check(s.Stdout(), check.Equals, "foo's home directory was migrated to ~/Snap\n")
c.Check(s.Stderr(), check.Equals, "")
}
func (s *MigrateHomeSuite) TestMigrateHomeManySnaps(c *C) {
n := 0
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
switch n {
case 0:
c.Check(r.Method, check.Equals, "POST")
c.Check(r.URL.Path, check.Equals, "/v2/debug")
c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]any{
"action": "migrate-home",
"snaps": []any{"foo", "bar"},
})
w.WriteHeader(202)
fmt.Fprintln(w, `{"type": "async", "status-code": 202, "result": {}, "change": "12"}`)
case 1:
c.Check(r.Method, check.Equals, "GET")
c.Check(r.URL.Path, check.Equals, "/v2/changes/12")
fmt.Fprintf(w, `{"type": "sync", "result": {"ready": true, "status": "Done", "data": {"snap-names": ["foo", "bar"]}}}\n`)
default:
failRequest(fmt.Sprintf("server expected to get 2 requests, now on %d", n+1), w, c)
}
n++
})
rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home", "foo", "bar"})
c.Assert(err, check.IsNil)
c.Assert(rest, check.DeepEquals, []string{})
c.Check(s.Stdout(), check.Equals, "\"foo\", \"bar\" migrated their home directories to ~/Snap\n")
c.Check(s.Stderr(), check.Equals, "")
}
func (s *MigrateHomeSuite) TestMigrateHomeNoSnaps(c *C) {
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
failRequest("unexpected request on server", w, c)
})
_, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home"})
c.Assert(err, check.ErrorMatches, "the required argument .* was not provided")
c.Check(s.Stdout(), check.Equals, "")
c.Check(s.Stderr(), check.Equals, "")
}
func (s *MigrateHomeSuite) TestMigrateHomeServerError(c *C) {
s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(500)
fmt.Fprintf(w, `{"type": "error", "status-code": 500, "result": {"message": "boom"}}`)
})
_, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home", "foo"})
c.Assert(err, check.ErrorMatches, "boom")
c.Check(s.Stdout(), check.Equals, "")
c.Check(s.Stderr(), check.Equals, "")
}
func (s *MigrateHomeSuite) TestMigrateHomeBadChangeNoSnaps(c *C) {
// broken change response: missing required "snap-names"
srv := serverWithChange(`{"type": "sync", "result": {"ready": true, "status": "Done", "data": {"snap-names": []}}}\n`, c)
s.RedirectClientToTestServer(srv)
_, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home", "foo"})
c.Assert(err, check.ErrorMatches, `expected "migrate-home" change to have non-empty "snap-names"`)
c.Check(s.Stdout(), check.Equals, "")
c.Check(s.Stderr(), check.Equals, "")
}
func (s *MigrateHomeSuite) TestMigrateHomeBadChangeNoData(c *C) {
// broken change response: missing data
srv := serverWithChange(`{"type": "sync", "result": {"ready": true, "status": "Done"}}\n`, c)
s.RedirectClientToTestServer(srv)
_, err := snap.Parser(snap.Client()).ParseArgs([]string{"debug", "migrate-home", "foo"})
c.Assert(err, check.ErrorMatches, `cannot get "snap-names" from change`)
c.Check(s.Stdout(), check.Equals, "")
c.Check(s.Stderr(), check.Equals, "")
}
|