github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/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  	"io/ioutil"
    25  	"net/http"
    26  	"path/filepath"
    27  	"strings"
    28  	"time"
    29  
    30  	. "gopkg.in/check.v1"
    31  
    32  	"github.com/snapcore/snapd/client"
    33  	main "github.com/snapcore/snapd/cmd/snap"
    34  	"github.com/snapcore/snapd/strutil/quantity"
    35  	"github.com/snapcore/snapd/testutil"
    36  )
    37  
    38  var snapshotsTests = []getCmdArgs{{
    39  	args:  "restore x",
    40  	error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`,
    41  }, {
    42  	args:  "saved --id=x",
    43  	error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`,
    44  }, {
    45  	args:   "saved --id=3",
    46  	stdout: "Set  Snap  Age    Version  Rev   Size    Notes\n3    htop  .*  2        1168      1B  auto\n",
    47  }, {
    48  	args:   "saved",
    49  	stdout: "Set  Snap  Age    Version  Rev   Size    Notes\n1    htop  .*  2        1168      1B  -\n",
    50  }, {
    51  	args:  "forget x",
    52  	error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`,
    53  }, {
    54  	args:  "check-snapshot x",
    55  	error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`,
    56  }, {
    57  	args:   "restore 1",
    58  	stdout: "Restored snapshot #1.\n",
    59  }, {
    60  	args:   "forget 2",
    61  	stdout: "Snapshot #2 forgotten.\n",
    62  }, {
    63  	args:   "forget 2 snap1 snap2",
    64  	stdout: "Snapshot #2 of snaps \"snap1\", \"snap2\" forgotten.\n",
    65  }, {
    66  	args:   "check-snapshot 4",
    67  	stdout: "Snapshot #4 verified successfully.\n",
    68  }, {
    69  	args:   "check-snapshot 4 snap1 snap2",
    70  	stdout: "Snapshot #4 of snaps \"snap1\", \"snap2\" verified successfully.\n",
    71  }, {
    72  	args:  "export-snapshot x snapshot-export.snapshot",
    73  	error: `invalid argument for snapshot set id: expected a non-negative integer argument \(see 'snap help saved'\)`,
    74  }, {
    75  	args:  "export-snapshot 1",
    76  	error: "the required argument `<filename>` was not provided",
    77  }}
    78  
    79  func (s *SnapSuite) TestSnapSnaphotsTest(c *C) {
    80  	s.mockSnapshotsServer(c)
    81  
    82  	restore := main.MockIsStdinTTY(true)
    83  	defer restore()
    84  
    85  	for _, test := range snapshotsTests {
    86  		s.stdout.Truncate(0)
    87  		s.stderr.Truncate(0)
    88  
    89  		c.Logf("Test: %s", test.args)
    90  
    91  		_, err := main.Parser(main.Client()).ParseArgs(strings.Fields(test.args))
    92  		if test.error != "" {
    93  			c.Check(err, ErrorMatches, test.error)
    94  		} else {
    95  			c.Check(err, IsNil)
    96  			c.Check(s.Stderr(), testutil.EqualsWrapped, test.stderr)
    97  			c.Check(s.Stdout(), testutil.MatchesWrapped, test.stdout)
    98  		}
    99  		c.Check("snapshot-export.snapshot", testutil.FileAbsent)
   100  		c.Check("snapshot-export.snapshot.part", testutil.FileAbsent)
   101  	}
   102  }
   103  
   104  func (s *SnapSuite) TestSnapshotExportHappy(c *C) {
   105  	s.mockSnapshotsServer(c)
   106  
   107  	exportedSnapshotPath := filepath.Join(c.MkDir(), "export-snapshot.snapshot")
   108  	_, err := main.Parser(main.Client()).ParseArgs([]string{"export-snapshot", "1", exportedSnapshotPath})
   109  	c.Check(err, IsNil)
   110  	c.Check(s.Stderr(), testutil.EqualsWrapped, "")
   111  	c.Check(s.Stdout(), testutil.MatchesWrapped, `Exported snapshot #1 into ".*/export-snapshot.snapshot"`)
   112  	c.Check(exportedSnapshotPath, testutil.FileEquals, "Hello World!")
   113  	c.Check(exportedSnapshotPath+".part", testutil.FileAbsent)
   114  }
   115  
   116  func (s *SnapSuite) mockSnapshotsServer(c *C) {
   117  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   118  		switch r.URL.Path {
   119  		case "/v2/snapshots":
   120  			if r.Method == "GET" {
   121  				// simulate a 1-month old snapshot
   122  				snapshotTime := time.Now().AddDate(0, -1, 0).Format(time.RFC3339)
   123  				if r.URL.Query().Get("set") == "3" {
   124  					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)
   125  					return
   126  				}
   127  				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)
   128  			}
   129  			if r.Method == "POST" {
   130  				if r.Header.Get("Content-Type") == client.SnapshotExportMediaType {
   131  					fmt.Fprintln(w, `{"type": "sync", "result": {"set-id": 42, "snaps": ["htop"]}}`)
   132  				} else {
   133  
   134  					w.WriteHeader(202)
   135  					fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "9"}`)
   136  				}
   137  			}
   138  		case "/v2/changes/9":
   139  			fmt.Fprintln(w, `{"type": "sync", "result": {"ready": true, "status": "Done", "data": {}}}`)
   140  		case "/v2/snapshots/1/export":
   141  			w.Header().Set("Content-Type", client.SnapshotExportMediaType)
   142  			fmt.Fprint(w, "Hello World!")
   143  		default:
   144  			c.Errorf("unexpected path %q", r.URL.Path)
   145  		}
   146  	})
   147  }
   148  
   149  func (s *SnapSuite) TestSnapshotImportHappy(c *C) {
   150  	// mockSnapshotServer will return set-id 42 and three snaps for all
   151  	// import calls
   152  	s.mockSnapshotsServer(c)
   153  
   154  	// time may be crossing DST change, so the age value should not be
   155  	// hardcoded, otherwise we'll see failures for 2 montsh during the year
   156  	expectedAge := time.Since(time.Now().AddDate(0, -1, 0))
   157  	ageStr := quantity.FormatDuration(expectedAge.Seconds())
   158  
   159  	exportedSnapshotPath := filepath.Join(c.MkDir(), "mocked-snapshot.snapshot")
   160  	ioutil.WriteFile(exportedSnapshotPath, []byte("this is really snapshot zip file data"), 0644)
   161  
   162  	_, err := main.Parser(main.Client()).ParseArgs([]string{"import-snapshot", exportedSnapshotPath})
   163  	c.Check(err, IsNil)
   164  	c.Check(s.Stderr(), testutil.EqualsWrapped, "")
   165  	c.Check(s.Stdout(), testutil.MatchesWrapped, fmt.Sprintf(`Imported snapshot as #42
   166  Set  Snap  Age    Version  Rev   Size    Notes
   167  1    htop  %-6s 2        1168      1B  -
   168  `, ageStr))
   169  }