Skip to content

Commit e9b39ff

Browse files
committed
test: add comprehensive unit tests for L2/L3 network validation
- Add TestResourceCloudStackNetworkSchema to validate schema structure - Add TestParseCIDR to test CIDR parsing logic for both L2 and L3 networks - Verify L2 networks return empty CIDR parsing results - Verify L3 networks require CIDR and parse correctly - All tests pass, ensuring functionality works as expected
1 parent 46944cc commit e9b39ff

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cloudstack
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func TestParseCIDR(t *testing.T) {
10+
networkResource := resourceCloudStackNetwork()
11+
12+
t.Run("L2 network should return empty map", func(t *testing.T) {
13+
config := map[string]interface{}{
14+
"type": "L2",
15+
}
16+
17+
resourceData := schema.TestResourceDataRaw(t, networkResource.Schema, config)
18+
19+
result, err := parseCIDR(resourceData, false)
20+
if err != nil {
21+
t.Errorf("Expected no error for L2 network, got: %v", err)
22+
}
23+
if result == nil {
24+
t.Error("Expected non-nil result for L2 network")
25+
}
26+
if len(result) != 0 {
27+
t.Errorf("Expected empty map for L2 network, got: %v", result)
28+
}
29+
})
30+
31+
t.Run("L3 network with valid CIDR should parse correctly", func(t *testing.T) {
32+
config := map[string]interface{}{
33+
"type": "L3",
34+
"cidr": "10.0.0.0/16",
35+
}
36+
37+
resourceData := schema.TestResourceDataRaw(t, networkResource.Schema, config)
38+
39+
result, err := parseCIDR(resourceData, true)
40+
if err != nil {
41+
t.Errorf("Expected no error for L3 network with valid CIDR, got: %v", err)
42+
}
43+
if result == nil {
44+
t.Error("Expected non-nil result for L3 network")
45+
}
46+
if result["gateway"] == "" {
47+
t.Error("Expected gateway to be set")
48+
}
49+
if result["netmask"] == "" {
50+
t.Error("Expected netmask to be set")
51+
}
52+
})
53+
54+
t.Run("L3 network without CIDR should return error", func(t *testing.T) {
55+
config := map[string]interface{}{
56+
"type": "L3",
57+
}
58+
59+
resourceData := schema.TestResourceDataRaw(t, networkResource.Schema, config)
60+
61+
_, err := parseCIDR(resourceData, true)
62+
if err == nil {
63+
t.Error("Expected error for L3 network without CIDR, but got none")
64+
}
65+
if err != nil && err.Error() != "cidr is required for L3 networks" {
66+
t.Errorf("Expected specific error message, got: %v", err)
67+
}
68+
})
69+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cloudstack
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
)
8+
9+
func TestResourceCloudStackNetworkSchema(t *testing.T) {
10+
networkResource := resourceCloudStackNetwork()
11+
12+
// Test that required fields exist
13+
t.Run("Schema should have type field", func(t *testing.T) {
14+
if typeField, ok := networkResource.Schema["type"]; !ok {
15+
t.Error("Schema should have 'type' field")
16+
} else {
17+
if typeField.Type != schema.TypeString {
18+
t.Errorf("Type field should be TypeString, got: %v", typeField.Type)
19+
}
20+
if typeField.Required {
21+
t.Error("Type field should not be required")
22+
}
23+
if typeField.Optional != true {
24+
t.Error("Type field should be optional")
25+
}
26+
if typeField.Default != "L3" {
27+
t.Errorf("Type field default should be 'L3', got: %v", typeField.Default)
28+
}
29+
}
30+
})
31+
32+
t.Run("Schema should have cidr field as optional", func(t *testing.T) {
33+
if cidrField, ok := networkResource.Schema["cidr"]; !ok {
34+
t.Error("Schema should have 'cidr' field")
35+
} else {
36+
if cidrField.Required {
37+
t.Error("CIDR field should not be required")
38+
}
39+
if cidrField.Optional != true {
40+
t.Error("CIDR field should be optional")
41+
}
42+
}
43+
})
44+
45+
t.Run("Schema should have CustomizeDiff", func(t *testing.T) {
46+
if networkResource.CustomizeDiff == nil {
47+
t.Error("Resource should have CustomizeDiff function")
48+
}
49+
})
50+
}

0 commit comments

Comments
 (0)