1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2024-12-10 09:50:58 +02:00
opentelemetry-go/sdk/resource/host_id_test.go
Matthew Wear 282a47e3d3
add host.id to resource auto-detection (#3812)
* add platform specific hostIDReaders

* add WithHostID option to Resource

* add changelog entry

* Apply suggestions from code review

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

* linting

* combine platform specific readers and tests

This allows us to run tests for the BSD, Darwin, and Linux readers
on all platforms.

* add todo to use assert.AnError after resource.Detect error handling is updated

* move HostID test utilities to host_id_test

* return assert.AnError from mockHostIDProviderWithError

* use assert.ErrorIs

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>

---------

Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com>
Co-authored-by: Aaron Clawson <3766680+MadVikingGod@users.noreply.github.com>
2023-03-21 14:45:30 -05:00

223 lines
6.2 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.
package resource
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
)
var (
expectedHostID = "f2c668b579780554f70f72a063dc0864"
readFileNoError = func(filename string) (string, error) {
return expectedHostID + "\n", nil
}
readFileError = func(filename string) (string, error) {
return "", errors.New("not found")
}
execCommandNoError = func(string, ...string) (string, error) {
return expectedHostID + "\n", nil
}
execCommandError = func(string, ...string) (string, error) {
return "", errors.New("not found")
}
)
func SetDefaultHostIDProvider() {
SetHostIDProvider(defaultHostIDProvider)
}
func SetHostIDProvider(hostIDProvider hostIDProvider) {
hostID = hostIDProvider
}
func TestHostIDReaderBSD(t *testing.T) {
tt := []struct {
name string
fileReader fileReader
commandExecutor commandExecutor
expectedHostID string
expectError bool
}{
{
name: "hostIDReaderBSD valid primary",
fileReader: readFileNoError,
commandExecutor: execCommandError,
expectedHostID: expectedHostID,
expectError: false,
},
{
name: "hostIDReaderBSD invalid primary",
fileReader: readFileError,
commandExecutor: execCommandNoError,
expectedHostID: expectedHostID,
expectError: false,
},
{
name: "hostIDReaderBSD invalid primary and secondary",
fileReader: readFileError,
commandExecutor: execCommandError,
expectedHostID: "",
expectError: true,
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
reader := hostIDReaderBSD{
readFile: tc.fileReader,
execCommand: tc.commandExecutor,
}
hostID, err := reader.read()
require.Equal(t, tc.expectError, err != nil)
require.Equal(t, tc.expectedHostID, hostID)
})
}
}
func TestHostIDReaderLinux(t *testing.T) {
readFilePrimaryError := func(filename string) (string, error) {
if filename == "/var/lib/dbus/machine-id" {
return readFileNoError(filename)
}
return readFileError(filename)
}
tt := []struct {
name string
fileReader fileReader
expectedHostID string
expectError bool
}{
{
name: "hostIDReaderLinux valid primary",
fileReader: readFileNoError,
expectedHostID: expectedHostID,
expectError: false,
},
{
name: "hostIDReaderLinux invalid primary",
fileReader: readFilePrimaryError,
expectedHostID: expectedHostID,
expectError: false,
},
{
name: "hostIDReaderLinux invalid primary and secondary",
fileReader: readFileError,
expectedHostID: "",
expectError: true,
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
reader := hostIDReaderLinux{
readFile: tc.fileReader,
}
hostID, err := reader.read()
require.Equal(t, tc.expectError, err != nil)
require.Equal(t, tc.expectedHostID, hostID)
})
}
}
func TestHostIDReaderDarwin(t *testing.T) {
validOutput := `+-o J316sAP <class IOPlatformExpertDevice, id 0x10000024d, registered, matched, active, busy 0 (132196 ms), retain 37>
{
"IOPolledInterface" = "AppleARMWatchdogTimerHibernateHandler is not serializable"
"#address-cells" = <02000000>
"AAPL,phandle" = <01000000>
"serial-number" = <94e1c79ec04cd3f153f600000000000000000000000000000000000000000000>
"IOBusyInterest" = "IOCommand is not serializable"
"target-type" = <"J316s">
"platform-name" = <7436303030000000000000000000000000000000000000000000000000000000>
"secure-root-prefix" = <"md">
"name" = <"device-tree">
"region-info" = <4c4c2f4100000000000000000000000000000000000000000000000000000000>
"manufacturer" = <"Apple Inc.">
"compatible" = <"J316sAP","MacBookPro18,1","AppleARM">
"config-number" = <00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000>
"IOPlatformSerialNumber" = "HDWLIF2LM7"
"regulatory-model-number" = <4132343835000000000000000000000000000000000000000000000000000000>
"time-stamp" = <"Fri Aug 5 20:25:38 PDT 2022">
"clock-frequency" = <00366e01>
"model" = <"MacBookPro18,1">
"mlb-serial-number" = <5c92d268d6cd789e475ffafc0d363fc950000000000000000000000000000000>
"model-number" = <5a31345930303136430000000000000000000000000000000000000000000000>
"IONWInterrupts" = "IONWInterrupts"
"model-config" = <"ICT;MoPED=0x03D053A605C84ED11C455A18D6C643140B41A239">
"device_type" = <"bootrom">
"#size-cells" = <02000000>
"IOPlatformUUID" = "81895B8D-9EF9-4EBB-B5DE-B00069CF53F0"
}
`
execCommandValid := func(string, ...string) (string, error) {
return validOutput, nil
}
execCommandInvalid := func(string, ...string) (string, error) {
return "wasn't expecting this", nil
}
tt := []struct {
name string
fileReader fileReader
commandExecutor commandExecutor
expectedHostID string
expectError bool
}{
{
name: "hostIDReaderDarwin valid output",
commandExecutor: execCommandValid,
expectedHostID: "81895B8D-9EF9-4EBB-B5DE-B00069CF53F0",
expectError: false,
},
{
name: "hostIDReaderDarwin invalid output",
commandExecutor: execCommandInvalid,
expectedHostID: "",
expectError: true,
},
{
name: "hostIDReaderDarwin error",
commandExecutor: execCommandError,
expectedHostID: "",
expectError: true,
},
}
for _, tc := range tt {
tc := tc
t.Run(tc.name, func(t *testing.T) {
reader := hostIDReaderDarwin{
execCommand: tc.commandExecutor,
}
hostID, err := reader.read()
require.Equal(t, tc.expectError, err != nil)
require.Equal(t, tc.expectedHostID, hostID)
})
}
}