diff --git a/pkg/kubelet/cadvisor/BUILD b/pkg/kubelet/cadvisor/BUILD index 3c430e1ed6b..86d32564ce2 100644 --- a/pkg/kubelet/cadvisor/BUILD +++ b/pkg/kubelet/cadvisor/BUILD @@ -96,6 +96,7 @@ go_test( srcs = select({ "@io_bazel_rules_go//go/platform:linux": [ "cadvisor_linux_test.go", + "util_test.go", ], "//conditions:default": [], }), @@ -103,9 +104,14 @@ go_test( importpath = "k8s.io/kubernetes/pkg/kubelet/cadvisor", deps = select({ "@io_bazel_rules_go//go/platform:linux": [ + "//pkg/apis/core/v1/helper:go_default_library", + "//pkg/features:go_default_library", "//pkg/kubelet/types:go_default_library", "//vendor/github.com/google/cadvisor/info/v1:go_default_library", "//vendor/github.com/google/cadvisor/metrics:go_default_library", + "//vendor/github.com/stretchr/testify/assert:go_default_library", + "//vendor/k8s.io/apimachinery/pkg/api/resource:go_default_library", + "//vendor/k8s.io/apiserver/pkg/util/feature:go_default_library", ], "//conditions:default": [], }), diff --git a/pkg/kubelet/cadvisor/util_test.go b/pkg/kubelet/cadvisor/util_test.go new file mode 100644 index 00000000000..dd8657f9341 --- /dev/null +++ b/pkg/kubelet/cadvisor/util_test.go @@ -0,0 +1,58 @@ +// +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") +}