github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/cmd/snap/cmd_reboot_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  	"io/ioutil"
    25  	"net/http"
    26  	"strings"
    27  
    28  	. "gopkg.in/check.v1"
    29  
    30  	snap "github.com/snapcore/snapd/cmd/snap"
    31  )
    32  
    33  func (s *SnapSuite) TestRebootHelp(c *C) {
    34  	msg := `Usage:
    35    snap.test reboot [reboot-OPTIONS] [<label>]
    36  
    37  The reboot command reboots the system into a particular mode of the selected
    38  recovery system.
    39  
    40  When called without a system label and without a mode it will just
    41  trigger a regular reboot.
    42  
    43  When called without a system label but with a mode it will use the
    44  current system to enter the given mode.
    45  
    46  Note that "recover" and "run" modes are only available for the
    47  current system.
    48  
    49  [reboot command options]
    50        --run        Boot into run mode
    51        --install    Boot into install mode
    52        --recover    Boot into recover mode
    53  
    54  [reboot command arguments]
    55    <label>:         The recovery system label
    56  `
    57  	s.testSubCommandHelp(c, "reboot", msg)
    58  }
    59  
    60  func (s *SnapSuite) TestRebootHappy(c *C) {
    61  
    62  	for _, tc := range []struct {
    63  		cmdline          []string
    64  		expectedEndpoint string
    65  		expectedJSON     string
    66  		expectedMsg      string
    67  	}{
    68  		{
    69  			cmdline:          []string{"reboot"},
    70  			expectedEndpoint: "/v2/systems",
    71  			expectedJSON:     `{"action":"reboot","mode":""}`,
    72  			expectedMsg:      `Reboot`,
    73  		},
    74  		{
    75  			cmdline:          []string{"reboot", "--recover"},
    76  			expectedEndpoint: "/v2/systems",
    77  			expectedJSON:     `{"action":"reboot","mode":"recover"}`,
    78  			expectedMsg:      `Reboot into "recover" mode.`,
    79  		},
    80  		{
    81  			cmdline:          []string{"reboot", "20200101"},
    82  			expectedEndpoint: "/v2/systems/20200101",
    83  			expectedJSON:     `{"action":"reboot","mode":""}`,
    84  			expectedMsg:      `Reboot into "20200101".`,
    85  		},
    86  		{
    87  			cmdline:          []string{"reboot", "--recover", "20200101"},
    88  			expectedEndpoint: "/v2/systems/20200101",
    89  			expectedJSON:     `{"action":"reboot","mode":"recover"}`,
    90  			expectedMsg:      `Reboot into "20200101" "recover" mode.`,
    91  		},
    92  	} {
    93  
    94  		n := 0
    95  		s.ResetStdStreams()
    96  
    97  		s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    98  			switch n {
    99  			case 0:
   100  				c.Check(r.Method, Equals, "POST")
   101  				c.Check(r.URL.Path, Equals, tc.expectedEndpoint, Commentf("%v", tc.cmdline))
   102  				c.Check(r.URL.RawQuery, Equals, "")
   103  				body, err := ioutil.ReadAll(r.Body)
   104  				c.Check(err, IsNil)
   105  				c.Check(string(body), Equals, tc.expectedJSON+"\n")
   106  				fmt.Fprintln(w, `{"type": "sync", "result": {}}`)
   107  			default:
   108  				c.Fatalf("expected to get 1 requests, now on %d", n+1)
   109  			}
   110  
   111  			n++
   112  		})
   113  
   114  		// The server side will work out if the request is valid
   115  		rest, err := snap.Parser(snap.Client()).ParseArgs(tc.cmdline)
   116  		c.Assert(err, IsNil)
   117  		c.Assert(rest, DeepEquals, []string{})
   118  		c.Check(s.Stdout(), Equals, tc.expectedMsg+"\n", Commentf("%v", tc.cmdline))
   119  		c.Check(s.Stderr(), Equals, "")
   120  	}
   121  }
   122  
   123  func (s *SnapSuite) TestRebootUnhappy(c *C) {
   124  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   125  		c.Fatalf("server should not be hit in this test")
   126  	})
   127  
   128  	var tc = []struct {
   129  		args   []string
   130  		errStr string
   131  	}{
   132  		{
   133  			args:   []string{"reboot", "--run", "--recover", "20200101"},
   134  			errStr: "Please specify a single mode",
   135  		},
   136  		{
   137  			args:   []string{"reboot", "--unknown-mode", "20200101"},
   138  			errStr: "unknown flag `unknown-mode'",
   139  		},
   140  	}
   141  
   142  	for _, t := range tc {
   143  		_, err := snap.Parser(snap.Client()).ParseArgs(t.args)
   144  		c.Check(err, ErrorMatches, t.errStr, Commentf(strings.Join(t.args, " ")))
   145  	}
   146  }
   147  
   148  func (s *SnapSuite) TestRebootAPIFail(c *C) {
   149  	n := 0
   150  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
   151  		switch n {
   152  		case 0:
   153  			c.Check(r.Method, Equals, "POST")
   154  			c.Check(r.URL.Path, Equals, "/v2/systems/20200101")
   155  			c.Check(r.URL.RawQuery, Equals, "")
   156  			w.WriteHeader(404)
   157  			fmt.Fprintln(w, `{"type": "error", "status-code":404, "result": {"message":"requested system does not exist"}}`)
   158  		default:
   159  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   160  		}
   161  
   162  		n++
   163  	})
   164  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"reboot", "--recover", "20200101"})
   165  	c.Assert(err, ErrorMatches, `cannot request system reboot into "20200101": requested system does not exist`)
   166  	c.Check(s.Stdout(), Equals, "")
   167  	c.Check(s.Stderr(), Equals, "")
   168  }