49 lines
1.6 KiB
Go
49 lines
1.6 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/docker/docker/api/types"
|
|
"github.com/shcizo/package-updater/internal/api"
|
|
"github.com/shcizo/package-updater/internal/discovery"
|
|
"github.com/shcizo/package-updater/internal/updater"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type swarmResultFinder struct{ jobs []discovery.Job }
|
|
|
|
func (f *swarmResultFinder) FindJobs(_ context.Context, _ string) ([]discovery.Job, error) {
|
|
return f.jobs, nil
|
|
}
|
|
|
|
type swarmResultSubmitter struct{ results []updater.Result }
|
|
|
|
func (s *swarmResultSubmitter) Submit(_ context.Context, _ []discovery.Job) []updater.Result {
|
|
return s.results
|
|
}
|
|
|
|
type swarmResultPinger struct{}
|
|
|
|
func (swarmResultPinger) Ping(_ context.Context) (types.Ping, error) { return types.Ping{}, nil }
|
|
|
|
func TestUpdate_SwarmJobPopulatesServiceID(t *testing.T) {
|
|
job := discovery.Job{Service: "myapp_web", ServiceID: "svc123", Image: "registry.example.com/myapp:v2"}
|
|
finder := &swarmResultFinder{jobs: []discovery.Job{job}}
|
|
submitter := &swarmResultSubmitter{results: []updater.Result{
|
|
{Job: job, Status: updater.StatusUpdated, DurationMs: 42},
|
|
}}
|
|
h := api.NewHandlers(finder, submitter, swarmResultPinger{}, "v", "c", "b", nil)
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/update", strings.NewReader(`{"image":"registry.example.com/myapp"}`))
|
|
rec := httptest.NewRecorder()
|
|
h.Update(rec, req)
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
require.Contains(t, rec.Body.String(), `"service_id":"svc123"`)
|
|
require.Contains(t, rec.Body.String(), `"service":"myapp_web"`)
|
|
}
|