Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create port additionalSubjects not populated in all cases #308

Merged
merged 3 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ debugConfig.yaml
.devcontainer/nats/resolver.conf
.devcontainer/nsc

# go work
go.work
go.work.sum

# helm test
test-output/

Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ generate: background-run .generate kill-running ## Generates code
go-run: ## Runs the app
@echo --- Running binary...
@date --rfc-3339=seconds
@go run main.go serve --dev
@go run main.go serve --dev --oidc=false

background-run: ## Runs in the app in the background
@date --rfc-3339=seconds
Expand Down
34 changes: 15 additions & 19 deletions internal/manualhooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -1007,30 +1007,26 @@ func PortHooks() []ent.Hook {
}

// Ensure we have additional relevant subjects in the event msg
addSubjPools, err := m.Client().Pool.Query().WithPorts(func(q *generated.PortQuery) {
q.WithLoadBalancer()
}).Where(pool.HasPortsWith(port.IDEQ(objID))).All(ctx)
addSubjPort, err := m.Client().Port.Query().WithPools().WithLoadBalancer().Where(port.IDEQ(objID)).Only(ctx)
if err == nil {
for _, pool := range addSubjPools {
for _, port := range pool.Edges.Ports {
if !slices.Contains(msg.AdditionalSubjectIDs, port.LoadBalancerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.LoadBalancerID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.LoadBalancerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.LoadBalancerID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.LocationID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.LocationID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.LocationID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.LocationID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.ProviderID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.ProviderID)
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.OwnerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.OwnerID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.OwnerID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, port.Edges.LoadBalancer.OwnerID)
}
}
if !slices.Contains(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.ProviderID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, addSubjPort.Edges.LoadBalancer.ProviderID)
}

if !slices.Contains(msg.AdditionalSubjectIDs, pool.ID) && objID != pool.ID {
for _, pool := range addSubjPort.Edges.Pools {
if !slices.Contains(msg.AdditionalSubjectIDs, pool.ID) {
msg.AdditionalSubjectIDs = append(msg.AdditionalSubjectIDs, pool.ID)
}

Expand Down
59 changes: 47 additions & 12 deletions internal/manualhooks/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -282,23 +282,58 @@ func Test_PortCreateHook(t *testing.T) {
changesChannel, err := testutils.EventsConn.SubscribeChanges(ctx, "create.load-balancer-port")
require.NoError(t, err, "failed to subscribe to changes")

lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{}).MustNew(ctx)

testutils.EntClient.Port.Use(manualhooks.PortHooks()...)

// Act
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)
t.Run("with pool", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)
msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, pool.OwnerID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs
// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})

t.Run("with multiple pools", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
pool := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
pool2 := (&testutils.PoolBuilder{OwnerID: lb.OwnerID}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{pool.ID, pool2.ID}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{pool.ID, pool2.ID, lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})

t.Run("with no pool", func(t *testing.T) {
// Act
lb := (&testutils.LoadBalancerBuilder{}).MustNew(ctx)
port := (&testutils.PortBuilder{PoolIDs: []gidx.PrefixedID{}, LoadBalancerID: lb.ID}).MustNew(ctx)

msg := testutils.ChannelReceiveWithTimeout[events.Message[events.ChangeMessage]](t, changesChannel, defaultTimeout)

// Assert
expectedAdditionalSubjectIDs := []gidx.PrefixedID{lb.ID, lb.LocationID, lb.ProviderID, lb.OwnerID}
actualAdditionalSubjectIDs := msg.Message().AdditionalSubjectIDs

assert.ElementsMatch(t, expectedAdditionalSubjectIDs, actualAdditionalSubjectIDs)
assert.Equal(t, port.ID, msg.Message().SubjectID)
assert.Equal(t, createEventType, msg.Message().EventType)
})
}

func Test_PortUpdateHook(t *testing.T) {
Expand Down