github.com/bugraaydogar/snapd@v0.0.0-20210315170335-8c70bb858939/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" 31 "github.com/snapcore/snapd/overlord/snapstate" 32 "github.com/snapcore/snapd/snap" 33 ) 34 35 var _ = check.Suite(&snapFileSuite{}) 36 37 type snapFileSuite struct{} 38 39 func (s *snapFileSuite) SetUpTest(c *check.C) { 40 dirs.SetRootDir(c.MkDir()) 41 } 42 43 func (s *snapFileSuite) TestGetFile(c *check.C) { 44 defer daemon.MockMuxVars(func(*http.Request) map[string]string { 45 return map[string]string{"name": "foo"} 46 })() 47 48 c.Check(daemon.SnapFileCmd.Path, check.Equals, "/v2/snaps/{name}/file") 49 50 o := overlord.Mock() 51 daemon.NewWithOverlord(o) 52 st := o.State() 53 54 type scenario struct { 55 status int 56 exists, active, try, wat bool 57 err string 58 } 59 60 req, err := http.NewRequest("GET", "/v2/snaps/foo/file", nil) 61 c.Assert(err, check.IsNil) 62 63 for i, scen := range []scenario{ 64 {exists: true, active: true}, 65 {exists: false, err: "no state entry for key"}, 66 {exists: true, active: false, err: `cannot download file of inactive snap "foo"`}, 67 {exists: true, active: true, try: true, err: `cannot download file for try-mode snap "foo"`}, 68 {exists: true, wat: true, err: `cannot download file for snap "foo": internal error: .*`}, 69 } { 70 var snapst snapstate.SnapState 71 if scen.wat { 72 st.Lock() 73 st.Set("snaps", 42) 74 st.Unlock() 75 } else { 76 if scen.exists { 77 sideInfo := &snap.SideInfo{Revision: snap.R(-1), RealName: "foo"} 78 snapst.Active = scen.active 79 snapst.Current = sideInfo.Revision 80 snapst.Sequence = append(snapst.Sequence, sideInfo) 81 if scen.try { 82 snapst.TryMode = true 83 } 84 } 85 st.Lock() 86 snapstate.Set(st, "foo", &snapst) 87 st.Unlock() 88 } 89 90 rsp := daemon.GetSnapFile(daemon.SnapFileCmd, req, nil) 91 if scen.err == "" { 92 c.Check(string(rsp.(daemon.FileResponse)), check.Equals, filepath.Join(dirs.SnapBlobDir, "foo_x1.snap"), check.Commentf("%d", i)) 93 } else { 94 c.Assert(rsp, check.FitsTypeOf, &daemon.Resp{}, check.Commentf("%d", i)) 95 result := rsp.(*daemon.Resp).Result 96 c.Assert(result, check.FitsTypeOf, &daemon.ErrorResult{}, check.Commentf("%d", i)) 97 c.Check(result.(*daemon.ErrorResult).Message, check.Matches, scen.err, check.Commentf("%d", i)) 98 } 99 } 100 }