mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-14 10:13:10 +02:00
484258eb36
* Added Linux-specific detector for the os.description attribute * Generalized OS description detector with placeholder function for unimplemented OSes * Extended osDescription function to *nix OSes based on golang.org/x/sys/unix * Added WithOS resource configuration function to configure all of the OS resource attributes * Implemented osDescription funtion for Windows OS * Improved documentation header for *nix version of the osDescription function * Added support for reading os-release file * Added/updated documentation headers for *nix implementation of osDescription and related functions * Changelog update * Added support for reading macOS version information * Mock approach to test OS description attribute * Extracted common function getFirstAvailableFile to read the first available file from a list of candidates * Upgraded golang.org/x/sys * Changelog update * Fixed wrong function name in documentation header for WithOSDescription * Updated documentation header for platformOSDescription function * Renamed restoreProcessAttributesProviders test helper function The function restoreProcessAttributesProviders was renamed to simply restoreAttributesProviders to better reflect its broader scope, which not only applies to process attribute's providers. * Fixed os_linux.go overriding build tags defined inside the file The suffix on os_linux.go was overriding the build tags already defined in that file. The file was renamed to os_release_unix.go, reflecting the main function defined in the file. For consistency, os_darwin.go was renamed to os_release_darwing.go, as its primary purpose is to also define the osRelease function. * Removed use of discontinued function resource.WithoutBuiltin * Added PR number to changelog entries * Updated go.sum files after run of make lint * Linux implementation: ignore lines with an empty key * Linux implementation: avoid unquoting strings less than two chars * WIP: added tests for Linux support functions * WIP: added tests for charsToString and getFirstAvailableFile functions * Replaced os.CreateTemp with ioutil.TempFile as the former only exists in Go 1.16 * Added unameProvider type to decouple direct reference to unix.Uname function inside Uname() * Added tests for Uname() function * Replaced *os.File with io.Reader in parseOSReleaseFile to ease testing * Added tests for parseOSReleaseFile function * Darwin implementation: added tests for buildOSRelease function * Replaced *os.File with io.Reader in parsePlistFile to ease testing * Darwin implementation: added tests for parsePlistFile function * Type in documentation header for Linux osRelease function * Extracted logic for reading specific registry values into helper functions * Added basic tests for Windows version of platformOSDescription and helper functions * Manually formatted uint64 to strings to have an uniform interface for test assertions * Asserts there's no error when opening registry key for testing Co-authored-by: Robert Pająk <pellared@hotmail.com> * Simplified subtests by using a single test with multiple asserts * go.sum update after running make * Fix typo Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * WIP: added placeholder implementation of platformOSDescription for unsupported OSes * Fixed typo on osRelease documentation header Co-authored-by: Chris Bandy <bandy.chris@gmail.com> * Fixed typo on test case name for ParsePlistFile tests Co-authored-by: Chris Bandy <bandy.chris@gmail.com> * Linter fix in changelog * go.sum updates after running make * Used strings.Replacer instead of multiple strings.ReplaceAll calls * Optimized implementation of charsToString * Safer temporary file deletion with t.TempDir() * Used t.Cleanup() for safer mocking of runtime providers in OS resource tests * Handled optionality of DisplayVersion registry key. For example, CI machine runs on: Windows Server 2019 Datacenter (1809) [Version 10.0.17763.1999] So, to not add an extra white space due to missing DisplayVersion, this value is checked to be not empty, and only in such case a trailing space is added for that component. * Workaround to handle the case of DisplayVersion registry key not present * Excluded unsupported GOOSes by negation of supported ones * go.sum update after running make Co-authored-by: Anthony Mirabella <a9@aneurysm9.com> Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Chris Bandy <bandy.chris@gmail.com>
307 lines
8.7 KiB
Go
307 lines
8.7 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
// +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)
|
|
})
|
|
}
|
|
}
|