Add more watcher tests

Signed-off-by: Gabriel Adrian Samfira <gsamfira@cloudbasesolutions.com>
This commit is contained in:
Gabriel Adrian Samfira 2024-06-18 17:45:48 +00:00
parent 37f6434ed8
commit 8a79d9e8f9
5 changed files with 366 additions and 23 deletions

View file

@ -8,6 +8,7 @@ import (
"github.com/cloudbase/garm/database/common"
"github.com/cloudbase/garm/database/watcher"
garmTesting "github.com/cloudbase/garm/internal/testing"
"github.com/cloudbase/garm/params"
)
@ -18,16 +19,292 @@ type WatcherStoreTestSuite struct {
ctx context.Context
}
func (s *WatcherStoreTestSuite) TestEnterpriseWatcher() {
consumer, err := watcher.RegisterConsumer(
s.ctx, "enterprise-test",
watcher.WithEntityTypeFilter(common.EnterpriseEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation),
watcher.WithOperationTypeFilter(common.DeleteOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
s.T().Cleanup(func() { consumer.Close() })
ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T())
creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep)
s.T().Cleanup(func() { s.store.DeleteGithubCredentials(s.ctx, creds.ID) })
ent, err := s.store.CreateEnterprise(s.ctx, "test-enterprise", creds.Name, "test-secret", params.PoolBalancerTypeRoundRobin)
s.Require().NoError(err)
s.Require().NotEmpty(ent.ID)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.EnterpriseEntityType,
Operation: common.CreateOperation,
Payload: ent,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
updateParams := params.UpdateEntityParams{
WebhookSecret: "updated",
}
updatedEnt, err := s.store.UpdateEnterprise(s.ctx, ent.ID, updateParams)
s.Require().NoError(err)
s.Require().Equal("updated", updatedEnt.WebhookSecret)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.EnterpriseEntityType,
Operation: common.UpdateOperation,
Payload: updatedEnt,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
err = s.store.DeleteEnterprise(s.ctx, ent.ID)
s.Require().NoError(err)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.EnterpriseEntityType,
Operation: common.DeleteOperation,
Payload: updatedEnt,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
}
func (s *WatcherStoreTestSuite) TestOrgWatcher() {
consumer, err := watcher.RegisterConsumer(
s.ctx, "org-test",
watcher.WithEntityTypeFilter(common.OrganizationEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation),
watcher.WithOperationTypeFilter(common.DeleteOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
s.T().Cleanup(func() { consumer.Close() })
ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T())
creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep)
s.T().Cleanup(func() { s.store.DeleteGithubCredentials(s.ctx, creds.ID) })
org, err := s.store.CreateOrganization(s.ctx, "test-org", creds.Name, "test-secret", params.PoolBalancerTypeRoundRobin)
s.Require().NoError(err)
s.Require().NotEmpty(org.ID)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.OrganizationEntityType,
Operation: common.CreateOperation,
Payload: org,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
updateParams := params.UpdateEntityParams{
WebhookSecret: "updated",
}
updatedOrg, err := s.store.UpdateOrganization(s.ctx, org.ID, updateParams)
s.Require().NoError(err)
s.Require().Equal("updated", updatedOrg.WebhookSecret)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.OrganizationEntityType,
Operation: common.UpdateOperation,
Payload: updatedOrg,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
err = s.store.DeleteOrganization(s.ctx, org.ID)
s.Require().NoError(err)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.OrganizationEntityType,
Operation: common.DeleteOperation,
Payload: updatedOrg,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
}
func (s *WatcherStoreTestSuite) TestRepoWatcher() {
consumer, err := watcher.RegisterConsumer(
s.ctx, "repo-test",
watcher.WithEntityTypeFilter(common.RepositoryEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation),
watcher.WithOperationTypeFilter(common.DeleteOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
s.T().Cleanup(func() { consumer.Close() })
ep := garmTesting.CreateDefaultGithubEndpoint(s.ctx, s.store, s.T())
creds := garmTesting.CreateTestGithubCredentials(s.ctx, "test-creds", s.store, s.T(), ep)
s.T().Cleanup(func() { s.store.DeleteGithubCredentials(s.ctx, creds.ID) })
repo, err := s.store.CreateRepository(s.ctx, "test-owner", "test-repo", creds.Name, "test-secret", params.PoolBalancerTypeRoundRobin)
s.Require().NoError(err)
s.Require().NotEmpty(repo.ID)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.RepositoryEntityType,
Operation: common.CreateOperation,
Payload: repo,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
newSecret := "updated"
updateParams := params.UpdateEntityParams{
WebhookSecret: newSecret,
}
updatedRepo, err := s.store.UpdateRepository(s.ctx, repo.ID, updateParams)
s.Require().NoError(err)
s.Require().Equal(newSecret, updatedRepo.WebhookSecret)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.RepositoryEntityType,
Operation: common.UpdateOperation,
Payload: updatedRepo,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
err = s.store.DeleteRepository(s.ctx, repo.ID)
s.Require().NoError(err)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.RepositoryEntityType,
Operation: common.DeleteOperation,
Payload: updatedRepo,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
}
func (s *WatcherStoreTestSuite) TestGithubCredentialsWatcher() {
consumer, err := watcher.RegisterConsumer(
s.ctx, "gh-cred-test",
watcher.WithEntityTypeFilter(common.GithubCredentialsEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation),
watcher.WithOperationTypeFilter(common.DeleteOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
s.T().Cleanup(func() { consumer.Close() })
ghCredParams := params.CreateGithubCredentialsParams{
Name: "test-creds",
Description: "test credentials",
Endpoint: "github.com",
AuthType: params.GithubAuthTypePAT,
PAT: params.GithubPAT{
OAuth2Token: "bogus",
},
}
ghCred, err := s.store.CreateGithubCredentials(s.ctx, ghCredParams)
s.Require().NoError(err)
s.Require().NotEmpty(ghCred.ID)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubCredentialsEntityType,
Operation: common.CreateOperation,
Payload: ghCred,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
newDesc := "updated description"
updateParams := params.UpdateGithubCredentialsParams{
Description: &newDesc,
}
updatedGhCred, err := s.store.UpdateGithubCredentials(s.ctx, ghCred.ID, updateParams)
s.Require().NoError(err)
s.Require().Equal(newDesc, updatedGhCred.Description)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubCredentialsEntityType,
Operation: common.UpdateOperation,
Payload: updatedGhCred,
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
err = s.store.DeleteGithubCredentials(s.ctx, ghCred.ID)
s.Require().NoError(err)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubCredentialsEntityType,
Operation: common.DeleteOperation,
// We only get the ID and Name of the deleted entity
Payload: params.GithubCredentials{ID: ghCred.ID, Name: ghCred.Name},
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
}
func (s *WatcherStoreTestSuite) TestGithubEndpointWatcher() {
consumer, err := watcher.RegisterConsumer(
s.ctx, "gh-ep-test",
watcher.WithEntityTypeFilter(common.GithubEndpointEntityType),
watcher.WithAny(
watcher.WithOperationTypeFilter(common.CreateOperation),
watcher.WithOperationTypeFilter(common.UpdateOperation)),
watcher.WithOperationTypeFilter(common.UpdateOperation),
watcher.WithOperationTypeFilter(common.DeleteOperation)),
)
s.Require().NoError(err)
s.Require().NotNil(consumer)
s.T().Cleanup(func() { consumer.Close() })
ghEpParams := params.CreateGithubEndpointParams{
Name: "test",
Description: "test endpoint",
@ -70,4 +347,19 @@ func (s *WatcherStoreTestSuite) TestGithubEndpointWatcher() {
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
err = s.store.DeleteGithubEndpoint(s.ctx, ghEp.Name)
s.Require().NoError(err)
select {
case event := <-consumer.Watch():
s.Require().Equal(common.ChangePayload{
EntityType: common.GithubEndpointEntityType,
Operation: common.DeleteOperation,
// We only get the name of the deleted entity
Payload: params.GithubEndpoint{Name: ghEp.Name},
}, event)
case <-time.After(1 * time.Second):
s.T().Fatal("expected payload not received")
}
}

View file

@ -163,17 +163,17 @@ func TestWatcherTestSuite(t *testing.T) {
}
suite.Run(t, watcherSuite)
// These tests run store changes and make sure that the store properly
// triggers watcher notifications.
ctx := context.TODO()
ctx := context.Background()
watcher.InitWatcher(ctx)
store, err := database.NewDatabase(ctx, garmTesting.GetTestSqliteDBConfig(t))
if err != nil {
t.Fatalf("failed to create db connection: %s", err)
}
adminCtx := garmTesting.ImpersonateAdminContext(ctx, store, t)
watcherStoreSuite := &WatcherStoreTestSuite{
ctx: context.TODO(),
ctx: adminCtx,
store: store,
}
suite.Run(t, watcherStoreSuite)