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