2021-10-22 12:49:10 +00:00
|
|
|
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
|
|
|
|
//
|
|
|
|
// 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://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.
|
|
|
|
|
2022-08-04 03:04:34 +00:00
|
|
|
//go:build linux
|
|
|
|
// +build linux
|
|
|
|
|
2022-10-14 09:51:24 +00:00
|
|
|
package hardware
|
2021-10-22 12:49:10 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-14 02:24:52 +00:00
|
|
|
"os"
|
2021-10-22 12:49:10 +00:00
|
|
|
"testing"
|
|
|
|
|
2024-01-12 02:30:51 +00:00
|
|
|
"github.com/containerd/cgroups/v3"
|
2021-10-22 12:49:10 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2024-01-12 02:30:51 +00:00
|
|
|
func TestGetCgroupStats(t *testing.T) {
|
|
|
|
if cgroups.Mode() == cgroups.Unified {
|
|
|
|
stats2, err := getCgroupV2Stats()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, stats2)
|
|
|
|
|
|
|
|
stats1, err := getCgroupV1Stats()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, stats1)
|
|
|
|
} else {
|
|
|
|
stats1, err := getCgroupV1Stats()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotNil(t, stats1)
|
|
|
|
|
|
|
|
stats2, err := getCgroupV2Stats()
|
|
|
|
assert.Error(t, err)
|
|
|
|
assert.Nil(t, stats2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-22 12:49:10 +00:00
|
|
|
func TestGetContainerMemLimit(t *testing.T) {
|
2021-12-16 09:25:13 +00:00
|
|
|
limit, err := getContainerMemLimit()
|
2021-10-22 12:49:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, limit > 0)
|
|
|
|
t.Log("limit memory:", limit)
|
2024-01-14 02:24:52 +00:00
|
|
|
|
|
|
|
err = os.Setenv("MEM_LIMIT", "5Gi")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
defer func() {
|
|
|
|
_ = os.Unsetenv("MEM_LIMIT")
|
|
|
|
assert.Equal(t, "", os.Getenv("MEM_LIMIT"))
|
|
|
|
}()
|
|
|
|
|
|
|
|
limit, err = getContainerMemLimit()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, limit, 5*1024*1024*1024)
|
2021-10-22 12:49:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetContainerMemUsed(t *testing.T) {
|
2021-12-16 09:25:13 +00:00
|
|
|
used, err := getContainerMemUsed()
|
2021-10-22 12:49:10 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.True(t, used > 0)
|
|
|
|
t.Log("used memory:", used)
|
|
|
|
}
|