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.
101 lines
3.3 KiB
Go
101 lines
3.3 KiB
Go
package weldr
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/osbuild/osbuild-composer/internal/distro"
|
|
"github.com/osbuild/osbuild-composer/internal/distro/test_distro"
|
|
rpmmd_mock "github.com/osbuild/osbuild-composer/internal/mocks/rpmmd"
|
|
"github.com/osbuild/osbuild-composer/internal/worker"
|
|
"github.com/osbuild/osbuild-composer/internal/worker/clienterrors"
|
|
)
|
|
|
|
func TestComposeStatusFromLegacyError(t *testing.T) {
|
|
|
|
if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 {
|
|
t.Skip("This test is for internal testing only")
|
|
}
|
|
|
|
api, _ := createWeldrAPI(t.TempDir(), rpmmd_mock.BaseFixture)
|
|
|
|
distroStruct := test_distro.New()
|
|
arch, err := distroStruct.GetArch(test_distro.TestArchName)
|
|
if err != nil {
|
|
t.Fatalf("error getting arch from distro: %v", err)
|
|
}
|
|
imageType, err := arch.GetImageType(test_distro.TestImageTypeName)
|
|
if err != nil {
|
|
t.Fatalf("error getting image type from arch: %v", err)
|
|
}
|
|
manifest, err := imageType.Manifest(nil, distro.ImageOptions{Size: imageType.Size(0)}, nil, nil, 0)
|
|
if err != nil {
|
|
t.Fatalf("error creating osbuild manifest: %v", err)
|
|
}
|
|
|
|
jobId, err := api.workers.EnqueueOSBuild(arch.Name(), &worker.OSBuildJob{Manifest: manifest}, "")
|
|
require.NoError(t, err)
|
|
|
|
j, token, _, _, _, err := api.workers.RequestJob(context.Background(), arch.Name(), []string{worker.JobTypeOSBuild}, []string{""})
|
|
require.NoError(t, err)
|
|
require.Equal(t, jobId, j)
|
|
|
|
jobResult := worker.OSBuildJobResult{TargetErrors: []string{"Upload error"}}
|
|
rawResult, err := json.Marshal(jobResult)
|
|
require.NoError(t, err)
|
|
err = api.workers.FinishJob(token, rawResult)
|
|
require.NoError(t, err)
|
|
|
|
jobStatus, _, err := api.workers.OSBuildJobStatus(jobId, &jobResult)
|
|
require.NoError(t, err)
|
|
|
|
state := composeStateFromJobStatus(jobStatus, &jobResult)
|
|
require.Equal(t, "FAILED", state.ToString())
|
|
}
|
|
|
|
func TestComposeStatusFromJobError(t *testing.T) {
|
|
|
|
if len(os.Getenv("OSBUILD_COMPOSER_TEST_EXTERNAL")) > 0 {
|
|
t.Skip("This test is for internal testing only")
|
|
}
|
|
|
|
api, _ := createWeldrAPI(t.TempDir(), rpmmd_mock.BaseFixture)
|
|
|
|
distroStruct := test_distro.New()
|
|
arch, err := distroStruct.GetArch(test_distro.TestArchName)
|
|
if err != nil {
|
|
t.Fatalf("error getting arch from distro: %v", err)
|
|
}
|
|
imageType, err := arch.GetImageType(test_distro.TestImageTypeName)
|
|
if err != nil {
|
|
t.Fatalf("error getting image type from arch: %v", err)
|
|
}
|
|
manifest, err := imageType.Manifest(nil, distro.ImageOptions{Size: imageType.Size(0)}, nil, nil, 0)
|
|
if err != nil {
|
|
t.Fatalf("error creating osbuild manifest: %v", err)
|
|
}
|
|
|
|
jobId, err := api.workers.EnqueueOSBuild(arch.Name(), &worker.OSBuildJob{Manifest: manifest}, "")
|
|
require.NoError(t, err)
|
|
|
|
j, token, _, _, _, err := api.workers.RequestJob(context.Background(), arch.Name(), []string{worker.JobTypeOSBuild}, []string{""})
|
|
require.NoError(t, err)
|
|
require.Equal(t, jobId, j)
|
|
|
|
jobResult := worker.OSBuildJobResult{}
|
|
jobResult.JobError = clienterrors.WorkerClientError(clienterrors.ErrorUploadingImage, "Upload error")
|
|
rawResult, err := json.Marshal(jobResult)
|
|
require.NoError(t, err)
|
|
err = api.workers.FinishJob(token, rawResult)
|
|
require.NoError(t, err)
|
|
|
|
jobStatus, _, err := api.workers.OSBuildJobStatus(jobId, &jobResult)
|
|
require.NoError(t, err)
|
|
|
|
state := composeStateFromJobStatus(jobStatus, &jobResult)
|
|
require.Equal(t, "FAILED", state.ToString())
|
|
}
|