container-canary icon indicating copy to clipboard operation
container-canary copied to clipboard

fix: pick random port instead of failing

Open fedeztk opened this issue 2 years ago • 1 comments

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.

fedeztk avatar Jun 03 '23 15:06 fedeztk

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!

fedeztk avatar Jun 05 '23 15:06 fedeztk