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