github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/cmd/snap/cmd_abort_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2018 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) TestAbortLast(c *check.C) {
    32  	n := 0
    33  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    34  		n++
    35  		switch n {
    36  		case 1:
    37  			c.Check(r.Method, check.Equals, "GET")
    38  			c.Check(r.URL.Path, check.Equals, "/v2/changes")
    39  			fmt.Fprintln(w, mockChangesJSON)
    40  		case 2:
    41  			c.Check(r.Method, check.Equals, "POST")
    42  			c.Check(r.URL.Path, check.Equals, "/v2/changes/two")
    43  			c.Check(DecodedRequestBody(c, r), check.DeepEquals, map[string]interface{}{"action": "abort"})
    44  			fmt.Fprintln(w, mockChangeJSON)
    45  		default:
    46  			c.Errorf("expected 2 queries, currently on %d", n)
    47  		}
    48  	})
    49  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"abort", "--last=install"})
    50  	c.Assert(err, check.IsNil)
    51  	c.Assert(rest, check.DeepEquals, []string{})
    52  	c.Check(s.Stdout(), check.Equals, "")
    53  	c.Check(s.Stderr(), check.Equals, "")
    54  
    55  	c.Assert(n, check.Equals, 2)
    56  }
    57  
    58  func (s *SnapSuite) TestAbortLastQuestionmark(c *check.C) {
    59  	n := 0
    60  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    61  		n++
    62  		c.Check(r.Method, check.Equals, "GET")
    63  		c.Assert(r.URL.Path, check.Equals, "/v2/changes")
    64  		switch n {
    65  		case 1, 2:
    66  			fmt.Fprintln(w, `{"type": "sync", "result": []}`)
    67  		case 3, 4:
    68  			fmt.Fprintln(w, mockChangesJSON)
    69  		default:
    70  			c.Errorf("expected 4 calls, now on %d", n)
    71  		}
    72  	})
    73  	for i := 0; i < 2; i++ {
    74  		rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"abort", "--last=foobar?"})
    75  		c.Assert(err, check.IsNil)
    76  		c.Assert(rest, check.DeepEquals, []string{})
    77  		c.Check(s.Stdout(), check.Matches, "")
    78  		c.Check(s.Stderr(), check.Equals, "")
    79  
    80  		_, err = snap.Parser(snap.Client()).ParseArgs([]string{"abort", "--last=foobar"})
    81  		if i == 0 {
    82  			c.Assert(err, check.ErrorMatches, `no changes found`)
    83  		} else {
    84  			c.Assert(err, check.ErrorMatches, `no changes of type "foobar" found`)
    85  		}
    86  	}
    87  
    88  	c.Check(n, check.Equals, 4)
    89  }