Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions cmd/unikraft/testdata/TestGolden/volumes/help

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 29 additions & 0 deletions cmd/unikraft/testdata/TestGolden/volumes/import/dir

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions cmd/unikraft/testdata/TestGolden/volumes/import/invalid-port

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

79 changes: 79 additions & 0 deletions cmd/unikraft/testdata/TestGolden/volumes/import/serve

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 119 additions & 1 deletion cmd/unikraft/volumes_test.go
Comment thread
craciunoiuc marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@

package main

import "testing"
import (
"regexp"
"testing"
)

func volumesTests(t *testing.T, r *testRunner) {
t.Run("help", func(t *testing.T) {
Expand All @@ -16,6 +19,7 @@ func volumesTests(t *testing.T, r *testRunner) {
{args: []string{unikraftCmd, "volume", "wait", "--help"}},
{args: []string{unikraftCmd, "volume", "create", "--help"}},
{args: []string{unikraftCmd, "volume", "clone", "--help"}},
{args: []string{unikraftCmd, "volume", "import", "--help"}},
{args: []string{unikraftCmd, "volume", "edit", "--help"}},
{args: []string{unikraftCmd, "volume", "delete", "--help"}},
})
Expand Down Expand Up @@ -59,4 +63,118 @@ func volumesTests(t *testing.T, r *testRunner) {
{args: []string{unikraftCmd, "volume", "delete", "test-$UNIQ_VOLUME", "test-$UNIQ_VOLUME_CLONE"}},
})
})

t.Run("import", func(t *testing.T) {
// Offline: missing --source errors before any network call.
t.Run("missing-source", func(t *testing.T) {
r.run(t, []command{
{args: []string{unikraftCmd, "volume", "import", "my-volume"}, allowErr: true},
})
})

// Offline: port below the allowed range errors before any network call.
t.Run("invalid-port", func(t *testing.T) {
r.run(t, []command{
{args: []string{unikraftCmd, "volume", "import", "my-volume", "--source", ".", "--port", "80"}, allowErr: true},
})
})

// Offline: port above the allowed range errors before any network call.
t.Run("invalid-port-high", func(t *testing.T) {
r.run(t, []command{
{args: []string{unikraftCmd, "volume", "import", "my-volume", "--source", ".", "--port", "99999"}, allowErr: true},
})
})

// Import a small directory into a freshly created volume.
t.Run("dir", func(t *testing.T) {
r.
online().
withCleaners([]cleaner{
// free size differs on every run
{
pattern: regexp.MustCompile(`free=[0-9]+.?[0-9]*MiB`),
repl: "free=10MiB",
},
}).
withContext(map[string]string{
"hello.txt": "hello from volume import\n",
}).
run(t, []command{
{args: []string{unikraftCmd, "volume", "create", "--output", "quiet", "--set", "name=test-$UNIQ_VOLUME", "--set", "size=10", "--set", "metro=" + metroName}},
{args: []string{unikraftCmd, "volume", "import", "test-$UNIQ_VOLUME", "--source", "."}},
{args: []string{unikraftCmd, "volume", "inspect", "test-$UNIQ_VOLUME"}},
{args: []string{unikraftCmd, "volume", "delete", "test-$UNIQ_VOLUME"}},
})
})

// Import a file into a freshly created volume and test connection.
t.Run("serve", func(t *testing.T) {
r.
online().
withCleaners(instanceCleaners).
withCleaners([]cleaner{
{
// free size differs on every run
pattern: regexp.MustCompile(`free=[0-9]+.?[0-9]*MiB`),
repl: "free=50MiB",
},
}).
withContext(map[string]string{
"index.html": "<html><body>hello from volume import</body></html>\n",
}).
run(t, []command{
// Create a volume to hold the custom web content
{args: []string{
unikraftCmd, "volume", "create",
"--output", "quiet",
"--set", "name=test-$UNIQ_VOL",
"--set", "size=50",
"--set", "metro=" + metroName,
}},
// Import the custom index.html into the volume
{args: []string{unikraftCmd, "volume", "import", "test-$UNIQ_VOL", "--source", "."}},
{args: []string{
unikraftCmd, "instance", "create",
"--set", "name=test-$UNIQ_INST",
"--set", "metro=" + metroName,
"--set", "image=test/nginx-compat:latest",
"--set", "autostart=true",
"--set", "resources.memory=256",
"--set", "resources.vcpus=1",
"--set", "volumes=test-$UNIQ_VOL:/wwwroot:ro",
"--set", "service.services=443:8080/tls+http",
"--set", "service.domains=name=$UNIQ_DOMAIN",
}},
// Capture the assigned FQDN
{
args: []string{
unikraftCmd, "instance", "inspect", "test-$UNIQ_INST",
"--output", "template=" + `{{ (index .service.domains 0).fqdn }}`,
},
captureEnv: "FQDN",
},
{args: []string{unikraftCmd, "instance", "wait", "--until", "state==running", "--timeout", "30s", "test-$UNIQ_INST"}},
// Curl the instance and write the body to a file for content verification.
{args: []string{
"curl",
"-k",
"--fail",
"--silent",
"--show-error",
"--output", "response.html",
"--retry", "10",
"--retry-delay", "2",
"--retry-all-errors",
"--connect-timeout", "5",
"--max-time", "10",
"https://$FQDN",
}},
// Assert the imported content is served.
{args: []string{"grep", "hello from volume import", "response.html"}},
{args: []string{unikraftCmd, "instance", "delete", "test-$UNIQ_INST"}},
{args: []string{unikraftCmd, "volume", "delete", "test-$UNIQ_VOL"}},
})
})
})
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ require (
github.com/sirupsen/logrus v1.9.4
github.com/stretchr/testify v1.11.1
github.com/tidwall/gjson v1.18.0
github.com/unikraft/go-archivefs v0.0.0-20260414073833-365b9a2f34bd
github.com/unikraft/go-cpio v0.0.0-20260209140144-8e02f12b23e0
github.com/unikraft/go-archivefs v0.0.0-20260416145000-5840b16b2c08
github.com/unikraft/go-cpio v0.0.0-20260415131742-4f1984ca41f3
golang.org/x/net v0.53.0
golang.org/x/sync v0.20.0
gopkg.in/yaml.v3 v3.0.1
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,10 +372,10 @@ github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea h1:SXhTLE6pb6eld/
github.com/tonistiigi/units v0.0.0-20180711220420-6950e57a87ea/go.mod h1:WPnis/6cRcDZSUvVmezrxJPkiO87ThFYsoUiMwWNDJk=
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab h1:H6aJ0yKQ0gF49Qb2z5hI1UHxSQt4JMyxebFR15KnApw=
github.com/tonistiigi/vt100 v0.0.0-20240514184818-90bafcd6abab/go.mod h1:ulncasL3N9uLrVann0m+CDlJKWsIAP34MPcOJF6VRvc=
github.com/unikraft/go-archivefs v0.0.0-20260414073833-365b9a2f34bd h1:J3+GCdIQOYNOBVf+lAMpZPXBqRHIHa665OnahbGsND4=
github.com/unikraft/go-archivefs v0.0.0-20260414073833-365b9a2f34bd/go.mod h1:NLWtSTdsTRLGQN87zqm9+vN01Ei9JX+FWwuOme2wJXw=
github.com/unikraft/go-cpio v0.0.0-20260209140144-8e02f12b23e0 h1:RNkUuza8WzrA3FL5aTmmO/IGXFppbevPVc2TSRPLsgg=
github.com/unikraft/go-cpio v0.0.0-20260209140144-8e02f12b23e0/go.mod h1:OFJWAQVOq1qppx75rzF7qDoE5TqGjM1oCXW5N06Lftg=
github.com/unikraft/go-archivefs v0.0.0-20260416145000-5840b16b2c08 h1:jXNZRYJ2Zq0xi9C9mEltqImgXmTRodK2la18uIoyIH0=
github.com/unikraft/go-archivefs v0.0.0-20260416145000-5840b16b2c08/go.mod h1:NLWtSTdsTRLGQN87zqm9+vN01Ei9JX+FWwuOme2wJXw=
github.com/unikraft/go-cpio v0.0.0-20260415131742-4f1984ca41f3 h1:C8GONmMNDK3spdZEHBIffmq1b1hjCm9nZrquv3+4H+U=
github.com/unikraft/go-cpio v0.0.0-20260415131742-4f1984ca41f3/go.mod h1:OFJWAQVOq1qppx75rzF7qDoE5TqGjM1oCXW5N06Lftg=
github.com/vbatts/tar-split v0.12.2 h1:w/Y6tjxpeiFMR47yzZPlPj/FcPLpXbTUi/9H7d3CPa4=
github.com/vbatts/tar-split v0.12.2/go.mod h1:eF6B6i6ftWQcDqEn3/iGFRFRo8cBIMSJVOpnNdfTMFA=
github.com/wk8/go-ordered-map/v2 v2.1.8 h1:5h/BUHu93oj4gIdvHHHGsScSTMijfx5PeYkE/fJgbpc=
Expand Down
4 changes: 4 additions & 0 deletions internal/builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type BuildOpts struct {

type RootfsOpts struct {
Path string
Type RootfsType

// Output params
Format kraftfile.FsType
Expand All @@ -51,6 +52,9 @@ type RootfsType string

const (
RootfsTypeDockerfile RootfsType = "dockerfile"
RootfsTypeCpio RootfsType = "cpio"
RootfsTypeErofs RootfsType = "erofs"
RootfsTypeDir RootfsType = "dir"
)

// Build a unikraft image based on the provided build options.
Expand Down
5 changes: 3 additions & 2 deletions internal/builder/build_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,9 @@ func integrationContext(t *testing.T) context.Context {
func writeDockerfile(t *testing.T, dockerfile string) string {
t.Helper()
dir := t.TempDir()
require.NoError(t, os.WriteFile(filepath.Join(dir, "Dockerfile"), []byte(dockerfile), 0o644))
return dir
dockerfilePath := filepath.Join(dir, "Dockerfile")
require.NoError(t, os.WriteFile(dockerfilePath, []byte(dockerfile), 0o644))
return dockerfilePath
}

func writeSecretFile(t *testing.T, contents string) string {
Expand Down
Loading
Loading