github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/cmd/snap/cmd_download_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 main_test 21 22 import ( 23 "fmt" 24 "os" 25 "path/filepath" 26 27 "gopkg.in/check.v1" 28 29 snapCmd "github.com/snapcore/snapd/cmd/snap" 30 "github.com/snapcore/snapd/image" 31 "github.com/snapcore/snapd/snap" 32 ) 33 34 // these only cover errors that happen before hitting the network, 35 // because we're not (yet!) mocking the tooling store 36 37 func (s *SnapSuite) TestDownloadBadBasename(c *check.C) { 38 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 39 "download", "--basename=/foo", "a-snap", 40 }) 41 42 c.Check(err, check.ErrorMatches, "cannot specify a path in basename .use --target-dir for that.") 43 } 44 45 func (s *SnapSuite) TestDownloadBadChannelCombo(c *check.C) { 46 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 47 "download", "--beta", "--channel=foo", "a-snap", 48 }) 49 50 c.Check(err, check.ErrorMatches, "Please specify a single channel") 51 } 52 53 func (s *SnapSuite) TestDownloadCohortAndRevision(c *check.C) { 54 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 55 "download", "--cohort=what", "--revision=1234", "a-snap", 56 }) 57 58 c.Check(err, check.ErrorMatches, "cannot specify both cohort and revision") 59 } 60 61 func (s *SnapSuite) TestDownloadChannelAndRevision(c *check.C) { 62 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 63 "download", "--beta", "--revision=1234", "a-snap", 64 }) 65 66 c.Check(err, check.ErrorMatches, "cannot specify both channel and revision") 67 } 68 69 func (s *SnapSuite) TestPrintInstalHint(c *check.C) { 70 snapCmd.PrintInstallHint("foo_1.assert", "foo_1.snap") 71 c.Check(s.Stdout(), check.Equals, `Install the snap with: 72 snap ack foo_1.assert 73 snap install foo_1.snap 74 `) 75 s.stdout.Reset() 76 77 cwd, err := os.Getwd() 78 c.Assert(err, check.IsNil) 79 as := filepath.Join(cwd, "some-dir/foo_1.assert") 80 sn := filepath.Join(cwd, "some-dir/foo_1.snap") 81 snapCmd.PrintInstallHint(as, sn) 82 c.Check(s.Stdout(), check.Equals, `Install the snap with: 83 snap ack some-dir/foo_1.assert 84 snap install some-dir/foo_1.snap 85 `) 86 } 87 88 func (s *SnapSuite) TestDownloadDirect(c *check.C) { 89 var n int 90 restore := snapCmd.MockDownloadDirect(func(snapName string, revision snap.Revision, dlOpts image.DownloadOptions) error { 91 c.Check(snapName, check.Equals, "a-snap") 92 c.Check(revision, check.Equals, snap.R(0)) 93 c.Check(dlOpts.Basename, check.Equals, "some-base-name") 94 c.Check(dlOpts.TargetDir, check.Equals, "some-target-dir") 95 c.Check(dlOpts.Channel, check.Equals, "some-channel") 96 c.Check(dlOpts.CohortKey, check.Equals, "some-cohort") 97 n++ 98 return nil 99 }) 100 defer restore() 101 102 // check that a direct download got issued 103 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 104 "download", 105 "--target-directory=some-target-dir", 106 "--basename=some-base-name", 107 "--channel=some-channel", 108 "--cohort=some-cohort", 109 "a-snap"}, 110 ) 111 c.Assert(err, check.IsNil) 112 c.Check(n, check.Equals, 1) 113 } 114 115 func (s *SnapSuite) TestDownloadDirectErrors(c *check.C) { 116 var n int 117 restore := snapCmd.MockDownloadDirect(func(snapName string, revision snap.Revision, dlOpts image.DownloadOptions) error { 118 n++ 119 return fmt.Errorf("some-error") 120 }) 121 defer restore() 122 123 // check that a direct download got issued 124 _, err := snapCmd.Parser(snapCmd.Client()).ParseArgs([]string{ 125 "download", 126 "a-snap"}, 127 ) 128 c.Assert(err, check.ErrorMatches, "some-error") 129 c.Check(n, check.Equals, 1) 130 }