Files

68 lines
2.2 KiB
Go

package oci
import (
"strings"
"testing"
"github.com/oracle/oci-go-sdk/v65/sch"
)
func TestRelayEventCondition(t *testing.T) {
tests := []struct {
name string
events []string
want string
}{
{name: "单事件", events: []string{"LaunchInstance"}, want: "data.eventName='LaunchInstance'"},
{
name: "多事件 or 连接",
events: []string{"CreateUser", "DeleteUser"},
want: "data.eventName='CreateUser' or data.eventName='DeleteUser'",
},
{name: "空清单", events: nil, want: ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RelayEventCondition(tt.events); got != tt.want {
t.Errorf("condition = %q, want %q", got, tt.want)
}
})
}
}
func TestRelayEventConditionQuoting(t *testing.T) {
// 清单值来自代码内常量,不受外部输入;此处只固化引号格式防手滑
cond := RelayEventCondition([]string{"InstanceAction"})
if strings.Count(cond, "'") != 2 {
t.Errorf("单引号数 = %d, want 2 (%s)", strings.Count(cond, "'"), cond)
}
}
func TestRelayConditionDiffers(t *testing.T) {
cond := "data.eventName='TerminateInstance'"
rule := func(c string) sch.TaskDetailsResponse {
return sch.LogRuleTaskDetailsResponse{Condition: &c}
}
tests := []struct {
name string
tasks []sch.TaskDetailsResponse
want string
diff bool
}{
{name: "条件一致不漂移", tasks: []sch.TaskDetailsResponse{rule(cond)}, want: cond, diff: false},
{name: "条件文本不同漂移", tasks: []sch.TaskDetailsResponse{rule("data.eventName='LaunchInstance'")}, want: cond, diff: true},
{name: "无任务但期望条件漂移", tasks: nil, want: cond, diff: true},
{name: "多任务漂移", tasks: []sch.TaskDetailsResponse{rule(cond), rule(cond)}, want: cond, diff: true},
{name: "期望空且无任务不漂移", tasks: nil, want: "", diff: false},
{name: "期望空但有任务漂移", tasks: []sch.TaskDetailsResponse{rule(cond)}, want: "", diff: true},
{name: "条件缺失漂移", tasks: []sch.TaskDetailsResponse{sch.LogRuleTaskDetailsResponse{}}, want: cond, diff: true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := relayConditionDiffers(tt.tasks, tt.want); got != tt.diff {
t.Errorf("differs = %v, want %v", got, tt.diff)
}
})
}
}