Fix race condition and add some tests

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2024-06-18 16:42:24 +00:00
parent b51683f1ae
commit 37f6434ed8
3 changed files with 100 additions and 37 deletions

View file

@ -2,13 +2,13 @@ package watcher_test
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/suite"
"github.com/cloudbase/garm/database"
"github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/stretchr/testify/suite"
"github.com/cloudbase/garm/params"
)
type WatcherStoreTestSuite struct {
@ -19,27 +19,55 @@ type WatcherStoreTestSuite struct {
}
func (s *WatcherStoreTestSuite) TestGithubEndpointWatcher() {
// ghEpParams := params.CreateGithubEndpointParams{
// Name: "test",
// Description: "test endpoint",
// APIBaseURL: "https://api.ghes.example.com",
// UploadBaseURL: "https://upload.ghes.example.com",
// BaseURL: "https://ghes.example.com",
// }
}
func TestWatcherStoreTestSuite(t *testing.T) {
ctx := context.TODO()
watcher.InitWatcher(ctx)
store, err := database.NewDatabase(ctx, garmTesting.GetTestSqliteDBConfig(t))
if err != nil {
t.Fatalf("failed to create db connection: %s", err)
consumer, err := watcher.RegisterConsumer(
s.ctx, "gh-ep-test",
watcher.WithEntityTypeFilter(common.GithubEndpointEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
ghEpParams := params.CreateGithubEndpointParams{
Name: "test",
Description: "test endpoint",
APIBaseURL: "https://api.ghes.example.com",
UploadBaseURL: "https://upload.ghes.example.com",
BaseURL: "https://ghes.example.com",
}
watcherSuite := &WatcherStoreTestSuite{
ctx: context.TODO(),
store: store,
ghEp, err := s.store.CreateGithubEndpoint(s.ctx, ghEpParams)
s.Require().NoError(err)
s.Require().NotEmpty(ghEp.Name)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubEndpointEntityType,
Operation: common.CreateOperation,
Payload: ghEp,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
newDesc := "updated description"
updateParams := params.UpdateGithubEndpointParams{
Description: &newDesc,
}
updatedGhEp, err := s.store.UpdateGithubEndpoint(s.ctx, ghEp.Name, updateParams)
s.Require().NoError(err)
s.Require().Equal(newDesc, updatedGhEp.Description)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubEndpointEntityType,
Operation: common.UpdateOperation,
Payload: updatedGhEp,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
suite.Run(t, watcherSuite)
}