|
| 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 | +} |
0 commit comments