Skip to content

Commit

Permalink
remove startupTimeout from the Container class
Browse files Browse the repository at this point in the history
Signed-off-by: James Lamb <[email protected]>
  • Loading branch information
jameslamb committed Jul 31, 2024
1 parent bc8f4e6 commit e5250d1
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 15 deletions.
7 changes: 3 additions & 4 deletions internal/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ type ContainerInfo struct {
}

type ContainerInterface interface {
Start() error
Start(timeoutSeconds int) error
Remove() error
Status() (*ContainerInfo, error)
Exec(command ...string) (string, error)
Logs() (string, error)
GetStartupTimeout() int
}

func New(image string, env []v1.EnvVar, ports []v1.ServicePort, volumes []canaryv1.Volume, command []string, dockerRunOptions []string, startupTimeout int) ContainerInterface {
func New(image string, env []v1.EnvVar, ports []v1.ServicePort, volumes []canaryv1.Volume, command []string, dockerRunOptions []string) ContainerInterface {
name := fmt.Sprintf("%s%s", "canary-runner-", uuid.New().String()[:8])
return &DockerContainer{Name: name, Image: image, Command: command, Env: env, Ports: ports, Volumes: volumes, RunOptions: dockerRunOptions, StartupTimeout: startupTimeout}
return &DockerContainer{Name: name, Image: image, Command: command, Env: env, Ports: ports, Volumes: volumes, RunOptions: dockerRunOptions}
}
6 changes: 3 additions & 3 deletions internal/container/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type DockerContainer struct {
}

// Start a container
func (c *DockerContainer) Start() error {
func (c *DockerContainer) Start(timeoutSeconds int) error {

commandArgs := []string{"run", "-d"}

Expand Down Expand Up @@ -79,12 +79,12 @@ func (c *DockerContainer) Start() error {
if info.State.Running {
break
}
if time.Since(startTime) > (time.Second * time.Duration(c.StartupTimeout)) {
if time.Since(startTime) > (time.Second * time.Duration(timeoutSeconds)) {
err := c.Remove()
if err != nil {
return err
}
return fmt.Errorf("container failed to start after %d seconds", c.StartupTimeout)
return fmt.Errorf("container failed to start after %d seconds", timeoutSeconds)
}
time.Sleep(time.Second)
}
Expand Down
10 changes: 4 additions & 6 deletions internal/container/docker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ func TestDockerContainer(t *testing.T) {
volumes := []canaryv1.Volume{
{MountPath: "/foo"},
}
c := New("nginx", env, ports, volumes, nil, nil, 7)
c := New("nginx", env, ports, volumes, nil, nil)

err := c.Start()
err := c.Start(10)

defer func() {
err := c.Remove()
Expand Down Expand Up @@ -67,13 +67,11 @@ func TestDockerContainer(t *testing.T) {
t.Error("Output for command 'uname' did not contain expected string 'Linux'")
return
}

assert.Equal(c.GetStartupTimeout(), 7)
}
func TestDockerContainerRemoves(t *testing.T) {
c := New("nginx", nil, nil, nil, nil, nil, 2)
c := New("nginx", nil, nil, nil, nil, nil)

err := c.Start()
err := c.Start(10)
if err != nil {
t.Errorf("Failed to start container: %s", err.Error())
return
Expand Down
4 changes: 2 additions & 2 deletions internal/validator/validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func loadConfig(filePath string) tea.Cmd {

func startContainer(image string, validator *canaryv1.Validator, startupTimeout int) tea.Cmd {
return func() tea.Msg {
container := container.New(image, validator.Env, validator.Ports, validator.Volumes, validator.Command, validator.DockerRunOptions, startupTimeout)
err := container.Start()
container := container.New(image, validator.Env, validator.Ports, validator.Volumes, validator.Command, validator.DockerRunOptions)
err := container.Start(startupTimeout)
if err != nil {
return containerFailed{Error: err}
}
Expand Down

0 comments on commit e5250d1

Please sign in to comment.