github.com/meulengracht/snapd@v0.0.0-20210719210640-8bde69bcc84e/release/release_test.go (about)

     1  // -*- Mode: Go; indent-tabs-mode: t -*-
     2  
     3  /*
     4   * Copyright (C) 2014-2015 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 release_test
    21  
    22  import (
    23  	"io/ioutil"
    24  	"path/filepath"
    25  	"testing"
    26  
    27  	. "gopkg.in/check.v1"
    28  
    29  	"github.com/snapcore/snapd/release"
    30  )
    31  
    32  // Hook up check.v1 into the "go test" runner
    33  func Test(t *testing.T) { TestingT(t) }
    34  
    35  type ReleaseTestSuite struct {
    36  }
    37  
    38  var _ = Suite(&ReleaseTestSuite{})
    39  
    40  func (s *ReleaseTestSuite) TestSetup(c *C) {
    41  	c.Check(release.Series, Equals, "16")
    42  }
    43  
    44  func mockOSRelease(c *C) string {
    45  	// FIXME: use AddCleanup here once available so that we
    46  	//        can do release.SetLSBReleasePath() here directly
    47  	mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
    48  	s := `
    49  NAME="Ubuntu"
    50  VERSION="18.09 (Awesome Artichoke)"
    51  ID=ubuntu
    52  ID_LIKE=debian
    53  PRETTY_NAME="I'm not real!"
    54  VERSION_ID="18.09"
    55  HOME_URL="http://www.ubuntu.com/"
    56  SUPPORT_URL="http://help.ubuntu.com/"
    57  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    58  `
    59  	err := ioutil.WriteFile(mockOSRelease, []byte(s), 0644)
    60  	c.Assert(err, IsNil)
    61  
    62  	return mockOSRelease
    63  }
    64  
    65  func (s *ReleaseTestSuite) TestReadOSRelease(c *C) {
    66  	reset := release.MockOSReleasePath(mockOSRelease(c))
    67  	defer reset()
    68  
    69  	os := release.ReadOSRelease()
    70  	c.Check(os.ID, Equals, "ubuntu")
    71  	c.Check(os.VersionID, Equals, "18.09")
    72  }
    73  
    74  func (s *ReleaseTestSuite) TestReadWonkyOSRelease(c *C) {
    75  	mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
    76  	dump := `NAME="elementary OS"
    77  VERSION="0.4 Loki"
    78  ID="elementary OS"
    79  ID_LIKE=ubuntu
    80  PRETTY_NAME="elementary OS Loki"
    81  VERSION_ID="0.4"
    82  HOME_URL="http://elementary.io/"
    83  SUPPORT_URL="http://elementary.io/support/"
    84  BUG_REPORT_URL="https://bugs.launchpad.net/elementary/+filebug"`
    85  	err := ioutil.WriteFile(mockOSRelease, []byte(dump), 0644)
    86  	c.Assert(err, IsNil)
    87  
    88  	reset := release.MockOSReleasePath(mockOSRelease)
    89  	defer reset()
    90  
    91  	os := release.ReadOSRelease()
    92  	c.Check(os.ID, Equals, "elementary")
    93  	c.Check(os.VersionID, Equals, "0.4")
    94  }
    95  
    96  func (s *ReleaseTestSuite) TestFamilyOSRelease(c *C) {
    97  	mockOSRelease := filepath.Join(c.MkDir(), "mock-os-release")
    98  	dump := `NAME="CentOS Linux"
    99  VERSION="7 (Core)"
   100  ID="centos"
   101  ID_LIKE="rhel fedora"
   102  VERSION_ID="7"
   103  PRETTY_NAME="CentOS Linux 7 (Core)"
   104  ANSI_COLOR="0;31"
   105  CPE_NAME="cpe:/o:centos:centos:7"
   106  HOME_URL="https://www.centos.org/"
   107  BUG_REPORT_URL="https://bugs.centos.org/"
   108  
   109  CENTOS_MANTISBT_PROJECT="CentOS-7"
   110  CENTOS_MANTISBT_PROJECT_VERSION="7"
   111  REDHAT_SUPPORT_PRODUCT="centos"
   112  REDHAT_SUPPORT_PRODUCT_VERSION="7"`
   113  	err := ioutil.WriteFile(mockOSRelease, []byte(dump), 0644)
   114  	c.Assert(err, IsNil)
   115  
   116  	reset := release.MockOSReleasePath(mockOSRelease)
   117  	defer reset()
   118  
   119  	os := release.ReadOSRelease()
   120  	c.Check(os.ID, Equals, "centos")
   121  	c.Check(os.VersionID, Equals, "7")
   122  	c.Check(os.IDLike, DeepEquals, []string{"rhel", "fedora"})
   123  }
   124  
   125  func (s *ReleaseTestSuite) TestReadOSReleaseNotFound(c *C) {
   126  	reset := release.MockOSReleasePath("not-there")
   127  	defer reset()
   128  
   129  	os := release.ReadOSRelease()
   130  	c.Assert(os, DeepEquals, release.OS{ID: "linux"})
   131  }
   132  
   133  func (s *ReleaseTestSuite) TestOnClassic(c *C) {
   134  	reset := release.MockOnClassic(true)
   135  	defer reset()
   136  	c.Assert(release.OnClassic, Equals, true)
   137  
   138  	reset = release.MockOnClassic(false)
   139  	defer reset()
   140  	c.Assert(release.OnClassic, Equals, false)
   141  }
   142  
   143  func (s *ReleaseTestSuite) TestReleaseInfo(c *C) {
   144  	reset := release.MockReleaseInfo(&release.OS{
   145  		ID: "distro-id",
   146  	})
   147  	defer reset()
   148  	c.Assert(release.ReleaseInfo.ID, Equals, "distro-id")
   149  }
   150  
   151  func (s *ReleaseTestSuite) TestNonWSL(c *C) {
   152  	defer release.MockIoutilReadfile(func(s string) ([]byte, error) {
   153  		c.Check(s, Equals, "/proc/version")
   154  		return []byte("Linux version 2.2.19 (herbert@gondolin) (gcc version 2.7.2.3) #1 Wed Mar 20 19:41:41 EST 2002"), nil
   155  	})()
   156  
   157  	c.Check(release.IsWSL(), Equals, false)
   158  }
   159  
   160  func (s *ReleaseTestSuite) TestWSL(c *C) {
   161  	defer release.MockIoutilReadfile(func(s string) ([]byte, error) {
   162  		c.Check(s, Equals, "/proc/version")
   163  		return []byte("Linux version 3.4.0-Microsoft (Microsoft@Microsoft.com) (gcc version 4.7 (GCC) ) #1 SMP PREEMPT Wed Dec 31 14:42:53 PST 2014"), nil
   164  	})()
   165  
   166  	c.Check(release.IsWSL(), Equals, true)
   167  }
   168  
   169  func (s *ReleaseTestSuite) TestSystemctlSupportsUserUnits(c *C) {
   170  	for _, t := range []struct {
   171  		id, versionID string
   172  		supported     bool
   173  	}{
   174  		// Non-Ubuntu releases are assumed to be new enough
   175  		{"distro-id", "version", true},
   176  		// Ubuntu 14.04's systemd is too old for user units
   177  		{"ubuntu", "14.04", false},
   178  		// Other Ubuntu releases are fine
   179  		{"ubuntu", "16.04", true},
   180  		{"ubuntu", "18.04", true},
   181  		{"ubuntu", "20.04", true},
   182  	} {
   183  		reset := release.MockReleaseInfo(&release.OS{
   184  			ID:        t.id,
   185  			VersionID: t.versionID,
   186  		})
   187  		defer reset()
   188  
   189  		c.Check(release.SystemctlSupportsUserUnits(), Equals, t.supported)
   190  	}
   191  }