...

Source file src/go.opentelemetry.io/otel/sdk/resource/os_unix_test.go

Documentation: go.opentelemetry.io/otel/sdk/resource

     1  // Copyright The OpenTelemetry Authors
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  //go:build aix || darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || zos
    16  // +build aix darwin dragonfly freebsd linux netbsd openbsd solaris zos
    17  
    18  package resource_test
    19  
    20  import (
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/stretchr/testify/require"
    26  	"golang.org/x/sys/unix"
    27  
    28  	"go.opentelemetry.io/otel/sdk/resource"
    29  )
    30  
    31  func fakeUnameProvider(buf *unix.Utsname) error {
    32  	copy(buf.Sysname[:], "Mock OS")
    33  	copy(buf.Nodename[:], "DESKTOP-PC")
    34  	copy(buf.Release[:], "5.0.0")
    35  	copy(buf.Version[:], "#1 SMP Thu May 6 12:34:56 UTC 2021")
    36  	copy(buf.Machine[:], "x86_64")
    37  
    38  	return nil
    39  }
    40  
    41  func fakeUnameProviderWithError(buf *unix.Utsname) error {
    42  	return fmt.Errorf("error invoking uname(2)")
    43  }
    44  
    45  func TestUname(t *testing.T) {
    46  	resource.SetUnameProvider(fakeUnameProvider)
    47  
    48  	uname, err := resource.Uname()
    49  
    50  	require.Equal(t, uname, "Mock OS DESKTOP-PC 5.0.0 #1 SMP Thu May 6 12:34:56 UTC 2021 x86_64")
    51  	require.NoError(t, err)
    52  
    53  	resource.SetDefaultUnameProvider()
    54  }
    55  
    56  func TestUnameError(t *testing.T) {
    57  	resource.SetUnameProvider(fakeUnameProviderWithError)
    58  
    59  	uname, err := resource.Uname()
    60  
    61  	require.Empty(t, uname)
    62  	require.Error(t, err)
    63  
    64  	resource.SetDefaultUnameProvider()
    65  }
    66  
    67  func TestGetFirstAvailableFile(t *testing.T) {
    68  	tempDir := t.TempDir()
    69  
    70  	file1, _ := os.CreateTemp(tempDir, "candidate_")
    71  	file2, _ := os.CreateTemp(tempDir, "candidate_")
    72  
    73  	filename1, filename2 := file1.Name(), file2.Name()
    74  
    75  	tt := []struct {
    76  		Name             string
    77  		Candidates       []string
    78  		ExpectedFileName string
    79  		ExpectedErr      string
    80  	}{
    81  		{"Gets first, skip second candidate", []string{filename1, filename2}, filename1, ""},
    82  		{"Skips first, gets second candidate", []string{"does_not_exists", filename2}, filename2, ""},
    83  		{"Skips first, gets second, ignores third candidate", []string{"does_not_exists", filename2, filename1}, filename2, ""},
    84  		{"No candidates (empty slice)", []string{}, "", "no candidate file available: []"},
    85  		{"No candidates (nil slice)", nil, "", "no candidate file available: []"},
    86  		{"Single nonexisting candidate", []string{"does_not_exists"}, "", "no candidate file available: [does_not_exists]"},
    87  		{"Multiple nonexisting candidates", []string{"does_not_exists", "this_either"}, "", "no candidate file available: [does_not_exists this_either]"},
    88  	}
    89  
    90  	for _, tc := range tt {
    91  		tc := tc
    92  
    93  		t.Run(tc.Name, func(t *testing.T) {
    94  			file, err := resource.GetFirstAvailableFile(tc.Candidates)
    95  
    96  			filename := ""
    97  			if file != nil {
    98  				filename = file.Name()
    99  			}
   100  
   101  			errString := ""
   102  			if err != nil {
   103  				errString = err.Error()
   104  			}
   105  
   106  			require.Equal(t, tc.ExpectedFileName, filename)
   107  			require.Equal(t, tc.ExpectedErr, errString)
   108  		})
   109  	}
   110  }
   111  

View as plain text