| 1 | // Copyright (c) 2022 Uber Technologies, Inc. |
| 2 | // |
| 3 | // Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | // of this software and associated documentation files (the "Software"), to deal |
| 5 | // in the Software without restriction, including without limitation the rights |
| 6 | // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | // copies of the Software, and to permit persons to whom the Software is |
| 8 | // furnished to do so, subject to the following conditions: |
| 9 | // |
| 10 | // The above copyright notice and this permission notice shall be included in |
| 11 | // all copies or substantial portions of the Software. |
| 12 | // |
| 13 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | // THE SOFTWARE. |
| 20 | |
| 21 | //go:build linux |
| 22 | // +build linux |
| 23 | |
| 24 | package runtime |
| 25 | |
| 26 | import ( |
| 27 | "errors" |
| 28 | "fmt" |
| 29 | "testing" |
| 30 | |
| 31 | "github.com/prashantv/gostub" |
| 32 | "github.com/stretchr/testify/assert" |
| 33 | "github.com/stretchr/testify/require" |
| 34 | "go.uber.org/automaxprocs/internal/cgroups" |
| 35 | ) |
| 36 | |
| 37 | func TestNewQueryer(t *testing.T) { |
| 38 | t.Run("use v2", func(t *testing.T) { |
| 39 | stubs := newStubs(t) |
| 40 | |
| 41 | c2 := new(cgroups.CGroups2) |
| 42 | stubs.StubFunc(&_newCgroups2, c2, nil) |
| 43 | |
| 44 | got, err := newQueryer() |
| 45 | require.NoError(t, err) |
| 46 | assert.Same(t, c2, got) |
| 47 | }) |
| 48 | |
| 49 | t.Run("v2 error", func(t *testing.T) { |
| 50 | stubs := newStubs(t) |
| 51 | |
| 52 | giveErr := errors.New("great sadness") |
| 53 | stubs.StubFunc(&_newCgroups2, nil, giveErr) |
| 54 | |
| 55 | _, err := newQueryer() |
| 56 | assert.ErrorIs(t, err, giveErr) |
| 57 | }) |
| 58 | |
| 59 | t.Run("use v1", func(t *testing.T) { |
| 60 | stubs := newStubs(t) |
| 61 | |
| 62 | stubs.StubFunc(&_newCgroups2, nil, |
| 63 | fmt.Errorf("not v2: %w", cgroups.ErrNotV2)) |
| 64 | |
| 65 | c1 := make(cgroups.CGroups) |
| 66 | stubs.StubFunc(&_newCgroups, c1, nil) |
| 67 | |
| 68 | got, err := newQueryer() |
| 69 | require.NoError(t, err) |
| 70 | assert.IsType(t, c1, got, "must be a v1 cgroup") |
| 71 | }) |
| 72 | |
| 73 | t.Run("v1 error", func(t *testing.T) { |
| 74 | stubs := newStubs(t) |
| 75 | |
| 76 | stubs.StubFunc(&_newCgroups2, nil, cgroups.ErrNotV2) |
| 77 | |
| 78 | giveErr := errors.New("great sadness") |
| 79 | stubs.StubFunc(&_newCgroups, nil, giveErr) |
| 80 | |
| 81 | _, err := newQueryer() |
| 82 | assert.ErrorIs(t, err, giveErr) |
| 83 | }) |
| 84 | } |
| 85 | |
| 86 | func newStubs(t *testing.T) *gostub.Stubs { |
| 87 | stubs := gostub.New() |
| 88 | t.Cleanup(stubs.Reset) |
| 89 | return stubs |
| 90 | } |