github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/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/"), 0755)
    43  	c.Assert(err, IsNil)
    44  }
    45  
    46  func (s *timezoneSuite) TestConfigureTimezoneInvalid(c *C) {
    47  	invalidTimezones := []string{
    48  		"no-#", "no-รค", "no/triple/slash/",
    49  	}
    50  
    51  	for _, tz := range invalidTimezones {
    52  		err := configcore.Run(coreDev, &mockConf{
    53  			state: s.state,
    54  			conf: map[string]interface{}{
    55  				"system.timezone": tz,
    56  			},
    57  		})
    58  		c.Assert(err, ErrorMatches, `cannot set timezone.*`)
    59  	}
    60  }
    61  
    62  func (s *timezoneSuite) TestConfigureTimezoneIntegration(c *C) {
    63  	mockedTimedatectl := testutil.MockCommand(c, "timedatectl", "")
    64  	defer mockedTimedatectl.Restore()
    65  
    66  	validTimezones := []string{
    67  		"UTC", "Europe/Malta", "US/Indiana-Starke", "Africa/Sao_Tome",
    68  		"America/Argentina/Cordoba", "America/Argentina/La_Rioja",
    69  		"Etc/GMT+1", "CST6CDT", "GMT0", "GMT-0", "PST8PDT",
    70  	}
    71  
    72  	for _, tz := range validTimezones {
    73  		err := configcore.Run(coreDev, &mockConf{
    74  			state: s.state,
    75  			conf: map[string]interface{}{
    76  				"system.timezone": tz,
    77  			},
    78  		})
    79  		c.Assert(err, IsNil)
    80  		c.Check(mockedTimedatectl.Calls(), DeepEquals, [][]string{
    81  			{"timedatectl", "set-timezone", tz},
    82  		})
    83  		mockedTimedatectl.ForgetCalls()
    84  	}
    85  }
    86  
    87  func (s *timezoneSuite) TestFilesystemOnlyApply(c *C) {
    88  	conf := configcore.PlainCoreConfig(map[string]interface{}{
    89  		"system.timezone": "Europe/Berlin",
    90  	})
    91  	tmpDir := c.MkDir()
    92  	c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), IsNil)
    93  
    94  	c.Check(filepath.Join(tmpDir, "/etc/writable/timezone"), testutil.FileEquals, "Europe/Berlin\n")
    95  	p, err := os.Readlink(filepath.Join(tmpDir, "/etc/writable/localtime"))
    96  	c.Assert(err, IsNil)
    97  	c.Check(p, Equals, "/usr/share/zoneinfo/Europe/Berlin")
    98  }