github.com/juju/juju@v0.0.0-20240430160146-1752b71fcf00/core/os/base_linux_test.go (about)

     1  // Copyright 2024 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package os
     5  
     6  import (
     7  	"io/ioutil"
     8  	"path/filepath"
     9  
    10  	"github.com/juju/testing"
    11  	jc "github.com/juju/testing/checkers"
    12  	gc "gopkg.in/check.v1"
    13  
    14  	corebase "github.com/juju/juju/core/base"
    15  )
    16  
    17  type linuxBaseSuite struct {
    18  	testing.CleanupSuite
    19  }
    20  
    21  var _ = gc.Suite(&linuxBaseSuite{})
    22  
    23  var readBaseTests = []struct {
    24  	contents string
    25  	base     corebase.Base
    26  	err      string
    27  }{{
    28  	`NAME="Ubuntu"
    29  VERSION="99.04 LTS, Star Trek"
    30  ID=ubuntu
    31  ID_LIKE=debian
    32  PRETTY_NAME="Ubuntu spock (99.04 LTS)"
    33  VERSION_ID="99.04"
    34  `,
    35  	corebase.MustParseBaseFromString("ubuntu@99.04"),
    36  	"",
    37  }, {
    38  	`NAME="Ubuntu"
    39  VERSION="12.04.5 LTS, Precise Pangolin"
    40  ID=ubuntu
    41  ID_LIKE=debian
    42  PRETTY_NAME="Ubuntu precise (12.04.5 LTS)"
    43  VERSION_ID="12.04"
    44  `,
    45  	corebase.MustParseBaseFromString("ubuntu@12.04"),
    46  	"",
    47  }, {
    48  	`NAME="Ubuntu"
    49  ID=ubuntu
    50  VERSION_ID= "12.04" `,
    51  	corebase.MustParseBaseFromString("ubuntu@12.04"),
    52  	"",
    53  }, {
    54  	`NAME='Ubuntu'
    55  ID='ubuntu'
    56  VERSION_ID='12.04'
    57  `,
    58  
    59  	corebase.MustParseBaseFromString("ubuntu@12.04"),
    60  	"",
    61  }, {
    62  	`NAME="CentOS Linux"
    63  ID="centos"
    64  VERSION_ID="7"
    65  `,
    66  	corebase.MustParseBaseFromString("centos@7"),
    67  	"",
    68  }, {
    69  	`NAME="openSUSE Leap"
    70  ID=opensuse
    71  VERSION_ID="42.2"
    72  `,
    73  	corebase.MustParseBaseFromString("opensuse@42.2"),
    74  	"",
    75  }, {
    76  	`NAME="Ubuntu"
    77  VERSION="14.04.1 LTS, Trusty Tahr"
    78  ID=ubuntu
    79  ID_LIKE=debian
    80  PRETTY_NAME="Ubuntu 14.04.1 LTS"
    81  VERSION_ID="14.04"
    82  HOME_URL="http://www.ubuntu.com/"
    83  SUPPORT_URL="http://help.ubuntu.com/"
    84  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
    85  `,
    86  	corebase.MustParseBaseFromString("ubuntu@14.04"),
    87  	"",
    88  }, {
    89  	`NAME=Fedora
    90  VERSION="24 (Twenty Four)"
    91  ID=fedora
    92  VERSION_ID=24
    93  PRETTY_NAME="Fedora 24 (Twenty Four)"
    94  CPE_NAME="cpe:/o:fedoraproject:fedora:24"
    95  HOME_URL="https://fedoraproject.org/"
    96  BUG_REPORT_URL="https://bugzilla.redhat.com/"
    97  `,
    98  	corebase.MustParseBaseFromString("fedora@24"),
    99  	"",
   100  }, {
   101  
   102  	"",
   103  	corebase.Base{},
   104  	"OS release file is missing ID",
   105  }, {
   106  	`NAME="CentOS Linux"
   107  ID="centos"
   108  `,
   109  	corebase.Base{},
   110  	"OS release file is missing VERSION_ID",
   111  }, {
   112  	`NAME=openSUSE
   113  ID=opensuse
   114  VERSION_ID="42.3"`,
   115  	corebase.MustParseBaseFromString("opensuse@42.3"),
   116  	"",
   117  },
   118  }
   119  
   120  func (s *linuxBaseSuite) TestReadSeries(c *gc.C) {
   121  	d := c.MkDir()
   122  	f := filepath.Join(d, "foo")
   123  	s.PatchValue(&osReleaseFile, f)
   124  	for i, t := range readBaseTests {
   125  		c.Logf("test %d", i)
   126  		err := ioutil.WriteFile(f, []byte(t.contents), 0666)
   127  		c.Assert(err, jc.ErrorIsNil)
   128  		b, err := readBase()
   129  		if t.err == "" {
   130  			c.Assert(err, jc.ErrorIsNil)
   131  			c.Assert(b, gc.Equals, t.base)
   132  		} else {
   133  			c.Assert(err, gc.ErrorMatches, t.err)
   134  		}
   135  	}
   136  }