Skip to content

Commit

Permalink
refactor: func NewWorkspace returns proper zero-values for empty func…
Browse files Browse the repository at this point in the history
…tion parameters
  • Loading branch information
kanngiesser committed Oct 8, 2024
1 parent f95bde1 commit 12de896
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 14 deletions.
10 changes: 4 additions & 6 deletions pkg/providers/tf/deployment_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ var _ = Describe("DeploymentManager", func() {
By("checking that the modules and instances are updated, but the state remains the same")
expectedWorkspace := &workspace.TerraformWorkspace{
Modules: []workspace.ModuleDefinition{{
Name: "brokertemplate",
Definition: template,
Definitions: map[string]string{},
Name: "brokertemplate",
Definition: template,
}},
Instances: []workspace.ModuleInstance{{
ModuleName: "brokertemplate",
Expand Down Expand Up @@ -506,9 +505,8 @@ var _ = Describe("DeploymentManager", func() {
By("checking that the modules and instances are updated, but the state remains the same")
expectedWorkspace := &workspace.TerraformWorkspace{
Modules: []workspace.ModuleDefinition{{
Name: "brokertemplate",
Definition: template,
Definitions: map[string]string{},
Name: "brokertemplate",
Definition: template,
}},
Instances: []workspace.ModuleInstance{{
ModuleName: "brokertemplate",
Expand Down
30 changes: 22 additions & 8 deletions pkg/providers/tf/workspace/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,11 @@ func NewWorkspace(templateVars map[string]any,
parametersToRemove []string,
parametersToAdd []ParameterMapping) (*TerraformWorkspace, error) {

terraformTemplatesCopy := make(map[string]string)
maps.Copy(terraformTemplatesCopy, terraformTemplates)
var terraformTemplatesCopy map[string]string
if len(terraformTemplates) > 0 {
terraformTemplatesCopy = make(map[string]string)
maps.Copy(terraformTemplatesCopy, terraformTemplates)
}

tfModule := ModuleDefinition{
Name: "brokertemplate",
Expand All @@ -75,12 +78,23 @@ func NewWorkspace(templateVars map[string]any,
limitedConfig[name] = templateVars[name]
}

parameterMappingsCopy := make([]ParameterMapping, len(importParameterMappings))
copy(parameterMappingsCopy, importParameterMappings)
parametersToRemoveCopy := make([]string, len(parametersToRemove))
copy(parametersToRemoveCopy, parametersToRemove)
parametersToAddCopy := make([]ParameterMapping, len(parametersToAdd))
copy(parametersToAddCopy, parametersToAdd)
var parameterMappingsCopy []ParameterMapping
if len(importParameterMappings) > 0 {
parameterMappingsCopy = make([]ParameterMapping, len(importParameterMappings))
copy(parameterMappingsCopy, importParameterMappings)
}

var parametersToRemoveCopy []string
if len(parametersToRemove) > 0 {
parametersToRemoveCopy = make([]string, len(parametersToRemove))
copy(parametersToRemoveCopy, parametersToRemove)
}

var parametersToAddCopy []ParameterMapping
if len(parametersToAdd) > 0 {
parametersToAddCopy = make([]ParameterMapping, len(parametersToAdd))
copy(parametersToAddCopy, parametersToAdd)
}

workspace := TerraformWorkspace{
Modules: []ModuleDefinition{tfModule},
Expand Down
72 changes: 72 additions & 0 deletions pkg/providers/tf/workspace/workspace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,4 +370,76 @@ func TestNewWorkspace(t *testing.T) {
t.Error("Expected NewWorkspace to create deep copy of ParametersToAdd")
}
})
t.Run("returns empty map for empty templateVars", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Instances[0].Configuration
expect := make(map[string]any)
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

t.Run("returns zero-value for empty terraformTemplate", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Modules[0].Definition
var expect string
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

t.Run("returns zero-value for empty terraformTemplates", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Modules[0].Definitions
var expect map[string]string
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

t.Run("returns zero-value for empty importParameterMappings", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Transformer.ParameterMappings
var expect []ParameterMapping
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

t.Run("returns zero-value for empty importParametersToRemove", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Transformer.ParametersToRemove
var expect []string
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

t.Run("returns zero-value for empty importParametersToAdd", func(t *testing.T) {
ws, err := NewWorkspace(nil, "", nil, nil, nil, nil)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
got := ws.Transformer.ParametersToAdd
var expect []ParameterMapping
if !reflect.DeepEqual(expect, got) {
t.Errorf("expected %v, got %v", expect, got)
}
})

}

0 comments on commit 12de896

Please sign in to comment.