github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_prefer_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2016 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  	. "github.com/snapcore/snapd/cmd/snap"
    29  )
    30  
    31  func (s *SnapSuite) TestPreferHelp(c *C) {
    32  	msg := `Usage:
    33    snap.test prefer [prefer-OPTIONS] [<snap>]
    34  
    35  The prefer command enables all aliases of the given snap in preference
    36  to conflicting aliases of other snaps whose aliases will be disabled
    37  (or removed, for manual ones).
    38  
    39  [prefer command options]
    40        --no-wait    Do not wait for the operation to finish but just print the
    41                     change id.
    42  `
    43  	s.testSubCommandHelp(c, "prefer", msg)
    44  }
    45  
    46  func (s *SnapSuite) TestPrefer(c *C) {
    47  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    48  		switch r.URL.Path {
    49  		case "/v2/aliases":
    50  			c.Check(r.Method, Equals, "POST")
    51  			c.Check(DecodedRequestBody(c, r), DeepEquals, map[string]interface{}{
    52  				"action": "prefer",
    53  				"snap":   "some-snap",
    54  			})
    55  			w.WriteHeader(202)
    56  			fmt.Fprintln(w, `{"type":"async", "status-code": 202, "change": "zzz"}`)
    57  		case "/v2/changes/zzz":
    58  			c.Check(r.Method, Equals, "GET")
    59  			fmt.Fprintln(w, `{"type":"sync", "result":{"ready": true, "status": "Done", "data": {"aliases-added": [{"alias": "alias1", "snap": "some-snap", "app": "cmd1"}]}}}`)
    60  		default:
    61  			c.Fatalf("unexpected path %q", r.URL.Path)
    62  		}
    63  	})
    64  	rest, err := Parser(Client()).ParseArgs([]string{"prefer", "some-snap"})
    65  	c.Assert(err, IsNil)
    66  	c.Assert(rest, DeepEquals, []string{})
    67  	c.Assert(s.Stdout(), Equals, ""+
    68  		"Added:\n"+
    69  		"  - some-snap.cmd1 as alias1\n",
    70  	)
    71  	c.Assert(s.Stderr(), Equals, "")
    72  }