Skip to content

Modern Docker Management, Designed for Everyone

5.2 KB
Raw Blame
package volumehelper

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
	"strings"
	"sync/atomic"
	"testing"

	"github.com/moby/moby/api/types/container"
	"github.com/moby/moby/client"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"

	"github.com/getarcaneapp/arcane/backend/v2/pkg/libarcane"
)

func TestLabels(t *testing.T) {
	labels := Labels()

	require.Equal(t, "true", labels[libarcane.InternalResourceLabel])
	require.Equal(t, "true", labels[ContainerLabel])
	require.Len(t, labels, 2)
}

func TestResolveHelperImage_UsesLocalToolsImage(t *testing.T) {
	var pullCalls atomic.Int32
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch {
		case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/images/") && strings.HasSuffix(r.URL.Path, "/json"):
			w.Header().Set("Content-Type", "application/json")
			if !assert.NoError(t, json.NewEncoder(w).Encode(map[string]any{"Id": "tools-image"})) {
				return
			}
		case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/images/create"):
			pullCalls.Add(1)
			http.Error(w, "unexpected pull", http.StatusInternalServerError)
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(server.Close)

	image, err := ResolveHelperImage(t.Context(), newTestDockerClientInternal(t, server), ToolsImage(""))

	require.NoError(t, err)
	require.Equal(t, ToolsImage(""), image)
	require.Zero(t, pullCalls.Load())
}

func TestResolveHelperImage_PullsToolsImageWhenMissing(t *testing.T) {
	var pullCalls atomic.Int32
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch {
		case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/images/") && strings.HasSuffix(r.URL.Path, "/json"):
			http.NotFound(w, r)
		case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/images/create"):
			pullCalls.Add(1)
			if !assert.Equal(t, "ghcr.io/getarcaneapp/tools", r.URL.Query().Get("fromImage")) {
				return
			}
			if !assert.Equal(t, "latest", r.URL.Query().Get("tag")) {
				return
			}
			w.Header().Set("Content-Type", "application/json")
			_, err := w.Write([]byte("{\"status\":\"pulled\"}\n"))
			if !assert.NoError(t, err) {
				return
			}
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(server.Close)

	image, err := ResolveHelperImage(t.Context(), newTestDockerClientInternal(t, server), ToolsImage(""))

	require.NoError(t, err)
	require.Equal(t, ToolsImage(""), image)
	require.EqualValues(t, 1, pullCalls.Load())
}

func TestResolveHelperImage_FallsBackToArcaneRuntimeWhenToolsPullFails(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch {
		case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/images/") && strings.HasSuffix(r.URL.Path, "/json"):
			http.NotFound(w, r)
		case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/images/create"):
			http.Error(w, "pull failed", http.StatusInternalServerError)
		case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/containers/json"):
			w.Header().Set("Content-Type", "application/json")
			if !assert.NoError(t, json.NewEncoder(w).Encode([]container.Summary{
				{ID: "arcane-container", Image: "arcane:local", State: container.StateRunning},
			})) {
				return
			}
		case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/containers/arcane-container/") && strings.HasSuffix(r.URL.Path, "/json"):
			w.Header().Set("Content-Type", "application/json")
			if !assert.NoError(t, json.NewEncoder(w).Encode(container.InspectResponse{
				ID: "arcane-container",
				Config: &container.Config{
					Image: "arcane:local",
				},
			})) {
				return
			}
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(server.Close)

	image, err := ResolveHelperImage(t.Context(), newTestDockerClientInternal(t, server), ToolsImage(""))

	require.NoError(t, err)
	require.Equal(t, "arcane:local", image)
}

func TestResolveHelperImage_ReturnsPullErrorWhenNoFallbackExists(t *testing.T) {
	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		switch {
		case r.Method == http.MethodGet && strings.Contains(r.URL.Path, "/images/") && strings.HasSuffix(r.URL.Path, "/json"):
			http.NotFound(w, r)
		case r.Method == http.MethodPost && strings.HasSuffix(r.URL.Path, "/images/create"):
			http.Error(w, "pull failed", http.StatusInternalServerError)
		case r.Method == http.MethodGet && strings.HasSuffix(r.URL.Path, "/containers/json"):
			w.Header().Set("Content-Type", "application/json")
			if !assert.NoError(t, json.NewEncoder(w).Encode([]container.Summary{})) {
				return
			}
		default:
			http.NotFound(w, r)
		}
	}))
	t.Cleanup(server.Close)

	image, err := ResolveHelperImage(t.Context(), newTestDockerClientInternal(t, server), ToolsImage(""))

	require.Error(t, err)
	require.Empty(t, image)
	require.ErrorContains(t, err, "failed to resolve helper image")
	require.ErrorContains(t, err, "pull failed")
}

func newTestDockerClientInternal(t *testing.T, server *httptest.Server) *client.Client {
	t.Helper()

	dockerClient, err := client.New(
		client.WithHost(server.URL),
		client.WithAPIVersion("1.54"),
	)
	require.NoError(t, err)
	return dockerClient
}