diff options
| author | Jonathan Amsterdam <jba@google.com> | 2026-04-09 16:55:25 -0400 |
|---|---|---|
| committer | Jonathan Amsterdam <jba@google.com> | 2026-04-14 09:19:25 -0700 |
| commit | 81095845401a2ac64bc5ee43a4f9b013207d3b7e (patch) | |
| tree | 87fd6462fcbd32ddae5e9eaa0081569cc5415c7f /internal/api/api_test.go | |
| parent | aca48c6d9dcc4892027cdad3243527882201358b (diff) | |
| download | go-x-pkgsite-81095845401a2ac64bc5ee43a4f9b013207d3b7e.tar.xz | |
internal/api: test ServePackage with db
Test the ServePackage function against a real database, as well as the
fake data source.
- Modules and packages must be redistributable, or the DB strips
the documentation.
- Documentation must contain source code.
We must temporarily skip the dependency tests, to reduce the diffs on
this change. The test is still important, because it asserts that the
pkgsite command has only a few dependencies. To re-establish it, we
will move the API tests to internal/testing/api.
Change-Id: I67243ea2345c5e735edbdd6ee5bf2349a6b1eb60
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/765502
kokoro-CI: kokoro <noreply+kokoro@google.com>
LUCI-TryBot-Result: golang-scoped@luci-project-accounts.iam.gserviceaccount.com <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: Ethan Lee <ethanalee@google.com>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
Diffstat (limited to 'internal/api/api_test.go')
| -rw-r--r-- | internal/api/api_test.go | 102 |
1 files changed, 70 insertions, 32 deletions
diff --git a/internal/api/api_test.go b/internal/api/api_test.go index 64b891c7..1c233a7c 100644 --- a/internal/api/api_test.go +++ b/internal/api/api_test.go @@ -11,13 +11,17 @@ import ( "errors" "net/http" "net/http/httptest" + "os" "strconv" "testing" "github.com/google/go-cmp/cmp" "github.com/google/go-cmp/cmp/cmpopts" "golang.org/x/pkgsite/internal" + "golang.org/x/pkgsite/internal/derrors" + "golang.org/x/pkgsite/internal/log" "golang.org/x/pkgsite/internal/osv" + "golang.org/x/pkgsite/internal/postgres" "golang.org/x/pkgsite/internal/testing/fakedatasource" "golang.org/x/pkgsite/internal/testing/sample" "golang.org/x/pkgsite/internal/vuln" @@ -40,8 +44,46 @@ func (ds fallbackDataSource) GetUnitMeta(ctx context.Context, path, requestedMod } func TestServePackage(t *testing.T) { + t.Run("fake", func(t *testing.T) { + ds := fakedatasource.New() + mustInsert := func(ctx context.Context, m *internal.Module, _ bool) { + ds.MustInsertModule(ctx, m) + } + testServePackage(t, ds, mustInsert) + }) + t.Run("db", func(t *testing.T) { + orig := log.GetLevel() + t.Cleanup(func() { log.SetLevel(orig.String()) }) + log.SetLevel("Info") + const testDB = "pkgsite_api" + db, err := postgres.SetupTestDB(testDB) + if err != nil { + if errors.Is(err, derrors.NotFound) && os.Getenv("GO_DISCOVERY_TESTDB") != "true" { + t.Skipf("could not connect to DB (see doc/postgres.md to set up): %v", err) + } + t.Fatalf("setting up DB: %v", err) + } + defer db.Close() + mustInsert := func(ctx context.Context, m *internal.Module, latest bool) { + for _, u := range m.Units { + if !u.IsRedistributable { + t.Logf("unit %s is not redistributable; DB will strip documentation", u.Path) + } + } + if latest { + postgres.MustInsertModule(ctx, t, db, m) + } else { + postgres.MustInsertModuleNotLatest(ctx, t, db, m) + } + } + testServePackage(t, db, mustInsert) + }) +} + +func testServePackage(t *testing.T, ds internal.DataSource, mustInsertModule func(context.Context, *internal.Module, bool)) { + // mustInsert must not be called from subtests, because it captures + // a parent testing.T. ctx := context.Background() - ds := fakedatasource.New() const ( pkgPath = "example.com/a/b" @@ -50,7 +92,7 @@ func TestServePackage(t *testing.T) { version = "v1.2.3" ) - ds.MustInsertModule(ctx, &internal.Module{ + mustInsertModule(ctx, &internal.Module{ ModuleInfo: internal.ModuleInfo{ ModulePath: "example.com", Version: version, @@ -61,48 +103,46 @@ func TestServePackage(t *testing.T) { UnitMeta: internal.UnitMeta{ Path: "example.com/pkg", ModuleInfo: internal.ModuleInfo{ - ModulePath: "example.com", - Version: version, - LatestVersion: "v1.2.4", + ModulePath: "example.com", + Version: version, + LatestVersion: "v1.2.4", + IsRedistributable: true, }, Name: "pkg", }, - Documentation: []*internal.Documentation{sample.Documentation("linux", "amd64", sample.DocContents)}, - Licenses: sample.LicenseMetadata(), - Imports: []string{pkgPath}, + Documentation: []*internal.Documentation{sample.Documentation("linux", "amd64", sample.DocContents)}, + Licenses: sample.LicenseMetadata(), + Imports: []string{pkgPath}, + IsRedistributable: true, }}, - }) + }, false) for _, mp := range []string{modulePath1, modulePath2} { u := &internal.Unit{ UnitMeta: internal.UnitMeta{ Path: pkgPath, ModuleInfo: internal.ModuleInfo{ - ModulePath: mp, - Version: version, - LatestVersion: version, + ModulePath: mp, + Version: version, + LatestVersion: version, + IsRedistributable: true, }, Name: "b", }, - Documentation: []*internal.Documentation{ - { - GOOS: "linux", - GOARCH: "amd64", - Synopsis: "Synopsis for " + mp, - }, - }, + Documentation: []*internal.Documentation{sample.Documentation("linux", "amd64", sample.DocContents)}, + IsRedistributable: true, } - ds.MustInsertModule(ctx, &internal.Module{ + mustInsertModule(ctx, &internal.Module{ ModuleInfo: internal.ModuleInfo{ ModulePath: mp, Version: version, LatestVersion: version, }, Units: []*internal.Unit{u}, - }) + }, true) } - ds.MustInsertModule(ctx, &internal.Module{ + mustInsertModule(ctx, &internal.Module{ ModuleInfo: internal.ModuleInfo{ ModulePath: "example.com", Version: "v1.2.4", @@ -118,15 +158,12 @@ func TestServePackage(t *testing.T) { }, Name: "pkg", }, - Documentation: []*internal.Documentation{{ - GOOS: "linux", - GOARCH: "amd64", - Synopsis: "Basic synopsis", - }}, + Documentation: []*internal.Documentation{sample.Documentation("linux", "amd64", sample.DocContents)}, + IsRedistributable: true, }}, - }) + }, true) - ds.MustInsertModule(ctx, &internal.Module{ + mustInsertModule(ctx, &internal.Module{ ModuleInfo: internal.ModuleInfo{ ModulePath: "example.com/ex", Version: "v1.0.0", @@ -142,6 +179,7 @@ func TestServePackage(t *testing.T) { }, Name: "pkg", }, + IsRedistributable: true, Documentation: []*internal.Documentation{sample.DocumentationWithExamples("linux", "amd64", "", ` import "fmt" func Example() { @@ -150,7 +188,7 @@ func TestServePackage(t *testing.T) { } `)}, }}, - }) + }, true) for _, test := range []struct { name string @@ -194,7 +232,7 @@ func TestServePackage(t *testing.T) { Path: pkgPath, ModulePath: modulePath1, ModuleVersion: version, - Synopsis: "Synopsis for " + modulePath1, + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", IsLatest: true, GOOS: "linux", GOARCH: "amd64", @@ -222,7 +260,7 @@ func TestServePackage(t *testing.T) { Path: "example.com/pkg", ModulePath: "example.com", ModuleVersion: "v1.2.4", - Synopsis: "Basic synopsis", + Synopsis: "This is a package synopsis for GOOS=linux, GOARCH=amd64", IsLatest: true, GOOS: "linux", GOARCH: "amd64", |
