github.com/stolowski/snapd@v0.0.0-20210407085831-115137ce5a22/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 status int 46 exists, active, try, wat bool 47 err string 48 } 49 50 req, err := http.NewRequest("GET", "/v2/snaps/foo/file", nil) 51 c.Assert(err, check.IsNil) 52 53 for i, scen := range []scenario{ 54 {exists: true, active: true}, 55 {exists: false, err: "no state entry for key"}, 56 {exists: true, active: false, err: `cannot download file of inactive snap "foo"`}, 57 {exists: true, active: true, try: true, err: `cannot download file for try-mode snap "foo"`}, 58 {exists: true, wat: true, err: `cannot download file for snap "foo": internal error: .*`}, 59 } { 60 var snapst snapstate.SnapState 61 if scen.wat { 62 st.Lock() 63 st.Set("snaps", 42) 64 st.Unlock() 65 } else { 66 if scen.exists { 67 sideInfo := &snap.SideInfo{Revision: snap.R(-1), RealName: "foo"} 68 snapst.Active = scen.active 69 snapst.Current = sideInfo.Revision 70 snapst.Sequence = append(snapst.Sequence, sideInfo) 71 if scen.try { 72 snapst.TryMode = true 73 } 74 } 75 st.Lock() 76 snapstate.Set(st, "foo", &snapst) 77 st.Unlock() 78 } 79 80 rsp := s.req(c, req, nil) 81 if scen.err == "" { 82 c.Check(string(rsp.(daemon.FileResponse)), check.Equals, filepath.Join(dirs.SnapBlobDir, "foo_x1.snap"), check.Commentf("%d", i)) 83 } else { 84 c.Assert(rsp, check.FitsTypeOf, &daemon.Resp{}, check.Commentf("%d", i)) 85 result := rsp.(*daemon.Resp).Result 86 c.Assert(result, check.FitsTypeOf, &daemon.ErrorResult{}, check.Commentf("%d", i)) 87 c.Check(result.(*daemon.ErrorResult).Message, check.Matches, scen.err, check.Commentf("%d", i)) 88 } 89 } 90 }