github.com/Lephar/snapd@v0.0.0-20210825215435-c7fba9cef4d2/overlord/configstate/configcore/timezone_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 configcore_test 21 22 import ( 23 "os" 24 "path/filepath" 25 26 . "gopkg.in/check.v1" 27 28 "github.com/snapcore/snapd/dirs" 29 "github.com/snapcore/snapd/overlord/configstate/configcore" 30 "github.com/snapcore/snapd/testutil" 31 ) 32 33 type timezoneSuite struct { 34 configcoreSuite 35 } 36 37 var _ = Suite(&timezoneSuite{}) 38 39 func (s *timezoneSuite) SetUpTest(c *C) { 40 s.configcoreSuite.SetUpTest(c) 41 42 err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/writable"), 0755) 43 c.Assert(err, IsNil) 44 localtimePath := filepath.Join(dirs.GlobalRootDir, "/etc/writable/localtime") 45 err = os.Symlink("/usr/share/zoneinfo/WET", localtimePath) 46 c.Assert(err, IsNil) 47 } 48 49 func (s *timezoneSuite) TestConfigureTimezoneInvalid(c *C) { 50 invalidTimezones := []string{ 51 "no-#", "no-รค", "no/triple/slash/", 52 } 53 54 for _, tz := range invalidTimezones { 55 err := configcore.Run(coreDev, &mockConf{ 56 state: s.state, 57 conf: map[string]interface{}{ 58 "system.timezone": tz, 59 }, 60 }) 61 c.Assert(err, ErrorMatches, `cannot set timezone.*`) 62 } 63 } 64 65 func (s *timezoneSuite) TestConfigureTimezoneIntegration(c *C) { 66 mockedTimedatectl := testutil.MockCommand(c, "timedatectl", "") 67 defer mockedTimedatectl.Restore() 68 69 validTimezones := []string{ 70 "UTC", "Europe/Malta", "US/Indiana-Starke", "Africa/Sao_Tome", 71 "America/Argentina/Cordoba", "America/Argentina/La_Rioja", 72 "Etc/GMT+1", "CST6CDT", "GMT0", "GMT-0", "PST8PDT", 73 } 74 75 for _, tz := range validTimezones { 76 err := configcore.Run(coreDev, &mockConf{ 77 state: s.state, 78 conf: map[string]interface{}{ 79 "system.timezone": tz, 80 }, 81 }) 82 c.Assert(err, IsNil) 83 c.Check(mockedTimedatectl.Calls(), DeepEquals, [][]string{ 84 {"timedatectl", "set-timezone", tz}, 85 }, Commentf("tested timezone: %v", tz)) 86 mockedTimedatectl.ForgetCalls() 87 } 88 } 89 90 func (s *timezoneSuite) TestFilesystemOnlyApply(c *C) { 91 conf := configcore.PlainCoreConfig(map[string]interface{}{ 92 "system.timezone": "Europe/Berlin", 93 }) 94 tmpDir := c.MkDir() 95 c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), IsNil) 96 97 c.Check(filepath.Join(tmpDir, "/etc/writable/timezone"), testutil.FileEquals, "Europe/Berlin\n") 98 p, err := os.Readlink(filepath.Join(tmpDir, "/etc/writable/localtime")) 99 c.Assert(err, IsNil) 100 c.Check(p, Equals, "/usr/share/zoneinfo/Europe/Berlin") 101 }