github.com/docker/docker@v299999999.0.0-20200612211812-aaf470eca7b5+incompatible/pkg/parsers/operatingsystem/operatingsystem_linux_test.go (about)

     1  // +build linux freebsd
     2  
     3  package operatingsystem // import "github.com/docker/docker/pkg/parsers/operatingsystem"
     4  
     5  import (
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	"gotest.tools/v3/assert"
    12  )
    13  
    14  type EtcReleaseParsingTest struct {
    15  	name        string
    16  	content     string
    17  	expected    string
    18  	expectedErr string
    19  }
    20  
    21  func TestGetOperatingSystem(t *testing.T) {
    22  	tests := []EtcReleaseParsingTest{
    23  		{
    24  			content: `PRETTY_NAME=Source Mage GNU/Linux
    25  PRETTY_NAME=Ubuntu 14.04.LTS`,
    26  			expectedErr: "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Source Mage GNU/Linux",
    27  		},
    28  		{
    29  			content: `PRETTY_NAME="Ubuntu Linux
    30  PRETTY_NAME=Ubuntu 14.04.LTS`,
    31  			expectedErr: "PRETTY_NAME is invalid: invalid command line string",
    32  		},
    33  		{
    34  			content: `PRETTY_NAME=Ubuntu'
    35  PRETTY_NAME=Ubuntu 14.04.LTS`,
    36  			expectedErr: "PRETTY_NAME is invalid: invalid command line string",
    37  		},
    38  		{
    39  			content: `PRETTY_NAME'
    40  PRETTY_NAME=Ubuntu 14.04.LTS`,
    41  			expectedErr: "PRETTY_NAME needs to be enclosed by quotes if they have spaces: Ubuntu 14.04.LTS",
    42  		},
    43  		{
    44  			content: `NAME="Ubuntu"
    45  PRETTY_NAME_AGAIN="Ubuntu 14.04.LTS"
    46  VERSION="14.04, Trusty Tahr"
    47  ID=ubuntu
    48  ID_LIKE=debian
    49  VERSION_ID="14.04"
    50  HOME_URL="http://www.ubuntu.com/"
    51  SUPPORT_URL="http://help.ubuntu.com/"
    52  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
    53  			expected: "Linux",
    54  		},
    55  		{
    56  			content: `NAME="Ubuntu"
    57  VERSION="14.04, Trusty Tahr"
    58  ID=ubuntu
    59  ID_LIKE=debian
    60  VERSION_ID="14.04"
    61  HOME_URL="http://www.ubuntu.com/"
    62  SUPPORT_URL="http://help.ubuntu.com/"
    63  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
    64  			expected: "Linux",
    65  		},
    66  		{
    67  			content: `NAME=Gentoo
    68  ID=gentoo
    69  PRETTY_NAME="Gentoo/Linux"
    70  ANSI_COLOR="1;32"
    71  HOME_URL="http://www.gentoo.org/"
    72  SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
    73  BUG_REPORT_URL="https://bugs.gentoo.org/"
    74  `,
    75  			expected: "Gentoo/Linux",
    76  		},
    77  		{
    78  			content: `NAME="Ubuntu"
    79  VERSION="14.04, Trusty Tahr"
    80  ID=ubuntu
    81  ID_LIKE=debian
    82  PRETTY_NAME="Ubuntu 14.04 LTS"
    83  VERSION_ID="14.04"
    84  HOME_URL="http://www.ubuntu.com/"
    85  SUPPORT_URL="http://help.ubuntu.com/"
    86  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
    87  			expected: "Ubuntu 14.04 LTS",
    88  		},
    89  		{
    90  			content: `NAME="Ubuntu"
    91  VERSION="14.04, Trusty Tahr"
    92  ID=ubuntu
    93  ID_LIKE=debian
    94  PRETTY_NAME='Ubuntu 14.04 LTS'`,
    95  			expected: "Ubuntu 14.04 LTS",
    96  		},
    97  		{
    98  			content: `PRETTY_NAME=Source
    99  NAME="Source Mage"`,
   100  			expected: "Source",
   101  		},
   102  		{
   103  			content: `PRETTY_NAME=Source
   104  PRETTY_NAME="Source Mage"`,
   105  			expected: "Source Mage",
   106  		},
   107  	}
   108  
   109  	runEtcReleaseParsingTests(t, tests, GetOperatingSystem)
   110  }
   111  
   112  func TestGetOperatingSystemVersion(t *testing.T) {
   113  	tests := []EtcReleaseParsingTest{
   114  		{
   115  			name: "invalid version id",
   116  			content: `VERSION_ID="18.04
   117  VERSION_ID=18.04`,
   118  			expectedErr: "VERSION_ID is invalid: invalid command line string",
   119  		},
   120  		{
   121  			name: "ubuntu 14.04",
   122  			content: `NAME="Ubuntu"
   123  PRETTY_NAME="Ubuntu 14.04.LTS"
   124  VERSION="14.04, Trusty Tahr"
   125  ID=ubuntu
   126  ID_LIKE=debian
   127  VERSION_ID="14.04"
   128  HOME_URL="http://www.ubuntu.com/"
   129  SUPPORT_URL="http://help.ubuntu.com/"
   130  BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`,
   131  			expected: "14.04",
   132  		},
   133  		{
   134  			name: "gentoo",
   135  			content: `NAME=Gentoo
   136  ID=gentoo
   137  PRETTY_NAME="Gentoo/Linux"
   138  ANSI_COLOR="1;32"
   139  HOME_URL="http://www.gentoo.org/"
   140  SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
   141  BUG_REPORT_URL="https://bugs.gentoo.org/"
   142  `,
   143  		},
   144  		{
   145  			name: "dual version id",
   146  			content: `VERSION_ID="14.04"
   147  VERSION_ID=18.04`,
   148  			expected: "18.04",
   149  		},
   150  	}
   151  
   152  	runEtcReleaseParsingTests(t, tests, GetOperatingSystemVersion)
   153  }
   154  
   155  func runEtcReleaseParsingTests(t *testing.T, tests []EtcReleaseParsingTest, parsingFunc func() (string, error)) {
   156  	var backup = etcOsRelease
   157  
   158  	dir := os.TempDir()
   159  	etcOsRelease = filepath.Join(dir, "etcOsRelease")
   160  
   161  	defer func() {
   162  		os.Remove(etcOsRelease)
   163  		etcOsRelease = backup
   164  	}()
   165  
   166  	for _, test := range tests {
   167  		t.Run(test.name, func(t *testing.T) {
   168  			if err := ioutil.WriteFile(etcOsRelease, []byte(test.content), 0600); err != nil {
   169  				t.Fatalf("failed to write to %s: %v", etcOsRelease, err)
   170  			}
   171  			s, err := parsingFunc()
   172  			if test.expectedErr == "" {
   173  				assert.NilError(t, err)
   174  			} else {
   175  				assert.Error(t, err, test.expectedErr)
   176  			}
   177  			assert.Equal(t, s, test.expected)
   178  		})
   179  	}
   180  }
   181  
   182  func TestIsContainerized(t *testing.T) {
   183  	var (
   184  		backup                                = proc1Cgroup
   185  		nonContainerizedProc1Cgroupsystemd226 = []byte(`9:memory:/init.scope
   186  8:net_cls,net_prio:/
   187  7:cpuset:/
   188  6:freezer:/
   189  5:devices:/init.scope
   190  4:blkio:/init.scope
   191  3:cpu,cpuacct:/init.scope
   192  2:perf_event:/
   193  1:name=systemd:/init.scope
   194  `)
   195  		nonContainerizedProc1Cgroup = []byte(`14:name=systemd:/
   196  13:hugetlb:/
   197  12:net_prio:/
   198  11:perf_event:/
   199  10:bfqio:/
   200  9:blkio:/
   201  8:net_cls:/
   202  7:freezer:/
   203  6:devices:/
   204  5:memory:/
   205  4:cpuacct:/
   206  3:cpu:/
   207  2:cpuset:/
   208  `)
   209  		containerizedProc1Cgroup = []byte(`9:perf_event:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   210  8:blkio:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   211  7:net_cls:/
   212  6:freezer:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   213  5:devices:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   214  4:memory:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   215  3:cpuacct:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   216  2:cpu:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d
   217  1:cpuset:/`)
   218  		nonContainerizedProc1CgroupNotSystemd = []byte(`9:memory:/not/init.scope
   219  	1:name=not_systemd:/not.init.scope
   220  `)
   221  	)
   222  
   223  	dir := os.TempDir()
   224  	proc1Cgroup = filepath.Join(dir, "proc1Cgroup")
   225  
   226  	defer func() {
   227  		os.Remove(proc1Cgroup)
   228  		proc1Cgroup = backup
   229  	}()
   230  
   231  	if err := ioutil.WriteFile(proc1Cgroup, nonContainerizedProc1Cgroup, 0600); err != nil {
   232  		t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
   233  	}
   234  	inContainer, err := IsContainerized()
   235  	if err != nil {
   236  		t.Fatal(err)
   237  	}
   238  	if inContainer {
   239  		t.Fatal("Wrongly assuming containerized")
   240  	}
   241  
   242  	if err := ioutil.WriteFile(proc1Cgroup, nonContainerizedProc1Cgroupsystemd226, 0600); err != nil {
   243  		t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
   244  	}
   245  	inContainer, err = IsContainerized()
   246  	if err != nil {
   247  		t.Fatal(err)
   248  	}
   249  	if inContainer {
   250  		t.Fatal("Wrongly assuming containerized for systemd /init.scope cgroup layout")
   251  	}
   252  
   253  	if err := ioutil.WriteFile(proc1Cgroup, nonContainerizedProc1CgroupNotSystemd, 0600); err != nil {
   254  		t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
   255  	}
   256  	inContainer, err = IsContainerized()
   257  	if err != nil {
   258  		t.Fatal(err)
   259  	}
   260  	if !inContainer {
   261  		t.Fatal("Wrongly assuming non-containerized")
   262  	}
   263  
   264  	if err := ioutil.WriteFile(proc1Cgroup, containerizedProc1Cgroup, 0600); err != nil {
   265  		t.Fatalf("failed to write to %s: %v", proc1Cgroup, err)
   266  	}
   267  	inContainer, err = IsContainerized()
   268  	if err != nil {
   269  		t.Fatal(err)
   270  	}
   271  	if !inContainer {
   272  		t.Fatal("Wrongly assuming non-containerized")
   273  	}
   274  }
   275  
   276  func TestOsReleaseFallback(t *testing.T) {
   277  	var backup = etcOsRelease
   278  	var altBackup = altOsRelease
   279  	dir := os.TempDir()
   280  	etcOsRelease = filepath.Join(dir, "etcOsRelease")
   281  	altOsRelease = filepath.Join(dir, "altOsRelease")
   282  
   283  	defer func() {
   284  		os.Remove(dir)
   285  		etcOsRelease = backup
   286  		altOsRelease = altBackup
   287  	}()
   288  	content := `NAME=Gentoo
   289  ID=gentoo
   290  PRETTY_NAME="Gentoo/Linux"
   291  ANSI_COLOR="1;32"
   292  HOME_URL="http://www.gentoo.org/"
   293  SUPPORT_URL="http://www.gentoo.org/main/en/support.xml"
   294  BUG_REPORT_URL="https://bugs.gentoo.org/"
   295  `
   296  	if err := ioutil.WriteFile(altOsRelease, []byte(content), 0600); err != nil {
   297  		t.Fatalf("failed to write to %s: %v", etcOsRelease, err)
   298  	}
   299  	s, err := GetOperatingSystem()
   300  	if err != nil || s != "Gentoo/Linux" {
   301  		t.Fatalf("Expected %q, got %q (err: %v)", "Gentoo/Linux", s, err)
   302  	}
   303  }