Skip to content

Commit

Permalink
Merge pull request #2977 from infrahq/dnephin/test-refactor-map
Browse files Browse the repository at this point in the history
Refactor a test function and remove a test helper
  • Loading branch information
dnephin committed Aug 22, 2022
2 parents 3dc86d8 + a1ff254 commit c88c3cf
Show file tree
Hide file tree
Showing 26 changed files with 101 additions and 139 deletions.
2 changes: 0 additions & 2 deletions internal/access/access_test.go
Expand Up @@ -30,8 +30,6 @@ func setupDB(t *testing.T) *gorm.DB {
patch.ModelsSymmetricKey(t)
db, err := data.NewDB(driver.Dialector, nil)
assert.NilError(t, err)
t.Cleanup(data.InvalidateCache)

return db.DB
}

Expand Down
2 changes: 0 additions & 2 deletions internal/cmd/login_test.go
Expand Up @@ -25,7 +25,6 @@ import (
"github.com/infrahq/infra/internal/cmd/types"
"github.com/infrahq/infra/internal/race"
"github.com/infrahq/infra/internal/server"
"github.com/infrahq/infra/internal/server/data"
"github.com/infrahq/infra/internal/testing/database"
"github.com/infrahq/infra/uid"
)
Expand Down Expand Up @@ -208,7 +207,6 @@ func setupEnv(t *testing.T) string {

func setupServerOptions(t *testing.T, opts *server.Options) {
t.Helper()
t.Cleanup(data.InvalidateCache)

opts.Addr = server.ListenerOptions{HTTPS: "127.0.0.1:0", HTTP: "127.0.0.1:0"}

Expand Down
4 changes: 1 addition & 3 deletions internal/openapigen/main.go
Expand Up @@ -4,8 +4,6 @@ import (
"fmt"
"os"

"github.com/prometheus/client_golang/prometheus"

"github.com/infrahq/infra/internal"
"github.com/infrahq/infra/internal/server"
)
Expand All @@ -24,7 +22,7 @@ func run(args []string) error {
filename := args[0]

s := server.Server{}
routes := s.GenerateRoutes(prometheus.NewRegistry())
routes := s.GenerateRoutes()

return server.WriteOpenAPIDocToFile(routes.OpenAPIDocument, internal.FullVersion(), filename)
}
7 changes: 3 additions & 4 deletions internal/server/access_keys_test.go
Expand Up @@ -10,7 +10,6 @@ import (
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"gotest.tools/v3/assert"

"github.com/infrahq/infra/api"
Expand All @@ -27,7 +26,7 @@ func TestAPI_CreateAccessKey(t *testing.T) {
}

srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

userResp := createUser(t, srv, routes, "usera@example.com")

Expand Down Expand Up @@ -118,7 +117,7 @@ func TestAPI_CreateAccessKey(t *testing.T) {

func TestAPI_ListAccessKeys_Success(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

run := func() api.ListResponse[api.AccessKey] {
req := httptest.NewRequest(http.MethodGet, "/api/access-keys", nil)
Expand Down Expand Up @@ -146,7 +145,7 @@ func TestAPI_ListAccessKeys_Success(t *testing.T) {

func TestAPI_ListAccessKeys(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

db := srv.DB()

Expand Down
1 change: 0 additions & 1 deletion internal/server/authn/authn_method_test.go
Expand Up @@ -27,7 +27,6 @@ func setupDB(t *testing.T) *gorm.DB {
patch.ModelsSymmetricKey(t)
db, err := data.NewDB(driver.Dialector, nil)
assert.NilError(t, err)
t.Cleanup(data.InvalidateCache)

return db.DB
}
Expand Down
3 changes: 1 addition & 2 deletions internal/server/authn_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"net/http/httptest"
"testing"

"github.com/prometheus/client_golang/prometheus"
"gotest.tools/v3/assert"

"github.com/infrahq/infra/api"
Expand All @@ -16,7 +15,7 @@ import (

func TestServerLimitsAccessWithTemporaryPassword(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes((prometheus.NewRegistry()))
routes := srv.GenerateRoutes()

// create a user
resp := createUser(t, srv, routes, "hubert@example.com")
Expand Down
15 changes: 4 additions & 11 deletions internal/server/data/data.go
Expand Up @@ -359,9 +359,8 @@ func GlobalCount[T models.Modelable](db *gorm.DB, selectors ...SelectorFunc) (in
return count, nil
}

// InfraProvider is a lazy-loaded cached reference to the infra provider. The
// cache lasts for the entire lifetime of the process, so any test or test
// helper that calls InfraProvider must call InvalidateCache to clean up.
// InfraProvider returns the infra provider for the organization set in the db
// context.
func InfraProvider(db *gorm.DB) *models.Provider {
org := MustGetOrgFromContext(db.Statement.Context)
infra, err := get[models.Provider](db, ByProviderKind(models.ProviderKindInfra), ByOrgID(org.ID))
Expand All @@ -372,20 +371,14 @@ func InfraProvider(db *gorm.DB) *models.Provider {
return infra
}

// InfraConnectorIdentity is a lazy-loaded reference to the connector identity.
// The cache lasts for the entire lifetime of the process, so any test or test
// helper that calls InfraConnectorIdentity must call InvalidateCache to clean up.
// InfraConnectorIdentity returns the connector identity for the organization set
// in the db context.
func InfraConnectorIdentity(db *gorm.DB) *models.Identity {
org := MustGetOrgFromContext(db.Statement.Context)
connector, err := GetIdentity(db, ByName(models.InternalInfraConnectorIdentityName), ByOrgID(org.ID))
if err != nil {
logging.L.Panic().Err(err).Msg("failed to retrieve connector identity")
return nil // unreachable, the line above panics
}

return connector
}

// InvalidateCache is used to clear references to frequently used resources
func InvalidateCache() {
}
1 change: 0 additions & 1 deletion internal/server/data/data_test.go
Expand Up @@ -24,7 +24,6 @@ func setupDB(t *testing.T, driver gorm.Dialector) *gorm.DB {
assert.NilError(t, err)

logging.PatchLogger(t, zerolog.NewTestWriter(t))
t.Cleanup(InvalidateCache)

return db.DB
}
Expand Down
5 changes: 2 additions & 3 deletions internal/server/debug_test.go
Expand Up @@ -7,7 +7,6 @@ import (
"testing"
"time"

"github.com/prometheus/client_golang/prometheus"
"gorm.io/gorm"
"gotest.tools/v3/assert"
is "gotest.tools/v3/assert/cmp"
Expand All @@ -26,8 +25,8 @@ func TestAPI_PProfHandler(t *testing.T) {
expectedResp func(t *testing.T, resp *httptest.ResponseRecorder)
}

s := &Server{db: setupDB(t)}
routes := s.GenerateRoutes(prometheus.NewRegistry())
s := setupServer(t)
routes := s.GenerateRoutes()

run := func(t *testing.T, tc testCase) {
// nolint:noctx
Expand Down
3 changes: 1 addition & 2 deletions internal/server/destination_test.go
Expand Up @@ -9,15 +9,14 @@ import (
"time"

gocmp "github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"gotest.tools/v3/assert"

"github.com/infrahq/infra/api"
)

func TestAPI_CreateDestination(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

type testCase struct {
name string
Expand Down
9 changes: 4 additions & 5 deletions internal/server/grants_test.go
Expand Up @@ -11,7 +11,6 @@ import (
"time"

gocmp "github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"gotest.tools/v3/assert"

"github.com/infrahq/infra/api"
Expand All @@ -22,7 +21,7 @@ import (

func TestAPI_ListGrants(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

createID := func(t *testing.T, name string) uid.ID {
t.Helper()
Expand Down Expand Up @@ -416,7 +415,7 @@ func TestAPI_ListGrants(t *testing.T) {

func TestAPI_ListGrants_InheritedGrants(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

createID := func(t *testing.T, name string) uid.ID {
t.Helper()
Expand Down Expand Up @@ -615,7 +614,7 @@ var cmpAPIGrantJSON = gocmp.Options{

func TestAPI_CreateGrant(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

accessKey, err := data.ValidateAccessKey(srv.DB(), adminAccessKey(srv))
assert.NilError(t, err)
Expand Down Expand Up @@ -776,7 +775,7 @@ func TestAPI_CreateGrant(t *testing.T) {

func TestAPI_DeleteGrant(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

user := &models.Identity{Name: "non-admin"}

Expand Down
9 changes: 4 additions & 5 deletions internal/server/groups_test.go
Expand Up @@ -9,7 +9,6 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"gorm.io/gorm"
"gotest.tools/v3/assert"

Expand Down Expand Up @@ -37,7 +36,7 @@ func createGroups(t *testing.T, db *gorm.DB, groups ...*models.Group) {

func TestAPI_ListGroups(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

var (
humans = models.Group{Name: "humans"}
Expand Down Expand Up @@ -193,7 +192,7 @@ func TestAPI_ListGroups(t *testing.T) {

func TestAPI_CreateGroup(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

type testCase struct {
setup func(t *testing.T, req *http.Request)
Expand Down Expand Up @@ -276,7 +275,7 @@ func TestAPI_CreateGroup(t *testing.T) {

func TestAPI_DeleteGroup(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

var humans = models.Group{Name: "humans"}
createGroups(t, srv.DB(), &humans)
Expand Down Expand Up @@ -354,7 +353,7 @@ func TestAPI_DeleteGroup(t *testing.T) {

func TestAPI_UpdateUsersInGroup(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

var humans = models.Group{Name: "humans"}
createGroups(t, srv.DB(), &humans)
Expand Down
3 changes: 1 addition & 2 deletions internal/server/handlers_test.go
Expand Up @@ -11,7 +11,6 @@ import (

"github.com/gin-gonic/gin"
gocmp "github.com/google/go-cmp/cmp"
"github.com/prometheus/client_golang/prometheus"
"gorm.io/gorm"
"gotest.tools/v3/assert"

Expand Down Expand Up @@ -165,7 +164,7 @@ var cmpAPIUserJSON = gocmp.Options{

func TestWellKnownJWKs(t *testing.T) {
srv := setupServer(t, withAdminUser, withSupportAdminGrant)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

type testCase struct {
name string
Expand Down
3 changes: 1 addition & 2 deletions internal/server/login_test.go
Expand Up @@ -6,7 +6,6 @@ import (
"net/http/httptest"
"testing"

"github.com/prometheus/client_golang/prometheus"
"golang.org/x/crypto/bcrypt"
"gotest.tools/v3/assert"

Expand All @@ -18,7 +17,7 @@ import (

func TestAPI_Login(t *testing.T) {
srv := setupServer(t, withAdminUser)
routes := srv.GenerateRoutes(prometheus.NewRegistry())
routes := srv.GenerateRoutes()

// setup user to login as
user := &models.Identity{Name: "steve"}
Expand Down

0 comments on commit c88c3cf

Please sign in to comment.