Skip to content

Commit

Permalink
v2_test: Add tests for Download
Browse files Browse the repository at this point in the history
This enables the artifact directory during the tests, it mocks up a
download artifact, and tests that it can be downloaded. The file
contains JSON because the TestRoute helper expects that as the response.

Related: RHEL-60142
  • Loading branch information
bcl committed Jan 31, 2025
1 parent aa9d1ed commit ec40673
Showing 1 changed file with 165 additions and 1 deletion.
166 changes: 165 additions & 1 deletion internal/cloudapi/v2/v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"net/http"
"os"
"sync"
"testing"
"time"
Expand Down Expand Up @@ -242,7 +243,14 @@ func mockSearch(t *testing.T, workerServer *worker.Server, wg *sync.WaitGroup, f
func newV2Server(t *testing.T, dir string, enableJWT bool, fail bool) (*v2.Server, *worker.Server, jobqueue.JobQueue, context.CancelFunc) {
q, err := fsjobqueue.New(dir)
require.NoError(t, err)
workerServer := worker.NewServer(nil, q, worker.Config{BasePath: "/api/worker/v1", JWTEnabled: enableJWT, TenantProviderFields: []string{"rh-org-id", "account_id"}})
workerServer := worker.NewServer(nil, q,
worker.Config{
// ArtifactsDir: t.TempDir(),
ArtifactsDir: "/var/tmp/test-facts/",
BasePath: "/api/worker/v1",
JWTEnabled: enableJWT,
TenantProviderFields: []string{"rh-org-id", "account_id"},
})

distros := distrofactory.NewTestDefault()
require.NotNil(t, distros)
Expand Down Expand Up @@ -1834,3 +1842,159 @@ func TestSearchArchErrors(t *testing.T) {
"reason": "Request could not be validated"
}`, "operation_id", "details")
}

func TestDownload(t *testing.T) {
srv, wrksrv, _, cancel := newV2Server(t, t.TempDir(), false, false)
defer cancel()

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "POST", "/api/image-builder-composer/v2/compose", fmt.Sprintf(`
{
"distribution": "%s",
"image_requests": [{
"architecture": "%s",
"image_type": "guest-image",
"repositories": [{
"baseurl": "somerepo.org",
"rhsm": false
}],
"upload_targets": [{
"type": "local",
"upload_options": {}
}]
}]
}`, test_distro.TestDistro1Name, test_distro.TestArch3Name), http.StatusCreated, `
{
"href": "/api/image-builder-composer/v2/compose",
"kind": "ComposeId"
}`, "id")

jobId, token, jobType, args, dynArgs, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""}, uuid.Nil)
require.NoError(t, err)
require.Equal(t, worker.JobTypeOSBuild, jobType)

var osbuildJob worker.OSBuildJob
err = json.Unmarshal(args, &osbuildJob)
require.NoError(t, err)
require.Equal(t, 0, len(osbuildJob.Manifest))
require.NotEqual(t, 0, len(dynArgs[0]))

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(`
{
"href": "/api/image-builder-composer/v2/composes/%v",
"kind": "ComposeStatus",
"id": "%v",
"image_status": {"status": "building"},
"status": "pending"
}`, jobId, jobId))

// Mock up a local target result
tr := target.NewWorkerServerTargetResult(
&target.WorkerServerTargetResultOptions{},
&target.OsbuildArtifact{
ExportFilename: "disk.qcow2",
ExportName: "qcow2",
})
res, err := json.Marshal(&worker.OSBuildJobResult{
Success: true,
OSBuildOutput: &osbuild.Result{Success: true},
TargetResults: []*target.TargetResult{
tr,
},
})
require.NoError(t, err)

err = wrksrv.FinishJob(token, res)
require.NoError(t, err)

// Write a fake disk.qcow2 file to the artifact directory
file, err := wrksrv.JobArtifactLocation(jobId, tr.OsbuildArtifact.ExportFilename)
// Error is expected, file doesn't exist yet
require.Error(t, err)
// Yes, the dummy file is json to make TestRoute happy
err = os.WriteFile(file, []byte("{\"msg\":\"This is the disk.qcow2 you are looking for\"}"), 0600)
require.NoError(t, err)

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET",
fmt.Sprintf("/api/image-builder-composer/v2/composes/%v/download", jobId),
``,
http.StatusOK,
`{
"msg": "This is the disk.qcow2 you are looking for"
}`)
}

func TestDownloadNotFinished(t *testing.T) {
srv, wrksrv, _, cancel := newV2Server(t, t.TempDir(), false, false)
defer cancel()

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "POST", "/api/image-builder-composer/v2/compose", fmt.Sprintf(`
{
"distribution": "%s",
"image_requests": [{
"architecture": "%s",
"image_type": "guest-image",
"repositories": [{
"baseurl": "somerepo.org",
"rhsm": false
}],
"upload_targets": [{
"type": "local",
"upload_options": {}
}]
}]
}`, test_distro.TestDistro1Name, test_distro.TestArch3Name), http.StatusCreated, `
{
"href": "/api/image-builder-composer/v2/compose",
"kind": "ComposeId"
}`, "id")

jobId, _, jobType, args, dynArgs, err := wrksrv.RequestJob(context.Background(), test_distro.TestArch3Name, []string{worker.JobTypeOSBuild}, []string{""}, uuid.Nil)
require.NoError(t, err)
require.Equal(t, worker.JobTypeOSBuild, jobType)

var osbuildJob worker.OSBuildJob
err = json.Unmarshal(args, &osbuildJob)
require.NoError(t, err)
require.Equal(t, 0, len(osbuildJob.Manifest))
require.NotEqual(t, 0, len(dynArgs[0]))

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(`
{
"href": "/api/image-builder-composer/v2/composes/%v",
"kind": "ComposeStatus",
"id": "%v",
"image_status": {"status": "building"},
"status": "pending"
}`, jobId, jobId))

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET",
fmt.Sprintf("/api/image-builder-composer/v2/composes/%v/download", jobId),
``,
http.StatusBadRequest,
fmt.Sprintf(`{
"href": "/api/image-builder-composer/v2/errors/1021",
"id": "1021",
"kind": "Error",
"code": "IMAGE-BUILDER-COMPOSER-1021",
"details": "Cannot access artifacts before job is finished: %s",
"reason": "Artifact not found"
}`, jobId), "operation_id")
}

func TestDownloadUnknown(t *testing.T) {
srv, _, _, cancel := newV2Server(t, t.TempDir(), false, false)
defer cancel()

test.TestRoute(t, srv.Handler("/api/image-builder-composer/v2"), false, "GET",
"/api/image-builder-composer/v2/composes/80977a35-1b27-4604-b998-9cd331f9089e/download",
``,
http.StatusNotFound,
`{
"href": "/api/image-builder-composer/v2/errors/15",
"id": "15",
"kind": "Error",
"code": "IMAGE-BUILDER-COMPOSER-15",
"details": "job does not exist",
"reason":"Compose with given id not found"
}`, "operation_id")
}

0 comments on commit ec40673

Please sign in to comment.