forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
host_linux_test.go
139 lines (132 loc) · 3.29 KB
/
host_linux_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright 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:https://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.
//go:build linux
// +build linux
// Taken from https://github.com/signalfx/golib/blob/master/metadata/hostmetadata/host-linux_test.go as is.
package hostmetadata
import (
"errors"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"golang.org/x/sys/unix"
)
func TestFillOSSpecificData(t *testing.T) {
type args struct {
syscallUname func(*unix.Utsname) error
etc string
}
tests := []struct {
name string
args args
want *hostOS
wantErr bool
}{
{
name: "get uname os information",
args: args{
etc: "./testdata/lsb-release",
syscallUname: func(in *unix.Utsname) error {
in.Version = [65]byte{35, 57, 45, 85, 98, 117, 110, 116,
117, 32, 83, 77, 80, 32, 87, 101, 100,
32, 77, 97, 121, 32, 49, 54, 32, 49,
53, 58, 50, 50, 58, 53, 52, 32, 85,
84, 67, 32, 50, 48, 49, 56}
return nil
},
},
want: &hostOS{
HostKernelVersion: "#9-Ubuntu SMP Wed May 16 15:22:54 UTC 2018",
HostLinuxVersion: "Ubuntu 18.04 LTS",
},
},
{
name: "get uname os information uname call fails",
args: args{
etc: "./testdata/lsb-release",
syscallUname: func(in *unix.Utsname) error {
in.Version = [65]byte{}
return errors.New("shouldn't work")
},
},
want: &hostOS{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
syscallUname = tt.args.syscallUname
t.Setenv("HOST_ETC", tt.args.etc)
in := &hostOS{}
err := fillPlatformSpecificOSData(in)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, in)
})
syscallUname = unix.Uname
}
}
func TestFillPlatformSpecificCPUData(t *testing.T) {
type args struct {
syscallUname func(*unix.Utsname) error
}
tests := []struct {
name string
args args
want *hostCPU
wantErr bool
}{
{
name: "get uname cpu information",
args: args{
syscallUname: func(in *unix.Utsname) error {
in.Machine = [65]byte{120, 56, 54, 95, 54, 52}
return nil
},
},
want: &hostCPU{
HostMachine: "x86_64",
HostProcessor: "x86_64",
},
},
{
name: "get uname cpu information and the call to uname fails",
args: args{
syscallUname: func(in *unix.Utsname) error {
in.Machine = [65]byte{}
return errors.New("shouldn't work")
},
},
want: &hostCPU{},
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
syscallUname = tt.args.syscallUname
in := &hostCPU{}
err := fillPlatformSpecificCPUData(in)
if tt.wantErr {
assert.Error(t, err)
return
}
require.NoError(t, err)
assert.Equal(t, tt.want, in)
})
syscallUname = unix.Uname
}
}