worker: define job types as constants

Define supported job type names as constants and use them in all places,
instead of string literals.

There are multiple benefits of this approach. Using constants removed
the room for typos in the string literals. One can use autocompletion in
IDE for job types. Using constant makes it easier to find all references
where it is used and thus all places that are handling a specific job
type.
This commit is contained in:
Tomas Hozza 2022-05-18 13:05:33 +02:00 committed by Tom Gundersen
parent 69b9f115c9
commit a4e6531565
11 changed files with 119 additions and 102 deletions

View file

@ -508,7 +508,7 @@ func (h *apiHandlers) GetComposeStatus(ctx echo.Context, id string) error {
return HTTPError(ErrorComposeNotFound)
}
if jobType == "osbuild" {
if jobType == worker.JobTypeOSBuild {
var result worker.OSBuildJobResult
status, deps, err := h.server.workers.OSBuildJobStatus(jobId, &result)
if err != nil {
@ -584,7 +584,7 @@ func (h *apiHandlers) GetComposeStatus(ctx echo.Context, id string) error {
UploadStatus: us,
},
})
} else if jobType == "koji-finalize" {
} else if jobType == worker.JobTypeKojiFinalize {
var result worker.KojiFinalizeJobResult
finalizeStatus, deps, err := h.server.workers.KojiFinalizeJobStatus(jobId, &result)
if err != nil {
@ -751,7 +751,7 @@ func (h *apiHandlers) GetComposeMetadata(ctx echo.Context, id string) error {
}
// TODO: support koji builds
if jobType != "osbuild" {
if jobType != worker.JobTypeOSBuild {
return HTTPError(ErrorInvalidJobType)
}
@ -862,7 +862,7 @@ func (h *apiHandlers) GetComposeLogs(ctx echo.Context, id string) error {
}
// TODO: support non-koji builds
if jobType != "koji-finalize" {
if jobType != worker.JobTypeKojiFinalize {
return HTTPError(ErrorInvalidJobType)
}
@ -920,7 +920,7 @@ func (h *apiHandlers) GetComposeManifests(ctx echo.Context, id string) error {
}
// TODO: support non-koji builds
if jobType != "koji-finalize" {
if jobType != worker.JobTypeKojiFinalize {
return HTTPError(ErrorInvalidJobType)
}

View file

@ -426,9 +426,9 @@ func TestKojiCompose(t *testing.T) {
c.composeReplyCode, c.composeReply, "id", "operation_id")
// handle koji-init
_, token, jobType, rawJob, _, err := workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"koji-init"}, []string{""})
_, token, jobType, rawJob, _, err := workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeKojiInit}, []string{""})
require.NoError(t, err)
require.Equal(t, "koji-init", jobType)
require.Equal(t, worker.JobTypeKojiInit, jobType)
var initJob worker.KojiInitJob
err = json.Unmarshal(rawJob, &initJob)
@ -444,9 +444,9 @@ func TestKojiCompose(t *testing.T) {
fmt.Sprintf(`{"href":"/api/worker/v1/jobs/%v","id":"%v","kind":"UpdateJobResponse"}`, token, token))
// handle osbuild-koji #1
_, token, jobType, rawJob, _, err = workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild-koji"}, []string{""})
_, token, jobType, rawJob, _, err = workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuildKoji}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild-koji", jobType)
require.Equal(t, worker.JobTypeOSBuildKoji, jobType)
var osbuildJob worker.OSBuildKojiJob
err = json.Unmarshal(rawJob, &osbuildJob)
@ -461,9 +461,9 @@ func TestKojiCompose(t *testing.T) {
fmt.Sprintf(`{"href":"/api/worker/v1/jobs/%v","id":"%v","kind":"UpdateJobResponse"}`, token, token))
// handle osbuild-koji #2
_, token, jobType, rawJob, _, err = workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild-koji"}, []string{""})
_, token, jobType, rawJob, _, err = workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuildKoji}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild-koji", jobType)
require.Equal(t, worker.JobTypeOSBuildKoji, jobType)
err = json.Unmarshal(rawJob, &osbuildJob)
require.NoError(t, err)
@ -485,9 +485,9 @@ func TestKojiCompose(t *testing.T) {
fmt.Sprintf(`{"href":"/api/worker/v1/jobs/%v","id":"%v","kind":"UpdateJobResponse"}`, token, token))
// handle koji-finalize
finalizeID, token, jobType, rawJob, _, err := workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"koji-finalize"}, []string{""})
finalizeID, token, jobType, rawJob, _, err := workerServer.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeKojiFinalize}, []string{""})
require.NoError(t, err)
require.Equal(t, "koji-finalize", jobType)
require.Equal(t, worker.JobTypeKojiFinalize, jobType)
var kojiFinalizeJob worker.KojiFinalizeJob
err = json.Unmarshal(rawJob, &kojiFinalizeJob)

View file

@ -17,6 +17,7 @@ import (
"github.com/osbuild/osbuild-composer/internal/distro/test_distro"
"github.com/osbuild/osbuild-composer/internal/jobqueue"
"github.com/osbuild/osbuild-composer/internal/test"
"github.com/osbuild/osbuild-composer/internal/worker"
)
func kojiRequest() string {
@ -121,14 +122,20 @@ func jobRequest() string {
return fmt.Sprintf(`
{
"types": [
"koji-init",
"osbuild",
"osbuild-koji",
"koji-finalize",
"depsolve"
%q,
%q,
%q,
%q,
%q
],
"arch": "%s"
}`, test_distro.TestArch3Name)
"arch": %q
}`,
worker.JobTypeKojiInit,
worker.JobTypeOSBuild,
worker.JobTypeOSBuildKoji,
worker.JobTypeKojiFinalize,
worker.JobTypeDepsolve,
test_distro.TestArch3Name)
}
func runNextJob(t *testing.T, jobs []uuid.UUID, workerHandler http.Handler, orgID string) {

View file

@ -47,7 +47,7 @@ func newV2Server(t *testing.T, dir string, depsolveChannels []string, enableJWT
go func() {
defer wg.Done()
for {
_, token, _, _, _, err := workerServer.RequestJob(depsolveContext, test_distro.TestDistroName, []string{"depsolve"}, depsolveChannels)
_, token, _, _, _, err := workerServer.RequestJob(depsolveContext, test_distro.TestDistroName, []string{worker.JobTypeDepsolve}, depsolveChannels)
select {
case <-depsolveContext.Done():
return
@ -572,9 +572,9 @@ func TestComposeStatusSuccess(t *testing.T) {
"kind": "ComposeId"
}`, "id")
jobId, token, jobType, args, dynArgs, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild"}, []string{""})
jobId, token, jobType, args, dynArgs, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild", jobType)
require.Equal(t, worker.JobTypeOSBuild, jobType)
var osbuildJob worker.OSBuildJob
err = json.Unmarshal(args, &osbuildJob)
@ -642,9 +642,9 @@ func TestComposeStatusFailure(t *testing.T) {
"kind": "ComposeId"
}`, "id")
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild"}, []string{""})
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild", jobType)
require.Equal(t, worker.JobTypeOSBuild, jobType)
test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET", fmt.Sprintf("/api/image-builder-composer/v2/composes/%v", jobId), ``, http.StatusOK, fmt.Sprintf(`
{
@ -698,9 +698,9 @@ func TestComposeLegacyError(t *testing.T) {
"kind": "ComposeId"
}`, "id")
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild"}, []string{""})
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild", jobType)
require.Equal(t, worker.JobTypeOSBuild, jobType)
test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET", fmt.Sprintf("/api/image-builder-composer/v2/composes/%v", jobId), ``, http.StatusOK, fmt.Sprintf(`
{
@ -757,9 +757,9 @@ func TestComposeJobError(t *testing.T) {
"kind": "ComposeId"
}`, "id")
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild"}, []string{""})
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild", jobType)
require.Equal(t, worker.JobTypeOSBuild, jobType)
test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET", fmt.Sprintf("/api/image-builder-composer/v2/composes/%v", jobId), ``, http.StatusOK, fmt.Sprintf(`
{
@ -819,9 +819,9 @@ func TestComposeDependencyError(t *testing.T) {
"kind": "ComposeId"
}`, "id")
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{"osbuild"}, []string{""})
jobId, token, jobType, _, _, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""})
require.NoError(t, err)
require.Equal(t, "osbuild", jobType)
require.Equal(t, worker.JobTypeOSBuild, jobType)
test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET", fmt.Sprintf("/api/image-builder-composer/v2/composes/%v", jobId), ``, http.StatusOK, fmt.Sprintf(`
{