github.com/david-imola/snapd@v0.0.0-20210611180407-2de8ddeece6d/daemon/api_snap_file_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2019 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 daemon_test
    21  
    22  import (
    23  	"net/http"
    24  	"path/filepath"
    25  
    26  	"gopkg.in/check.v1"
    27  
    28  	"github.com/snapcore/snapd/daemon"
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/overlord/snapstate"
    31  	"github.com/snapcore/snapd/snap"
    32  )
    33  
    34  var _ = check.Suite(&snapFileSuite{})
    35  
    36  type snapFileSuite struct {
    37  	apiBaseSuite
    38  }
    39  
    40  func (s *snapFileSuite) TestGetFile(c *check.C) {
    41  	d := s.daemonWithOverlordMock(c)
    42  	st := d.Overlord().State()
    43  
    44  	type scenario struct {
    45  		exists, active, try, wat bool
    46  		err                      string
    47  	}
    48  
    49  	req, err := http.NewRequest("GET", "/v2/snaps/foo/file", nil)
    50  	c.Assert(err, check.IsNil)
    51  
    52  	for i, scen := range []scenario{
    53  		{exists: true, active: true},
    54  		{exists: false, err: "no state entry for key"},
    55  		{exists: true, active: false, err: `cannot download file of inactive snap "foo"`},
    56  		{exists: true, active: true, try: true, err: `cannot download file for try-mode snap "foo"`},
    57  		{exists: true, wat: true, err: `cannot download file for snap "foo": internal error: .*`},
    58  	} {
    59  		var snapst snapstate.SnapState
    60  		if scen.wat {
    61  			st.Lock()
    62  			st.Set("snaps", 42)
    63  			st.Unlock()
    64  		} else {
    65  			if scen.exists {
    66  				sideInfo := &snap.SideInfo{Revision: snap.R(-1), RealName: "foo"}
    67  				snapst.Active = scen.active
    68  				snapst.Current = sideInfo.Revision
    69  				snapst.Sequence = append(snapst.Sequence, sideInfo)
    70  				if scen.try {
    71  					snapst.TryMode = true
    72  				}
    73  			}
    74  			st.Lock()
    75  			snapstate.Set(st, "foo", &snapst)
    76  			st.Unlock()
    77  		}
    78  
    79  		rsp := s.req(c, req, nil)
    80  		if scen.err == "" {
    81  			c.Check(string(rsp.(daemon.FileResponse)), check.Equals, filepath.Join(dirs.SnapBlobDir, "foo_x1.snap"), check.Commentf("%d", i))
    82  		} else {
    83  			rspe, ok := rsp.(*daemon.APIError)
    84  			c.Assert(ok, check.Equals, true, check.Commentf("%d", i))
    85  			c.Check(rspe.Message, check.Matches, scen.err, check.Commentf("%d", i))
    86  		}
    87  	}
    88  }