1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-06-23 00:07:52 +02:00
Files
opentelemetry-go/sdk/resource/os_test.go

56 lines
1.4 KiB
Go
Raw Normal View History

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package resource_test
import (
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/sdk/resource"
semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)
func mockRuntimeProviders() {
resource.SetRuntimeProviders(
fakeRuntimeNameProvider,
fakeRuntimeVersionProvider,
func() string { return "LINUX" },
fakeRuntimeArchProvider,
)
OS description attribute detector (#1840) * 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>
2021-07-08 20:35:27 +00:00
resource.SetOSDescriptionProvider(
func() (string, error) { return "Test", nil },
)
}
func TestMapRuntimeOSToSemconvOSType(t *testing.T) {
tt := []struct {
Name string
Goos string
OSType attribute.KeyValue
}{
{"Apple Darwin", "darwin", semconv.OSTypeDarwin},
{"DragonFly BSD", "dragonfly", semconv.OSTypeDragonflyBSD},
{"FreeBSD", "freebsd", semconv.OSTypeFreeBSD},
{"Linux", "linux", semconv.OSTypeLinux},
{"NetBSD", "netbsd", semconv.OSTypeNetBSD},
{"OpenBSD", "openbsd", semconv.OSTypeOpenBSD},
{"Oracle Solaris", "solaris", semconv.OSTypeSolaris},
{"Microsoft Windows", "windows", semconv.OSTypeWindows},
{"Unknown", "unknown", semconv.OSTypeKey.String("unknown")},
{"UNKNOWN", "UNKNOWN", semconv.OSTypeKey.String("unknown")},
}
for _, tc := range tt {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
osTypeAttribute := resource.MapRuntimeOSToSemconvOSType(tc.Goos)
chore(deps): update module github.com/antonboom/testifylint to v1.6.0 (#6440) This PR contains the following updates: | Package | Change | Age | Adoption | Passing | Confidence | |---|---|---|---|---|---| | [github.com/Antonboom/testifylint](https://redirect.github.com/Antonboom/testifylint) | `v1.5.2` -> `v1.6.0` | [![age](https://developer.mend.io/api/mc/badges/age/go/github.com%2fAntonboom%2ftestifylint/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![adoption](https://developer.mend.io/api/mc/badges/adoption/go/github.com%2fAntonboom%2ftestifylint/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![passing](https://developer.mend.io/api/mc/badges/compatibility/go/github.com%2fAntonboom%2ftestifylint/v1.5.2/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | [![confidence](https://developer.mend.io/api/mc/badges/confidence/go/github.com%2fAntonboom%2ftestifylint/v1.5.2/v1.6.0?slim=true)](https://docs.renovatebot.com/merge-confidence/) | --- > [!WARNING] > Some dependencies could not be looked up. Check the Dependency Dashboard for more information. --- ### Release Notes <details> <summary>Antonboom/testifylint (github.com/Antonboom/testifylint)</summary> ### [`v1.6.0`](https://redirect.github.com/Antonboom/testifylint/releases/tag/v1.6.0): – new `equal-values` and `suite-method-signature` [Compare Source](https://redirect.github.com/Antonboom/testifylint/compare/v1.5.2...v1.6.0) #### What's Changed ##### New checkers - new checker `equal-values` by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/223](https://redirect.github.com/Antonboom/testifylint/pull/223) - new checker `suite-method-signature` by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/228](https://redirect.github.com/Antonboom/testifylint/pull/228) ##### New features - `len`: support len-len and value-len cases by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/204](https://redirect.github.com/Antonboom/testifylint/pull/204) - `error-is-as`: support NotErrorAs by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/219](https://redirect.github.com/Antonboom/testifylint/pull/219) - `useless-assert`: add NotElementsMatch and NotErrorAs by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/220](https://redirect.github.com/Antonboom/testifylint/pull/220) - `formatter`: support non-string-message checks by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/221](https://redirect.github.com/Antonboom/testifylint/pull/221) - `formatter`: warn on empty message by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/225](https://redirect.github.com/Antonboom/testifylint/pull/225) - `empty`: support empty strings, Zero for strings and len + bubbled useless-assert cases by [@&#8203;ccoVeille](https://redirect.github.com/ccoVeille) in [https://github.com/Antonboom/testifylint/pull/129](https://redirect.github.com/Antonboom/testifylint/pull/129) ##### New fixes - `negative-positive`: remove untyping, ignore Negative for len comparisons by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/226](https://redirect.github.com/Antonboom/testifylint/pull/226) - fixes: support `assert.CollectT` by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/233](https://redirect.github.com/Antonboom/testifylint/pull/233) ##### Bump deps - Upgrade testdata to v1.10.0 of testify by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/218](https://redirect.github.com/Antonboom/testifylint/pull/218) - Go 1.24 by [@&#8203;Antonboom](https://redirect.github.com/Antonboom) in [https://github.com/Antonboom/testifylint/pull/234](https://redirect.github.com/Antonboom/testifylint/pull/234) - build(deps): bump golang.org/x/tools from 0.26.0 to 0.27.0 by [@&#8203;dependabot](https://redirect.github.com/dependabot) in [https://github.com/Antonboom/testifylint/pull/206](https://redirect.github.com/Antonboom/testifylint/pull/206) - build(deps): bump rlespinasse/github-slug-action from 4.4.1 to 5.0.0 by [@&#8203;dependabot](https://redirect.github.com/dependabot) in [https://github.com/Antonboom/testifylint/pull/207](https://redirect.github.com/Antonboom/testifylint/pull/207) - build(deps): bump golang.org/x/tools from 0.27.0 to 0.29.0 by [@&#8203;dependabot](https://redirect.github.com/dependabot) in [https://github.com/Antonboom/testifylint/pull/214](https://redirect.github.com/Antonboom/testifylint/pull/214) **Full Changelog**: https://github.com/Antonboom/testifylint/compare/v1.5.2...v1.6.0 </details> --- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR is behind base branch, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/open-telemetry/opentelemetry-go). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiIzOS4yMDAuMiIsInVwZGF0ZWRJblZlciI6IjM5LjIwMC4yIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJTa2lwIENoYW5nZWxvZyIsImRlcGVuZGVuY2llcyJdfQ==--> --------- Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Tyler Yahn <codingalias@gmail.com>
2025-03-13 14:31:12 -07:00
require.Equal(t, osTypeAttribute, tc.OSType)
})
}
}