1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-11-23 22:34:47 +02:00
Files
opentelemetry-go/sdk/resource/os_release_unix_test.go
renovate[bot] 56138d1060 fix(deps): update golang.org/x (#7482)
This PR contains the following updates:

| Package | Type | Update | Change | Age | Confidence |
|---|---|---|---|---|---|
| golang.org/x/exp | require | digest | `27f1f14` -> `90e834f` |
[![age](https://developer.mend.io/api/mc/badges/age/go/golang.org%2fx%2fexp/v0.0.0-20251017212417-90e834f514db?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/golang.org%2fx%2fexp/v0.0.0-20251002181428-27f1f14c8bb9/v0.0.0-20251017212417-90e834f514db?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| golang.org/x/exp/typeparams | indirect | digest | `27f1f14` ->
`90e834f` |
[![age](https://developer.mend.io/api/mc/badges/age/go/golang.org%2fx%2fexp%2ftypeparams/v0.0.0-20251017212417-90e834f514db?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/golang.org%2fx%2fexp%2ftypeparams/v0.0.0-20251002181428-27f1f14c8bb9/v0.0.0-20251017212417-90e834f514db?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| golang.org/x/net | indirect | minor | `v0.45.0` -> `v0.46.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/golang.org%2fx%2fnet/v0.46.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/golang.org%2fx%2fnet/v0.45.0/v0.46.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| golang.org/x/telemetry | indirect | digest | `ca0c2a9` -> `24f779f` |
[![age](https://developer.mend.io/api/mc/badges/age/go/golang.org%2fx%2ftelemetry/v0.0.0-20251014153721-24f779f6aaef?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/golang.org%2fx%2ftelemetry/v0.0.0-20251008162818-ca0c2a905e73/v0.0.0-20251014153721-24f779f6aaef?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
| golang.org/x/tools | require | minor | `v0.37.0` -> `v0.38.0` |
[![age](https://developer.mend.io/api/mc/badges/age/go/golang.org%2fx%2ftools/v0.38.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|
[![confidence](https://developer.mend.io/api/mc/badges/confidence/go/golang.org%2fx%2ftools/v0.37.0/v0.38.0?slim=true)](https://docs.renovatebot.com/merge-confidence/)
|

---

> [!WARNING]
> Some dependencies could not be looked up. Check the Dependency
Dashboard for more information.

---

### 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.

👻 **Immortal**: This PR will be recreated if closed unmerged. Get
[config
help](https://redirect.github.com/renovatebot/renovate/discussions) if
that's undesired.

---

- [ ] <!-- 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:eyJjcmVhdGVkSW5WZXIiOiI0MS4xMzEuOSIsInVwZGF0ZWRJblZlciI6IjQxLjE0My4xIiwidGFyZ2V0QnJhbmNoIjoibWFpbiIsImxhYmVscyI6WyJTa2lwIENoYW5nZWxvZyIsImRlcGVuZGVuY2llcyJdfQ==-->

---------

Co-authored-by: renovate[bot] <29139614+renovate[bot]@users.noreply.github.com>
Co-authored-by: Tyler Yahn <codingalias@gmail.com>
2025-10-20 09:09:34 -07:00

296 lines
8.2 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
//go: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.Equal(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.Equal(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.Equal(t, tc.ExpectedKey, key)
require.Equal(t, tc.ExpectedValue, value)
require.Equal(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.Equal(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.Equal(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.Equal(t, tc.Expected, result)
})
}
}