Skip to content

Commit

Permalink
fix tenant topic tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bbengfort committed Sep 11, 2024
1 parent c923cc4 commit e03daa1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 14 deletions.
5 changes: 5 additions & 0 deletions pkg/tenant/topics.go
Original file line number Diff line number Diff line change
Expand Up @@ -822,11 +822,16 @@ func (s *Server) TopicDelete(c *gin.Context) {
// Request topic delete from Ensign, which will destroy the topic and all of its data
if _, err = s.ensign.InvokeOnce(accessToken).DestroyTopic(ctx, topic.ID.String()); err != nil {
// TODO: Update with the standard errors defined by the SDK
serr, isStatusError := status.FromError(err)

switch {
case err.Error() == "not implemented yet":
sentry.Warn(c).Err(err).Msg("this version of the Go SDK does not support topic deletion")
c.JSON(http.StatusNotImplemented, api.ErrorResponse("deleting a topic is not supported"))
return
case isStatusError && serr.Code() == codes.NotFound:
c.JSON(http.StatusNotFound, api.ErrorResponse("topic not found"))
return
default:
sentry.Debug(c).Err(err).Msg("tracing ensign error in tenant")
c.JSON(http.StatusInternalServerError, api.ErrorResponse("could not delete topic"))
Expand Down
28 changes: 14 additions & 14 deletions pkg/tenant/topics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1129,17 +1129,16 @@ func (suite *tenantTestSuite) TestTopicUpdate() {
suite.requireError(err, http.StatusBadRequest, "topic is already being deleted", "expected error when topic is already being deleted")

// Valid request to update the topic state.
// TODO: Update this test when topic archive is implemented in the SDK.
topic.State = sdk.TopicState_UNDEFINED
topic.State = sdk.TopicState_READY
data, err = topic.MarshalValue()
require.NoError(err, "could not marshal the topic data")
_, err = suite.client.TopicUpdate(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "archiving a topic is not supported")
require.NoError(err, "could not update topic")

// Make another topic update request to exercise the cache.
req.Name = "AnotherTopicName"
_, err = suite.client.TopicUpdate(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "archiving a topic is not supported")
require.NoError(err, "could not update topic")

// Quarterdeck should only be called once, subsequent calls should use the cache.
require.Equal(1, suite.quarterdeck.ProjectsAccessCount(), "expected only one call to Quarterdeck for project access")
Expand Down Expand Up @@ -1181,14 +1180,14 @@ func (suite *tenantTestSuite) TestTopicUpdate() {
return nil, status.Error(codes.NotFound, "could not archive topic")
}
_, err = suite.client.TopicUpdate(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "archiving a topic is not supported", "expected error when Ensign returns an error")
suite.requireError(err, http.StatusInternalServerError, "could not update topic", "expected error when Ensign returns an error")

// Should return an error if Ensign returns an error.
suite.ensign.OnDeleteTopic = func(ctx context.Context, req *sdk.TopicMod) (*sdk.TopicStatus, error) {
return nil, status.Error(codes.Internal, "could not archive topic")
}
_, err = suite.client.TopicUpdate(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "archiving a topic is not supported", "expected error when Ensign returns an error")
suite.requireError(err, http.StatusInternalServerError, "could not update topic", "expected error when Ensign returns an error")
}

func (suite *tenantTestSuite) TestTopicDelete() {
Expand Down Expand Up @@ -1331,14 +1330,15 @@ func (suite *tenantTestSuite) TestTopicDelete() {
suite.requireError(err, http.StatusPreconditionFailed, "invalid confirmation token", "expected error when wrong token is provided")

// Valid delete request
// TODO: Update when the DestroyTopic is implemented in the Go SDK.
req.Token = reply.Token
_, err = suite.client.TopicDelete(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "deleting a topic is not supported")
confirm, err := suite.client.TopicDelete(ctx, req)
require.NoError(err, "could not delete topic")
require.Equal(sdk.TopicState_DELETING.String(), confirm.Status, "unexpected delete status returned")

// Make a second call to the delete endpoint to exercise the cache.
_, err = suite.client.TopicDelete(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "deleting a topic is not supported")
confirm, err = suite.client.TopicDelete(ctx, req)
require.NoError(err, "could not delete topic cache")
require.Equal(sdk.TopicState_DELETING.String(), confirm.Status, "unexpected delete status returned")

// Quarterdeck should only be called once, subsequent calls should use the cache.
require.Equal(1, suite.quarterdeck.ProjectsAccessCount(), "expected only one call to Quarterdeck for project access")
Expand Down Expand Up @@ -1376,15 +1376,15 @@ func (suite *tenantTestSuite) TestTopicDelete() {
// Should return not found if Ensign returns not found.
suite.quarterdeck.OnProjectsAccess(mock.UseStatus(http.StatusOK), mock.UseJSONFixture(qdReply))
suite.ensign.OnDeleteTopic = func(ctx context.Context, req *sdk.TopicMod) (*sdk.TopicStatus, error) {
return nil, status.Error(codes.NotFound, "could not delete topic")
return nil, status.Error(codes.NotFound, "topic not found")
}
_, err = suite.client.TopicDelete(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "deleting a topic is not supported", "expected error when Ensign returns an error")
suite.requireError(err, http.StatusNotFound, "topic not found", "expected error when Ensign returns an error")

// Should return an error if Ensign returns an error.
suite.ensign.OnDeleteTopic = func(ctx context.Context, req *sdk.TopicMod) (*sdk.TopicStatus, error) {
return nil, status.Error(codes.Internal, "could not delete topic")
}
_, err = suite.client.TopicDelete(ctx, req)
suite.requireError(err, http.StatusNotImplemented, "deleting a topic is not supported", "expected error when Ensign returns an error")
suite.requireError(err, http.StatusInternalServerError, "could not delete topic", "expected error when Ensign returns an error")
}
Binary file modified pkg/utils/backups/testdata/sqlite.db
Binary file not shown.

0 comments on commit e03daa1

Please sign in to comment.