Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
190 changes: 171 additions & 19 deletions api/v1alpha1/azure/azure_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,193 @@ import (

func TestAzureConfig(t *testing.T) {
t.Run("Merge", func(t *testing.T) {
base := &AzureConfig{
Enabled: boolPtr(false),
}
overlay := &AzureConfig{
Enabled: boolPtr(true),
tests := []struct {
name string
base *AzureConfig
overlay *AzureConfig
expected *AzureConfig
}{
{
name: "AllFields",
base: &AzureConfig{
Enabled: boolPtr(false),
SubscriptionID: stringPtr("old-sub"),
TenantID: stringPtr("old-tenant"),
Environment: stringPtr("old-env"),
},
overlay: &AzureConfig{
Enabled: boolPtr(true),
SubscriptionID: stringPtr("new-sub"),
TenantID: stringPtr("new-tenant"),
Environment: stringPtr("new-env"),
},
expected: &AzureConfig{
Enabled: boolPtr(true),
SubscriptionID: stringPtr("new-sub"),
TenantID: stringPtr("new-tenant"),
Environment: stringPtr("new-env"),
},
},
{
name: "PartialOverlay",
base: &AzureConfig{
Enabled: boolPtr(false),
SubscriptionID: stringPtr("old-sub"),
TenantID: stringPtr("old-tenant"),
Environment: stringPtr("old-env"),
},
overlay: &AzureConfig{
Enabled: boolPtr(true),
},
expected: &AzureConfig{
Enabled: boolPtr(true),
SubscriptionID: stringPtr("old-sub"),
TenantID: stringPtr("old-tenant"),
Environment: stringPtr("old-env"),
},
},
{
name: "NilOverlay",
base: &AzureConfig{
Enabled: boolPtr(false),
SubscriptionID: stringPtr("old-sub"),
TenantID: stringPtr("old-tenant"),
Environment: stringPtr("old-env"),
},
overlay: nil,
expected: &AzureConfig{
Enabled: boolPtr(false),
SubscriptionID: stringPtr("old-sub"),
TenantID: stringPtr("old-tenant"),
Environment: stringPtr("old-env"),
},
},
}

base.Merge(overlay)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.base.Merge(tt.overlay)

if tt.base.Enabled == nil || tt.expected.Enabled == nil {
if tt.base.Enabled != tt.expected.Enabled {
t.Errorf("Expected Enabled to be %v, got %v", tt.expected.Enabled, tt.base.Enabled)
}
} else if *tt.base.Enabled != *tt.expected.Enabled {
t.Errorf("Expected Enabled to be %v, got %v", *tt.expected.Enabled, *tt.base.Enabled)
}

if tt.base.SubscriptionID == nil || tt.expected.SubscriptionID == nil {
if tt.base.SubscriptionID != tt.expected.SubscriptionID {
t.Errorf("Expected SubscriptionID to be %v, got %v", tt.expected.SubscriptionID, tt.base.SubscriptionID)
}
} else if *tt.base.SubscriptionID != *tt.expected.SubscriptionID {
t.Errorf("Expected SubscriptionID to be %v, got %v", *tt.expected.SubscriptionID, *tt.base.SubscriptionID)
}

if *base.Enabled != true {
t.Errorf("Expected Enabled to be true, got %v", *base.Enabled)
if tt.base.TenantID == nil || tt.expected.TenantID == nil {
if tt.base.TenantID != tt.expected.TenantID {
t.Errorf("Expected TenantID to be %v, got %v", tt.expected.TenantID, tt.base.TenantID)
}
} else if *tt.base.TenantID != *tt.expected.TenantID {
t.Errorf("Expected TenantID to be %v, got %v", *tt.expected.TenantID, *tt.base.TenantID)
}

if tt.base.Environment == nil || tt.expected.Environment == nil {
if tt.base.Environment != tt.expected.Environment {
t.Errorf("Expected Environment to be %v, got %v", tt.expected.Environment, tt.base.Environment)
}
} else if *tt.base.Environment != *tt.expected.Environment {
t.Errorf("Expected Environment to be %v, got %v", *tt.expected.Environment, *tt.base.Environment)
}
})
}
})

t.Run("Copy", func(t *testing.T) {
original := &AzureConfig{
Enabled: boolPtr(true),
tests := []struct {
name string
original *AzureConfig
}{
{
name: "AllFields",
original: &AzureConfig{
Enabled: boolPtr(true),
SubscriptionID: stringPtr("sub"),
TenantID: stringPtr("tenant"),
Environment: stringPtr("env"),
},
},
{
name: "SomeFields",
original: &AzureConfig{
Enabled: boolPtr(true),
},
},
{
name: "Nil",
original: nil,
},
}

copy := original.Copy()
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
copy := tt.original.Copy()

if copy == nil {
t.Fatal("Expected non-nil copy")
}
if tt.original == nil {
if copy != nil {
t.Error("Expected nil copy for nil original")
}
return
}

if copy == original {
t.Error("Expected copy to be a new instance")
}
if copy == nil {
t.Fatal("Expected non-nil copy")
}

if *copy.Enabled != *original.Enabled {
t.Errorf("Expected Enabled to be %v, got %v", *original.Enabled, *copy.Enabled)
if copy == tt.original {
t.Error("Expected copy to be a new instance")
}

if tt.original.Enabled == nil {
if copy.Enabled != nil {
t.Error("Expected Enabled to be nil")
}
} else if copy.Enabled == nil || *copy.Enabled != *tt.original.Enabled {
t.Errorf("Expected Enabled to be %v, got %v", tt.original.Enabled, copy.Enabled)
}

if tt.original.SubscriptionID == nil {
if copy.SubscriptionID != nil {
t.Error("Expected SubscriptionID to be nil")
}
} else if copy.SubscriptionID == nil || *copy.SubscriptionID != *tt.original.SubscriptionID {
t.Errorf("Expected SubscriptionID to be %v, got %v", tt.original.SubscriptionID, copy.SubscriptionID)
}

if tt.original.TenantID == nil {
if copy.TenantID != nil {
t.Error("Expected TenantID to be nil")
}
} else if copy.TenantID == nil || *copy.TenantID != *tt.original.TenantID {
t.Errorf("Expected TenantID to be %v, got %v", tt.original.TenantID, copy.TenantID)
}

if tt.original.Environment == nil {
if copy.Environment != nil {
t.Error("Expected Environment to be nil")
}
} else if copy.Environment == nil || *copy.Environment != *tt.original.Environment {
t.Errorf("Expected Environment to be %v, got %v", tt.original.Environment, copy.Environment)
}
})
}
})
}

func boolPtr(b bool) *bool {
return &b
}

func stringPtr(s string) *string {
return &s
}
10 changes: 10 additions & 0 deletions api/v1alpha1/blueprint_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ type TerraformComponent struct {

// Values are configuration values for the module.
Values map[string]any `yaml:"values,omitempty"`

// Destroy determines if the component should be destroyed during down operations.
// Defaults to true if not specified.
Destroy *bool `yaml:"destroy,omitempty"`
}

// Kustomization defines a kustomization config in a blueprint.
Expand Down Expand Up @@ -230,6 +234,7 @@ func (b *Blueprint) DeepCopy() *Blueprint {
Path: component.Path,
FullPath: component.FullPath,
Values: valuesCopy,
Destroy: component.Destroy,
}
}

Expand Down Expand Up @@ -360,6 +365,11 @@ func (b *Blueprint) Merge(overlay *Blueprint) {
if overlayComponent.FullPath != "" {
mergedComponent.FullPath = overlayComponent.FullPath
}

if overlayComponent.Destroy != nil {
mergedComponent.Destroy = overlayComponent.Destroy
}

b.TerraformComponents = append(b.TerraformComponents, mergedComponent)
} else {
b.TerraformComponents = append(b.TerraformComponents, overlayComponent)
Expand Down
Loading
Loading