Created
November 9, 2021 11:27
-
-
Save shivanshuraj1333/3e7b7cda9fad55c2065900cd30f75781 to your computer and use it in GitHub Desktop.
unit test for `formatter.go` in `kubernetes/pkg/scheduler/internal/cache/debugger`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Copyright 2018 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 debugger | |
import ( | |
"flag" | |
"fmt" | |
"github.com/go-logr/logr" | |
"k8s.io/api/core/v1" | |
"k8s.io/apimachinery/pkg/api/resource" | |
"k8s.io/apimachinery/pkg/types" | |
"k8s.io/component-base/config" | |
"k8s.io/component-base/logs" | |
_ "k8s.io/component-base/logs/json/register" | |
"k8s.io/klog/v2" | |
"k8s.io/kubernetes/pkg/scheduler/framework" | |
"k8s.io/kubernetes/pkg/scheduler/internal/cache" | |
_ "k8s.io/kubernetes/pkg/scheduler/internal/cache" | |
"os" | |
"strconv" | |
"testing" | |
//"testing" | |
//"reflect" | |
//"strings" | |
//"testing" | |
// | |
//"github.com/google/go-cmp/cmp" | |
//v1 "k8s.io/api/core/v1" | |
//"k8s.io/apimachinery/pkg/api/resource" | |
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | |
//"k8s.io/apimachinery/pkg/types" | |
//"k8s.io/apimachinery/pkg/util/sets" | |
) | |
func init() { | |
klog.InitFlags(nil) | |
} | |
func TestExample(t *testing.T) { | |
var _ logr.Marshaler = formatPod{} | |
var _ logr.Marshaler = formatNode{} | |
var podSlice []*v1.Pod | |
for i := 0; i < 3; i++ { | |
pod := &v1.Pod{} | |
pod.UID = types.UID("some-uid" + strconv.Itoa(i)) | |
pod.Status.Phase = v1.PodPhase("pd-phase" + strconv.Itoa(i)) | |
pod.Namespace = "ns" + strconv.Itoa(i) | |
pod.Name = "pod-name" + strconv.Itoa(i) | |
pod.Status.NominatedNodeName = "nom" + strconv.Itoa(i) | |
podSlice = append(podSlice, pod) | |
} | |
var dumper cache.Dump | |
nodeName := "test-node" | |
dumper.Nodes = make(map[string]*framework.NodeInfo) | |
dumper.Nodes["testNode"] = &framework.NodeInfo{ | |
Requested: &framework.Resource{ | |
MilliCPU: 2300, | |
Memory: 209716700, //1500 + 200MB in initContainers | |
EphemeralStorage: 0, | |
AllowedPodNumber: 0, | |
ScalarResources: map[v1.ResourceName]int64(nil), | |
}, | |
NonZeroRequested: &framework.Resource{ | |
MilliCPU: 2300, | |
Memory: 419431900, //200MB(initContainers) + 200MB(default memory value) + 1500 specified in requests/overhead | |
EphemeralStorage: 0, | |
AllowedPodNumber: 0, | |
ScalarResources: map[v1.ResourceName]int64(nil), | |
}, | |
Allocatable: &framework.Resource{}, | |
Generation: 2, | |
UsedPorts: framework.HostPortInfo{ | |
"127.0.0.1": map[framework.ProtocolPort]struct{}{ | |
{Protocol: "TCP", Port: 80}: {}, | |
{Protocol: "TCP", Port: 8080}: {}, | |
}, | |
}, | |
ImageStates: map[string]*framework.ImageStateSummary{}, | |
PVCRefCounts: map[string]int{"node_info_cache_test/pvc-1": 2, "node_info_cache_test/pvc-2": 1}, | |
Pods: []*framework.PodInfo{ | |
{ | |
Pod: &v1.Pod{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Namespace: "node_info_cache_test", | |
Name: "test-1", | |
UID: types.UID("test-1"), | |
}, | |
Spec: v1.PodSpec{ | |
Containers: []v1.Container{ | |
{ | |
Resources: v1.ResourceRequirements{ | |
Requests: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("100m"), | |
v1.ResourceMemory: resource.MustParse("500"), | |
}, | |
}, | |
Ports: []v1.ContainerPort{ | |
{ | |
HostIP: "127.0.0.1", | |
HostPort: 80, | |
Protocol: "TCP", | |
}, | |
}, | |
}, | |
}, | |
NodeName: nodeName, | |
Overhead: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("500m"), | |
}, | |
Volumes: []v1.Volume{ | |
{ | |
VolumeSource: v1.VolumeSource{ | |
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ | |
ClaimName: "pvc-1", | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
Pod: &v1.Pod{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Namespace: "node_info_cache_test", | |
Name: "test-2", | |
UID: types.UID("test-2"), | |
}, | |
Spec: v1.PodSpec{ | |
Containers: []v1.Container{ | |
{ | |
Resources: v1.ResourceRequirements{ | |
Requests: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("200m"), | |
}, | |
}, | |
Ports: []v1.ContainerPort{ | |
{ | |
HostIP: "127.0.0.1", | |
HostPort: 8080, | |
Protocol: "TCP", | |
}, | |
}, | |
}, | |
}, | |
NodeName: nodeName, | |
Overhead: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("500m"), | |
v1.ResourceMemory: resource.MustParse("500"), | |
}, | |
Volumes: []v1.Volume{ | |
{ | |
VolumeSource: v1.VolumeSource{ | |
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ | |
ClaimName: "pvc-1", | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
{ | |
Pod: &v1.Pod{ | |
ObjectMeta: metav1.ObjectMeta{ | |
Namespace: "node_info_cache_test", | |
Name: "test-3", | |
UID: types.UID("test-3"), | |
}, | |
Spec: v1.PodSpec{ | |
Containers: []v1.Container{ | |
{ | |
Resources: v1.ResourceRequirements{ | |
Requests: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("200m"), | |
}, | |
}, | |
Ports: []v1.ContainerPort{ | |
{ | |
HostIP: "127.0.0.1", | |
HostPort: 8080, | |
Protocol: "TCP", | |
}, | |
}, | |
}, | |
}, | |
InitContainers: []v1.Container{ | |
{ | |
Resources: v1.ResourceRequirements{ | |
Requests: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("500m"), | |
v1.ResourceMemory: resource.MustParse("200Mi"), | |
}, | |
}, | |
}, | |
}, | |
NodeName: nodeName, | |
Overhead: v1.ResourceList{ | |
v1.ResourceCPU: resource.MustParse("500m"), | |
v1.ResourceMemory: resource.MustParse("500"), | |
}, | |
Volumes: []v1.Volume{ | |
{ | |
VolumeSource: v1.VolumeSource{ | |
PersistentVolumeClaim: &v1.PersistentVolumeClaimVolumeSource{ | |
ClaimName: "pvc-2", | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
}, | |
} | |
// Print as JSON to stdout. | |
os.Stderr = os.Stdout | |
options := logs.Options{ | |
Config: config.LoggingConfiguration{ | |
Format: "json", | |
}, | |
} | |
if err := options.ValidateAndApply(); err != nil { | |
fmt.Printf("unexpected error: %v\n", err) | |
} | |
klog.InfoS("JSON output", "pod", formatPod{podSlice}) | |
klog.InfoS("JSON output", "nodeInfo", formatNode{"testNode", dumper.Nodes["testNode"], nil}) | |
options.Config.Format = "text" | |
if err := options.ValidateAndApply(); err != nil { | |
fmt.Printf("unexpected error: %v\n", err) | |
} | |
flag.Set("skip_headers", "true") | |
flag.Set("alsologtostderr", "false") | |
flag.Set("logtostderr", "false") | |
klog.SetOutput(os.Stdout) | |
klog.InfoS("text output", "pod", formatPod{podSlice}) | |
klog.InfoS("text output", "nodeInfo", formatNode{"testNode", dumper.Nodes["testNode"], nil}) | |
// Output: | |
// === RUN TestExample | |
// {"ts":1636456974407.1072,"caller":"debugger/formatter_test.go:240","msg":"JSON output","v":0,"pod":[{"Name":"pod-name0","Namespace":"ns0","NominatedNode":"nom0","UID":"some-uid0","Phase":"pd-phase0"},{"Name":"pod-name1","Namespace":"ns1","NominatedNode":"nom1","UID":"some-uid1","Phase":"pd-phase1"},{"Name":"pod-name2","Namespace":"ns2","NominatedNode":"nom2","UID":"some-uid2","Phase":"pd-phase2"}]} | |
// {"ts":1636456974407.198,"caller":"debugger/formatter_test.go:241","msg":"JSON output","v":0,"nodeInfo":{"NodeName":"testNode","Deleted":true,"RequestedResources":{"MilliCPU":2300,"Memory":209716700,"EphemeralStorage":0,"AllowedPodNumber":0,"ScalarResources":null},"AllocatableResources":{"MilliCPU":0,"Memory":0,"EphemeralStorage":0,"AllowedPodNumber":0,"ScalarResources":null},"ScheduledPods":3,"PodInfo":[{"Name":"test-1","Namespace":"node_info_cache_test","NominatedNode":"","UID":"test-1","Phase":""},{"Name":"test-2","Namespace":"node_info_cache_test","NominatedNode":"","UID":"test-2","Phase":""},{"Name":"test-3","Namespace":"node_info_cache_test","NominatedNode":"","UID":"test-3","Phase":""}],"NominatedPods":0,"NominatedPodsInfo":null}} | |
// "text output" pod="Name: pod-name0, Namespace: ns0, UID: some-uid0, Phase: pd-phase0, NominatedNode: nom0\nName: pod-name1, Namespace: ns1, UID: some-uid1, Phase: pd-phase1, NominatedNode: nom1\nName: pod-name2, Namespace: ns2, UID: some-uid2, Phase: pd-phase2, NominatedNode: nom2\n" | |
// "text output" nodeInfo="\nNodeName: testNode\nDeleted true\nRequestedResources: &{MilliCPU:2300 Memory:209716700 EphemeralStorage:0 AllowedPodNumber:0 ScalarResources:map[]}\nAllocatableResources: &{MilliCPU:0 Memory:0 EphemeralStorage:0 AllowedPodNumber:0 ScalarResources:map[]}\nScheduledPods: 3\nNominatedPods: 0\nName: test-1, Namespace: node_info_cache_test, UID: test-1, Phase: , NominatedNode: \nName: test-2, Namespace: node_info_cache_test, UID: test-2, Phase: , NominatedNode: \nName: test-3, Namespace: node_info_cache_test, UID: test-3, Phase: , NominatedNode: \n" | |
// --- PASS: TestExample (0.00s) | |
// PASS | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment