Files
alibabacloud-eci/eci_test.go
D8D Developer b5df40b865 feat(eci): 实现抢占式实例注解支持并更新开发状态
- 在 eci.go 中添加 `AnnotationECISpotInstance` 常量定义
- 实现注解解析逻辑,通过 `k8s.aliyun.com/eci-spot-instance: "true"` 启用抢占式实例
- 添加完整的单元测试覆盖各种注解值场景(true、false、未设置、无效值、大小写变体)
- 修复 eci.go 中预存在的类型转换错误(int → string)
- 更新 docs/eci.md 文档,添加简化注解使用说明
- 将开发状态从 ready-for-dev 更新为 review

🔧 chore(claude): 扩展本地设置允许的命令

- 在 .claude/settings.local.json 中添加 go build、go test 和 make 命令的 Bash 执行权限
2026-01-01 09:27:33 +00:00

94 lines
2.4 KiB
Go

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)
}
}
})
}
}