github.com/stulluk/snapd@v0.0.0-20210611110309-f6d5d5bd24b0/kernel/kernel_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 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 kernel_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "testing" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/kernel" 31 ) 32 33 func makeMockKernel(c *C, kernelYaml string, filesWithContent map[string]string) string { 34 kernelRootDir := c.MkDir() 35 err := os.MkdirAll(filepath.Join(kernelRootDir, "meta"), 0755) 36 c.Assert(err, IsNil) 37 err = ioutil.WriteFile(filepath.Join(kernelRootDir, "meta/kernel.yaml"), []byte(kernelYaml), 0644) 38 c.Assert(err, IsNil) 39 40 for fname, content := range filesWithContent { 41 p := filepath.Join(kernelRootDir, fname) 42 err = os.MkdirAll(filepath.Dir(p), 0755) 43 c.Assert(err, IsNil) 44 err = ioutil.WriteFile(p, []byte(content), 0644) 45 c.Assert(err, IsNil) 46 } 47 48 return kernelRootDir 49 } 50 51 type kernelYamlTestSuite struct{} 52 53 var _ = Suite(&kernelYamlTestSuite{}) 54 55 func TestCommand(t *testing.T) { TestingT(t) } 56 57 var mockKernelYaml = []byte(` 58 assets: 59 dtbs: 60 update: true 61 content: 62 - dtbs/bcm2711-rpi-4-b.dtb 63 - dtbs/bcm2836-rpi-2-b.dtb 64 `) 65 66 var mockInvalidKernelYaml = []byte(` 67 assets: 68 non-#alphanumeric: 69 `) 70 71 func (s *kernelYamlTestSuite) TestInfoFromKernelYamlSad(c *C) { 72 ki, err := kernel.InfoFromKernelYaml([]byte("foo")) 73 c.Check(err, ErrorMatches, "(?m)cannot parse kernel metadata: .*") 74 c.Check(ki, IsNil) 75 } 76 77 func (s *kernelYamlTestSuite) TestInfoFromKernelYamlBadName(c *C) { 78 ki, err := kernel.InfoFromKernelYaml(mockInvalidKernelYaml) 79 c.Check(err, ErrorMatches, `invalid asset name "non-#alphanumeric", please use only alphanumeric characters and dashes`) 80 c.Check(ki, IsNil) 81 } 82 83 func (s *kernelYamlTestSuite) TestInfoFromKernelYamlHappy(c *C) { 84 ki, err := kernel.InfoFromKernelYaml(mockKernelYaml) 85 c.Check(err, IsNil) 86 c.Check(ki, DeepEquals, &kernel.Info{ 87 Assets: map[string]*kernel.Asset{ 88 "dtbs": { 89 Update: true, 90 Content: []string{ 91 "dtbs/bcm2711-rpi-4-b.dtb", 92 "dtbs/bcm2836-rpi-2-b.dtb", 93 }, 94 }, 95 }, 96 }) 97 } 98 99 func (s *kernelYamlTestSuite) TestReadKernelYamlOptional(c *C) { 100 ki, err := kernel.ReadInfo("this-path-does-not-exist") 101 c.Check(err, IsNil) 102 c.Check(ki, DeepEquals, &kernel.Info{}) 103 } 104 105 func (s *kernelYamlTestSuite) TestReadKernelYamlSad(c *C) { 106 mockKernelSnapRoot := c.MkDir() 107 kernelYamlPath := filepath.Join(mockKernelSnapRoot, "meta/kernel.yaml") 108 err := os.MkdirAll(filepath.Dir(kernelYamlPath), 0755) 109 c.Assert(err, IsNil) 110 err = ioutil.WriteFile(kernelYamlPath, []byte(`invalid-kernel-yaml`), 0644) 111 c.Assert(err, IsNil) 112 113 ki, err := kernel.ReadInfo(mockKernelSnapRoot) 114 c.Check(err, ErrorMatches, `(?m)cannot parse kernel metadata: yaml: unmarshal errors:.*`) 115 c.Check(ki, IsNil) 116 } 117 118 func (s *kernelYamlTestSuite) TestReadKernelYamlHappy(c *C) { 119 mockKernelSnapRoot := c.MkDir() 120 kernelYamlPath := filepath.Join(mockKernelSnapRoot, "meta/kernel.yaml") 121 err := os.MkdirAll(filepath.Dir(kernelYamlPath), 0755) 122 c.Assert(err, IsNil) 123 err = ioutil.WriteFile(kernelYamlPath, mockKernelYaml, 0644) 124 c.Assert(err, IsNil) 125 126 ki, err := kernel.ReadInfo(mockKernelSnapRoot) 127 c.Assert(err, IsNil) 128 c.Check(ki, DeepEquals, &kernel.Info{ 129 Assets: map[string]*kernel.Asset{ 130 "dtbs": { 131 Update: true, 132 Content: []string{ 133 "dtbs/bcm2711-rpi-4-b.dtb", 134 "dtbs/bcm2836-rpi-2-b.dtb", 135 }, 136 }, 137 }, 138 }) 139 }