github.com/damirazo/docker@v1.9.0/pkg/parsers/operatingsystem/operatingsystem_test.go (about) 1 package operatingsystem 2 3 import ( 4 "io/ioutil" 5 "os" 6 "path/filepath" 7 "testing" 8 ) 9 10 func TestGetOperatingSystem(t *testing.T) { 11 var ( 12 backup = etcOsRelease 13 ubuntuTrusty = []byte(`NAME="Ubuntu" 14 VERSION="14.04, Trusty Tahr" 15 ID=ubuntu 16 ID_LIKE=debian 17 PRETTY_NAME="Ubuntu 14.04 LTS" 18 VERSION_ID="14.04" 19 HOME_URL="http://www.ubuntu.com/" 20 SUPPORT_URL="http://help.ubuntu.com/" 21 BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`) 22 gentoo = []byte(`NAME=Gentoo 23 ID=gentoo 24 PRETTY_NAME="Gentoo/Linux" 25 ANSI_COLOR="1;32" 26 HOME_URL="http://www.gentoo.org/" 27 SUPPORT_URL="http://www.gentoo.org/main/en/support.xml" 28 BUG_REPORT_URL="https://bugs.gentoo.org/" 29 `) 30 noPrettyName = []byte(`NAME="Ubuntu" 31 VERSION="14.04, Trusty Tahr" 32 ID=ubuntu 33 ID_LIKE=debian 34 VERSION_ID="14.04" 35 HOME_URL="http://www.ubuntu.com/" 36 SUPPORT_URL="http://help.ubuntu.com/" 37 BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"`) 38 ) 39 40 dir := os.TempDir() 41 etcOsRelease = filepath.Join(dir, "etcOsRelease") 42 43 defer func() { 44 os.Remove(etcOsRelease) 45 etcOsRelease = backup 46 }() 47 48 for expect, osRelease := range map[string][]byte{ 49 "Ubuntu 14.04 LTS": ubuntuTrusty, 50 "Gentoo/Linux": gentoo, 51 "": noPrettyName, 52 } { 53 if err := ioutil.WriteFile(etcOsRelease, osRelease, 0600); err != nil { 54 t.Fatalf("failed to write to %s: %v", etcOsRelease, err) 55 } 56 s, err := GetOperatingSystem() 57 if s != expect { 58 if expect == "" { 59 t.Fatalf("Expected error 'PRETTY_NAME not found', but got %v", err) 60 } else { 61 t.Fatalf("Expected '%s', but got '%s'. Err=%v", expect, s, err) 62 } 63 } 64 } 65 } 66 67 func TestIsContainerized(t *testing.T) { 68 var ( 69 backup = proc1Cgroup 70 nonContainerizedProc1Cgroup = []byte(`14:name=systemd:/ 71 13:hugetlb:/ 72 12:net_prio:/ 73 11:perf_event:/ 74 10:bfqio:/ 75 9:blkio:/ 76 8:net_cls:/ 77 7:freezer:/ 78 6:devices:/ 79 5:memory:/ 80 4:cpuacct:/ 81 3:cpu:/ 82 2:cpuset:/ 83 `) 84 containerizedProc1Cgroup = []byte(`9:perf_event:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 85 8:blkio:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 86 7:net_cls:/ 87 6:freezer:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 88 5:devices:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 89 4:memory:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 90 3:cpuacct:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 91 2:cpu:/docker/3cef1b53c50b0fa357d994f8a1a8cd783c76bbf4f5dd08b226e38a8bd331338d 92 1:cpuset:/`) 93 ) 94 95 dir := os.TempDir() 96 proc1Cgroup = filepath.Join(dir, "proc1Cgroup") 97 98 defer func() { 99 os.Remove(proc1Cgroup) 100 proc1Cgroup = backup 101 }() 102 103 if err := ioutil.WriteFile(proc1Cgroup, nonContainerizedProc1Cgroup, 0600); err != nil { 104 t.Fatalf("failed to write to %s: %v", proc1Cgroup, err) 105 } 106 inContainer, err := IsContainerized() 107 if err != nil { 108 t.Fatal(err) 109 } 110 if inContainer { 111 t.Fatal("Wrongly assuming containerized") 112 } 113 114 if err := ioutil.WriteFile(proc1Cgroup, containerizedProc1Cgroup, 0600); err != nil { 115 t.Fatalf("failed to write to %s: %v", proc1Cgroup, err) 116 } 117 inContainer, err = IsContainerized() 118 if err != nil { 119 t.Fatal(err) 120 } 121 if !inContainer { 122 t.Fatal("Wrongly assuming non-containerized") 123 } 124 }