Skip to content

Commit

Permalink
Added MQTT util tests, injected logger instead of the whole observabi…
Browse files Browse the repository at this point in the history
…lity
  • Loading branch information
xBlaz3kx committed Jul 29, 2024
1 parent bcfee17 commit 5410695
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 21 deletions.
4 changes: 2 additions & 2 deletions mqtt/mqtt-v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func NewMqttClient(clientSettings Configuration, obs observability.Observability
opts.SetMaxReconnectInterval(time.Second * 5)

// Set TLS settings (required by AWS)
opts.SetTLSConfig(createTlsConfiguration(obs, clientSettings.TLS))
opts.SetTLSConfig(createTlsConfiguration(obs.Log(), clientSettings.TLS))

opts.SetOnConnectHandler(func(client mqtt.Client) {
obs.Log().Info("Connected to broker")
Expand Down Expand Up @@ -133,7 +133,7 @@ func (c *clientImpl) SubscribeToAny(topic Topic, handler MessageHandler) {
}

// Parse the topic and get the Ids based on the original topic.
ids, err := GetIdsFromTopic(c.obs, message.Topic(), topic)
ids, err := GetIdsFromTopic(c.obs.Log(), message.Topic(), topic)
if err != nil {
logInfo.Sugar().Errorf("Error getting the topic info: %v", err)
return
Expand Down
18 changes: 9 additions & 9 deletions mqtt/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ import (
"os"
"strings"

"github.com/GLCharge/otelzap"
"github.com/agrison/go-commons-lang/stringUtils"
"github.com/xBlaz3kx/DevX/configuration"
"github.com/xBlaz3kx/DevX/observability"
"go.uber.org/zap"
)

Expand All @@ -27,8 +27,8 @@ var (
// subscription topic = some/+/subscription/+/topic
// should return ["exampleId1", "exampleId2"]
// If the topic are not the same length or don't contain the same words, it will return an error
func GetIdsFromTopic(obs observability.Observability, actualTopic string, subTopic Topic) ([]string, error) {
obs.Log().With(
func GetIdsFromTopic(logger *otelzap.Logger, actualTopic string, subTopic Topic) ([]string, error) {
logger.With(
zap.String("actualTopic", actualTopic),
zap.String("originalTopic", string(subTopic)),
).Debug("Getting Ids from topic")
Expand Down Expand Up @@ -61,8 +61,8 @@ func GetIdsFromTopic(obs observability.Observability, actualTopic string, subTop
}

// CreateTopicWithIds replaces all the + sign in a topic used for subscription with ids. Works only if the number of pluses is matches the number of ids.
func CreateTopicWithIds(obs observability.Observability, topicTemplate Topic, ids ...string) (string, error) {
obs.Log().With(
func CreateTopicWithIds(logger *otelzap.Logger, topicTemplate Topic, ids ...string) (string, error) {
logger.With(
zap.String("topic", string(topicTemplate)),
zap.Strings("ids", ids),
).Debug("Creating publish topic")
Expand All @@ -87,10 +87,10 @@ func CreateTopicWithIds(obs observability.Observability, topicTemplate Topic, id
}

// createTlsConfiguration Create a TLS cert using the private key and certificate
func createTlsConfiguration(obs observability.Observability, tlsSettings configuration.TLS) *tls.Config {
func createTlsConfiguration(logger *otelzap.Logger, tlsSettings configuration.TLS) *tls.Config {
clientCert, err := tls.LoadX509KeyPair(tlsSettings.CertificatePath, tlsSettings.PrivateKeyPath)
if err != nil {
obs.Log().Sugar().Fatalf("invalid key pair: %v", err)
logger.Sugar().Fatalf("invalid key pair: %v", err)
}

rootCAs, _ := x509.SystemCertPool()
Expand All @@ -101,11 +101,11 @@ func createTlsConfiguration(obs observability.Observability, tlsSettings configu
// Read in the cert file
certs, err := os.ReadFile(tlsSettings.RootCertificatePath)
if err != nil {
obs.Log().Sugar().Fatalf("Failed to append to RootCAs: %v", err)
logger.Sugar().Fatalf("Failed to append to RootCAs: %v", err)
}

if !rootCAs.AppendCertsFromPEM(certs) {
obs.Log().Sugar().Error("No certs appended, using system certs only")
logger.Sugar().Error("No certs appended, using system certs only")
}

return &tls.Config{
Expand Down
23 changes: 13 additions & 10 deletions mqtt/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,54 @@ import (
"testing"

"github.com/stretchr/testify/suite"
"github.com/xBlaz3kx/DevX/observability"
)

type mqttTestSuite struct {
suite.Suite
obs observability.Observability
}

func (suite *mqttTestSuite) SetupTest() {
suite.obs = observability.NewNoopObservability()
}

func (suite *mqttTestSuite) TestGetIdsFromTopic() {
expectedIds := []string{"examplePlugin"}
ids, err := GetIdsFromTopic(nil, "cmd/examplePlugin/execute", "cmd/+/execute")
ids, err := GetIdsFromTopic(suite.obs.Log(), "cmd/examplePlugin/execute", "cmd/+/execute")
suite.Require().NoError(err)
suite.Require().Equal(expectedIds, ids)

ids, err = GetIdsFromTopic(nil, "cmd/execute", "cmd/+/execute")
ids, err = GetIdsFromTopic(suite.obs.Log(), "cmd/execute", "cmd/+/execute")
suite.Require().Error(err)

ids, err = GetIdsFromTopic(nil, "ploogin/examplePlugin/execute", "cmd/+/execute")
ids, err = GetIdsFromTopic(suite.obs.Log(), "ploogin/examplePlugin/execute", "cmd/+/execute")
suite.Require().Error(err)

ids, err = GetIdsFromTopic(nil, "ploogin/examplePlugin/execute", "cmd/execute")
ids, err = GetIdsFromTopic(suite.obs.Log(), "ploogin/examplePlugin/execute", "cmd/execute")
suite.Require().Error(err)

ids, err = GetIdsFromTopic(nil, "cmd/examplePlugin/execute", "cmd/examplePlugin/execute")
ids, err = GetIdsFromTopic(suite.obs.Log(), "cmd/examplePlugin/execute", "cmd/examplePlugin/execute")
suite.Require().Error(err)

ids, err = GetIdsFromTopic(nil, "cmd/examplePlugin/execute/example2/abc", "cmd/+/execute/+/abc")
ids, err = GetIdsFromTopic(suite.obs.Log(), "cmd/examplePlugin/execute/example2/abc", "cmd/+/execute/+/abc")
suite.Require().NoError(err)
suite.Require().Equal([]string{"examplePlugin", "example2"}, ids)
}

func (suite *mqttTestSuite) TestCreateTopicWithIds() {
ids, err := CreateTopicWithIds(nil, "cmd/+/execute", "exampleId")
ids, err := CreateTopicWithIds(suite.obs.Log(), "cmd/+/execute", "exampleId")
suite.Require().NoError(err)
suite.Require().Equal("cmd/exampleId/execute", ids)

ids, err = CreateTopicWithIds(nil, "cmd/+/execute/+/", "exampleId1", "exampleId2")
ids, err = CreateTopicWithIds(suite.obs.Log(), "cmd/+/execute/+/", "exampleId1", "exampleId2")
suite.Require().NoError(err)
suite.Require().Equal("cmd/exampleId1/execute/exampleId2/", ids)

ids, err = CreateTopicWithIds(nil, "cmd/+/execute/+/", "exampleId")
ids, err = CreateTopicWithIds(suite.obs.Log(), "cmd/+/execute/+/", "exampleId")
suite.Require().Error(err)

ids, err = CreateTopicWithIds(nil, "cmd/+/execute/+/", "exampleId", "")
ids, err = CreateTopicWithIds(suite.obs.Log(), "cmd/+/execute/+/", "exampleId", "")
suite.Require().Error(err)
}

Expand Down

0 comments on commit 5410695

Please sign in to comment.