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

service/agent: Fix bug when soft-deleted agents are listed #100

Merged
merged 1 commit into from
Dec 11, 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
2 changes: 1 addition & 1 deletion internal/service/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

func (h *ServiceHandler) ListAgents(ctx context.Context, request server.ListAgentsRequestObject) (server.ListAgentsResponseObject, error) {
result, err := h.store.Agent().List(ctx, store.NewAgentQueryFilter(), store.NewAgentQueryOptions().WithIncludeSoftDeleted(true))
result, err := h.store.Agent().List(ctx, store.NewAgentQueryFilter(), store.NewAgentQueryOptions().WithIncludeSoftDeleted(false))
if err != nil {
return nil, err
}
Expand Down
24 changes: 22 additions & 2 deletions internal/service/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"reflect"
"time"

"github.com/google/uuid"
"github.com/kubev2v/migration-planner/internal/api/server"
Expand All @@ -17,8 +18,9 @@ import (
)

const (
insertAgentStm = "INSERT INTO agents (id, status, status_info, cred_url, version) VALUES ('%s', '%s', '%s', '%s', 'version_1');"
insertAssociatedAgentStm = "INSERT INTO agents (id, associated) VALUES ('%s', TRUE);"
insertAgentStm = "INSERT INTO agents (id, status, status_info, cred_url, version) VALUES ('%s', '%s', '%s', '%s', 'version_1');"
insertSoftDeletedAgentStm = "INSERT INTO agents (id, deleted_at) VALUES ('%s', '%s');"
insertAssociatedAgentStm = "INSERT INTO agents (id, associated) VALUES ('%s', TRUE);"
)

var _ = Describe("agent handler", Ordered, func() {
Expand Down Expand Up @@ -49,6 +51,24 @@ var _ = Describe("agent handler", Ordered, func() {

eventWriter := newTestWriter()
srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter))

resp, err := srv.ListAgents(context.TODO(), server.ListAgentsRequestObject{})
Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp)).To(Equal(reflect.TypeOf(server.ListAgents200JSONResponse{})))
Expect(resp).To(HaveLen(2))
})

It("successfully list agents -- except soft-deleted agents", func() {
tx := gormdb.Exec(fmt.Sprintf(insertAgentStm, "agent-1", "not-connected", "status-info-1", "cred_url-1"))
Expect(tx.Error).To(BeNil())
tx = gormdb.Exec(fmt.Sprintf(insertAgentStm, "agent-2", "not-connected", "status-info-2", "cred_url-2"))
Expect(tx.Error).To(BeNil())
tx = gormdb.Exec(fmt.Sprintf(insertSoftDeletedAgentStm, "agent-3", time.Now().Format(time.RFC3339)))
Expect(tx.Error).To(BeNil())

eventWriter := newTestWriter()
srv := service.NewServiceHandler(s, events.NewEventProducer(eventWriter))

resp, err := srv.ListAgents(context.TODO(), server.ListAgentsRequestObject{})
Expect(err).To(BeNil())
Expect(reflect.TypeOf(resp)).To(Equal(reflect.TypeOf(server.ListAgents200JSONResponse{})))
Expand Down
Loading