autoscaler/cluster-autoscaler/cloudprovider/aws/aws_manager_test.go

105 lines
2.9 KiB
Go

/*
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 aws
import (
"testing"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/autoscaling"
"github.com/stretchr/testify/assert"
apiv1 "k8s.io/api/core/v1"
"k8s.io/autoscaler/cluster-autoscaler/cloudprovider"
kubeletapis "k8s.io/kubernetes/pkg/kubelet/apis"
"runtime"
)
func TestBuildGenericLabels(t *testing.T) {
labels := buildGenericLabels(&asgTemplate{
InstanceType: &instanceType{
InstanceType: "c4.large",
VCPU: 2,
MemoryMb: 3840,
},
Region: "us-east-1",
}, "sillyname")
assert.Equal(t, "us-east-1", labels[kubeletapis.LabelZoneRegion])
assert.Equal(t, "sillyname", labels[kubeletapis.LabelHostname])
assert.Equal(t, "c4.large", labels[kubeletapis.LabelInstanceType])
assert.Equal(t, cloudprovider.DefaultArch, labels[kubeletapis.LabelArch])
assert.Equal(t, cloudprovider.DefaultOS, labels[kubeletapis.LabelOS])
}
func TestExtractLabelsFromAsg(t *testing.T) {
tags := []*autoscaling.TagDescription{
{
Key: aws.String("k8s.io/cluster-autoscaler/node-template/label/foo"),
Value: aws.String("bar"),
},
{
Key: aws.String("bar"),
Value: aws.String("baz"),
},
}
labels := extractLabelsFromAsg(tags)
assert.Equal(t, 1, len(labels))
assert.Equal(t, "bar", labels["foo"])
}
func TestExtractTaintsFromAsg(t *testing.T) {
tags := []*autoscaling.TagDescription{
{
Key: aws.String("k8s.io/cluster-autoscaler/node-template/taint/dedicated"),
Value: aws.String("foo:NoSchedule"),
},
{
Key: aws.String("bar"),
Value: aws.String("baz"),
},
}
expectedTaints := []apiv1.Taint{
{
Key: "dedicated",
Value: "foo",
Effect: apiv1.TaintEffectNoSchedule,
},
}
taints := extractTaintsFromAsg(tags)
assert.Equal(t, 1, len(taints))
assert.Equal(t, makeTaintSet(expectedTaints), makeTaintSet(taints))
}
func makeTaintSet(taints []apiv1.Taint) map[apiv1.Taint]bool {
set := make(map[apiv1.Taint]bool)
for _, taint := range taints {
set[taint] = true
}
return set
}
func testCreateAWSManager(t *testing.T) {
manager, awsError := createAWSManagerInternal(nil, &testService)
assert.Nil(t, awsError, "Expected nil from the error when creating AWS Manager")
currentNumberRoutines := runtime.NumGoroutine()
manager.Cleanup()
assert.True(t, currentNumberRoutines-1 == runtime.NumGoroutine(), "current number of go routines should be one less since we called close")
}