github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/seed/internal/seed_yaml_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2014-2016 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 internal_test 21 22 import ( 23 "io/ioutil" 24 "path/filepath" 25 "testing" 26 27 . "gopkg.in/check.v1" 28 29 "github.com/snapcore/snapd/seed/internal" 30 ) 31 32 func Test(t *testing.T) { TestingT(t) } 33 34 type seedYamlTestSuite struct{} 35 36 var _ = Suite(&seedYamlTestSuite{}) 37 38 var mockSeedYaml = []byte(` 39 snaps: 40 - name: foo 41 snap-id: snapidsnapidsnapid 42 channel: stable 43 devmode: true 44 file: foo_1.0_all.snap 45 - name: local 46 unasserted: true 47 file: local.snap 48 `) 49 50 func (s *seedYamlTestSuite) TestSimple(c *C) { 51 fn := filepath.Join(c.MkDir(), "seed.yaml") 52 err := ioutil.WriteFile(fn, mockSeedYaml, 0644) 53 c.Assert(err, IsNil) 54 55 seedYaml, err := internal.ReadSeedYaml(fn) 56 c.Assert(err, IsNil) 57 c.Assert(seedYaml.Snaps, HasLen, 2) 58 c.Assert(seedYaml.Snaps[0], DeepEquals, &internal.Snap16{ 59 File: "foo_1.0_all.snap", 60 Name: "foo", 61 SnapID: "snapidsnapidsnapid", 62 63 Channel: "stable", 64 DevMode: true, 65 }) 66 c.Assert(seedYaml.Snaps[1], DeepEquals, &internal.Snap16{ 67 File: "local.snap", 68 Name: "local", 69 Unasserted: true, 70 }) 71 } 72 73 var badMockSeedYaml = []byte(` 74 snaps: 75 - name: foo 76 file: foo/bar.snap 77 `) 78 79 func (s *seedYamlTestSuite) TestNoPathAllowed(c *C) { 80 fn := filepath.Join(c.MkDir(), "seed.yaml") 81 err := ioutil.WriteFile(fn, badMockSeedYaml, 0644) 82 c.Assert(err, IsNil) 83 84 _, err = internal.ReadSeedYaml(fn) 85 c.Assert(err, ErrorMatches, `cannot read seed yaml: "foo/bar.snap" must be a filename, not a path`) 86 } 87 88 func (s *seedYamlTestSuite) TestDuplicatedSnapName(c *C) { 89 fn := filepath.Join(c.MkDir(), "seed.yaml") 90 err := ioutil.WriteFile(fn, []byte(` 91 snaps: 92 - name: foo 93 channel: stable 94 file: foo_1.0_all.snap 95 - name: foo 96 channel: edge 97 file: bar_1.0_all.snap 98 `), 0644) 99 c.Assert(err, IsNil) 100 101 _, err = internal.ReadSeedYaml(fn) 102 c.Assert(err, ErrorMatches, `cannot read seed yaml: snap name "foo" must be unique`) 103 } 104 105 func (s *seedYamlTestSuite) TestValidateChannelUnhappy(c *C) { 106 fn := filepath.Join(c.MkDir(), "seed.yaml") 107 err := ioutil.WriteFile(fn, []byte(` 108 snaps: 109 - name: foo 110 channel: invalid/channel/ 111 `), 0644) 112 c.Assert(err, IsNil) 113 114 _, err = internal.ReadSeedYaml(fn) 115 c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid risk in channel name: invalid/channel/`) 116 } 117 118 func (s *seedYamlTestSuite) TestValidateNameUnhappy(c *C) { 119 fn := filepath.Join(c.MkDir(), "seed.yaml") 120 err := ioutil.WriteFile(fn, []byte(` 121 snaps: 122 - name: invalid--name 123 file: ./foo.snap 124 `), 0644) 125 c.Assert(err, IsNil) 126 127 _, err = internal.ReadSeedYaml(fn) 128 c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: "invalid--name"`) 129 } 130 131 func (s *seedYamlTestSuite) TestValidateNameInstanceUnsupported(c *C) { 132 fn := filepath.Join(c.MkDir(), "seed.yaml") 133 err := ioutil.WriteFile(fn, []byte(` 134 snaps: 135 - name: foo_1 136 file: ./foo.snap 137 `), 0644) 138 c.Assert(err, IsNil) 139 140 _, err = internal.ReadSeedYaml(fn) 141 c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: "foo_1"`) 142 } 143 144 func (s *seedYamlTestSuite) TestValidateNameMissing(c *C) { 145 fn := filepath.Join(c.MkDir(), "seed.yaml") 146 err := ioutil.WriteFile(fn, []byte(` 147 snaps: 148 - file: ./foo.snap 149 `), 0644) 150 c.Assert(err, IsNil) 151 152 _, err = internal.ReadSeedYaml(fn) 153 c.Assert(err, ErrorMatches, `cannot read seed yaml: invalid snap name: ""`) 154 } 155 156 func (s *seedYamlTestSuite) TestValidateFileMissing(c *C) { 157 fn := filepath.Join(c.MkDir(), "seed.yaml") 158 err := ioutil.WriteFile(fn, []byte(` 159 snaps: 160 - name: foo 161 `), 0644) 162 c.Assert(err, IsNil) 163 164 _, err = internal.ReadSeedYaml(fn) 165 c.Assert(err, ErrorMatches, `cannot read seed yaml: "file" attribute for "foo" cannot be empty`) 166 }