|
| 1 | +package features |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 8 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 9 | +) |
| 10 | + |
| 11 | +func TestBetaResourcesEnabled(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + description string |
| 14 | + data *core.ProviderData |
| 15 | + envSet bool |
| 16 | + envValue string |
| 17 | + expected bool |
| 18 | + expectWarn bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + description: "Feature flag enabled, env var not set", |
| 22 | + data: &core.ProviderData{ |
| 23 | + EnableBetaResources: true, |
| 24 | + }, |
| 25 | + expected: true, |
| 26 | + }, |
| 27 | + { |
| 28 | + description: "Feature flag is disabled, env var not set", |
| 29 | + data: &core.ProviderData{ |
| 30 | + EnableBetaResources: false, |
| 31 | + }, |
| 32 | + expected: false, |
| 33 | + }, |
| 34 | + { |
| 35 | + description: "Feature flag, Env var not set", |
| 36 | + data: &core.ProviderData{}, |
| 37 | + expected: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + description: "Feature flag not set, Env var is true", |
| 41 | + data: &core.ProviderData{}, |
| 42 | + envSet: true, |
| 43 | + envValue: "true", |
| 44 | + expected: true, |
| 45 | + }, |
| 46 | + { |
| 47 | + description: "Feature flag not set, Env var is false", |
| 48 | + data: &core.ProviderData{}, |
| 49 | + envSet: true, |
| 50 | + envValue: "false", |
| 51 | + expected: false, |
| 52 | + }, |
| 53 | + { |
| 54 | + description: "Feature flag not set, Env var is empty", |
| 55 | + data: &core.ProviderData{}, |
| 56 | + envSet: true, |
| 57 | + envValue: "", |
| 58 | + expectWarn: true, |
| 59 | + expected: false, |
| 60 | + }, |
| 61 | + { |
| 62 | + description: "Feature flag not set, Env var is gibberish", |
| 63 | + data: &core.ProviderData{}, |
| 64 | + envSet: true, |
| 65 | + envValue: "gibberish", |
| 66 | + expectWarn: true, |
| 67 | + expected: false, |
| 68 | + }, |
| 69 | + { |
| 70 | + description: "Feature flag enabled, Env var is true", |
| 71 | + data: &core.ProviderData{ |
| 72 | + EnableBetaResources: true, |
| 73 | + }, |
| 74 | + envSet: true, |
| 75 | + envValue: "true", |
| 76 | + expected: true, |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "Feature flag enabled, Env var is false", |
| 80 | + data: &core.ProviderData{ |
| 81 | + EnableBetaResources: true, |
| 82 | + }, |
| 83 | + envSet: true, |
| 84 | + envValue: "false", |
| 85 | + expected: false, |
| 86 | + }, |
| 87 | + { |
| 88 | + description: "Feature flag enabled, Env var is empty", |
| 89 | + data: &core.ProviderData{ |
| 90 | + EnableBetaResources: true, |
| 91 | + }, |
| 92 | + envSet: true, |
| 93 | + envValue: "", |
| 94 | + expectWarn: true, |
| 95 | + expected: true, |
| 96 | + }, |
| 97 | + { |
| 98 | + description: "Feature flag enabled, Env var is gibberish", |
| 99 | + data: &core.ProviderData{ |
| 100 | + EnableBetaResources: true, |
| 101 | + }, |
| 102 | + envSet: true, |
| 103 | + envValue: "gibberish", |
| 104 | + expectWarn: true, |
| 105 | + expected: true, |
| 106 | + }, |
| 107 | + { |
| 108 | + description: "Feature flag disabled, Env var is true", |
| 109 | + data: &core.ProviderData{ |
| 110 | + EnableBetaResources: false, |
| 111 | + }, |
| 112 | + envSet: true, |
| 113 | + envValue: "true", |
| 114 | + expected: true, |
| 115 | + }, |
| 116 | + { |
| 117 | + description: "Feature flag disabled, Env var is false", |
| 118 | + data: &core.ProviderData{ |
| 119 | + EnableBetaResources: false, |
| 120 | + }, |
| 121 | + envSet: true, |
| 122 | + envValue: "false", |
| 123 | + expected: false, |
| 124 | + }, |
| 125 | + { |
| 126 | + description: "Feature flag disabled, Env var is empty", |
| 127 | + data: &core.ProviderData{ |
| 128 | + EnableBetaResources: false, |
| 129 | + }, |
| 130 | + envSet: true, |
| 131 | + envValue: "", |
| 132 | + expectWarn: true, |
| 133 | + expected: false, |
| 134 | + }, |
| 135 | + { |
| 136 | + description: "Feature flag disabled, Env var is gibberish", |
| 137 | + data: &core.ProviderData{ |
| 138 | + EnableBetaResources: false, |
| 139 | + }, |
| 140 | + envSet: true, |
| 141 | + envValue: "gibberish", |
| 142 | + expectWarn: true, |
| 143 | + expected: false, |
| 144 | + }, |
| 145 | + } |
| 146 | + |
| 147 | + for _, tt := range tests { |
| 148 | + t.Run(tt.description, func(t *testing.T) { |
| 149 | + if tt.envSet { |
| 150 | + t.Setenv("STACKIT_TF_ENABLE_BETA_RESOURCES", tt.envValue) |
| 151 | + } |
| 152 | + diags := diag.Diagnostics{} |
| 153 | + |
| 154 | + result := BetaResourcesEnabled(context.Background(), tt.data, &diags) |
| 155 | + if result != tt.expected { |
| 156 | + t.Fatalf("Expected %t, got %t", tt.expected, result) |
| 157 | + } |
| 158 | + |
| 159 | + if tt.expectWarn && diags.WarningsCount() == 0 { |
| 160 | + t.Fatalf("Expected warning, got none") |
| 161 | + } |
| 162 | + if !tt.expectWarn && diags.WarningsCount() > 0 { |
| 163 | + t.Fatalf("Expected no warning, got %d", diags.WarningsCount()) |
| 164 | + } |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestCheckBetaResourcesEnabled(t *testing.T) { |
| 170 | + tests := []struct { |
| 171 | + description string |
| 172 | + betaEnabled bool |
| 173 | + expectError bool |
| 174 | + expectWarn bool |
| 175 | + }{ |
| 176 | + { |
| 177 | + description: "Beta enabled, show warning", |
| 178 | + betaEnabled: true, |
| 179 | + expectWarn: true, |
| 180 | + }, |
| 181 | + { |
| 182 | + description: "Beta disabled, show error", |
| 183 | + betaEnabled: false, |
| 184 | + expectError: true, |
| 185 | + }, |
| 186 | + } |
| 187 | + |
| 188 | + for _, tt := range tests { |
| 189 | + t.Run(tt.description, func(t *testing.T) { |
| 190 | + var envValue string |
| 191 | + if tt.betaEnabled { |
| 192 | + envValue = "true" |
| 193 | + } else { |
| 194 | + envValue = "false" |
| 195 | + } |
| 196 | + t.Setenv("STACKIT_TF_ENABLE_BETA_RESOURCES", envValue) |
| 197 | + |
| 198 | + diags := diag.Diagnostics{} |
| 199 | + CheckBetaResourcesEnabled(context.Background(), &core.ProviderData{}, &diags, "test") |
| 200 | + |
| 201 | + if tt.expectError && diags.ErrorsCount() == 0 { |
| 202 | + t.Fatalf("Expected error, got none") |
| 203 | + } |
| 204 | + if !tt.expectError && diags.ErrorsCount() > 0 { |
| 205 | + t.Fatalf("Expected no error, got %d", diags.ErrorsCount()) |
| 206 | + } |
| 207 | + |
| 208 | + if tt.expectWarn && diags.WarningsCount() == 0 { |
| 209 | + t.Fatalf("Expected warning, got none") |
| 210 | + } |
| 211 | + if !tt.expectWarn && diags.WarningsCount() > 0 { |
| 212 | + t.Fatalf("Expected no warning, got %d", diags.WarningsCount()) |
| 213 | + } |
| 214 | + }) |
| 215 | + } |
| 216 | +} |
0 commit comments