github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_known_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  	"net/http/httptest"
    26  	"net/url"
    27  
    28  	"github.com/jessevdk/go-flags"
    29  	"gopkg.in/check.v1"
    30  
    31  	"github.com/snapcore/snapd/store"
    32  
    33  	snap "github.com/snapcore/snapd/cmd/snap"
    34  )
    35  
    36  // acquire example data via:
    37  // curl  -H "accept: application/x.ubuntu.assertion" https://assertions.ubuntu.com/v1/assertions/model/16/canonical/pi2
    38  const mockModelAssertion = `type: model
    39  authority-id: canonical
    40  series: 16
    41  brand-id: canonical
    42  model: pi99
    43  architecture: armhf
    44  gadget: pi99
    45  kernel: pi99-kernel
    46  timestamp: 2016-08-31T00:00:00.0Z
    47  sign-key-sha3-384: 9tydnLa6MTJ-jaQTFUXEwHl1yRx7ZS4K5cyFDhYDcPzhS7uyEkDxdUjg9g08BtNn
    48  
    49  AcLorsomethingthatlooksvaguelylikeasignature==
    50  `
    51  
    52  func (s *SnapSuite) TestKnownRemote(c *check.C) {
    53  	var server *httptest.Server
    54  
    55  	restorer := snap.MockStoreNew(func(cfg *store.Config, stoCtx store.DeviceAndAuthContext) *store.Store {
    56  		if cfg == nil {
    57  			cfg = store.DefaultConfig()
    58  		}
    59  		serverURL, _ := url.Parse(server.URL)
    60  		cfg.AssertionsBaseURL = serverURL
    61  		return store.New(cfg, stoCtx)
    62  	})
    63  	defer restorer()
    64  
    65  	n := 0
    66  	server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    67  		c.Assert(r.URL.Path, check.Matches, ".*/assertions/.*") // sanity check request
    68  		switch n {
    69  		case 0:
    70  			c.Check(r.Method, check.Equals, "GET")
    71  			c.Check(r.URL.Path, check.Equals, "/api/v1/snaps/assertions/model/16/canonical/pi99")
    72  			fmt.Fprintln(w, mockModelAssertion)
    73  		default:
    74  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
    75  		}
    76  
    77  		n++
    78  	}))
    79  
    80  	rest, err := snap.Parser(snap.Client()).ParseArgs([]string{"known", "--remote", "model", "series=16", "brand-id=canonical", "model=pi99"})
    81  	c.Assert(err, check.IsNil)
    82  	c.Assert(rest, check.DeepEquals, []string{})
    83  	c.Check(s.Stdout(), check.Equals, mockModelAssertion)
    84  	c.Check(s.Stderr(), check.Equals, "")
    85  }
    86  
    87  func (s *SnapSuite) TestKnownRemoteMissingPrimaryKey(c *check.C) {
    88  	_, err := snap.Parser(snap.Client()).ParseArgs([]string{"known", "--remote", "model", "series=16", "brand-id=canonical"})
    89  	c.Assert(err, check.ErrorMatches, `cannot query remote assertion: must provide primary key: model`)
    90  }
    91  
    92  func (s *SnapSuite) TestAssertTypeNameCompletion(c *check.C) {
    93  	n := 0
    94  	s.RedirectClientToTestServer(func(w http.ResponseWriter, r *http.Request) {
    95  		switch n {
    96  		case 0:
    97  			c.Check(r.Method, check.Equals, "GET")
    98  			c.Check(r.URL.Path, check.Equals, "/v2/assertions")
    99  			fmt.Fprintln(w, `{"type": "sync", "result": { "types": [ "account", "... more stuff ...", "validation" ] } }`)
   100  		default:
   101  			c.Fatalf("expected to get 1 requests, now on %d", n+1)
   102  		}
   103  
   104  		n++
   105  	})
   106  
   107  	c.Check(snap.AssertTypeNameCompletion("v"), check.DeepEquals, []flags.Completion{{Item: "validation"}})
   108  }