Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 137 additions & 0 deletions internal/provider/node_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,143 @@ func TestNodePolicyResourceModel(t *testing.T) {
}
})

// Test LabelSelector with multiple MatchExpressions and multiple values
t.Run("LabelSelector_WithMultipleMatchExpressions", func(t *testing.T) {
selector := &LabelSelector{
MatchLabels: types.MapValueMust(types.StringType, map[string]attr.Value{
"app": types.StringValue("api"),
"env": types.StringValue("prod"),
}),
MatchExpressions: types.ListValueMust(
types.ObjectType{
AttrTypes: map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
},
[]attr.Value{
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("instanceGenerations"),
"operator": types.StringValue("Gt"),
"values": types.ListValueMust(types.StringType, []attr.Value{types.StringValue("4")}),
},
),
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("instanceFamily"),
"operator": types.StringValue("In"),
"values": types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("m5"),
types.StringValue("m6i"),
types.StringValue("m7i"),
}),
},
),
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("zone"),
"operator": types.StringValue("NotIn"),
"values": types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("us-west-2a"),
types.StringValue("us-west-2b"),
}),
},
),
},
),
}

// Test toProto
ctx := context.Background()
proto, err := selector.toProto(ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if proto == nil {
t.Fatal("Expected non-nil proto")
}
if len(proto.MatchLabels) != 2 {
t.Errorf("Expected 2 match labels, got %d", len(proto.MatchLabels))
}
if proto.MatchLabels["app"] != "api" {
t.Errorf("Expected app=api, got %s", proto.MatchLabels["app"])
}
if proto.MatchLabels["env"] != "prod" {
t.Errorf("Expected env=prod, got %s", proto.MatchLabels["env"])
}
if len(proto.MatchExpressions) != 3 {
t.Fatalf("Expected 3 match expressions, got %d", len(proto.MatchExpressions))
}

// Check first expression (Gt with single value)
expr1 := proto.MatchExpressions[0]
if expr1.Key != "instanceGenerations" {
t.Errorf("Expected key=instanceGenerations, got %s", expr1.Key)
}
if expr1.Operator != 5 { // GT = 5
t.Errorf("Expected operator=GT (5), got %d", expr1.Operator)
}
if len(expr1.Values) != 1 {
t.Errorf("Expected 1 value, got %d", len(expr1.Values))
}
if expr1.Values[0] != "4" {
t.Errorf("Expected value '4', got %s", expr1.Values[0])
}

// Check second expression (In with multiple values)
expr2 := proto.MatchExpressions[1]
if expr2.Key != "instanceFamily" {
t.Errorf("Expected key=instanceFamily, got %s", expr2.Key)
}
if expr2.Operator != 1 { // IN = 1
t.Errorf("Expected operator=IN (1), got %d", expr2.Operator)
}
if len(expr2.Values) != 3 {
t.Errorf("Expected 3 values, got %d", len(expr2.Values))
}
expectedValues := []string{"m5", "m6i", "m7i"}
for i, expected := range expectedValues {
if expr2.Values[i] != expected {
t.Errorf("Expected value[%d]='%s', got '%s'", i, expected, expr2.Values[i])
}
}

// Check third expression (NotIn with multiple values)
expr3 := proto.MatchExpressions[2]
if expr3.Key != "zone" {
t.Errorf("Expected key=zone, got %s", expr3.Key)
}
if expr3.Operator != 2 { // NOT_IN = 2
t.Errorf("Expected operator=NOT_IN (2), got %d", expr3.Operator)
}
if len(expr3.Values) != 2 {
t.Errorf("Expected 2 values, got %d", len(expr3.Values))
}
expectedZones := []string{"us-west-2a", "us-west-2b"}
for i, expected := range expectedZones {
if expr3.Values[i] != expected {
t.Errorf("Expected value[%d]='%s', got '%s'", i, expected, expr3.Values[i])
}
}
})

// Test DisruptionPolicy with nested budgets (full toProto conversion)
t.Run("DisruptionPolicy_WithBudgets", func(t *testing.T) {
policy := &DisruptionPolicy{
Expand Down
2 changes: 1 addition & 1 deletion internal/provider/workload_policy_target.go
Original file line number Diff line number Diff line change
Expand Up @@ -645,7 +645,7 @@ func (m *LabelSelector) fromProto(selector *apiv1.LabelSelector) {
m = &LabelSelector{}
}
m.MatchLabels = types.MapValueMust(types.StringType, fromStringMap(selector.MatchLabels))
m.MatchExpressions = types.ListValueMust(types.StringType, fromElementList(selector.MatchExpressions, MatchExpression{}.AttrTypes()))
m.MatchExpressions = types.ListValueMust(types.ObjectType{AttrTypes: MatchExpression{}.AttrTypes()}, fromElementList(selector.MatchExpressions, MatchExpression{}.AttrTypes()))
}

func (m *RegexPattern) fromProto(pattern *apiv1.RegexPattern) {
Expand Down
134 changes: 134 additions & 0 deletions internal/provider/workload_policy_target_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,140 @@ func TestWorkloadPolicyTargetResourceModel(t *testing.T) {
}
})

// Test LabelSelector with multiple MatchExpressions and multiple values
t.Run("LabelSelector_WithMultipleMatchExpressions", func(t *testing.T) {
selector := &LabelSelector{
MatchLabels: types.MapValueMust(types.StringType, map[string]attr.Value{
"app": types.StringValue("web"),
"environment": types.StringValue("production"),
}),
MatchExpressions: types.ListValueMust(
types.ObjectType{
AttrTypes: map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
},
[]attr.Value{
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("tier"),
"operator": types.StringValue("In"),
"values": types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("frontend"),
types.StringValue("backend"),
types.StringValue("middleware"),
}),
},
),
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("region"),
"operator": types.StringValue("NotIn"),
"values": types.ListValueMust(types.StringType, []attr.Value{
types.StringValue("us-east-1"),
types.StringValue("us-west-1"),
}),
},
),
types.ObjectValueMust(
map[string]attr.Type{
"key": types.StringType,
"operator": types.StringType,
"values": types.ListType{ElemType: types.StringType},
},
map[string]attr.Value{
"key": types.StringValue("version"),
"operator": types.StringValue("Exists"),
"values": types.ListValueMust(types.StringType, []attr.Value{}),
},
),
},
),
}

// Test toProto
ctx := context.Background()
proto, err := selector.toProto(ctx)
if err != nil {
t.Fatalf("Expected no error, got %v", err)
}
if proto == nil {
t.Fatal("Expected non-nil proto")
}
if len(proto.MatchLabels) != 2 {
t.Errorf("Expected 2 match labels, got %d", len(proto.MatchLabels))
}
if proto.MatchLabels["app"] != "web" {
t.Errorf("Expected app=web, got %s", proto.MatchLabels["app"])
}
if proto.MatchLabels["environment"] != "production" {
t.Errorf("Expected environment=production, got %s", proto.MatchLabels["environment"])
}
if len(proto.MatchExpressions) != 3 {
t.Fatalf("Expected 3 match expressions, got %d", len(proto.MatchExpressions))
}

// Check first expression (In with multiple values)
expr1 := proto.MatchExpressions[0]
if expr1.Key != "tier" {
t.Errorf("Expected key=tier, got %s", expr1.Key)
}
if expr1.Operator != 1 { // IN = 1
t.Errorf("Expected operator=IN (1), got %d", expr1.Operator)
}
if len(expr1.Values) != 3 {
t.Errorf("Expected 3 values, got %d", len(expr1.Values))
}
expectedTiers := []string{"frontend", "backend", "middleware"}
for i, expected := range expectedTiers {
if expr1.Values[i] != expected {
t.Errorf("Expected value[%d]='%s', got '%s'", i, expected, expr1.Values[i])
}
}

// Check second expression (NotIn with multiple values)
expr2 := proto.MatchExpressions[1]
if expr2.Key != "region" {
t.Errorf("Expected key=region, got %s", expr2.Key)
}
if expr2.Operator != 2 { // NOT_IN = 2
t.Errorf("Expected operator=NOT_IN (2), got %d", expr2.Operator)
}
if len(expr2.Values) != 2 {
t.Errorf("Expected 2 values, got %d", len(expr2.Values))
}
expectedRegions := []string{"us-east-1", "us-west-1"}
for i, expected := range expectedRegions {
if expr2.Values[i] != expected {
t.Errorf("Expected value[%d]='%s', got '%s'", i, expected, expr2.Values[i])
}
}

// Check third expression (Exists with no values)
expr3 := proto.MatchExpressions[2]
if expr3.Key != "version" {
t.Errorf("Expected key=version, got %s", expr3.Key)
}
if expr3.Operator != 3 { // EXISTS = 3
t.Errorf("Expected operator=EXISTS (3), got %d", expr3.Operator)
}
if len(expr3.Values) != 0 {
t.Errorf("Expected 0 values for Exists operator, got %d", len(expr3.Values))
}
})

// Test RegexPattern
t.Run("RegexPattern", func(t *testing.T) {
pattern := &RegexPattern{
Expand Down