github.com/ph/moby@v1.13.1/pkg/parsers/operatingsystem/operatingsystem_unix_test.go (about)

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