github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_snapshot_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2019 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 "strings" 26 "time" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/cmd/snap" 31 "github.com/snapcore/snapd/testutil" 32 ) 33 34 var snapshotsTests = []getCmdArgs{{ 35 args: "restore x", 36 error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`, 37 }, { 38 args: "saved --id=x", 39 error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`, 40 }, { 41 args: "saved --id=3", 42 stdout: "Set Snap Age Version Rev Size Notes\n3 htop .* 2 1168 1B auto\n", 43 }, { 44 args: "saved", 45 stdout: "Set Snap Age Version Rev Size Notes\n1 htop .* 2 1168 1B -\n", 46 }, { 47 args: "forget x", 48 error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`, 49 }, { 50 args: "check-snapshot x", 51 error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`, 52 }, { 53 args: "restore 1", 54 stdout: "Restored snapshot #1.\n", 55 }, { 56 args: "forget 2", 57 stdout: "Snapshot #2 forgotten.\n", 58 }, { 59 args: "forget 2 snap1 snap2", 60 stdout: "Snapshot #2 of snaps \"snap1\", \"snap2\" forgotten.\n", 61 }, { 62 args: "check-snapshot 4", 63 stdout: "Snapshot #4 verified successfully.\n", 64 }, { 65 args: "check-snapshot 4 snap1 snap2", 66 stdout: "Snapshot #4 of snaps \"snap1\", \"snap2\" verified successfully.\n", 67 }} 68 69 func (s *SnapSuite) TestSnapSnaphotsTest(c *C) { 70 s.mockSnapshotsServer(c) 71 72 restore := main.MockIsStdinTTY(true) 73 defer restore() 74 75 for _, test := range snapshotsTests { 76 s.stdout.Truncate(0) 77 s.stderr.Truncate(0) 78 79 c.Logf("Test: %s", test.args) 80 81 _, err := main.Parser(main.Client()).ParseArgs(strings.Fields(test.args)) 82 if test.error != "" { 83 c.Check(err, ErrorMatches, test.error) 84 } else { 85 c.Check(err, IsNil) 86 c.Check(s.Stderr(), testutil.EqualsWrapped, test.stderr) 87 c.Check(s.Stdout(), testutil.MatchesWrapped, test.stdout) 88 } 89 } 90 } 91 92 func (s *SnapSuite) mockSnapshotsServer(c *C) { 93 s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) { 94 switch r.URL.Path { 95 case "/v2/snapshots": 96 if r.Method == "GET" { 97 // simulate a 1-month old snapshot 98 snapshotTime := time.Now().AddDate(0, -1, 0).Format(time.RFC3339) 99 if r.URL.Query().Get("set") == "3" { 100 fmt.Fprintf(w, `{"type":"sync","status-code":200,"status":"OK","result":[{"id":3,"snapshots":[{"set":3,"time":%q,"snap":"htop","revision":"1168","snap-id":"Z","auto":true,"epoch":{"read":[0],"write":[0]},"summary":"","version":"2","sha3-384":{"archive.tgz":""},"size":1}]}]}`, snapshotTime) 101 return 102 } 103 fmt.Fprintf(w, `{"type":"sync","status-code":200,"status":"OK","result":[{"id":1,"snapshots":[{"set":1,"time":%q,"snap":"htop","revision":"1168","snap-id":"Z","epoch":{"read":[0],"write":[0]},"summary":"","version":"2","sha3-384":{"archive.tgz":""},"size":1}]}]}`, snapshotTime) 104 } else { 105 w.WriteHeader(202) 106 fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "9"}`) 107 } 108 case "/v2/changes/9": 109 fmt.Fprintln(w, `{"type": "sync", "result": {"ready": true, "status": "Done", "data": {}}}`) 110 default: 111 c.Errorf("unexpected path %q", r.URL.Path) 112 } 113 }) 114 }