Skip to content

Commit

Permalink
Added kafka testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Wil Simpson committed Mar 9, 2024
1 parent a5bb8e3 commit 18188e9
Show file tree
Hide file tree
Showing 7 changed files with 386 additions and 89 deletions.
235 changes: 199 additions & 36 deletions coverage.out

Large diffs are not rendered by default.

39 changes: 27 additions & 12 deletions pkg/mocks/chat_r_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions pkg/mocks/chat_s_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 0 additions & 14 deletions pkg/mocks/inventory_r_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

34 changes: 17 additions & 17 deletions pkg/repository/kafka_test.go
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@
package repository_test

import (
// "strconv"
"strconv"

. "github.com/onsi/ginkgo/v2"
// . "github.com/onsi/gomega"
//
// "github.com/ShatteredRealms/go-backend/pkg/config"
// "github.com/ShatteredRealms/go-backend/pkg/repository"
// testdb "github.com/ShatteredRealms/go-backend/test/db"
. "github.com/onsi/gomega"

"github.com/ShatteredRealms/go-backend/pkg/config"
"github.com/ShatteredRealms/go-backend/pkg/repository"
testdb "github.com/ShatteredRealms/go-backend/test/db"
)

var _ = Describe("Kafka repository", func() {
Describe("ConnectKafka", func() {
Context("valid input", func() {
It("should work", func() {
// cleanupFunc, _, kResource := testdb.SetupKafkaWithDocker()
// sPort := kResource.GetPort("29092/tcp")
// port, err := strconv.ParseUint(sPort, 10, 64)
// Expect(err).NotTo(HaveOccurred())
// kafka, err := repository.ConnectKafka(config.ServerAddress{
// Port: uint(port),
// Host: "127.0.0.1",
// })
// Expect(err).NotTo(HaveOccurred())
// Expect(kafka).NotTo(BeNil())
// cleanupFunc()
cleanupFunc, _, kResource := testdb.SetupKafkaWithDocker()
defer cleanupFunc()
sPort := kResource.GetPort("29093/tcp")
port, err := strconv.ParseUint(sPort, 10, 64)
Expect(err).NotTo(HaveOccurred())
kafka, err := repository.ConnectKafka(config.ServerAddress{
Port: uint(port),
Host: "127.0.0.1",
})
Expect(err).NotTo(HaveOccurred())
Expect(kafka).NotTo(BeNil())
})
})
})
Expand Down
129 changes: 129 additions & 0 deletions pkg/service/chat_s_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package service_test

import (
"context"
"fmt"
"strconv"
"time"

"github.com/ShatteredRealms/go-backend/pkg/config"
"github.com/ShatteredRealms/go-backend/pkg/mocks"
"github.com/ShatteredRealms/go-backend/pkg/model"
"github.com/ShatteredRealms/go-backend/pkg/service"
testdb "github.com/ShatteredRealms/go-backend/test/db"
"github.com/bxcodec/faker/v4"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/ory/dockertest/v3"
"github.com/sirupsen/logrus/hooks/test"
"go.uber.org/mock/gomock"
"gorm.io/gorm"
)

var _ = Describe("Chat service", Ordered, func() {

var (
hook *test.Hook

mockController *gomock.Controller
mockRepository *mocks.MockChatRepository
cleanupFunc func()
kResource *dockertest.Resource

chatService service.ChatService

kafkaPort uint

err error
fakeError = fmt.Errorf("error")
channels = []*model.ChatChannel{
{
Model: gorm.Model{
ID: 1,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
},
Name: faker.Username(),
Dimension: faker.Username(),
},
{
Model: gorm.Model{
ID: 2,
CreatedAt: time.Now().Add(time.Minute),
UpdatedAt: time.Now().Add(time.Minute),
},
Name: faker.Username(),
Dimension: faker.Username(),
},
}
)

BeforeAll(func() {
_, hook = test.NewNullLogger()
mockController = gomock.NewController(GinkgoT())
mockRepository = mocks.NewMockChatRepository(mockController)

cleanupFunc, _, kResource = testdb.SetupKafkaWithDocker()
sPort := kResource.GetPort("29093/tcp")
port, err := strconv.ParseUint(sPort, 10, 64)
kafkaPort = uint(port)
Expect(err).NotTo(HaveOccurred())
hook.Reset()
})

Describe("NewChatService", Ordered, func() {
When("given invalid input", func() {
It("should fail if migration fails", func() {
mockRepository.EXPECT().Migrate(gomock.Any()).Return(fakeError)
chatService, err = service.NewChatService(context.Background(), mockRepository, config.ServerAddress{
Port: kafkaPort,
Host: "localhost",
})

Expect(err).To(MatchError(fakeError))
Expect(chatService).To(BeNil())
})

It("should fail if kafka connect fails", func() {
mockRepository.EXPECT().Migrate(gomock.Any()).Return(nil)
chatService, err = service.NewChatService(context.Background(), mockRepository, config.ServerAddress{
Port: 0,
Host: "nowhere",
})

Expect(err).To(HaveOccurred())
Expect(chatService).To(BeNil())
})

It("should fail if kafka all channels", func() {
mockRepository.EXPECT().Migrate(gomock.Any()).Return(nil)
mockRepository.EXPECT().AllChannels(gomock.Any()).Return(model.ChatChannels{}, fakeError)
chatService, err = service.NewChatService(context.Background(), mockRepository, config.ServerAddress{
Port: kafkaPort,
Host: "localhost",
})

Expect(err).To(HaveOccurred())
Expect(chatService).To(BeNil())
})
})

When("valid input is given", func() {
It("should succeed", func() {
mockRepository.EXPECT().Migrate(gomock.Any()).Return(nil)
mockRepository.EXPECT().AllChannels(gomock.Any()).Return(channels, nil)
chatService, err = service.NewChatService(context.Background(), mockRepository, config.ServerAddress{
Port: kafkaPort,
Host: "localhost",
})

Expect(err).To(BeNil())
Expect(chatService).NotTo(BeNil())
})
})
})

AfterAll(func() {
cleanupFunc()
})
})
17 changes: 10 additions & 7 deletions test/db/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,16 @@ func SetupKafkaWithDocker() (func(), *dockertest.Resource, *dockertest.Resource)
Env: []string{
"KAFKA_BROKER_ID=1",
"KAFKA_ZOOKEEPER_CONNECT=gozookeeper:2181",
"KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://gokafka:9092,PLAINTEXT_HOST://gokafka:29092",
"KAFKA_ADVERTISED_LISTENERS=PLAINTEXT://localhost:9093,PLAINTEXT_HOST://localhost:29093",
"KAFKA_LISTENER_SECURITY_PROTOCOL_MAP=PLAINTEXT:PLAINTEXT,PLAINTEXT_HOST:PLAINTEXT",
"KAFKA_INTER_BROKER_LISTENER_NAME=PLAINTEXT",
// "KAFKA_OFFSETS_TOPIC_REPLICATION_FACTOR=1",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"39092/tcp": {{HostIP: "gokafka", HostPort: "29092/tcp"}},
"49092/tcp": {{HostIP: "gokafka", HostPort: "9092/tcp"}},
"29093/tcp": {{HostIP: "localhost", HostPort: "29093/tcp"}},
"9093/tcp": {{HostIP: "localhost", HostPort: "9093/tcp"}},
},
ExposedPorts: []string{"29092/tcp"},
ExposedPorts: []string{"29093/tcp"},
Networks: []*dockertest.Network{net},
}

Expand All @@ -85,9 +85,12 @@ func SetupKafkaWithDocker() (func(), *dockertest.Resource, *dockertest.Resource)
}))

fnCleanup := func() {
chk(kafkaResource.Close())
chk(zookeeperResource.Close())
chk(net.Close())
err1 := kafkaResource.Close()
err2 := zookeeperResource.Close()
err3 := net.Close()
chk(err1)
chk(err2)
chk(err3)
}

return fnCleanup, zookeeperResource, kafkaResource
Expand Down

0 comments on commit 18188e9

Please sign in to comment.