worker: prefix all routes with /api/worker/v1

Mention this in the `servers` section of the openapi.yml (relative URLs
are allowed) too, even though our generator does not consider it.
This commit is contained in:
Lars Karlitski 2020-09-23 15:57:55 +02:00 committed by Tom Gundersen
parent 9008a1defc
commit a8ba969f6e
6 changed files with 35 additions and 21 deletions

View file

@ -145,7 +145,7 @@ func TestWorkerAPIAuth(t *testing.T) {
require.NoError(t, err)
defer ckp.remove()
testRoute(t, "https://localhost:8700/status", ckp, c.success)
testRoute(t, "https://localhost:8700/api/worker/v1/status", ckp, c.success)
})
}
})
@ -161,7 +161,7 @@ func TestWorkerAPIAuth(t *testing.T) {
require.NoError(t, err)
defer ckp.remove()
testRoute(t, "https://localhost:8700/status", ckp, false)
testRoute(t, "https://localhost:8700/api/worker/v1/status", ckp, false)
})
t.Run("self-signed certificate", func(t *testing.T) {
@ -170,7 +170,7 @@ func TestWorkerAPIAuth(t *testing.T) {
require.NoError(t, err)
defer ckp.remove()
testRoute(t, "https://localhost:8700/status", ckp, false)
testRoute(t, "https://localhost:8700/api/worker/v1/status", ckp, false)
})
}

View file

@ -1,3 +1,5 @@
//go:generate go run github.com/deepmap/oapi-codegen/cmd/oapi-codegen -package=api -generate types,server -o api.gen.go openapi.yml
package api
const BasePath = "/api/worker/v1"

View file

@ -1,8 +1,10 @@
openapi: 3.0.0
info:
title: worker
version: '1.0'
servers: []
title: OSBuild Composer - Worker
version: '1'
description: This is an API for workers to request and handle jobs.
servers:
- url: /api/worker/v1
paths:
/status:
get:

View file

@ -48,6 +48,11 @@ func NewClient(baseURL string, conf *tls.Config) (*Client, error) {
return nil, err
}
server, err = server.Parse(api.BasePath + "/")
if err != nil {
panic(err)
}
requester := &http.Client{
Transport: &http.Transport{
TLSClientConfig: conf,
@ -58,7 +63,12 @@ func NewClient(baseURL string, conf *tls.Config) (*Client, error) {
}
func NewClientUnix(path string) *Client {
server, err := url.Parse("http://localhost")
server, err := url.Parse("http://localhost/")
if err != nil {
panic(err)
}
server, err = server.Parse(api.BasePath + "/")
if err != nil {
panic(err)
}
@ -75,9 +85,9 @@ func NewClientUnix(path string) *Client {
}
func (c *Client) RequestJob() (Job, error) {
url, err := c.server.Parse("/jobs")
url, err := c.server.Parse("jobs")
if err != nil {
// This only happens when "/jobs" cannot be parsed.
// This only happens when "jobs" cannot be parsed.
panic(err)
}

View file

@ -65,7 +65,7 @@ func NewServer(logger *log.Logger, jobs jobqueue.JobQueue, artifactsDir string)
e.Binder = binder{}
e.StdLogger = logger
api.RegisterHandlers(e, &apiHandlers{s})
api.RegisterHandlers(e.Group(api.BasePath), &apiHandlers{s})
s.server = &http.Server{
ErrorLog: logger,
@ -277,8 +277,8 @@ func (h *apiHandlers) RequestJob(ctx echo.Context) error {
return ctx.JSON(http.StatusCreated, requestJobResponse{
Id: jobId,
Location: fmt.Sprintf("/jobs/%v", token),
ArtifactLocation: fmt.Sprintf("/jobs/%v/artifacts/", token),
Location: fmt.Sprintf("%s/jobs/%v", api.BasePath, token),
ArtifactLocation: fmt.Sprintf("%s/jobs/%v/artifacts/", api.BasePath, token),
Type: "osbuild",
Args: serializedArgs,
})

View file

@ -18,7 +18,7 @@ import (
// Ensure that the status request returns OK.
func TestStatus(t *testing.T) {
server := worker.NewServer(nil, testjobqueue.New(), "")
test.TestRoute(t, server, false, "GET", "/status", ``, http.StatusOK, `{"status":"OK"}`, "message")
test.TestRoute(t, server, false, "GET", "/api/worker/v1/status", ``, http.StatusOK, `{"status":"OK"}`, "message")
}
func TestErrors(t *testing.T) {
@ -29,17 +29,17 @@ func TestErrors(t *testing.T) {
ExpectedStatus int
}{
// Bogus path
{"GET", "/foo", ``, http.StatusNotFound},
{"GET", "/api/worker/v1/foo", ``, http.StatusNotFound},
// Create job with invalid body
{"POST", "/jobs", ``, http.StatusBadRequest},
{"POST", "/api/worker/v1/jobs", ``, http.StatusBadRequest},
// Wrong method
{"GET", "/jobs", ``, http.StatusMethodNotAllowed},
{"GET", "/api/worker/v1/jobs", ``, http.StatusMethodNotAllowed},
// Update job with invalid ID
{"PATCH", "/jobs/foo", `{"status":"FINISHED"}`, http.StatusBadRequest},
{"PATCH", "/api/worker/v1/jobs/foo", `{"status":"FINISHED"}`, http.StatusBadRequest},
// Update job that does not exist, with invalid body
{"PATCH", "/jobs/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", ``, http.StatusBadRequest},
{"PATCH", "/api/worker/v1/jobs/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", ``, http.StatusBadRequest},
// Update job that does not exist
{"PATCH", "/jobs/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", `{"status":"FINISHED"}`, http.StatusNotFound},
{"PATCH", "/api/worker/v1/jobs/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa", `{"status":"FINISHED"}`, http.StatusNotFound},
}
for _, c := range cases {
@ -67,7 +67,7 @@ func TestCreate(t *testing.T) {
_, err = server.Enqueue(arch.Name(), manifest, nil)
require.NoError(t, err)
test.TestRoute(t, server, false, "POST", "/jobs", `{"types":["osbuild"],"arch":"x86_64"}`, http.StatusCreated,
test.TestRoute(t, server, false, "POST", "/api/worker/v1/jobs", `{"types":["osbuild"],"arch":"x86_64"}`, http.StatusCreated,
`{"type":"osbuild","args":{"manifest":{"pipeline":{},"sources":{}}}}`, "id", "location", "artifact_location")
}
@ -97,6 +97,6 @@ func TestCancel(t *testing.T) {
err = server.Cancel(jobId)
require.NoError(t, err)
test.TestRoute(t, server, false, "GET", fmt.Sprintf("/jobs/%s", token), `{}`, http.StatusOK,
test.TestRoute(t, server, false, "GET", fmt.Sprintf("/api/worker/v1/jobs/%s", token), `{}`, http.StatusOK,
`{"canceled":true}`)
}