Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
UPSTREAM: <carry>: Update unit tests using upstream annotations new v…
Browse files Browse the repository at this point in the history
…alues
racheljpg committed Jan 30, 2025
1 parent 757a3eb commit 86e5935
Showing 2 changed files with 121 additions and 14 deletions.
Original file line number Diff line number Diff line change
@@ -1328,7 +1328,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
},
{
name: "When the NodeGroup can scale from zero",
name: "When the NodeGroup can scale from zero with upstream annotations",
nodeGroupAnnotations: map[string]string{
memoryKey: "2048",
cpuKey: "2",
@@ -1343,7 +1343,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourceMemory: 2048,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
@@ -1355,7 +1355,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
},
{
name: "When the NodeGroup can scale from zero and the nodegroup adds labels to the Node",
name: "When the NodeGroup can scale from zero and the nodegroup adds labels to the Node with upstream annotations",
nodeGroupAnnotations: map[string]string{
memoryKey: "2048",
cpuKey: "2",
@@ -1368,7 +1368,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourceMemory: 2048,
corev1.ResourcePods: 110,
},
expectedNodeLabels: map[string]string{
@@ -1381,7 +1381,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
},
{
name: "When the NodeGroup can scale from zero, the label capacity annotations merge with the pre-built node labels and take precedence if the same key is defined in both",
name: "When the NodeGroup can scale from zero, the label capacity annotations merge with the pre-built node labels and take precedence if the same key is defined in both with upstream annotations",
nodeGroupAnnotations: map[string]string{
memoryKey: "2048",
cpuKey: "2",
@@ -1398,7 +1398,7 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourceMemory: 2048,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
@@ -1413,11 +1413,123 @@ func TestNodeGroupTemplateNodeInfo(t *testing.T) {
},
},
{
name: "When the NodeGroup can scale from zero and the Node still exists, it includes the known node labels",
name: "When the NodeGroup can scale from zero and the Node still exists, it includes the known node labels with upstream annotations",
nodeGroupAnnotations: map[string]string{
memoryKey: "2048",
cpuKey: "2",
},
config: testCaseConfig{
includeNodes: true,
expectedErr: nil,
nodeLabels: map[string]string{
"kubernetes.io/os": "windows",
"kubernetes.io/arch": "arm64",
"node.kubernetes.io/instance-type": "instance1",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048,
corev1.ResourcePods: 110,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/hostname": "random value",
"kubernetes.io/os": "windows",
"kubernetes.io/arch": "arm64",
"node.kubernetes.io/instance-type": "instance1",
},
},
},
{
name: "When the NodeGroup can scale from zero with downstream annotations",
nodeGroupAnnotations: map[string]string{
deprecatedMemoryKey: "2048",
deprecatedCpuKey: "2",
gpuTypeKey: gpuapis.ResourceNvidiaGPU,
deprecatedGpuCountKey: "1",
},
config: testCaseConfig{
expectedErr: nil,
nodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"kubernetes.io/hostname": "random value",
},
},
},
{
name: "When the NodeGroup can scale from zero and the nodegroup adds labels to the Node with downstream annotations",
nodeGroupAnnotations: map[string]string{
deprecatedMemoryKey: "2048",
deprecatedCpuKey: "2",
},
config: testCaseConfig{
expectedErr: nil,
nodegroupLabels: map[string]string{
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourcePods: 110,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "amd64",
"kubernetes.io/hostname": "random value",
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
},
},
},
{
name: "When the NodeGroup can scale from zero, the label capacity annotations merge with the pre-built node labels and take precedence if the same key is defined in both with downstream annotations",
nodeGroupAnnotations: map[string]string{
deprecatedMemoryKey: "2048",
deprecatedCpuKey: "2",
gpuTypeKey: gpuapis.ResourceNvidiaGPU,
deprecatedGpuCountKey: "1",
labelsKey: "kubernetes.io/arch=arm64,my-custom-label=custom-value",
},
config: testCaseConfig{
expectedErr: nil,
nodegroupLabels: map[string]string{
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
"my-custom-label": "not-what-i-want",
},
expectedCapacity: map[corev1.ResourceName]int64{
corev1.ResourceCPU: 2,
corev1.ResourceMemory: 2048 * 1024 * 1024,
corev1.ResourcePods: 110,
gpuapis.ResourceNvidiaGPU: 1,
},
expectedNodeLabels: map[string]string{
"kubernetes.io/hostname": "random value",
"kubernetes.io/os": "linux",
"kubernetes.io/arch": "arm64",
"nodeGroupLabel": "value",
"anotherLabel": "anotherValue",
"my-custom-label": "custom-value",
},
},
},
{
name: "When the NodeGroup can scale from zero and the Node still exists, it includes the known node labels with downstream annotations",
nodeGroupAnnotations: map[string]string{
deprecatedMemoryKey: "2048",
deprecatedCpuKey: "2",
},
config: testCaseConfig{
includeNodes: true,
expectedErr: nil,
Original file line number Diff line number Diff line change
@@ -28,7 +28,6 @@ import (
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/autoscaler/cluster-autoscaler/utils/units"
"k8s.io/client-go/tools/cache"
)

@@ -351,6 +350,7 @@ func TestSetSizeAndReplicas(t *testing.T) {
})
}

// This test now tests both the upstream and downstream annotation values while we support them both.
func TestAnnotations(t *testing.T) {
cpuQuantity := resource.MustParse("2")
diskQuantity := resource.MustParse("100Gi")
@@ -376,11 +376,6 @@ func TestAnnotations(t *testing.T) {
labelsKey: "key3=value3,key4=value4,key5=value5",
}

// convert the initial memory value from Mebibytes to bytes as this conversion happens internally
// when we use InstanceMemoryCapacity()
memVal, _ := memQuantity.AsInt64()
memQuantityAsBytes := resource.NewQuantity(memVal*units.MiB, resource.DecimalSI)

test := func(t *testing.T, testConfig *testConfig, testResource *unstructured.Unstructured) {
controller, stop := mustCreateTestController(t, testConfig)
defer stop()
@@ -398,7 +393,7 @@ func TestAnnotations(t *testing.T) {

if mem, err := sr.InstanceMemoryCapacityAnnotation(); err != nil {
t.Fatal(err)
} else if memQuantityAsBytes.Cmp(mem) != 0 {
} else if memQuantity.Cmp(mem) != 0 {
t.Errorf("expected %v, got %v", memQuantity, mem)
}

0 comments on commit 86e5935

Please sign in to comment.