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