|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +//go:build linux |
| 5 | + |
| 6 | +package gopsutilenv |
| 7 | + |
| 8 | +import ( |
| 9 | + "context" |
| 10 | + "testing" |
| 11 | + |
| 12 | + "github.com/shirou/gopsutil/v4/common" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRootPaths(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + testName string |
| 19 | + rootPath string |
| 20 | + errMsg string |
| 21 | + }{ |
| 22 | + { |
| 23 | + testName: "valid path", |
| 24 | + rootPath: "testdata", |
| 25 | + }, |
| 26 | + { |
| 27 | + testName: "empty path", |
| 28 | + rootPath: "", |
| 29 | + }, |
| 30 | + { |
| 31 | + testName: "root path", |
| 32 | + rootPath: "/", |
| 33 | + }, |
| 34 | + { |
| 35 | + testName: "invalid path", |
| 36 | + rootPath: "invalidpath", |
| 37 | + errMsg: "invalid root_path: stat invalidpath: no such file or directory", |
| 38 | + }, |
| 39 | + } |
| 40 | + for _, test := range tests { |
| 41 | + t.Run(test.testName, func(t *testing.T) { |
| 42 | + err := ValidateRootPath(test.rootPath) |
| 43 | + if test.errMsg != "" { |
| 44 | + assert.EqualError(t, err, test.errMsg) |
| 45 | + } else { |
| 46 | + assert.NoError(t, err) |
| 47 | + } |
| 48 | + t.Cleanup(func() { SetGlobalRootPath("") }) |
| 49 | + }) |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func TestInconsistentRootPaths(t *testing.T) { |
| 54 | + SetGlobalRootPath("foo") |
| 55 | + err := ValidateRootPath("testdata") |
| 56 | + assert.EqualError(t, err, "inconsistent root_path configuration detected among components: `foo` != `testdata`") |
| 57 | + |
| 58 | + t.Cleanup(func() { SetGlobalRootPath("") }) |
| 59 | +} |
| 60 | + |
| 61 | +func TestSetGoPsutilEnvVars(t *testing.T) { |
| 62 | + envMap := SetGoPsutilEnvVars("testdata") |
| 63 | + expectedEnvMap := common.EnvMap{ |
| 64 | + common.HostDevEnvKey: "testdata/dev", |
| 65 | + common.HostEtcEnvKey: "testdata/etc", |
| 66 | + common.HostRunEnvKey: "testdata/run", |
| 67 | + common.HostSysEnvKey: "testdata/sys", |
| 68 | + common.HostVarEnvKey: "testdata/var", |
| 69 | + common.HostProcEnvKey: "testdata/proc", |
| 70 | + } |
| 71 | + assert.Equal(t, expectedEnvMap, envMap) |
| 72 | + ctx := context.WithValue(context.Background(), common.EnvKey, envMap) |
| 73 | + assert.Equal(t, "testdata/proc", GetEnvWithContext(ctx, string(common.HostProcEnvKey), "default")) |
| 74 | + assert.Equal(t, "testdata/sys", GetEnvWithContext(ctx, string(common.HostSysEnvKey), "default")) |
| 75 | + assert.Equal(t, "testdata/etc", GetEnvWithContext(ctx, string(common.HostEtcEnvKey), "default")) |
| 76 | + assert.Equal(t, "testdata/var", GetEnvWithContext(ctx, string(common.HostVarEnvKey), "default")) |
| 77 | + assert.Equal(t, "testdata/run", GetEnvWithContext(ctx, string(common.HostRunEnvKey), "default")) |
| 78 | + assert.Equal(t, "testdata/dev", GetEnvWithContext(ctx, string(common.HostDevEnvKey), "default")) |
| 79 | + |
| 80 | + t.Cleanup(func() { SetGlobalRootPath("") }) |
| 81 | +} |
| 82 | + |
| 83 | +func TestGetEnvWithContext(t *testing.T) { |
| 84 | + envMap := SetGoPsutilEnvVars("testdata") |
| 85 | + |
| 86 | + ctxEnv := context.WithValue(context.Background(), common.EnvKey, envMap) |
| 87 | + assert.Equal(t, "testdata/proc", GetEnvWithContext(ctxEnv, string(common.HostProcEnvKey), "default")) |
| 88 | + assert.Equal(t, "testdata/sys", GetEnvWithContext(ctxEnv, string(common.HostSysEnvKey), "default")) |
| 89 | + assert.Equal(t, "testdata/etc", GetEnvWithContext(ctxEnv, string(common.HostEtcEnvKey), "default")) |
| 90 | + assert.Equal(t, "testdata/var", GetEnvWithContext(ctxEnv, string(common.HostVarEnvKey), "default")) |
| 91 | + assert.Equal(t, "testdata/run", GetEnvWithContext(ctxEnv, string(common.HostRunEnvKey), "default")) |
| 92 | + assert.Equal(t, "testdata/dev", GetEnvWithContext(ctxEnv, string(common.HostDevEnvKey), "default")) |
| 93 | + |
| 94 | + assert.Equal(t, "/default", GetEnvWithContext(context.Background(), string(common.HostProcEnvKey), "/default")) |
| 95 | + assert.Equal(t, "/default/foo", GetEnvWithContext(context.Background(), string(common.HostProcEnvKey), "/default", "foo")) |
| 96 | + assert.Equal(t, "/default/foo/bar", GetEnvWithContext(context.Background(), string(common.HostProcEnvKey), "/default", "foo", "bar")) |
| 97 | + |
| 98 | + t.Cleanup(func() { SetGlobalRootPath("") }) |
| 99 | +} |
| 100 | + |
| 101 | +func TestSetGoPsutilEnvVarsInvalidRootPath(t *testing.T) { |
| 102 | + err := ValidateRootPath("invalidpath") |
| 103 | + assert.EqualError(t, err, "invalid root_path: stat invalidpath: no such file or directory") |
| 104 | + |
| 105 | + t.Cleanup(func() { SetGlobalRootPath("") }) |
| 106 | +} |
0 commit comments