github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/seed/internal/options20_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 internal_test 21 22 import ( 23 "io/ioutil" 24 "path/filepath" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/seed/internal" 29 ) 30 31 type options20Suite struct{} 32 33 var _ = Suite(&options20Suite{}) 34 35 var mockOptions20 = []byte(` 36 snaps: 37 - name: foo 38 id: snapidsnapidsnapidsnapidsnapidsn 39 channel: edge 40 - name: local 41 unasserted: local_v1.snap 42 `) 43 44 func (s *options20Suite) TestSimple(c *C) { 45 fn := filepath.Join(c.MkDir(), "options.yaml") 46 err := ioutil.WriteFile(fn, mockOptions20, 0644) 47 c.Assert(err, IsNil) 48 49 options20, err := internal.ReadOptions20(fn) 50 c.Assert(err, IsNil) 51 c.Assert(options20.Snaps, HasLen, 2) 52 c.Assert(options20.Snaps[0], DeepEquals, &internal.Snap20{ 53 Name: "foo", 54 SnapID: "snapidsnapidsnapidsnapidsnapidsn", 55 Channel: "edge", 56 }) 57 c.Assert(options20.Snaps[1], DeepEquals, &internal.Snap20{ 58 Name: "local", 59 Unasserted: "local_v1.snap", 60 }) 61 } 62 63 func (s *options20Suite) TestEmpty(c *C) { 64 fn := filepath.Join(c.MkDir(), "options.yaml") 65 err := ioutil.WriteFile(fn, []byte(` 66 snaps: 67 - 68 `), 0644) 69 c.Assert(err, IsNil) 70 71 _, err = internal.ReadOptions20(fn) 72 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: empty snaps element`) 73 } 74 75 func (s *options20Suite) TestNoPathAllowed(c *C) { 76 fn := filepath.Join(c.MkDir(), "options.yaml") 77 err := ioutil.WriteFile(fn, []byte(` 78 snaps: 79 - name: foo 80 unasserted: foo/bar.snap 81 `), 0644) 82 c.Assert(err, IsNil) 83 84 _, err = internal.ReadOptions20(fn) 85 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: "foo/bar.snap" must be a filename, not a path`) 86 } 87 88 func (s *options20Suite) TestDuplicatedSnapName(c *C) { 89 fn := filepath.Join(c.MkDir(), "options.yaml") 90 err := ioutil.WriteFile(fn, []byte(` 91 snaps: 92 - name: foo 93 channel: stable 94 - name: foo 95 channel: edge 96 `), 0644) 97 c.Assert(err, IsNil) 98 99 _, err = internal.ReadOptions20(fn) 100 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: snap name "foo" must be unique`) 101 } 102 103 func (s *options20Suite) TestValidateChannelUnhappy(c *C) { 104 fn := filepath.Join(c.MkDir(), "options.yaml") 105 err := ioutil.WriteFile(fn, []byte(` 106 snaps: 107 - name: foo 108 channel: invalid/channel/ 109 `), 0644) 110 c.Assert(err, IsNil) 111 112 _, err = internal.ReadOptions20(fn) 113 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: invalid risk in channel name: invalid/channel/`) 114 } 115 116 func (s *options20Suite) TestValidateSnapIDUnhappy(c *C) { 117 fn := filepath.Join(c.MkDir(), "options.yaml") 118 err := ioutil.WriteFile(fn, []byte(` 119 snaps: 120 - name: foo 121 id: foo 122 `), 0644) 123 c.Assert(err, IsNil) 124 125 _, err = internal.ReadOptions20(fn) 126 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: invalid snap-id: "foo"`) 127 } 128 129 func (s *options20Suite) TestValidateNameUnhappy(c *C) { 130 fn := filepath.Join(c.MkDir(), "options.yaml") 131 err := ioutil.WriteFile(fn, []byte(` 132 snaps: 133 - name: invalid--name 134 unasserted: ./foo.snap 135 `), 0644) 136 c.Assert(err, IsNil) 137 138 _, err = internal.ReadOptions20(fn) 139 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: invalid snap name: "invalid--name"`) 140 } 141 142 func (s *options20Suite) TestValidateNameInstanceUnsupported(c *C) { 143 fn := filepath.Join(c.MkDir(), "options.yaml") 144 err := ioutil.WriteFile(fn, []byte(` 145 snaps: 146 - name: foo_1 147 unasserted: ./foo.snap 148 `), 0644) 149 c.Assert(err, IsNil) 150 151 _, err = internal.ReadOptions20(fn) 152 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: invalid snap name: "foo_1"`) 153 } 154 155 func (s *options20Suite) TestValidateNameMissing(c *C) { 156 fn := filepath.Join(c.MkDir(), "options.yaml") 157 err := ioutil.WriteFile(fn, []byte(` 158 snaps: 159 - unasserted: ./foo.snap 160 `), 0644) 161 c.Assert(err, IsNil) 162 163 _, err = internal.ReadOptions20(fn) 164 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: invalid snap name: ""`) 165 } 166 167 func (s *options20Suite) TestValidateOptionMissing(c *C) { 168 fn := filepath.Join(c.MkDir(), "options.yaml") 169 err := ioutil.WriteFile(fn, []byte(` 170 snaps: 171 - name: foo 172 `), 0644) 173 c.Assert(err, IsNil) 174 175 _, err = internal.ReadOptions20(fn) 176 c.Assert(err, ErrorMatches, `cannot read grade dangerous options yaml: at least one of id, channel or unasserted must be set for snap "foo"`) 177 }