Skip to content

Commit

Permalink
Refactored unit tests (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
LewisKSaint authored Oct 4, 2023
1 parent 319f260 commit 4477fc0
Show file tree
Hide file tree
Showing 5 changed files with 241 additions and 299 deletions.
2 changes: 1 addition & 1 deletion cmd/command/configgenerator.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func ParseTerraformObject(ctx context.Context, client semp.Client, resourceName
for _, ds := range internalbroker.Entities {
if strings.ToLower(ds.TerraformName) == strings.ToLower(brokerObjectTerraformName) {
entityToRead = ds
break
}
}
var path string
Expand Down Expand Up @@ -193,7 +194,6 @@ func GetNameForResource(resourceTerraformName string, attributeResourceTerraform

resourceTerraformName = strings.Split(resourceTerraformName, " ")[0]
resourceTerraformName = strings.ReplaceAll(strings.ToLower(resourceTerraformName), "solacebroker_", "")
// resources := ConvertAttributeTextToMap(attributeResourceTerraform)

//Get identifying attribute name to differentiate from multiples
for _, ds := range internalbroker.Entities {
Expand Down
145 changes: 82 additions & 63 deletions cmd/command/configgenerator_test.go
Original file line number Diff line number Diff line change
@@ -1,95 +1,114 @@
// terraform-provider-solacebroker
//
// Copyright 2023 Solace Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terraform

import (
"context"
"fmt"
"reflect"
"terraform-provider-solacebroker/internal/semp"
"testing"
)

func TestCreateBrokerObjectRelationships(t *testing.T) {
tests := []struct {
name string
}{}
}{
{"Generate Broker Relationship"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
CreateBrokerObjectRelationships()
println("Broker relationship object is " + fmt.Sprint(BrokerObjectRelationship))
if len(BrokerObjectRelationship) == 0 {
t.Errorf("Broker relationship not built ")
}
_, exist := BrokerObjectRelationship["msg_vpn"]
if !exist {
t.Errorf("Broker relationship does not contain msgVPn relation")
}
})
}
}

func TestGetNameForResource(t *testing.T) {
type args struct {
resourceTerraformName string
attributeResourceTerraform string
attributeResourceTerraform ResourceConfig
}
tests := []struct {
name string
args args
want string
}{
// TODO: Add test cases.
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// TODO: fix unit test
// if got := GetNameForResource(tt.args.resourceTerraformName, tt.args.attributeResourceTerraform); got != tt.want {
// t.Errorf("GetNameForResource() = %v, want %v", got, tt.want)
// }
})
}
}

func TestParseTerraformObject(t *testing.T) {
type args struct {
ctx context.Context
client semp.Client
resourceName string
brokerObjectTerraformName string
providerSpecificIdentifier string
parentBrokerResourceAttributesRelationship map[string]string
parentResult map[string]any
}
tests := []struct {
name string
args args
want GeneratorTerraformOutput
}{
// TODO: Add test cases.
{
"GetNameForMsgVPN",
args{
resourceTerraformName: "solacebroker_msg_vpn qn",
attributeResourceTerraform: ResourceConfig{
ResourceAttributes: map[string]ResourceAttributeInfo{"msg_vpn_name": {
"test",
"no comment",
},
"ingress": {
"0",
"comment here",
},
},
},
},
"_test",
},
{
"GetNameForAclProfile",
args{
resourceTerraformName: "solacebroker_msg_vpn_acl_profile acl",
attributeResourceTerraform: ResourceConfig{
ResourceAttributes: map[string]ResourceAttributeInfo{"acl_profile_name": {
"default",
"no comment",
},
"random": {
"0",
"comment here",
},
},
},
},
"_default",
},
{
"GetNameForTopicName",
args{
resourceTerraformName: "solacebroker_msg_vpn_jndi_topic topic",
attributeResourceTerraform: ResourceConfig{
ResourceAttributes: map[string]ResourceAttributeInfo{"topic_name": {
"random",
"no comment",
},
"mock": {
"parameter",
"comment here",
},
},
},
},
"_random",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ParseTerraformObject(tt.args.ctx, tt.args.client, tt.args.resourceName, tt.args.brokerObjectTerraformName, tt.args.providerSpecificIdentifier, tt.args.parentBrokerResourceAttributesRelationship, tt.args.parentResult); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ParseTerraformObject() = %v, want %v", got, tt.want)
if got := GetNameForResource(tt.args.resourceTerraformName, tt.args.attributeResourceTerraform); got != tt.want {
t.Errorf("GetNameForResource() = %v, want %v", got, tt.want)
}
})
}
}

//func Test_getDataSourceNameIfDatasource(t *testing.T) {
// type args struct {
// parent string
// child string
// }
// tests := []struct {
// name string
// args args
// want []string
// want1 bool
// }{
// // TODO: Add test cases.
// }
// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// got, got1 := getDataSourceNameIfDatasource(tt.args.parent, tt.args.child)
// if !reflect.DeepEqual(got, tt.want) {
// t.Errorf("getDataSourceNameIfDatasource() got = %v, want %v", got, tt.want)
// }
// if got1 != tt.want1 {
// t.Errorf("getDataSourceNameIfDatasource() got1 = %v, want %v", got1, tt.want1)
// }
// })
// }
//}
34 changes: 33 additions & 1 deletion cmd/command/configwriter_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
// terraform-provider-solacebroker
//
// Copyright 2023 Solace Corporation. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package terraform

import "testing"
Expand All @@ -11,7 +26,24 @@ func TestGenerateTerraformFile(t *testing.T) {
args args
wantErr bool
}{
// TODO: Add test cases.
{
"CanGenerateFile",
args{terraformObjectInfo: &ObjectInfo{
Registry: "",
BrokerURL: "http://localhost:8080",
Username: "admin",
Password: "admin",
BearerToken: "",
FileName: "somefile.tf",
BrokerResources: []map[string]string{}},
},
false,
},
{
"FailToGenerateFile",
args{terraformObjectInfo: &ObjectInfo{}},
true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down
22 changes: 9 additions & 13 deletions cmd/command/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ var charset = []byte("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

type ResourceAttributeInfo struct {
AttributeValue string
Comment string
Comment string
}

type ResourceConfig struct {
ResourceAttributes map[string]ResourceAttributeInfo // indexed by resource attribute name
ResourceAttributes map[string]ResourceAttributeInfo // indexed by resource attribute name
}

type ObjectInfo struct {
Expand Down Expand Up @@ -196,7 +196,7 @@ func addCommentToAttributeInfo(info ResourceAttributeInfo, comment string) Resou

func GenerateTerraformString(attributes []*broker.AttributeInfo, values []map[string]interface{}, parentBrokerResourceAttributes map[string]string, brokerObjectTerraformName string) ([]ResourceConfig, error) {
var tfBrokerObjects []ResourceConfig
var attributesWithDefaultValue = []string{} // list of attributes, collected but not used
var attributesWithDefaultValue = []string{} // list of attributes, collected but not used
for k := range values {
resourceConfig := ResourceConfig{
ResourceAttributes: map[string]ResourceAttributeInfo{},
Expand Down Expand Up @@ -240,9 +240,9 @@ func GenerateTerraformString(attributes []*broker.AttributeInfo, values []map[st
fmt.Println("Applying workaround: not ignoring default for `msg_vpn` attribute `authentication_basic_type`")
}
}

/// => value in val

val := "\"" + valuesRes.(string) + "\""
if strings.Contains(valuesRes.(string), "{") {
valueOutput := strings.ReplaceAll(valuesRes.(string), "\"", "\\\"")
Expand Down Expand Up @@ -338,7 +338,7 @@ func GetParentResourceAttributes(parentObjectName string, brokerParentResource m
parentResourceAttributes := map[string]string{}
parentResourceName := strings.ReplaceAll(parentObjectName, " ", ".")
for parentResourceObject := range brokerParentResource {
resourceAttributes := brokerParentResource[parentResourceObject].ResourceAttributes
resourceAttributes := brokerParentResource[parentResourceObject].ResourceAttributes
for resourceAttributeName := range resourceAttributes {
parentResourceAttributes[resourceAttributeName] = parentResourceName + "." + resourceAttributeName
}
Expand Down Expand Up @@ -367,10 +367,6 @@ func IndexOf(elm BrokerObjectType, data []BrokerObjectType) int {
return -1
}

func RemoveIndex(s []BrokerObjectType, index int) []BrokerObjectType {
return append(s[:index], s[index+1:]...)
}

func ToFormattedHCL(brokerResources []map[string]ResourceConfig) []map[string]string {
var formattedResult []map[string]string
for _, resources := range brokerResources {
Expand All @@ -386,11 +382,11 @@ func ToFormattedHCL(brokerResources []map[string]ResourceConfig) []map[string]st

func hclFormatResource(resourceConfig ResourceConfig) string {
var attributeNames []string
for attributeName := range resourceConfig.ResourceAttributes {
for attributeName := range resourceConfig.ResourceAttributes {
attributeNames = append(attributeNames, attributeName)
}
sort.Strings(attributeNames)
var b bytes.Buffer
var b bytes.Buffer
w := tabwriter.NewWriter(&b, 0, 0, 2, ' ', 0)
for pos := range attributeNames {
attributeName := attributeNames[pos]
Expand All @@ -402,4 +398,4 @@ func hclFormatResource(resourceConfig ResourceConfig) string {
w.Flush()
config := b.String()
return config
}
}
Loading

0 comments on commit 4477fc0

Please sign in to comment.