github.com/rigado/snapd@v2.42.5-go-mod+incompatible/overlord/configstate/configcore/picfg_test.go (about) 1 // -*- Mode: Go; indent-tabs-mode: t -*- 2 3 /* 4 * Copyright (C) 2017 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 configcore_test 21 22 import ( 23 "io/ioutil" 24 "os" 25 "path/filepath" 26 "strings" 27 28 . "gopkg.in/check.v1" 29 30 "github.com/snapcore/snapd/dirs" 31 "github.com/snapcore/snapd/overlord/configstate/configcore" 32 "github.com/snapcore/snapd/release" 33 "github.com/snapcore/snapd/testutil" 34 ) 35 36 type piCfgSuite struct { 37 configcoreSuite 38 39 mockConfigPath string 40 } 41 42 var _ = Suite(&piCfgSuite{}) 43 44 var mockConfigTxt = ` 45 # For more options and information see 46 # http://www.raspberrypi.org/documentation/configuration/config-txt.md 47 #hdmi_group=1 48 # uncomment this if your display has a black border of unused pixels visible 49 # and your display can output without overscan 50 #disable_overscan=1 51 unrelated_options=are-kept 52 #gpu_mem_512=true 53 ` 54 55 func (s *piCfgSuite) SetUpTest(c *C) { 56 s.configcoreSuite.SetUpTest(c) 57 dirs.SetRootDir(c.MkDir()) 58 c.Assert(os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "etc"), 0755), IsNil) 59 60 s.mockConfigPath = filepath.Join(dirs.GlobalRootDir, "/boot/uboot/config.txt") 61 err := os.MkdirAll(filepath.Dir(s.mockConfigPath), 0755) 62 c.Assert(err, IsNil) 63 s.mockConfig(c, mockConfigTxt) 64 } 65 66 func (s *piCfgSuite) TearDownTest(c *C) { 67 dirs.SetRootDir("/") 68 } 69 70 func (s *piCfgSuite) mockConfig(c *C, txt string) { 71 err := ioutil.WriteFile(s.mockConfigPath, []byte(txt), 0644) 72 c.Assert(err, IsNil) 73 } 74 75 func (s *piCfgSuite) checkMockConfig(c *C, expected string) { 76 c.Check(s.mockConfigPath, testutil.FileEquals, expected) 77 } 78 79 func (s *piCfgSuite) TestConfigurePiConfigUncommentExisting(c *C) { 80 err := configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"disable_overscan": "1"}) 81 c.Assert(err, IsNil) 82 83 expected := strings.Replace(mockConfigTxt, "#disable_overscan=1", "disable_overscan=1", -1) 84 s.checkMockConfig(c, expected) 85 } 86 87 func (s *piCfgSuite) TestConfigurePiConfigCommentExisting(c *C) { 88 s.mockConfig(c, mockConfigTxt+"avoid_warnings=1\n") 89 90 err := configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"avoid_warnings": ""}) 91 c.Assert(err, IsNil) 92 93 expected := mockConfigTxt + "#avoid_warnings=1\n" 94 s.checkMockConfig(c, expected) 95 } 96 97 func (s *piCfgSuite) TestConfigurePiConfigAddNewOption(c *C) { 98 err := configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"framebuffer_depth": "16"}) 99 c.Assert(err, IsNil) 100 101 expected := mockConfigTxt + "framebuffer_depth=16\n" 102 s.checkMockConfig(c, expected) 103 104 // add again, verify its not added twice but updated 105 err = configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"framebuffer_depth": "32"}) 106 c.Assert(err, IsNil) 107 expected = mockConfigTxt + "framebuffer_depth=32\n" 108 s.checkMockConfig(c, expected) 109 } 110 111 func (s *piCfgSuite) TestConfigurePiConfigNoChangeUnset(c *C) { 112 // ensure we cannot write to the dir to test that we really 113 // do not update the file 114 err := os.Chmod(filepath.Dir(s.mockConfigPath), 0500) 115 c.Assert(err, IsNil) 116 defer os.Chmod(filepath.Dir(s.mockConfigPath), 0755) 117 118 err = configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"hdmi_group": ""}) 119 c.Assert(err, IsNil) 120 } 121 122 func (s *piCfgSuite) TestConfigurePiConfigNoChangeSet(c *C) { 123 // ensure we cannot write to the dir to test that we really 124 // do not update the file 125 err := os.Chmod(filepath.Dir(s.mockConfigPath), 0500) 126 c.Assert(err, IsNil) 127 defer os.Chmod(filepath.Dir(s.mockConfigPath), 0755) 128 129 err = configcore.UpdatePiConfig(s.mockConfigPath, map[string]string{"unrelated_options": "cannot-be-set"}) 130 c.Assert(err, ErrorMatches, `cannot set unsupported configuration value "unrelated_options"`) 131 } 132 133 func (s *piCfgSuite) TestConfigurePiConfigIntegration(c *C) { 134 restore := release.MockOnClassic(false) 135 defer restore() 136 137 err := configcore.Run(&mockConf{ 138 state: s.state, 139 conf: map[string]interface{}{ 140 "pi-config.disable-overscan": 1, 141 }, 142 }) 143 c.Assert(err, IsNil) 144 145 expected := strings.Replace(mockConfigTxt, "#disable_overscan=1", "disable_overscan=1", -1) 146 s.checkMockConfig(c, expected) 147 148 err = configcore.Run(&mockConf{ 149 state: s.state, 150 conf: map[string]interface{}{ 151 "pi-config.disable-overscan": "", 152 }, 153 }) 154 c.Assert(err, IsNil) 155 156 s.checkMockConfig(c, mockConfigTxt) 157 } 158 159 func (s *piCfgSuite) TestConfigurePiConfigRegression(c *C) { 160 restore := release.MockOnClassic(false) 161 defer restore() 162 163 err := configcore.Run(&mockConf{ 164 state: s.state, 165 conf: map[string]interface{}{ 166 "pi-config.gpu-mem-512": true, 167 }, 168 }) 169 c.Assert(err, IsNil) 170 expected := strings.Replace(mockConfigTxt, "#gpu_mem_512=true", "gpu_mem_512=true", -1) 171 s.checkMockConfig(c, expected) 172 }