github.com/kubiko/snapd@v0.0.0-20201013125620-d4f3094d9ddf/image/helpers_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2020 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 image_test 21 22 import ( 23 "os" 24 "path/filepath" 25 "runtime" 26 27 "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/image" 30 "github.com/snapcore/snapd/logger" 31 "github.com/snapcore/snapd/snap" 32 ) 33 34 func (s *imageSuite) TestDownloadpOptionsString(c *check.C) { 35 tests := []struct { 36 opts image.DownloadOptions 37 str string 38 }{ 39 {image.DownloadOptions{LeavePartialOnError: true}, ""}, 40 {image.DownloadOptions{}, ""}, 41 {image.DownloadOptions{TargetDir: "/foo"}, `in "/foo"`}, 42 {image.DownloadOptions{Basename: "foo"}, `to "foo.snap"`}, 43 {image.DownloadOptions{Channel: "foo"}, `from channel "foo"`}, 44 {image.DownloadOptions{Revision: snap.R(42)}, `(42)`}, 45 {image.DownloadOptions{ 46 CohortKey: "AbCdEfGhIjKlMnOpQrStUvWxYz", 47 }, `from cohort "…rStUvWxYz"`}, 48 {image.DownloadOptions{ 49 TargetDir: "/foo", 50 Basename: "bar", 51 Channel: "baz", 52 Revision: snap.R(13), 53 CohortKey: "MSBIc3dwOW9PemozYjRtdzhnY0MwMFh0eFduS0g5UWlDUSAxNTU1NDExNDE1IDBjYzJhNTc1ZjNjOTQ3ZDEwMWE1NTNjZWFkNmFmZDE3ZWJhYTYyNjM4ZWQ3ZGMzNjI5YmU4YjQ3NzAwMjdlMDk=", 54 }, `(13) from channel "baz" from cohort "…wMjdlMDk=" to "bar.snap" in "/foo"`}, // note this one is not 'valid' so it's ok if the string is a bit wonky 55 56 } 57 58 for _, t := range tests { 59 c.Check(t.opts.String(), check.Equals, t.str) 60 } 61 } 62 63 func (s *imageSuite) TestDownloadOptionsValid(c *check.C) { 64 tests := []struct { 65 opts image.DownloadOptions 66 err error 67 }{ 68 {image.DownloadOptions{}, nil}, // might want to error if no targetdir 69 {image.DownloadOptions{TargetDir: "foo"}, nil}, 70 {image.DownloadOptions{Channel: "foo"}, nil}, 71 {image.DownloadOptions{Revision: snap.R(42)}, nil}, 72 {image.DownloadOptions{ 73 CohortKey: "AbCdEfGhIjKlMnOpQrStUvWxYz", 74 }, nil}, 75 {image.DownloadOptions{ 76 Channel: "foo", 77 Revision: snap.R(42), 78 }, nil}, 79 {image.DownloadOptions{ 80 Channel: "foo", 81 CohortKey: "bar", 82 }, nil}, 83 {image.DownloadOptions{ 84 Revision: snap.R(1), 85 CohortKey: "bar", 86 }, image.ErrRevisionAndCohort}, 87 {image.DownloadOptions{ 88 Basename: "/foo", 89 }, image.ErrPathInBase}, 90 } 91 92 for _, t := range tests { 93 t.opts.LeavePartialOnError = true 94 c.Check(t.opts.Validate(), check.Equals, t.err) 95 t.opts.LeavePartialOnError = false 96 c.Check(t.opts.Validate(), check.Equals, t.err) 97 } 98 } 99 100 func (s *imageSuite) TestDownloadSnap(c *check.C) { 101 // TODO: maybe expand on this (test coverage of DownloadSnap is really bad) 102 103 // env shenanigans 104 runtime.LockOSThread() 105 defer runtime.UnlockOSThread() 106 107 debug, hadDebug := os.LookupEnv("SNAPD_DEBUG") 108 os.Setenv("SNAPD_DEBUG", "1") 109 if hadDebug { 110 defer os.Setenv("SNAPD_DEBUG", debug) 111 } else { 112 defer os.Unsetenv("SNAPD_DEBUG") 113 } 114 logbuf, restore := logger.MockLogger() 115 defer restore() 116 117 s.setupSnaps(c, map[string]string{ 118 "core": "canonical", 119 }, "") 120 121 dlDir := c.MkDir() 122 opts := image.DownloadOptions{ 123 TargetDir: dlDir, 124 } 125 fn, info, redirectChannel, err := s.tsto.DownloadSnap("core", opts) 126 c.Assert(err, check.IsNil) 127 c.Check(fn, check.Matches, filepath.Join(dlDir, `core_\d+.snap`)) 128 c.Check(info.SnapName(), check.Equals, "core") 129 c.Check(redirectChannel, check.Equals, "") 130 131 c.Check(logbuf.String(), check.Matches, `.* DEBUG: Going to download snap "core" `+opts.String()+".\n") 132 }