MCPcopy Create free account
hub / github.com/GoogleChrome/webstatus.dev / getTestDatabase

Function getTestDatabase

lib/gds/client_test.go:36–91  ·  view source on GitHub ↗

nolint: exhaustruct // No need to use every option of 3rd party struct.

(ctx context.Context, t *testing.T)

Source from the content-addressed store, hash-verified

34
35// nolint: exhaustruct // No need to use every option of 3rd party struct.
36func getTestDatabase(ctx context.Context, t *testing.T) (*Client, func()) {
37 datastoreFolder, err := filepath.Abs(filepath.Join(".", "..", "..", ".dev", "datastore"))
38 if err != nil {
39 t.Fatal(err)
40 }
41 req := testcontainers.ContainerRequest{
42 FromDockerfile: testcontainers.FromDockerfile{
43 Dockerfile: filepath.Join("Dockerfile"),
44 Context: datastoreFolder,
45 },
46 ExposedPorts: []string{"8086/tcp"},
47 WaitingFor: wait.ForHTTP("/").WithPort("8086/tcp"),
48 Name: "webstatus-dev-test-datastore",
49 }
50 container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
51 ContainerRequest: req,
52 Started: true,
53 })
54 if err != nil {
55 t.Fatal(err)
56 }
57
58 mappedPort, err := container.MappedPort(ctx, "8086")
59 if err != nil {
60 t.Fatal(err)
61 }
62
63 db := ""
64 dbPtr := &db
65 os.Setenv("DATASTORE_EMULATOR_HOST", fmt.Sprintf("localhost:%s", mappedPort.Port()))
66 dsClient, err := NewDatastoreClient(testDatastoreProject, dbPtr)
67 if err != nil {
68 if unsetErr := os.Unsetenv("DATASTORE_EMULATOR_HOST"); unsetErr != nil {
69 t.Errorf("failed to unset env. %s", unsetErr.Error())
70 }
71 if closeErr := dsClient.Close(); closeErr != nil {
72 t.Errorf("failed to close datastore client. %s", closeErr.Error())
73 }
74 if terminateErr := container.Terminate(ctx); terminateErr != nil {
75 t.Errorf("failed to terminate datastore. %s", terminateErr.Error())
76 }
77 t.Fatalf("failed to create datastore client. %s", err.Error())
78 }
79
80 return dsClient, func() {
81 if unsetErr := os.Unsetenv("DATASTORE_EMULATOR_HOST"); unsetErr != nil {
82 t.Errorf("failed to unset env. %s", unsetErr.Error())
83 }
84 if err := dsClient.Close(); err != nil {
85 t.Errorf("failed to close datastore client. %s", err.Error())
86 }
87 if err := container.Terminate(ctx); err != nil {
88 t.Errorf("failed to terminate datastore. %s", err.Error())
89 }
90 }
91}
92
93const sampleKey = "SampleData"

Callers 2

Calls 3

NewDatastoreClientFunction · 0.85
ErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected