func StartPGAdapterWithCredentials()

in benchmarks/latency-comparison/golang/runners/pgadapter/pgadapter.go [51:102]


func StartPGAdapterWithCredentials(ctx context.Context, project, instance string, credentials *google.Credentials) (port int, cleanup func(), err error) {
	if credentials == nil {
		return 0, func() {}, fmt.Errorf("credentials cannot be nil")
	}
	if credentials.JSON == nil || len(credentials.JSON) == 0 {
		return 0, func() {}, fmt.Errorf("only JSON based credentials are supported")
	}
	credentialsFile, err := os.CreateTemp(os.TempDir(), "pgadapter-credentials")
	if err != nil {
		return 0, func() {}, err
	}
	defer os.RemoveAll(credentialsFile.Name())
	if _, err = credentialsFile.Write(credentials.JSON); err != nil {
		return 0, func() {}, err
	}
	req := testcontainers.ContainerRequest{
		Image:           "gcr.io/cloud-spanner-pg-adapter/pgadapter",
		AlwaysPullImage: true,
		HostConfigModifier: func(config *container.HostConfig) {
			config.AutoRemove = true
			config.Binds = []string{"/tmp:/tmp"}
		},
		Cmd: []string{
			"-p", project,
			"-i", instance,
			"-x",
		},
		Files: []testcontainers.ContainerFile{
			{
				HostFilePath:      credentialsFile.Name(),
				ContainerFilePath: "/credentials.json",
				FileMode:          700,
			},
		},
		Env:          map[string]string{"GOOGLE_APPLICATION_CREDENTIALS": "/credentials.json"},
		ExposedPorts: []string{"5432/tcp"},
		WaitingFor:   wait.ForExposedPort(),
	}
	pgadapter, err = testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: req,
		Started:          true,
	})
	if err != nil {
		return 0, func() {}, err
	}
	mappedPort, err := pgadapter.MappedPort(ctx, "5432/tcp")
	if err != nil {
		return 0, stopPGAdapter, err
	}
	time.Sleep(time.Second)
	return mappedPort.Int(), stopPGAdapter, nil
}