github.com/tompreston/snapd@v0.0.0-20210817193607-954edfcb9611/overlord/configstate/configcore/hostname_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  	"strings"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/dirs"
    30  	"github.com/snapcore/snapd/overlord/configstate/configcore"
    31  	"github.com/snapcore/snapd/release"
    32  	"github.com/snapcore/snapd/testutil"
    33  )
    34  
    35  type hostnameSuite struct {
    36  	configcoreSuite
    37  
    38  	mockedHostnamectl *testutil.MockCmd
    39  }
    40  
    41  var _ = Suite(&hostnameSuite{})
    42  
    43  func (s *hostnameSuite) SetUpTest(c *C) {
    44  	s.configcoreSuite.SetUpTest(c)
    45  
    46  	err := os.MkdirAll(filepath.Join(dirs.GlobalRootDir, "/etc/"), 0755)
    47  	c.Assert(err, IsNil)
    48  
    49  	s.mockedHostnamectl = testutil.MockCommand(c, "hostnamectl", "")
    50  	s.AddCleanup(s.mockedHostnamectl.Restore)
    51  }
    52  
    53  func (s *hostnameSuite) TestConfigureHostnameInvalid(c *C) {
    54  	invalidHostnames := []string{
    55  		"-no-start-with-dash", "no-upper-A", "no-รค", "no/slash",
    56  		"ALL-CAPS-IS-NEVER-OKAY", "no-SHOUTING-allowed",
    57  		strings.Repeat("x", 64),
    58  	}
    59  
    60  	for _, name := range invalidHostnames {
    61  		err := configcore.Run(coreDev, &mockConf{
    62  			state: s.state,
    63  			conf: map[string]interface{}{
    64  				"system.hostname": name,
    65  			},
    66  		})
    67  		c.Assert(err, ErrorMatches, `cannot set hostname.*`)
    68  	}
    69  
    70  	c.Check(s.mockedHostnamectl.Calls(), HasLen, 0)
    71  }
    72  
    73  func (s *hostnameSuite) TestConfigureHostnameIntegration(c *C) {
    74  	restore := release.MockOnClassic(false)
    75  	defer restore()
    76  
    77  	mockedHostname := testutil.MockCommand(c, "hostname", "echo bar")
    78  	defer mockedHostname.Restore()
    79  
    80  	validHostnames := []string{
    81  		"foo",
    82  		strings.Repeat("x", 63),
    83  		"foo-bar",
    84  		"foo-------bar",
    85  		"foo99",
    86  		"99foo",
    87  		"can-end-with-a-dash-",
    88  	}
    89  
    90  	for _, hostname := range validHostnames {
    91  		err := configcore.Run(coreDev, &mockConf{
    92  			state: s.state,
    93  			conf: map[string]interface{}{
    94  				"system.hostname": hostname,
    95  			},
    96  		})
    97  		c.Assert(err, IsNil)
    98  		c.Check(mockedHostname.Calls(), DeepEquals, [][]string{
    99  			{"hostname"},
   100  		})
   101  		c.Check(s.mockedHostnamectl.Calls(), DeepEquals, [][]string{
   102  			{"hostnamectl", "set-hostname", hostname},
   103  		})
   104  		s.mockedHostnamectl.ForgetCalls()
   105  		mockedHostname.ForgetCalls()
   106  	}
   107  }
   108  
   109  func (s *hostnameSuite) TestConfigureHostnameIntegrationSameHostname(c *C) {
   110  	restore := release.MockOnClassic(false)
   111  	defer restore()
   112  
   113  	// pretent current hostname is "foo"
   114  	mockedHostname := testutil.MockCommand(c, "hostname", "echo foo")
   115  	defer mockedHostname.Restore()
   116  	// and set new hostname to "foo"
   117  	err := configcore.Run(coreDev, &mockConf{
   118  		state: s.state,
   119  		conf: map[string]interface{}{
   120  			"system.hostname": "foo",
   121  		},
   122  	})
   123  	c.Assert(err, IsNil)
   124  	c.Check(mockedHostname.Calls(), DeepEquals, [][]string{
   125  		{"hostname"},
   126  	})
   127  	c.Check(s.mockedHostnamectl.Calls(), HasLen, 0)
   128  }
   129  
   130  func (s *hostnameSuite) TestFilesystemOnlyApply(c *C) {
   131  	conf := configcore.PlainCoreConfig(map[string]interface{}{
   132  		"system.hostname": "bar",
   133  	})
   134  	tmpDir := c.MkDir()
   135  	c.Assert(configcore.FilesystemOnlyApply(coreDev, tmpDir, conf), IsNil)
   136  
   137  	c.Check(filepath.Join(tmpDir, "/etc/writable/hostname"), testutil.FileEquals, "bar\n")
   138  }