Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage with testContainers-go #711

Closed
majelbstoat opened this issue Mar 10, 2022 · 4 comments · Fixed by #712
Closed

Usage with testContainers-go #711

majelbstoat opened this issue Mar 10, 2022 · 4 comments · Fixed by #712

Comments

@majelbstoat
Copy link
Contributor

I'm trying to programmatically start fake-gcs-server using testcontainers-go. Example:

       // Start a container listening on port 3000 internally.
        storagePort := 3000
	container, err := testcontainers.GenericContainer(ctx, testcontainers.GenericContainerRequest{
		ContainerRequest: testcontainers.ContainerRequest{
			Image:          "fsouza/fake-gcs-server:latest",
			Entrypoint:     []string{"/bin/fake-gcs-server", "-data", "/data", "-scheme", "http"},
			BindMounts:     map[string]string{"/data": e.hostDir},
			ExposedPorts:   []string{TCPPort(storagePort)},
			WaitingFor:     wait.ForLog("server started at"),
			Networks:       []string{Network},
			NetworkAliases: map[string][]string{Network: {StorageHostname}},
		},
		Started: true,
	})
	if err != nil {
		return err
	}

        // map this internal port to a randomly chosen external port.
	port, err := container.MappedPort(ctx, storagePort)
	if err != nil {
		return err
	}

        // set the emulator host and create a client.
        os.Setenv("STORAGE_EMULATOR_HOST", fmt.Sprintf("localhost:%s", port.Port())
        client, err  := storage.NewClient()
}

As with other folks, I can curl --insecure http://localhost:XXXX/storage/v1/b/sample-bucket/o and see my files in there, where XXXX is the mapped external port, but I can't retrieve them using the storage client.

From other issues, I believe this is because I'm not setting a public host. My problem is, I don't know what the full public host will be until after the container starts, because that's when I get the mapped port.

As others have note, -public-host 0.0.0.0 and -public-host localhost don't work, so I have this circular dependency which I can't figure out how to break.

@fsouza
Copy link
Owner

fsouza commented Mar 10, 2022

@sergseven recently added an internal endpoint for updating some configuration parameters, aimed specifically at setting the external URL after starting the container with test containers, you can see a Java example in his PR: #659 (comment)

Let me know if that's enough information is enough for you.

We should probably document that endpoint in the README at some point.

@majelbstoat
Copy link
Contributor Author

So, that looks like it might be helpful, thanks! But, at the moment, it looks like it can only be used to change the ExternalUrl.

https://github.com/fsouza/fake-gcs-server/blob/main/fakestorage/config.go

I can submit a patch for changing the public host, if that's acceptable?

@fsouza
Copy link
Owner

fsouza commented Mar 10, 2022

@majelbstoat ohh sorry I misread your message. Yeah, that's acceptable, feel free to send the PR adding support for overriding the public host!

@majelbstoat
Copy link
Contributor Author

Cool, PR here: #712 :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants