github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/cmd/snap/cmd_recovery_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2020 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  
    26  	. "gopkg.in/check.v1"
    27  
    28  	snap "github.com/snapcore/snapd/cmd/snap"
    29  )
    30  
    31  func (s *SnapSuite) TestRecoveryHelp(c *C) {
    32  	msg := `Usage:
    33    snap.test recovery [recovery-OPTIONS]
    34  
    35  The recovery command lists the available recovery systems.
    36  
    37  [recovery command options]
    38        --color=[auto|never|always]
    39        --unicode=[auto|never|always]
    40  `
    41  	s.testSubCommandHelp(c, "recovery", msg)
    42  }
    43  
    44  func (s *SnapSuite) TestRecovery(c *C) {
    45  	n := 0
    46  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    47  		switch n {
    48  		case 0:
    49  			c.Check(r.Method, Equals, "GET")
    50  			c.Check(r.URL.Path, Equals, "/v2/systems")
    51  			c.Check(r.URL.RawQuery, Equals, "")
    52  			fmt.Fprintln(w, `{"type": "sync", "result": {
    53          "systems": [
    54             {
    55                  "current": true,
    56                  "label": "20200101",
    57                  "model": {
    58                      "model": "model-id-1",
    59                      "brand-id": "brand-id-1",
    60                      "display-name": "Wonky Model"
    61                  },
    62                  "brand": {
    63                      "id": "brand-id-1",
    64                      "username": "brand-1",
    65                      "display-name": "Wonky Publishing"
    66                  },
    67                  "actions": [
    68                      {"title": "recover", "mode": "recover"},
    69                      {"title": "reinstall", "mode": "install"}
    70                  ]
    71             },
    72             {
    73                  "label": "20200802",
    74                  "model": {
    75                      "model": "model-id-2",
    76                      "brand-id": "brand-id-1",
    77                      "display-name": "Other Model"
    78                  },
    79                  "brand": {
    80                      "id": "brand-id-2",
    81                      "username": "brand-2",
    82                      "display-name": "Other Publishing"
    83                  },
    84                  "actions": [
    85                      {"title": "recover", "mode": "recover"},
    86                      {"title": "reinstall", "mode": "install"}
    87                  ]
    88             }
    89          ]
    90  }}`)
    91  		default:
    92  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    93  		}
    94  
    95  		n++
    96  	})
    97  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"recovery"})
    98  	c.Assert(err, IsNil)
    99  	c.Assert(rest, DeepEquals, []string{})
   100  	c.Check(s.Stdout(), Equals, `
   101  Label     Brand    Model       Notes
   102  20200101  brand-1  model-id-1  current
   103  20200802  brand-2  model-id-2  -
   104  `[1:])
   105  	c.Check(s.Stderr(), Equals, "")
   106  }
   107  
   108  func (s *SnapSuite) TestNoRecoverySystems(c *C) {
   109  	n := 0
   110  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   111  		switch n {
   112  		case 0:
   113  			c.Check(r.Method, Equals, "GET")
   114  			c.Check(r.URL.Path, Equals, "/v2/systems")
   115  			c.Check(r.URL.RawQuery, Equals, "")
   116  			fmt.Fprintln(w, `{"type": "sync", "result": {}}`)
   117  		default:
   118  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   119  		}
   120  
   121  		n++
   122  	})
   123  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"recovery"})
   124  	c.Assert(err, IsNil)
   125  	c.Assert(rest, DeepEquals, []string{})
   126  	c.Check(s.Stdout(), Equals, "")
   127  	c.Check(s.Stderr(), Equals, "No recovery systems available.\n")
   128  }
   129  
   130  func (s *SnapSuite) TestNoRecoverySystemsError(c *C) {
   131  	n := 0
   132  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   133  		switch n {
   134  		case 0:
   135  			c.Check(r.Method, Equals, "GET")
   136  			c.Check(r.URL.Path, Equals, "/v2/systems")
   137  			c.Check(r.URL.RawQuery, Equals, "")
   138  			fmt.Fprintln(w, `{"type": "error", "result": {"message": "permission denied"}, "status-code": 403}`)
   139  		default:
   140  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   141  		}
   142  
   143  		n++
   144  	})
   145  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"recovery"})
   146  	c.Check(err, ErrorMatches, `cannot list recovery systems: permission denied`)
   147  }