Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matthias Bertschy <matthias.bertschy@gmail.com>
  • Loading branch information
matthyx committed Nov 21, 2023
1 parent 137a0e4 commit 2bbbf54
Show file tree
Hide file tree
Showing 17 changed files with 1,196 additions and 32 deletions.
9 changes: 5 additions & 4 deletions adapters/backend/v1/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/kubescape/synchronizer/adapters"
"github.com/kubescape/synchronizer/domain"
"github.com/kubescape/synchronizer/messaging"
"github.com/kubescape/synchronizer/utils"
)

type Adapter struct {
Expand All @@ -31,15 +32,15 @@ func NewBackendAdapter(mainContext context.Context, messageProducer messaging.Me
var _ adapters.Adapter = (*Adapter)(nil)

func (b *Adapter) getClient(ctx context.Context) (adapters.Client, error) {
id := domain.ClientIdentifierFromContext(ctx)
id := utils.ClientIdentifierFromContext(ctx)
if client, ok := b.clientsMap.Load(id.String()); ok {
return client.(adapters.Client), nil
}
return nil, fmt.Errorf("unknown resource %s", id.String())
}

func (b *Adapter) Callbacks(ctx context.Context) (domain.Callbacks, error) {
id := domain.ClientIdentifierFromContext(ctx)
id := utils.ClientIdentifierFromContext(ctx)
if callbacks, ok := b.callbacksMap.Load(id.String()); ok {
return callbacks.(domain.Callbacks), nil
}
Expand Down Expand Up @@ -79,7 +80,7 @@ func (b *Adapter) PutObject(ctx context.Context, id domain.KindName, object []by
}

func (b *Adapter) RegisterCallbacks(ctx context.Context, callbacks domain.Callbacks) {
id := domain.ClientIdentifierFromContext(ctx)
id := utils.ClientIdentifierFromContext(ctx)
b.callbacksMap.Store(id.String(), callbacks)
}

Expand All @@ -89,7 +90,7 @@ func (b *Adapter) Start(ctx context.Context) error {
})

client := NewClient(b.producer)
id := domain.ClientIdentifierFromContext(ctx)
id := utils.ClientIdentifierFromContext(ctx)
b.clientsMap.Store(id.String(), client)
callbacks, err := b.Callbacks(ctx)
if err != nil {
Expand Down
14 changes: 7 additions & 7 deletions adapters/backend/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (c *Client) sendServerConnectedMessage(ctx context.Context) error {

depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
id := domain.ClientIdentifierFromContext(ctx)
id := utils.ClientIdentifierFromContext(ctx)

msg := messaging.ServerConnectedMessage{
Cluster: id.Cluster,
Expand Down Expand Up @@ -95,7 +95,7 @@ func (c *Client) PutObject(ctx context.Context, id domain.KindName, object []byt
return c.sendPutObjectMessage(ctx, id, object)
}

func (c *Client) RegisterCallbacks(mainCtx context.Context, callbacks domain.Callbacks) {
func (c *Client) RegisterCallbacks(_ context.Context, callbacks domain.Callbacks) {
c.callbacks = callbacks
}

Expand All @@ -111,7 +111,7 @@ func (c *Client) VerifyObject(ctx context.Context, id domain.KindName, checksum
func (c *Client) sendDeleteObjectMessage(ctx context.Context, id domain.KindName) error {
depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
cId := domain.ClientIdentifierFromContext(ctx)
cId := utils.ClientIdentifierFromContext(ctx)

msg := messaging.DeleteObjectMessage{
Cluster: cId.Cluster,
Expand All @@ -138,7 +138,7 @@ func (c *Client) sendDeleteObjectMessage(ctx context.Context, id domain.KindName
func (c *Client) sendGetObjectMessage(ctx context.Context, id domain.KindName, baseObject []byte) error {
depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
cId := domain.ClientIdentifierFromContext(ctx)
cId := utils.ClientIdentifierFromContext(ctx)

msg := messaging.GetObjectMessage{
BaseObject: baseObject,
Expand Down Expand Up @@ -167,7 +167,7 @@ func (c *Client) sendGetObjectMessage(ctx context.Context, id domain.KindName, b
func (c *Client) sendPatchObjectMessage(ctx context.Context, id domain.KindName, checksum string, patch []byte) error {
depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
cId := domain.ClientIdentifierFromContext(ctx)
cId := utils.ClientIdentifierFromContext(ctx)

msg := messaging.PatchObjectMessage{
Checksum: checksum,
Expand Down Expand Up @@ -198,7 +198,7 @@ func (c *Client) sendPatchObjectMessage(ctx context.Context, id domain.KindName,
func (c *Client) sendPutObjectMessage(ctx context.Context, id domain.KindName, object []byte) error {
depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
cId := domain.ClientIdentifierFromContext(ctx)
cId := utils.ClientIdentifierFromContext(ctx)

msg := messaging.PutObjectMessage{
Cluster: cId.Cluster,
Expand Down Expand Up @@ -227,7 +227,7 @@ func (c *Client) sendPutObjectMessage(ctx context.Context, id domain.KindName, o
func (c *Client) sendVerifyObjectMessage(ctx context.Context, id domain.KindName, checksum string) error {
depth := ctx.Value(domain.ContextKeyDepth).(int)
msgId := ctx.Value(domain.ContextKeyMsgId).(string)
cId := domain.ClientIdentifierFromContext(ctx)
cId := utils.ClientIdentifierFromContext(ctx)

msg := messaging.VerifyObjectMessage{
Checksum: checksum,
Expand Down
2 changes: 1 addition & 1 deletion adapters/backend/v1/pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ type PulsarMessageProducer struct {

func NewPulsarMessageProducer(cfg config.Config, pulsarClient pulsarconnector.Client) (*PulsarMessageProducer, error) {
topic := cfg.Backend.Topic
fullTopic := pulsarconnector.BuildPersistentTopic(pulsarClient.GetConfig().Tenant, pulsarClient.GetConfig().Namespace, pulsarconnector.TopicName(topic))
fullTopic := pulsarconnector.BuildPersistentTopic(pulsarClient.GetConfig().Tenant, pulsarClient.GetConfig().Namespace, topic)

options := pulsar.ProducerOptions{
DisableBatching: true,
Expand Down
4 changes: 2 additions & 2 deletions adapters/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (m *MockAdapter) saveIfNewer(id domain.KindName, newObject []byte) {
m.Resources[id.String()] = newObject
}

func (m *MockAdapter) RegisterCallbacks(mainCtx context.Context, callbacks domain.Callbacks) {
func (m *MockAdapter) RegisterCallbacks(_ context.Context, callbacks domain.Callbacks) {
m.callbacks = callbacks
}

Expand Down Expand Up @@ -148,7 +148,7 @@ func (m *MockAdapter) verifyObject(id domain.KindName, newChecksum string) ([]by
}
checksum, err := utils.CanonicalHash(object)
if err != nil {
return object, fmt.Errorf("calculate checksum: %w", err)
return nil, fmt.Errorf("calculate checksum: %w", err)
}
if checksum != newChecksum {
return object, fmt.Errorf("checksum mismatch: %s != %s", newChecksum, checksum)
Expand Down

0 comments on commit 2bbbf54

Please sign in to comment.