Support beta functionality (#412)
* support beta functionality * add testing, improve logic and code quality * improve testing and code quality * Fix warning message
This commit is contained in:
parent
890e38f22a
commit
a07ff3f9ba
4 changed files with 288 additions and 0 deletions
216
stackit/internal/features/beta_test.go
Normal file
216
stackit/internal/features/beta_test.go
Normal file
|
|
@ -0,0 +1,216 @@
|
|||
package features
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
)
|
||||
|
||||
func TestBetaResourcesEnabled(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
data *core.ProviderData
|
||||
envSet bool
|
||||
envValue string
|
||||
expected bool
|
||||
expectWarn bool
|
||||
}{
|
||||
{
|
||||
description: "Feature flag enabled, env var not set",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: true,
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag is disabled, env var not set",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: false,
|
||||
},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag, Env var not set",
|
||||
data: &core.ProviderData{},
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag not set, Env var is true",
|
||||
data: &core.ProviderData{},
|
||||
envSet: true,
|
||||
envValue: "true",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag not set, Env var is false",
|
||||
data: &core.ProviderData{},
|
||||
envSet: true,
|
||||
envValue: "false",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag not set, Env var is empty",
|
||||
data: &core.ProviderData{},
|
||||
envSet: true,
|
||||
envValue: "",
|
||||
expectWarn: true,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag not set, Env var is gibberish",
|
||||
data: &core.ProviderData{},
|
||||
envSet: true,
|
||||
envValue: "gibberish",
|
||||
expectWarn: true,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag enabled, Env var is true",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: true,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "true",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag enabled, Env var is false",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: true,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "false",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag enabled, Env var is empty",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: true,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "",
|
||||
expectWarn: true,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag enabled, Env var is gibberish",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: true,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "gibberish",
|
||||
expectWarn: true,
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag disabled, Env var is true",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: false,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "true",
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
description: "Feature flag disabled, Env var is false",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: false,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "false",
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag disabled, Env var is empty",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: false,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "",
|
||||
expectWarn: true,
|
||||
expected: false,
|
||||
},
|
||||
{
|
||||
description: "Feature flag disabled, Env var is gibberish",
|
||||
data: &core.ProviderData{
|
||||
EnableBetaResources: false,
|
||||
},
|
||||
envSet: true,
|
||||
envValue: "gibberish",
|
||||
expectWarn: true,
|
||||
expected: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
if tt.envSet {
|
||||
t.Setenv("STACKIT_TF_ENABLE_BETA_RESOURCES", tt.envValue)
|
||||
}
|
||||
diags := diag.Diagnostics{}
|
||||
|
||||
result := BetaResourcesEnabled(context.Background(), tt.data, &diags)
|
||||
if result != tt.expected {
|
||||
t.Fatalf("Expected %t, got %t", tt.expected, result)
|
||||
}
|
||||
|
||||
if tt.expectWarn && diags.WarningsCount() == 0 {
|
||||
t.Fatalf("Expected warning, got none")
|
||||
}
|
||||
if !tt.expectWarn && diags.WarningsCount() > 0 {
|
||||
t.Fatalf("Expected no warning, got %d", diags.WarningsCount())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCheckBetaResourcesEnabled(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
betaEnabled bool
|
||||
expectError bool
|
||||
expectWarn bool
|
||||
}{
|
||||
{
|
||||
description: "Beta enabled, show warning",
|
||||
betaEnabled: true,
|
||||
expectWarn: true,
|
||||
},
|
||||
{
|
||||
description: "Beta disabled, show error",
|
||||
betaEnabled: false,
|
||||
expectError: true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
var envValue string
|
||||
if tt.betaEnabled {
|
||||
envValue = "true"
|
||||
} else {
|
||||
envValue = "false"
|
||||
}
|
||||
t.Setenv("STACKIT_TF_ENABLE_BETA_RESOURCES", envValue)
|
||||
|
||||
diags := diag.Diagnostics{}
|
||||
CheckBetaResourcesEnabled(context.Background(), &core.ProviderData{}, &diags, "test")
|
||||
|
||||
if tt.expectError && diags.ErrorsCount() == 0 {
|
||||
t.Fatalf("Expected error, got none")
|
||||
}
|
||||
if !tt.expectError && diags.ErrorsCount() > 0 {
|
||||
t.Fatalf("Expected no error, got %d", diags.ErrorsCount())
|
||||
}
|
||||
|
||||
if tt.expectWarn && diags.WarningsCount() == 0 {
|
||||
t.Fatalf("Expected warning, got none")
|
||||
}
|
||||
if !tt.expectWarn && diags.WarningsCount() > 0 {
|
||||
t.Fatalf("Expected no warning, got %d", diags.WarningsCount())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue