Skip to content

Commit

Permalink
fix: pointer error when bootstrap host is empty (#214)
Browse files Browse the repository at this point in the history
* fix: pointer error when bootstrap host is empty

* test: add tests
  • Loading branch information
Enda authored Jan 11, 2021
1 parent b442ba2 commit c05ccb5
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 5 deletions.
16 changes: 12 additions & 4 deletions pkg/sdk/kafka/kafka.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,17 @@ func ValidateName(name string) error {

// TransformResponse modifies fields from the KafkaRequest payload object
// The main transformation is appending ":443" to the Bootstrap Server URL
func TransformResponse(kafkaInstance *managedservices.KafkaRequest) {
if !strings.HasSuffix(*kafkaInstance.BootstrapServerHost, ":443") {
hostURL := fmt.Sprintf("%v:443", *kafkaInstance.BootstrapServerHost)
kafkaInstance.BootstrapServerHost = &hostURL
func TransformResponse(kafkaInstance *managedservices.KafkaRequest) *managedservices.KafkaRequest {
bootstrapHost := kafkaInstance.GetBootstrapServerHost()

if bootstrapHost == "" {
return kafkaInstance
}

if !strings.HasSuffix(bootstrapHost, ":443") {
hostURL := fmt.Sprintf("%v:443", bootstrapHost)
kafkaInstance.SetBootstrapServerHost(hostURL)
}

return kafkaInstance
}
63 changes: 62 additions & 1 deletion pkg/sdk/kafka/kafka_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package kafka

import "testing"
import (
"testing"

"github.com/bf2fc6cc711aee1a0c2a/cli/pkg/api/managedservices"
)

func TestValidateName(t *testing.T) {
type args struct {
Expand Down Expand Up @@ -91,3 +95,60 @@ func TestValidateName(t *testing.T) {
})
}
}

func TestTransformResponse(t *testing.T) {
hostWithSSLPort := "my-kafka-url:443"
hostWithNoPort := "my-kafka-url"

type args struct {
kafkaInstance *managedservices.KafkaRequest
}
tests := []struct {
name string
args args
wantBootstrapServerHost string
}{
{
name: "bootstrapServerHost should be transformed to empty string when nil",
args: args{
kafkaInstance: &managedservices.KafkaRequest{
BootstrapServerHost: nil,
},
},
wantBootstrapServerHost: "",
},
{
name: "bootstrapServerHost should be the same when SSL port already exists",
args: args{
kafkaInstance: &managedservices.KafkaRequest{
BootstrapServerHost: &hostWithSSLPort,
},
},
wantBootstrapServerHost: hostWithSSLPort,
},
{
name: "bootstrapServerHost should get SSL port when none exists",
args: args{
kafkaInstance: &managedservices.KafkaRequest{
BootstrapServerHost: &hostWithNoPort,
},
},
wantBootstrapServerHost: hostWithSSLPort,
},
}
for _, tt := range tests {
// nolint
t.Run(tt.name, func(t *testing.T) {
transformedInstance := TransformResponse(tt.args.kafkaInstance)

if transformedInstance == nil {
t.Errorf("Expected KafkaRequest type, but got nil")
}

transformedHost := transformedInstance.GetBootstrapServerHost()
if tt.wantBootstrapServerHost != transformedHost {
t.Errorf("Expected bootstrapServerHost: %v, got %v", tt.wantBootstrapServerHost, transformedHost)
}
})
}
}

0 comments on commit c05ccb5

Please sign in to comment.