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

fix: pick random port instead of failing #42

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

fedeztk
Copy link

@fedeztk fedeztk commented Jun 3, 2023

This should fix #30.

It is important to notice that such fix does not ensure that all the tests will run successfully as expected, it simply won't fail to start by changing the ports of the container and the checks.
As an example, if we pick the scenario in the aforementioned issue, the test will still fail since nginx does not know which is the right port it should listen to.

Copy link
Member

@jacobtomlinson jacobtomlinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Many thanks for this. Would you be able to add a small test which opens a port and then tried to validate a container image that uses that same ports, then assert that a different port is used and that the validation passes as expected?

@fedeztk
Copy link
Author

fedeztk commented Jun 5, 2023

I'm having a bit of trouble figuring out how to test it.
First of all, where should I test this? The more reasonable place seems to be in validate_test.go, however, I can't seem to find a way to get the validatorConfig (that I need to check that the port has been changed):

func TestBusyPort(t *testing.T) {
	port := 8080
	go http.ListenAndServe(fmt.Sprintf(":%d", port), nil)

	assert := assert.New(t)
	b := new(bytes.Buffer)
	rootCmd.SetOut(b)
	rootCmd.SetErr(b)
	rootCmd.SetArgs([]string{"validate", "--file", "../examples/check-port.yaml", "testcontainers/helloworld"})
	err := rootCmd.Execute()

	assert.Nil(err, "should not error")
	assert.Contains(b.String(), "Validating testcontainers/helloworld against check-port", "did not validate")
	assert.Contains(b.String(), "validation passed", "did not pass")
        assert.NotEqual(port, ???) // how do I get the port from here? I need the validatorConfig
}

If I test it inside the validator package instead, I don't have access to a cobra.Command, needed to call the Validate function.

Can you please advise on how to solve this? Thanks!

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 this pull request may close these issues.

Container fails to start if port already in use
2 participants