package alibabacloud import ( "strings" "testing" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" ) func TestAnnotationECISpotInstance(t *testing.T) { // Test that the annotation constant is correctly defined if AnnotationECISpotInstance != "k8s.aliyun.com/eci-spot-instance" { t.Errorf("AnnotationECISpotInstance = %s, want %s", AnnotationECISpotInstance, "k8s.aliyun.com/eci-spot-instance") } } func TestSpotStrategyAnnotation(t *testing.T) { tests := []struct { name string annotationValue string expectSpotSet bool }{ { name: "annotation true sets SpotStrategy", annotationValue: "true", expectSpotSet: true, }, { name: "annotation false does not set SpotStrategy", annotationValue: "false", expectSpotSet: false, }, { name: "no annotation does not set SpotStrategy", annotationValue: "", expectSpotSet: false, }, { name: "invalid annotation value does not set SpotStrategy", annotationValue: "invalid", expectSpotSet: false, }, { name: "case insensitive TRUE sets SpotStrategy", annotationValue: "TRUE", expectSpotSet: true, }, { name: "case insensitive True sets SpotStrategy", annotationValue: "True", expectSpotSet: true, }, { name: "case insensitive FaLsE does not set SpotStrategy", annotationValue: "FaLsE", expectSpotSet: false, }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { pod := &v1.Pod{ ObjectMeta: metav1.ObjectMeta{ Annotations: make(map[string]string), }, } if tt.annotationValue != "" { pod.Annotations[AnnotationECISpotInstance] = tt.annotationValue } // Verify annotation is correctly set on pod if tt.annotationValue != "" { val, exists := pod.Annotations[AnnotationECISpotInstance] if !exists { t.Errorf("Annotation %s should exist", AnnotationECISpotInstance) } if val != tt.annotationValue { t.Errorf("Expected annotation value %s, got %s", tt.annotationValue, val) } } // Verify case-insensitive comparison logic if tt.annotationValue != "" { result := strings.ToLower(tt.annotationValue) == "true" if result != tt.expectSpotSet { t.Errorf("strings.ToLower(%q) == \"true\" = %v, want %v", tt.annotationValue, result, tt.expectSpotSet) } } }) } }