|
6 | 6 |
|
7 | 7 | "github.com/google/go-cmp/cmp" |
8 | 8 | "github.com/hashicorp/terraform-plugin-framework/attr" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
9 | 10 | "github.com/hashicorp/terraform-plugin-framework/types" |
10 | 11 | iaas "github.com/stackitcloud/stackit-sdk-go/services/iaas/v2api" |
11 | 12 | ) |
@@ -943,3 +944,135 @@ func TestModelIsIPv4ConfigSet(t *testing.T) { |
943 | 944 | }) |
944 | 945 | } |
945 | 946 | } |
| 947 | + |
| 948 | +func TestValidateConfig(t *testing.T) { |
| 949 | + tests := []struct { |
| 950 | + name string |
| 951 | + model Model |
| 952 | + wantErr bool |
| 953 | + }{ |
| 954 | + { |
| 955 | + name: "happy case: IPv4 only with prefix", |
| 956 | + model: Model{ |
| 957 | + IPv4Prefix: types.StringValue("192.168.0.0/24"), |
| 958 | + IPv4Gateway: types.StringValue("192.168.0.1"), |
| 959 | + }, |
| 960 | + wantErr: false, |
| 961 | + }, |
| 962 | + { |
| 963 | + name: "happy case: IPv4 only with prefix_length", |
| 964 | + model: Model{ |
| 965 | + IPv4PrefixLength: types.Int64Value(24), |
| 966 | + NoIPv4Gateway: types.BoolValue(true), |
| 967 | + }, |
| 968 | + wantErr: false, |
| 969 | + }, |
| 970 | + { |
| 971 | + name: "happy case: IPv6 only with prefix", |
| 972 | + model: Model{ |
| 973 | + IPv6Prefix: types.StringValue("2001:db8::/64"), |
| 974 | + IPv6Gateway: types.StringValue("2001:db8::1"), |
| 975 | + }, |
| 976 | + wantErr: false, |
| 977 | + }, |
| 978 | + { |
| 979 | + name: "happy case: IPv6 only with prefix_length", |
| 980 | + model: Model{ |
| 981 | + IPv6PrefixLength: types.Int64Value(64), |
| 982 | + NoIPv6Gateway: types.BoolValue(true), |
| 983 | + }, |
| 984 | + wantErr: false, |
| 985 | + }, |
| 986 | + { |
| 987 | + name: "happy case: both IPv4 and IPv6 configured correctly", |
| 988 | + model: Model{ |
| 989 | + IPv4Prefix: types.StringValue("192.168.0.0/24"), |
| 990 | + NoIPv4Gateway: types.BoolValue(true), |
| 991 | + IPv6Prefix: types.StringValue("2001:db8::/64"), |
| 992 | + NoIPv6Gateway: types.BoolValue(true), |
| 993 | + }, |
| 994 | + wantErr: false, |
| 995 | + }, |
| 996 | + { |
| 997 | + name: "happy case: only IPv4 prefix", |
| 998 | + model: Model{ |
| 999 | + IPv4Prefix: types.StringValue("192.168.0.0/24"), |
| 1000 | + }, |
| 1001 | + wantErr: false, |
| 1002 | + }, |
| 1003 | + { |
| 1004 | + name: "happy case: only IPv6 prefix", |
| 1005 | + model: Model{ |
| 1006 | + IPv6Prefix: types.StringValue("2001:db8::/64"), |
| 1007 | + }, |
| 1008 | + wantErr: false, |
| 1009 | + }, |
| 1010 | + { |
| 1011 | + name: "happy case: only IPv4 prefix_length", |
| 1012 | + model: Model{ |
| 1013 | + IPv4PrefixLength: types.Int64Value(24), |
| 1014 | + }, |
| 1015 | + wantErr: false, |
| 1016 | + }, |
| 1017 | + { |
| 1018 | + name: "happy case: only IPv6 prefix_length", |
| 1019 | + model: Model{ |
| 1020 | + IPv6PrefixLength: types.Int64Value(64), |
| 1021 | + }, |
| 1022 | + wantErr: false, |
| 1023 | + }, |
| 1024 | + { |
| 1025 | + name: "error case: IPv4 active via gateway but missing prefix and prefix_length", |
| 1026 | + model: Model{ |
| 1027 | + IPv4Gateway: types.StringValue("192.168.0.1"), |
| 1028 | + }, |
| 1029 | + wantErr: true, |
| 1030 | + }, |
| 1031 | + { |
| 1032 | + name: "error case: IPv6 active via no_gateway but missing prefix and prefix_length", |
| 1033 | + model: Model{ |
| 1034 | + NoIPv6Gateway: types.BoolValue(true), |
| 1035 | + }, |
| 1036 | + wantErr: true, |
| 1037 | + }, |
| 1038 | + { |
| 1039 | + name: "error case: IPv4 active via nameservers but missing prefix and prefix_length", |
| 1040 | + model: Model{ |
| 1041 | + IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{ |
| 1042 | + types.StringValue("1.1.1.1"), |
| 1043 | + }), |
| 1044 | + }, |
| 1045 | + wantErr: true, |
| 1046 | + }, |
| 1047 | + { |
| 1048 | + name: "error case: IPv6 active via nameservers but missing prefix and prefix_length", |
| 1049 | + model: Model{ |
| 1050 | + IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{ |
| 1051 | + types.StringValue("2606:4700:4700::1111"), |
| 1052 | + }), |
| 1053 | + }, |
| 1054 | + wantErr: true, |
| 1055 | + }, |
| 1056 | + { |
| 1057 | + name: "error case: dual-stack failure (both active via nameservers, both missing prefixes)", |
| 1058 | + model: Model{ |
| 1059 | + IPv4Nameservers: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("8.8.8.8")}), |
| 1060 | + IPv6Nameservers: types.ListValueMust(types.StringType, []attr.Value{types.StringValue("2001:4860:4860::8888")}), |
| 1061 | + }, |
| 1062 | + wantErr: true, |
| 1063 | + }, |
| 1064 | + } |
| 1065 | + |
| 1066 | + for _, tt := range tests { |
| 1067 | + t.Run(tt.name, func(t *testing.T) { |
| 1068 | + ctx := context.Background() |
| 1069 | + diags := diag.Diagnostics{} |
| 1070 | + |
| 1071 | + validateConfig(ctx, &diags, &tt.model) |
| 1072 | + |
| 1073 | + if diags.HasError() != tt.wantErr { |
| 1074 | + t.Errorf("validateConfig() error = %v, wantErr %v. Diagnostics: %v", diags.HasError(), tt.wantErr, diags) |
| 1075 | + } |
| 1076 | + }) |
| 1077 | + } |
| 1078 | +} |
0 commit comments