2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2019-10-03 16:43:52 +00:00
|
|
|
package hostutil
|
|
|
|
|
|
|
|
import (
|
2019-11-01 17:12:22 +00:00
|
|
|
"context"
|
|
|
|
"strings"
|
2019-10-03 16:43:52 +00:00
|
|
|
"testing"
|
2019-11-01 17:12:22 +00:00
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
2019-10-03 16:43:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestCollectHostInfo(t *testing.T) {
|
2019-11-01 17:12:22 +00:00
|
|
|
info, err := CollectHostInfo(context.Background())
|
|
|
|
if err != nil && !errwrap.ContainsType(err, new(HostInfoError)) {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
|
|
|
|
// Get all the possible HostInfoError errors and check for the resulting
|
|
|
|
// stat if the package is able to fetch it for the platform we're testing
|
|
|
|
// on.
|
|
|
|
errs := errwrap.GetAllType(err, new(HostInfoError))
|
|
|
|
|
2019-10-03 16:43:52 +00:00
|
|
|
if info.Timestamp.IsZero() {
|
|
|
|
t.Fatal("expected non-zero Timestamp")
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
if !checkErrTypeExists(errs, "cpu") && info.CPU == nil {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal("expected non-nil CPU value")
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
if !checkErrTypeExists(errs, "cpu_times") && info.CPUTimes == nil {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal("expected non-nil CPUTimes value")
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
if !checkErrTypeExists(errs, "disk") && info.Disk == nil {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal("expected non-nil Disk value")
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
if !checkErrTypeExists(errs, "host") && info.Host == nil {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal("expected non-nil Host value")
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
if !checkErrTypeExists(errs, "memory") && info.Memory == nil {
|
2019-10-03 16:43:52 +00:00
|
|
|
t.Fatal("expected non-nil Memory value")
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 17:12:22 +00:00
|
|
|
|
|
|
|
// checkErrTypeExists is a helper that checks whether an particular
|
|
|
|
// HostInfoError.Type exists within a set of errors.
|
|
|
|
func checkErrTypeExists(errs []error, errType string) bool {
|
|
|
|
for _, e := range errs {
|
|
|
|
err, ok := e.(*HostInfoError)
|
|
|
|
if !ok {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is mainly for disk error since the type string can contain an
|
|
|
|
// index for the disk.
|
|
|
|
parts := strings.SplitN(err.Type, ".", 2)
|
|
|
|
|
|
|
|
if parts[0] == errType {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|