File: os_release_unix_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (296 lines) | stat: -rw-r--r-- 8,466 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

//go:build aix || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
// +build aix dragonfly freebsd linux netbsd openbsd solaris zos

package resource_test

import (
	"bytes"
	"io"
	"testing"

	"github.com/stretchr/testify/require"

	"go.opentelemetry.io/otel/sdk/resource"
)

func TestParseOSReleaseFile(t *testing.T) {
	osReleaseUbuntu := bytes.NewBufferString(`NAME="Ubuntu"
VERSION="20.04.2 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.2 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal`)

	parsedUbuntu := map[string]string{
		"NAME":               "Ubuntu",
		"VERSION":            "20.04.2 LTS (Focal Fossa)",
		"ID":                 "ubuntu",
		"ID_LIKE":            "debian",
		"PRETTY_NAME":        "Ubuntu 20.04.2 LTS",
		"VERSION_ID":         "20.04",
		"HOME_URL":           "https://www.ubuntu.com/",
		"SUPPORT_URL":        "https://help.ubuntu.com/",
		"BUG_REPORT_URL":     "https://bugs.launchpad.net/ubuntu/",
		"PRIVACY_POLICY_URL": "https://www.ubuntu.com/legal/terms-and-policies/privacy-policy",
		"VERSION_CODENAME":   "focal",
		"UBUNTU_CODENAME":    "focal",
	}

	osReleaseDebian := bytes.NewBufferString(`PRETTY_NAME="Debian GNU/Linux 10 (buster)"
NAME="Debian GNU/Linux"
VERSION_ID="10"
VERSION="10 (buster)"
VERSION_CODENAME=buster
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"`)

	parsedDebian := map[string]string{
		"PRETTY_NAME":      "Debian GNU/Linux 10 (buster)",
		"NAME":             "Debian GNU/Linux",
		"VERSION_ID":       "10",
		"VERSION":          "10 (buster)",
		"VERSION_CODENAME": "buster",
		"ID":               "debian",
		"HOME_URL":         "https://www.debian.org/",
		"SUPPORT_URL":      "https://www.debian.org/support",
		"BUG_REPORT_URL":   "https://bugs.debian.org/",
	}

	osReleaseAlpine := bytes.NewBufferString(`NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.13.4
PRETTY_NAME="Alpine Linux v3.13"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://bugs.alpinelinux.org/"`)

	parsedAlpine := map[string]string{
		"NAME":           "Alpine Linux",
		"ID":             "alpine",
		"VERSION_ID":     "3.13.4",
		"PRETTY_NAME":    "Alpine Linux v3.13",
		"HOME_URL":       "https://alpinelinux.org/",
		"BUG_REPORT_URL": "https://bugs.alpinelinux.org/",
	}

	osReleaseMock := bytes.NewBufferString(`
# This line should be skipped

QUOTED1="Quoted value 1"
QUOTED2='Quoted value 2'
ESCAPED1="\$HOME"
ESCAPED2="\"release\""
ESCAPED3="rock\'n\'roll"
ESCAPED4="\\var"

=line with missing key should be skipped

PROP1=name=john
	PROP2  =  Value
PROP3='This value will be overwritten by the next one'
PROP3='Final value'`)

	parsedMock := map[string]string{
		"QUOTED1":  "Quoted value 1",
		"QUOTED2":  "Quoted value 2",
		"ESCAPED1": "$HOME",
		"ESCAPED2": `"release"`,
		"ESCAPED3": "rock'n'roll",
		"ESCAPED4": `\var`,
		"PROP1":    "name=john",
		"PROP2":    "Value",
		"PROP3":    "Final value",
	}

	tt := []struct {
		Name      string
		OSRelease io.Reader
		Parsed    map[string]string
	}{
		{"Ubuntu", osReleaseUbuntu, parsedUbuntu},
		{"Debian", osReleaseDebian, parsedDebian},
		{"Alpine", osReleaseAlpine, parsedAlpine},
		{"Mock", osReleaseMock, parsedMock},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			result := resource.ParseOSReleaseFile(tc.OSRelease)
			require.EqualValues(t, tc.Parsed, result)
		})
	}
}

func TestSkip(t *testing.T) {
	tt := []struct {
		Name     string
		Line     string
		Expected bool
	}{
		{"Empty string", "", true},
		{"Only whitespace", "   ", true},
		{"Hashtag prefix 1", "# Sample text", true},
		{"Hashtag prefix 2", "  # Sample text", true},
		{"Hashtag and whitespace 1", "#  ", true},
		{"Hashtag and whitespace 2", "  #", true},
		{"Hashtag and whitespace 3", "  #  ", true},
		{"Nonempty string", "Sample text", false},
		{"Nonempty string with whitespace around", " Sample text ", false},
		{"Nonempty string with middle hashtag", "Sample #text", false},
		{"Nonempty string with ending hashtag", "Sample text #", false},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			result := resource.Skip(tc.Line)
			require.EqualValues(t, tc.Expected, result)
		})
	}
}

func TestParse(t *testing.T) {
	tt := []struct {
		Name          string
		Line          string
		ExpectedKey   string
		ExpectedValue string
		OK            bool
	}{
		{"Empty string", "", "", "", false},
		{"No separator", "wrong", "", "", false},
		{"Empty key", "=john", "", "", false},
		{"Empty key value", "=", "", "", false},
		{"Empty value", "name=", "name", "", true},
		{"Key value 1", "name=john", "name", "john", true},
		{"Key value 2", "name=john=dev", "name", "john=dev", true},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			key, value, ok := resource.Parse(tc.Line)
			require.EqualValues(t, tc.ExpectedKey, key)
			require.EqualValues(t, tc.ExpectedValue, value)
			require.EqualValues(t, tc.OK, ok)
		})
	}
}

func TestUnquote(t *testing.T) {
	tt := []struct {
		Name     string
		Text     string
		Expected string
	}{
		{"Empty string", ``, ``},
		{"Single double quote", `"`, `"`},
		{"Single single quote", `'`, `'`},
		{"Empty double quotes", `""`, ``},
		{"Empty single quotes", `''`, ``},
		{"Empty mixed quotes 1", `"'`, `"'`},
		{"Empty mixed quotes 2", `'"`, `'"`},
		{"Double quotes", `"Sample text"`, `Sample text`},
		{"Single quotes", `'Sample text'`, `Sample text`},
		{"Half-open starting double quote", `"Sample text`, `"Sample text`},
		{"Half-open ending double quote", `Sample text"`, `Sample text"`},
		{"Half-open starting single quote", `'Sample text`, `'Sample text`},
		{"Half-open ending single quote", `Sample text'`, `Sample text'`},
		{"Double double quotes", `""Sample text""`, `"Sample text"`},
		{"Double single quotes", `''Sample text''`, `'Sample text'`},
		{"Mismatch quotes 1", `"Sample text'`, `"Sample text'`},
		{"Mismatch quotes 2", `'Sample text"`, `'Sample text"`},
		{"No quotes", `Sample text`, `Sample text`},
		{"Internal double quote", `Sample "text"`, `Sample "text"`},
		{"Internal single quote", `Sample 'text'`, `Sample 'text'`},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			result := resource.Unquote(tc.Text)
			require.EqualValues(t, tc.Expected, result)
		})
	}
}

func TestUnescape(t *testing.T) {
	tt := []struct {
		Name     string
		Text     string
		Expected string
	}{
		{"Empty string", ``, ``},
		{"Escaped dollar sign", `\$var`, `$var`},
		{"Escaped double quote", `\"var`, `"var`},
		{"Escaped single quote", `\'var`, `'var`},
		{"Escaped backslash", `\\var`, `\var`},
		{"Escaped backtick", "\\`var", "`var"},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			result := resource.Unescape(tc.Text)
			require.EqualValues(t, tc.Expected, result)
		})
	}
}

func TestBuildOSRelease(t *testing.T) {
	tt := []struct {
		Name     string
		Values   map[string]string
		Expected string
	}{
		{"Nil values", nil, ""},
		{"Empty values", map[string]string{}, ""},
		{"Name and version only", map[string]string{
			"NAME":    "Ubuntu",
			"VERSION": "20.04.2 LTS (Focal Fossa)",
		}, "Ubuntu 20.04.2 LTS (Focal Fossa)"},
		{"Name and version preferred", map[string]string{
			"NAME":        "Ubuntu",
			"VERSION":     "20.04.2 LTS (Focal Fossa)",
			"VERSION_ID":  "20.04",
			"PRETTY_NAME": "Ubuntu 20.04.2 LTS",
		}, "Ubuntu 20.04.2 LTS (Focal Fossa)"},
		{"Version ID fallback", map[string]string{
			"NAME":       "Ubuntu",
			"VERSION_ID": "20.04",
		}, "Ubuntu 20.04"},
		{"Pretty name fallback due to missing name", map[string]string{
			"VERSION":     "20.04.2 LTS (Focal Fossa)",
			"PRETTY_NAME": "Ubuntu 20.04.2 LTS",
		}, "Ubuntu 20.04.2 LTS"},
		{"Pretty name fallback due to missing version", map[string]string{
			"NAME":        "Ubuntu",
			"PRETTY_NAME": "Ubuntu 20.04.2 LTS",
		}, "Ubuntu 20.04.2 LTS"},
	}

	for _, tc := range tt {
		tc := tc

		t.Run(tc.Name, func(t *testing.T) {
			result := resource.BuildOSRelease(tc.Values)
			require.EqualValues(t, tc.Expected, result)
		})
	}
}