osbuildexecutor: move tests to separate _test package
This commit is contained in:
parent
984f51feb8
commit
db43ec8e60
3 changed files with 123 additions and 126 deletions
|
|
@ -1,3 +1,8 @@
|
||||||
package osbuildexecutor
|
package osbuildexecutor
|
||||||
|
|
||||||
|
var ExtractOutputArchive = extractOutputArchive
|
||||||
|
var FetchOutputArchive = fetchOutputArchive
|
||||||
|
var HandleBuild = handleBuild
|
||||||
var ValidateOutputArchive = validateOutputArchive
|
var ValidateOutputArchive = validateOutputArchive
|
||||||
|
var WaitForSI = waitForSI
|
||||||
|
var WriteInputArchive = writeInputArchive
|
||||||
|
|
|
||||||
|
|
@ -1,126 +0,0 @@
|
||||||
package osbuildexecutor
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"encoding/json"
|
|
||||||
"io"
|
|
||||||
"net/http"
|
|
||||||
"net/http/httptest"
|
|
||||||
"os"
|
|
||||||
"os/exec"
|
|
||||||
"path/filepath"
|
|
||||||
"strings"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/osbuild/images/pkg/osbuild"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestWaitForSI(t *testing.T) {
|
|
||||||
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
}))
|
|
||||||
|
|
||||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
|
|
||||||
defer cancel()
|
|
||||||
require.False(t, waitForSI(ctx, server.URL))
|
|
||||||
|
|
||||||
server.Start()
|
|
||||||
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second*1)
|
|
||||||
defer cancel2()
|
|
||||||
require.True(t, waitForSI(ctx2, server.URL))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestWriteInputArchive(t *testing.T) {
|
|
||||||
cacheDir := t.TempDir()
|
|
||||||
storeDir := filepath.Join(cacheDir, "store")
|
|
||||||
require.NoError(t, os.Mkdir(storeDir, 0755))
|
|
||||||
storeSubDir := filepath.Join(storeDir, "subdir")
|
|
||||||
require.NoError(t, os.Mkdir(storeSubDir, 0755))
|
|
||||||
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "contents"), []byte("storedata"), 0600))
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(storeSubDir, "contents"), []byte("storedata"), 0600))
|
|
||||||
|
|
||||||
archive, err := writeInputArchive(cacheDir, storeDir, []string{"image"}, []byte("{\"version\": 2}"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
cmd := exec.Command("tar",
|
|
||||||
"-tf",
|
|
||||||
archive,
|
|
||||||
)
|
|
||||||
out, err := cmd.CombinedOutput()
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.ElementsMatch(t, []string{
|
|
||||||
"control.json",
|
|
||||||
"manifest.json",
|
|
||||||
"store/",
|
|
||||||
"store/subdir/",
|
|
||||||
"store/subdir/contents",
|
|
||||||
"store/contents",
|
|
||||||
"",
|
|
||||||
}, strings.Split(string(out), "\n"))
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleBuild(t *testing.T) {
|
|
||||||
buildServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusCreated)
|
|
||||||
osbuildResult := osbuild.Result{
|
|
||||||
Success: true,
|
|
||||||
}
|
|
||||||
data, err := json.Marshal(osbuildResult)
|
|
||||||
require.NoError(t, err)
|
|
||||||
_, err = w.Write(data)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}))
|
|
||||||
|
|
||||||
cacheDir := t.TempDir()
|
|
||||||
inputArchive := filepath.Join(cacheDir, "test.tar")
|
|
||||||
require.NoError(t, os.WriteFile(inputArchive, []byte("test"), 0600))
|
|
||||||
|
|
||||||
osbuildResult, err := handleBuild(inputArchive, buildServer.URL)
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.True(t, osbuildResult.Success)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestHandleOutputArchive(t *testing.T) {
|
|
||||||
serverDir := t.TempDir()
|
|
||||||
serverOutputDir := filepath.Join(serverDir, "output")
|
|
||||||
require.NoError(t, os.Mkdir(serverOutputDir, 0755))
|
|
||||||
serverImageDir := filepath.Join(serverOutputDir, "image")
|
|
||||||
require.NoError(t, os.Mkdir(serverImageDir, 0755))
|
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(serverImageDir, "disk.img"), []byte("image"), 0600))
|
|
||||||
|
|
||||||
serverOutput := filepath.Join(serverDir, "server-output.tar")
|
|
||||||
cmd := exec.Command("tar",
|
|
||||||
"-C",
|
|
||||||
serverDir,
|
|
||||||
"-cf",
|
|
||||||
serverOutput,
|
|
||||||
filepath.Base(serverOutputDir),
|
|
||||||
)
|
|
||||||
require.NoError(t, cmd.Run())
|
|
||||||
|
|
||||||
resultServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
w.WriteHeader(http.StatusOK)
|
|
||||||
file, err := os.Open(serverOutput)
|
|
||||||
if err != nil {
|
|
||||||
require.NoError(t, err)
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
_, err = io.Copy(w, file)
|
|
||||||
require.NoError(t, err)
|
|
||||||
}))
|
|
||||||
|
|
||||||
outputDir := t.TempDir()
|
|
||||||
archive, err := fetchOutputArchive(outputDir, resultServer.URL)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
extractDir := filepath.Join(outputDir, "extracted")
|
|
||||||
require.NoError(t, os.Mkdir(extractDir, 0755))
|
|
||||||
require.NoError(t, extractOutputArchive(extractDir, archive))
|
|
||||||
|
|
||||||
content, err := os.ReadFile(filepath.Join(extractDir, "image", "disk.img"))
|
|
||||||
require.NoError(t, err)
|
|
||||||
require.Equal(t, []byte("image"), content)
|
|
||||||
}
|
|
||||||
|
|
@ -2,15 +2,133 @@ package osbuildexecutor_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"archive/tar"
|
"archive/tar"
|
||||||
|
"context"
|
||||||
|
"encoding/json"
|
||||||
|
"io"
|
||||||
|
"net/http"
|
||||||
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
|
"os/exec"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/osbuild/images/pkg/osbuild"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
|
||||||
"github.com/osbuild/osbuild-composer/internal/osbuildexecutor"
|
"github.com/osbuild/osbuild-composer/internal/osbuildexecutor"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestWaitForSI(t *testing.T) {
|
||||||
|
server := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
}))
|
||||||
|
|
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
|
||||||
|
defer cancel()
|
||||||
|
require.False(t, osbuildexecutor.WaitForSI(ctx, server.URL))
|
||||||
|
|
||||||
|
server.Start()
|
||||||
|
ctx2, cancel2 := context.WithTimeout(context.Background(), time.Second*1)
|
||||||
|
defer cancel2()
|
||||||
|
require.True(t, osbuildexecutor.WaitForSI(ctx2, server.URL))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestWriteInputArchive(t *testing.T) {
|
||||||
|
cacheDir := t.TempDir()
|
||||||
|
storeDir := filepath.Join(cacheDir, "store")
|
||||||
|
require.NoError(t, os.Mkdir(storeDir, 0755))
|
||||||
|
storeSubDir := filepath.Join(storeDir, "subdir")
|
||||||
|
require.NoError(t, os.Mkdir(storeSubDir, 0755))
|
||||||
|
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(storeDir, "contents"), []byte("storedata"), 0600))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(storeSubDir, "contents"), []byte("storedata"), 0600))
|
||||||
|
|
||||||
|
archive, err := osbuildexecutor.WriteInputArchive(cacheDir, storeDir, []string{"image"}, []byte("{\"version\": 2}"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
cmd := exec.Command("tar",
|
||||||
|
"-tf",
|
||||||
|
archive,
|
||||||
|
)
|
||||||
|
out, err := cmd.CombinedOutput()
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.ElementsMatch(t, []string{
|
||||||
|
"control.json",
|
||||||
|
"manifest.json",
|
||||||
|
"store/",
|
||||||
|
"store/subdir/",
|
||||||
|
"store/subdir/contents",
|
||||||
|
"store/contents",
|
||||||
|
"",
|
||||||
|
}, strings.Split(string(out), "\n"))
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleBuild(t *testing.T) {
|
||||||
|
buildServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusCreated)
|
||||||
|
osbuildResult := osbuild.Result{
|
||||||
|
Success: true,
|
||||||
|
}
|
||||||
|
data, err := json.Marshal(osbuildResult)
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = w.Write(data)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}))
|
||||||
|
|
||||||
|
cacheDir := t.TempDir()
|
||||||
|
inputArchive := filepath.Join(cacheDir, "test.tar")
|
||||||
|
require.NoError(t, os.WriteFile(inputArchive, []byte("test"), 0600))
|
||||||
|
|
||||||
|
osbuildResult, err := osbuildexecutor.HandleBuild(inputArchive, buildServer.URL)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.True(t, osbuildResult.Success)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleOutputArchive(t *testing.T) {
|
||||||
|
serverDir := t.TempDir()
|
||||||
|
serverOutputDir := filepath.Join(serverDir, "output")
|
||||||
|
require.NoError(t, os.Mkdir(serverOutputDir, 0755))
|
||||||
|
serverImageDir := filepath.Join(serverOutputDir, "image")
|
||||||
|
require.NoError(t, os.Mkdir(serverImageDir, 0755))
|
||||||
|
require.NoError(t, os.WriteFile(filepath.Join(serverImageDir, "disk.img"), []byte("image"), 0600))
|
||||||
|
|
||||||
|
serverOutput := filepath.Join(serverDir, "server-output.tar")
|
||||||
|
cmd := exec.Command("tar",
|
||||||
|
"-C",
|
||||||
|
serverDir,
|
||||||
|
"-cf",
|
||||||
|
serverOutput,
|
||||||
|
filepath.Base(serverOutputDir),
|
||||||
|
)
|
||||||
|
require.NoError(t, cmd.Run())
|
||||||
|
|
||||||
|
resultServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
w.WriteHeader(http.StatusOK)
|
||||||
|
file, err := os.Open(serverOutput)
|
||||||
|
if err != nil {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
_, err = io.Copy(w, file)
|
||||||
|
require.NoError(t, err)
|
||||||
|
}))
|
||||||
|
|
||||||
|
outputDir := t.TempDir()
|
||||||
|
archive, err := osbuildexecutor.FetchOutputArchive(outputDir, resultServer.URL)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
extractDir := filepath.Join(outputDir, "extracted")
|
||||||
|
require.NoError(t, os.Mkdir(extractDir, 0755))
|
||||||
|
require.NoError(t, osbuildexecutor.ExtractOutputArchive(extractDir, archive))
|
||||||
|
|
||||||
|
content, err := os.ReadFile(filepath.Join(extractDir, "image", "disk.img"))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, []byte("image"), content)
|
||||||
|
}
|
||||||
|
|
||||||
func makeTestTarfile(t *testing.T, content map[*tar.Header]string) string {
|
func makeTestTarfile(t *testing.T, content map[*tar.Header]string) string {
|
||||||
tmpdir := t.TempDir()
|
tmpdir := t.TempDir()
|
||||||
testTarPath := filepath.Join(tmpdir, "test.tar")
|
testTarPath := filepath.Join(tmpdir, "test.tar")
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue