octoplex/internal/container/pull_test.go
2025-03-19 20:54:19 +01:00

51 lines
1.3 KiB
Go

package container
import (
"bytes"
_ "embed"
"io"
"testing"
"git.netflux.io/rob/octoplex/internal/container/mocks"
"git.netflux.io/rob/octoplex/internal/domain"
"github.com/docker/docker/api/types/image"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
)
//go:embed testdata/pull_progress.json
var pullProgressJSON []byte
func TestHandleImagePull(t *testing.T) {
const imageName = "alpine"
containerStateC := make(chan domain.Container)
var dockerClient mocks.DockerClient
defer dockerClient.AssertExpectations(t)
dockerClient.
EXPECT().
ImagePull(mock.Anything, imageName, image.PullOptions{}).
Return(io.NopCloser(bytes.NewReader(pullProgressJSON)), nil)
var containerStates []domain.Container
go func() {
require.NoError(t, handleImagePull(t.Context(), imageName, &dockerClient, containerStateC))
}()
const expectedContainerStates = 46
for range expectedContainerStates {
containerStates = append(containerStates, <-containerStateC)
}
assert.Len(t, containerStates, expectedContainerStates)
for _, containerState := range containerStates {
assert.Equal(t, domain.ContainerStatusPulling, containerState.Status)
assert.Equal(t, imageName, containerState.ImageName)
assert.NotZero(t, containerState.PullStatus)
}
}