| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
//go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos |
| 5 |
|
| 6 |
package resource_test |
| 7 |
|
| 8 |
import ( |
| 9 |
"bytes" |
| 10 |
"io" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
|
| 15 |
"go.opentelemetry.io/otel/sdk/resource" |
| 16 |
) |
| 17 |
|
| 18 |
func TestParseOSReleaseFile(t *testing.T) { |
| 19 |
osReleaseUbuntu := bytes.NewBufferString(`NAME="Ubuntu" |
| 20 |
VERSION="20.04.2 LTS (Focal Fossa)" |
| 21 |
ID=ubuntu |
| 22 |
ID_LIKE=debian |
| 23 |
PRETTY_NAME="Ubuntu 20.04.2 LTS" |
| 24 |
VERSION_ID="20.04" |
| 25 |
HOME_URL="https://www.ubuntu.com/" |
| 26 |
SUPPORT_URL="https://help.ubuntu.com/" |
| 27 |
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" |
| 28 |
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" |
| 29 |
VERSION_CODENAME=focal |
| 30 |
UBUNTU_CODENAME=focal`) |
| 31 |
|
| 32 |
parsedUbuntu := map[string]string{ |
| 33 |
"NAME": "Ubuntu", |
| 34 |
"VERSION": "20.04.2 LTS (Focal Fossa)", |
| 35 |
"ID": "ubuntu", |
| 36 |
"ID_LIKE": "debian", |
| 37 |
"PRETTY_NAME": "Ubuntu 20.04.2 LTS", |
| 38 |
"VERSION_ID": "20.04", |
| 39 |
"HOME_URL": "https://www.ubuntu.com/", |
| 40 |
"SUPPORT_URL": "https://help.ubuntu.com/", |
| 41 |
"BUG_REPORT_URL": "https://bugs.launchpad.net/ubuntu/", |
| 42 |
"PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy", |
| 43 |
"VERSION_CODENAME": "focal", |
| 44 |
"UBUNTU_CODENAME": "focal", |
| 45 |
} |
| 46 |
|
| 47 |
osReleaseDebian := bytes.NewBufferString(`PRETTY_NAME="Debian GNU/Linux 10 (buster)" |
| 48 |
NAME="Debian GNU/Linux" |
| 49 |
VERSION_ID="10" |
| 50 |
VERSION="10 (buster)" |
| 51 |
VERSION_CODENAME=buster |
| 52 |
ID=debian |
| 53 |
HOME_URL="https://www.debian.org/" |
| 54 |
SUPPORT_URL="https://www.debian.org/support" |
| 55 |
BUG_REPORT_URL="https://bugs.debian.org/"`) |
| 56 |
|
| 57 |
parsedDebian := map[string]string{ |
| 58 |
"PRETTY_NAME": "Debian GNU/Linux 10 (buster)", |
| 59 |
"NAME": "Debian GNU/Linux", |
| 60 |
"VERSION_ID": "10", |
| 61 |
"VERSION": "10 (buster)", |
| 62 |
"VERSION_CODENAME": "buster", |
| 63 |
"ID": "debian", |
| 64 |
"HOME_URL": "https://www.debian.org/", |
| 65 |
"SUPPORT_URL": "https://www.debian.org/support", |
| 66 |
"BUG_REPORT_URL": "https://bugs.debian.org/", |
| 67 |
} |
| 68 |
|
| 69 |
osReleaseAlpine := bytes.NewBufferString(`NAME="Alpine Linux" |
| 70 |
ID=alpine |
| 71 |
VERSION_ID=3.13.4 |
| 72 |
PRETTY_NAME="Alpine Linux v3.13" |
| 73 |
HOME_URL="https://alpinelinux.org/" |
| 74 |
BUG_REPORT_URL="https://bugs.alpinelinux.org/"`) |
| 75 |
|
| 76 |
parsedAlpine := map[string]string{ |
| 77 |
"NAME": "Alpine Linux", |
| 78 |
"ID": "alpine", |
| 79 |
"VERSION_ID": "3.13.4", |
| 80 |
"PRETTY_NAME": "Alpine Linux v3.13", |
| 81 |
"HOME_URL": "https://alpinelinux.org/", |
| 82 |
"BUG_REPORT_URL": "https://bugs.alpinelinux.org/", |
| 83 |
} |
| 84 |
|
| 85 |
osReleaseMock := bytes.NewBufferString(` |
| 86 |
# This line should be skipped |
| 87 |
|
| 88 |
QUOTED1="Quoted value 1" |
| 89 |
QUOTED2='Quoted value 2' |
| 90 |
ESCAPED1="\$HOME" |
| 91 |
ESCAPED2="\"release\"" |
| 92 |
ESCAPED3="rock\'n\'roll" |
| 93 |
ESCAPED4="\\var" |
| 94 |
|
| 95 |
=line with missing key should be skipped |
| 96 |
|
| 97 |
PROP1=name=john |
| 98 |
PROP2 = Value |
| 99 |
PROP3='This value will be overwritten by the next one' |
| 100 |
PROP3='Final value'`) |
| 101 |
|
| 102 |
parsedMock := map[string]string{ |
| 103 |
"QUOTED1": "Quoted value 1", |
| 104 |
"QUOTED2": "Quoted value 2", |
| 105 |
"ESCAPED1": "$HOME", |
| 106 |
"ESCAPED2": `"release"`, |
| 107 |
"ESCAPED3": "rock'n'roll", |
| 108 |
"ESCAPED4": `\var`, |
| 109 |
"PROP1": "name=john", |
| 110 |
"PROP2": "Value", |
| 111 |
"PROP3": "Final value", |
| 112 |
} |
| 113 |
|
| 114 |
tt := []struct { |
| 115 |
Name string |
| 116 |
OSRelease io.Reader |
| 117 |
Parsed map[string]string |
| 118 |
}{ |
| 119 |
{"Ubuntu", osReleaseUbuntu, parsedUbuntu}, |
| 120 |
{"Debian", osReleaseDebian, parsedDebian}, |
| 121 |
{"Alpine", osReleaseAlpine, parsedAlpine}, |
| 122 |
{"Mock", osReleaseMock, parsedMock}, |
| 123 |
} |
| 124 |
|
| 125 |
for _, tc := range tt { |
| 126 |
t.Run(tc.Name, func(t *testing.T) { |
| 127 |
result := resource.ParseOSReleaseFile(tc.OSRelease) |
| 128 |
require.Equal(t, tc.Parsed, result) |
| 129 |
}) |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
func TestSkip(t *testing.T) { |
| 134 |
tt := []struct { |
| 135 |
Name string |
| 136 |
Line string |
| 137 |
Expected bool |
| 138 |
}{ |
| 139 |
{"Empty string", "", true}, |
| 140 |
{"Only whitespace", " ", true}, |
| 141 |
{"Hashtag prefix 1", "# Sample text", true}, |
| 142 |
{"Hashtag prefix 2", " # Sample text", true}, |
| 143 |
{"Hashtag and whitespace 1", "# ", true}, |
| 144 |
{"Hashtag and whitespace 2", " #", true}, |
| 145 |
{"Hashtag and whitespace 3", " # ", true}, |
| 146 |
{"Nonempty string", "Sample text", false}, |
| 147 |
{"Nonempty string with whitespace around", " Sample text ", false}, |
| 148 |
{"Nonempty string with middle hashtag", "Sample #text", false}, |
| 149 |
{"Nonempty string with ending hashtag", "Sample text #", false}, |
| 150 |
} |
| 151 |
|
| 152 |
for _, tc := range tt { |
| 153 |
t.Run(tc.Name, func(t *testing.T) { |
| 154 |
result := resource.Skip(tc.Line) |
| 155 |
require.Equal(t, tc.Expected, result) |
| 156 |
}) |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
func TestParse(t *testing.T) { |
| 161 |
tt := []struct { |
| 162 |
Name string |
| 163 |
Line string |
| 164 |
ExpectedKey string |
| 165 |
ExpectedValue string |
| 166 |
OK bool |
| 167 |
}{ |
| 168 |
{"Empty string", "", "", "", false}, |
| 169 |
{"No separator", "wrong", "", "", false}, |
| 170 |
{"Empty key", "=john", "", "", false}, |
| 171 |
{"Empty key value", "=", "", "", false}, |
| 172 |
{"Empty value", "name=", "name", "", true}, |
| 173 |
{"Key value 1", "name=john", "name", "john", true}, |
| 174 |
{"Key value 2", "name=john=dev", "name", "john=dev", true}, |
| 175 |
} |
| 176 |
|
| 177 |
for _, tc := range tt { |
| 178 |
t.Run(tc.Name, func(t *testing.T) { |
| 179 |
key, value, ok := resource.Parse(tc.Line) |
| 180 |
require.Equal(t, tc.ExpectedKey, key) |
| 181 |
require.Equal(t, tc.ExpectedValue, value) |
| 182 |
require.Equal(t, tc.OK, ok) |
| 183 |
}) |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
func TestUnquote(t *testing.T) { |
| 188 |
tt := []struct { |
| 189 |
Name string |
| 190 |
Text string |
| 191 |
Expected string |
| 192 |
}{ |
| 193 |
{"Empty string", ``, ``}, |
| 194 |
{"Single double quote", `"`, `"`}, |
| 195 |
{"Single single quote", `'`, `'`}, |
| 196 |
{"Empty double quotes", `""`, ``}, |
| 197 |
{"Empty single quotes", `''`, ``}, |
| 198 |
{"Empty mixed quotes 1", `"'`, `"'`}, |
| 199 |
{"Empty mixed quotes 2", `'"`, `'"`}, |
| 200 |
{"Double quotes", `"Sample text"`, `Sample text`}, |
| 201 |
{"Single quotes", `'Sample text'`, `Sample text`}, |
| 202 |
{"Half-open starting double quote", `"Sample text`, `"Sample text`}, |
| 203 |
{"Half-open ending double quote", `Sample text"`, `Sample text"`}, |
| 204 |
{"Half-open starting single quote", `'Sample text`, `'Sample text`}, |
| 205 |
{"Half-open ending single quote", `Sample text'`, `Sample text'`}, |
| 206 |
{"Double double quotes", `""Sample text""`, `"Sample text"`}, |
| 207 |
{"Double single quotes", `''Sample text''`, `'Sample text'`}, |
| 208 |
{"Mismatch quotes 1", `"Sample text'`, `"Sample text'`}, |
| 209 |
{"Mismatch quotes 2", `'Sample text"`, `'Sample text"`}, |
| 210 |
{"No quotes", `Sample text`, `Sample text`}, |
| 211 |
{"Internal double quote", `Sample "text"`, `Sample "text"`}, |
| 212 |
{"Internal single quote", `Sample 'text'`, `Sample 'text'`}, |
| 213 |
} |
| 214 |
|
| 215 |
for _, tc := range tt { |
| 216 |
t.Run(tc.Name, func(t *testing.T) { |
| 217 |
result := resource.Unquote(tc.Text) |
| 218 |
require.Equal(t, tc.Expected, result) |
| 219 |
}) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
func TestUnescape(t *testing.T) { |
| 224 |
tt := []struct { |
| 225 |
Name string |
| 226 |
Text string |
| 227 |
Expected string |
| 228 |
}{ |
| 229 |
{"Empty string", ``, ``}, |
| 230 |
{"Escaped dollar sign", `\$var`, `$var`}, |
| 231 |
{"Escaped double quote", `\"var`, `"var`}, |
| 232 |
{"Escaped single quote", `\'var`, `'var`}, |
| 233 |
{"Escaped backslash", `\\var`, `\var`}, |
| 234 |
{"Escaped backtick", "\\`var", "`var"}, |
| 235 |
} |
| 236 |
|
| 237 |
for _, tc := range tt { |
| 238 |
t.Run(tc.Name, func(t *testing.T) { |
| 239 |
result := resource.Unescape(tc.Text) |
| 240 |
require.Equal(t, tc.Expected, result) |
| 241 |
}) |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
func TestBuildOSRelease(t *testing.T) { |
| 246 |
tt := []struct { |
| 247 |
Name string |
| 248 |
Values map[string]string |
| 249 |
Expected string |
| 250 |
}{ |
| 251 |
{"Nil values", nil, ""}, |
| 252 |
{"Empty values", map[string]string{}, ""}, |
| 253 |
{"Name and version only", map[string]string{ |
| 254 |
"NAME": "Ubuntu", |
| 255 |
"VERSION": "20.04.2 LTS (Focal Fossa)", |
| 256 |
}, "Ubuntu 20.04.2 LTS (Focal Fossa)"}, |
| 257 |
{"Name and version preferred", map[string]string{ |
| 258 |
"NAME": "Ubuntu", |
| 259 |
"VERSION": "20.04.2 LTS (Focal Fossa)", |
| 260 |
"VERSION_ID": "20.04", |
| 261 |
"PRETTY_NAME": "Ubuntu 20.04.2 LTS", |
| 262 |
}, "Ubuntu 20.04.2 LTS (Focal Fossa)"}, |
| 263 |
{"Version ID fallback", map[string]string{ |
| 264 |
"NAME": "Ubuntu", |
| 265 |
"VERSION_ID": "20.04", |
| 266 |
}, "Ubuntu 20.04"}, |
| 267 |
{"Pretty name fallback due to missing name", map[string]string{ |
| 268 |
"VERSION": "20.04.2 LTS (Focal Fossa)", |
| 269 |
"PRETTY_NAME": "Ubuntu 20.04.2 LTS", |
| 270 |
}, "Ubuntu 20.04.2 LTS"}, |
| 271 |
{"Pretty name fallback due to missing version", map[string]string{ |
| 272 |
"NAME": "Ubuntu", |
| 273 |
"PRETTY_NAME": "Ubuntu 20.04.2 LTS", |
| 274 |
}, "Ubuntu 20.04.2 LTS"}, |
| 275 |
} |
| 276 |
|
| 277 |
for _, tc := range tt { |
| 278 |
t.Run(tc.Name, func(t *testing.T) { |
| 279 |
result := resource.BuildOSRelease(tc.Values) |
| 280 |
require.Equal(t, tc.Expected, result) |
| 281 |
}) |
| 282 |
} |
| 283 |
} |