mirror of https://github.com/k3s-io/k3s
59 lines
1.8 KiB
Go
59 lines
1.8 KiB
Go
![]() |
// +build cgo,linux
|
||
|
|
||
|
/*
|
||
|
Copyright 2017 The Kubernetes 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://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.
|
||
|
*/
|
||
|
|
||
|
package cadvisor
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
info "github.com/google/cadvisor/info/v1"
|
||
|
"github.com/stretchr/testify/assert"
|
||
|
"k8s.io/apimachinery/pkg/api/resource"
|
||
|
utilfeature "k8s.io/apiserver/pkg/util/feature"
|
||
|
v1helper "k8s.io/kubernetes/pkg/apis/core/v1/helper"
|
||
|
"k8s.io/kubernetes/pkg/features"
|
||
|
"testing"
|
||
|
)
|
||
|
|
||
|
func TestCapacityFromMachineInfo(t *testing.T) {
|
||
|
machineInfo := &info.MachineInfo{
|
||
|
NumCores: 2,
|
||
|
MemoryCapacity: 2048,
|
||
|
HugePages: []info.HugePagesInfo{
|
||
|
{
|
||
|
PageSize: 5,
|
||
|
NumPages: 10,
|
||
|
},
|
||
|
},
|
||
|
}
|
||
|
|
||
|
// enable the features.HugePages
|
||
|
utilfeature.DefaultFeatureGate.Set(fmt.Sprintf("%s=true", features.HugePages))
|
||
|
|
||
|
resourceList := CapacityFromMachineInfo(machineInfo)
|
||
|
|
||
|
// assert the cpu and memory
|
||
|
assert.EqualValues(t, machineInfo.NumCores*1000, resourceList.Cpu().MilliValue(), "unexpected CPU value")
|
||
|
assert.EqualValues(t, machineInfo.MemoryCapacity, resourceList.Memory().Value(), "unexpected memory value")
|
||
|
|
||
|
// assert the hugepage
|
||
|
hugePageKey := int64(5 * 1024)
|
||
|
value, found := resourceList[v1helper.HugePageResourceName(*resource.NewQuantity(hugePageKey, resource.BinarySI))]
|
||
|
assert.True(t, found, "hugepage not found")
|
||
|
assert.EqualValues(t, hugePageKey*10, value.Value(), "unexpected hugepage value")
|
||
|
}
|