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
36 changes: 36 additions & 0 deletions cmd/unikraft/integration/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package integration

import (
"encoding/json"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -50,6 +52,40 @@ func TestAPI(t *testing.T) {
assert.True(t, json.Valid([]byte(out)), "expected JSON response, got: %s", out)
})

t.Run("volume from data file", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
volName := uniq()
payload, err := json.Marshal(map[string]any{
"name": "test-" + volName,
"size_mb": 10,
})
require.NoError(t, err)
Comment thread
craciunoiuc marked this conversation as resolved.
payloadPath := filepath.Join(t.TempDir(), "volume.json")
require.NoError(t, os.WriteFile(payloadPath, payload, 0o600))
deletePayload, err := json.Marshal([]map[string]string{{"name": "test-" + volName}})
require.NoError(t, err)
deleteArgs := []string{
"unikraft", "api",
"--metro=" + r.Config.MetroName,
"/v1/volumes",
"--method", "DELETE",
"--data", string(deletePayload),
}

out := r.Run(t, []string{
"unikraft", "api",
"--metro=" + r.Config.MetroName,
"/v1/volumes",
"--data", "@" + payloadPath,
})
t.Cleanup(func() {
out, err := r.RunRaw(t, deleteArgs, integ.WithoutCancel())
assert.NoError(t, err, "deleting raw API-created volume: %s", out)
})
require.True(t, json.Valid([]byte(out)), "expected JSON response, got: %s", out)
assert.Contains(t, out, "test-"+volName)
})

t.Run("missing endpoint fails", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})

Expand Down
28 changes: 28 additions & 0 deletions cmd/unikraft/integration/certificate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ package integration

import (
"net"
"os"
"path/filepath"
"strings"
"testing"

Expand Down Expand Up @@ -46,6 +48,32 @@ func TestCertificates(t *testing.T) {
assert.Regexp(t, `test-`, out)
})

t.Run("create-shortcut-files", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
certName := uniq()
cert := integ.GenerateCert(t, "")
dir := t.TempDir()
chainPath := filepath.Join(dir, "chain.pem")
keyPath := filepath.Join(dir, "key.pem")
require.NoError(t, os.WriteFile(chainPath, []byte(cert.Chain), 0o600))
require.NoError(t, os.WriteFile(keyPath, []byte(cert.Key), 0o600))

out := r.Run(t, []string{
"unikraft", "certificate", "create",
"--set", "name=test-" + certName,
"--set", "metro=" + r.Config.MetroName,
"--cn", cert.CN,
"--chain", chainPath,
"--pkey", keyPath,
})
assert.Regexp(t, `state:\s+valid`, out)

out = r.Run(t, []string{"unikraft", "certificate", "inspect", "test-" + certName})
assert.Regexp(t, `state:\s+valid`, out)

r.Run(t, []string{"unikraft", "certificate", "delete", "test-" + certName})
})

t.Run("serve", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
certName := uniq()
Expand Down
28 changes: 28 additions & 0 deletions cmd/unikraft/integration/config_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, Unikraft GmbH and The Unikraft CLI Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package integration

import (
"encoding/json"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"unikraft.com/cli/internal/config"
)

func TestConfig(t *testing.T) {
Expand All @@ -26,4 +32,26 @@ func TestConfig(t *testing.T) {
assert.Regexp(t, `token:`, out)
assert.Regexp(t, `profile:`, out)
})

t.Run("get-explicit-file", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
profileName := "test-" + uniq()
profile := *r.Config.Profile
profile.Name = profileName
configPath := filepath.Join(t.TempDir(), "alternate.yaml")
cfg := &config.Config{
Path: configPath,
DefaultProfile: profileName,
Profiles: map[string]config.Profile{profileName: profile},
}
require.NoError(t, cfg.Save())

out := r.Run(t, []string{"unikraft", "config", "get", configPath, "--output", "json"})
var cfgs []struct {
Profile string `json:"profile"`
}
require.NoError(t, json.Unmarshal([]byte(out), &cfgs))
require.Len(t, cfgs, 1)
assert.Equal(t, profileName, cfgs[0].Profile)
})
}
22 changes: 22 additions & 0 deletions cmd/unikraft/integration/image_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package integration

import (
"fmt"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -38,4 +39,25 @@ func TestImages(t *testing.T) {
assert.Regexp(t, `kernel.dbg:`, out)
r.Run(t, []string{"unikraft", "image", "delete", imageFull})
})

t.Run("copy-local-archive", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
archive := filepath.Join(t.TempDir(), "nginx.oci.tar")
imageTag := uniq()
imageName := r.Config.Profile.Organization + "/nginx-archive:" + imageTag
imageFull := fmt.Sprintf("%s/%s", r.Config.Metro.Index().Host, imageName)

r.Run(t, []string{"unikraft", "image", "copy", "nginx:latest", archive})
assert.FileExists(t, archive)

out := r.Run(t, []string{"unikraft", "image", "inspect", archive})
assert.Regexp(t, `digest:\s+sha256:`, out)
assert.Regexp(t, `config:`, out)

r.Run(t, []string{"unikraft", "image", "copy", archive, imageFull})
out = r.Run(t, []string{"unikraft", "image", "inspect", imageFull})
assert.Regexp(t, `ref:\s+.*`+imageName, out)

r.Run(t, []string{"unikraft", "image", "delete", imageFull})
})
}
28 changes: 28 additions & 0 deletions cmd/unikraft/integration/instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,6 +1005,34 @@ cmd: ["cat", "/marker.txt"]
r.Run(t, []string{"unikraft", "image", "delete", image})
})

t.Run("replicas", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
instName := uniq()

out := r.Run(t, []string{
"unikraft", "instance", "create",
"--output", "template={{ .name }}",
"--name", "test-" + instName,
"--metro", r.Config.MetroName,
"--image", "nginx:latest",
"--memory", "128",
"--vcpus", "1",
"--replicas", "2",
})
instances := strings.Fields(out)
require.Len(t, instances, 3)
assert.Len(t, map[string]struct{}{
instances[0]: {},
instances[1]: {},
instances[2]: {},
}, 3)
Comment thread
craciunoiuc marked this conversation as resolved.

out = r.Run(t, append([]string{"unikraft", "instance", "inspect"}, instances...))
assert.Regexp(t, `image:\s+nginx`, out)

r.Run(t, append([]string{"unikraft", "instance", "delete"}, instances...))
})

t.Run("watch-timeout", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
r.Run(t, []string{"unikraft", "--timeout=1s", "instance", "ls", "-w"}, integ.AllowFail())
Expand Down
32 changes: 32 additions & 0 deletions cmd/unikraft/integration/metro_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, Unikraft GmbH and The Unikraft CLI Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

package integration

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestMetros(t *testing.T) {
t.Run("get", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})

out := r.Run(t, []string{"unikraft", "metro", "get", r.Config.MetroName})
assert.Contains(t, out, "name:")
assert.Contains(t, out, r.Config.MetroName)
assert.Contains(t, out, "endpoint:")
})

t.Run("get-quotas", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})

out := r.Run(t, []string{"unikraft", "metro", "get", r.Config.MetroName, "-f", "+quotas"})
assert.Contains(t, out, "quotas:")
assert.Contains(t, out, "instances:")
assert.Contains(t, out, "vcpus:")
})
}
45 changes: 45 additions & 0 deletions cmd/unikraft/integration/resource_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2026, Unikraft GmbH and The Unikraft CLI Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm. Just noticed this is inconsistent, weird. Let's add this to the LICENSE.fragment.txt and fix all instances. Can be a follow-up.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, in a separate PR


package integration

Expand All @@ -9,6 +10,8 @@ import (
"testing"

"github.com/stretchr/testify/assert"

integ "unikraft.com/cli/internal/integration"
)

func TestResources(t *testing.T) {
Expand Down Expand Up @@ -37,4 +40,46 @@ func TestResources(t *testing.T) {
out = r.Run(t, []string{"unikraft", "volume", "list", "--output", "quiet"})
assert.Empty(t, strings.TrimSpace(out))
})

t.Run("multi-type-flow", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
svcName := uniq()
instName := uniq()
domainName := uniq()
svcKey := "service:" + r.Config.MetroName + "/test-" + svcName
instKey := "instance:" + r.Config.MetroName + "/test-" + instName

r.Run(t, []string{
"unikraft", "resource", "create",
"--set", "type=service",
"--set", "name=test-" + svcName,
"--set", "metro=" + r.Config.MetroName,
"--set", "services=443:8080/tls+http",
})
r.Run(t, []string{
"unikraft", "resource", "create",
"--set", "type=instance",
"--set", "name=test-" + instName,
"--set", "metro=" + r.Config.MetroName,
"--set", "image=nginx:latest",
"--set", "autostart=false",
"--set", "resources.memory=128",
"--set", "resources.vcpus=1",
})

out := r.Run(t, []string{"unikraft", "resource", "get", svcKey, instKey})
assert.Contains(t, out, "test-"+svcName)
assert.Contains(t, out, "test-"+instName)

r.Run(t, []string{
"unikraft", "resource", "edit", svcKey,
"--add", "domains=fqdn=" + domainName + ".unikraft.example",
})
out = r.Run(t, []string{"unikraft", "resource", "get", svcKey})
assert.Contains(t, out, domainName+".unikraft.example")

r.Run(t, []string{"unikraft", "resource", "delete", svcKey, instKey})
r.Run(t, []string{"unikraft", "service", "inspect", "test-" + svcName}, integ.ExpectFail())
r.Run(t, []string{"unikraft", "instance", "inspect", "test-" + instName}, integ.ExpectFail())
})
}
39 changes: 39 additions & 0 deletions cmd/unikraft/integration/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,43 @@ func TestServices(t *testing.T) {

r.Run(t, []string{"unikraft", "service", "delete", "test-" + svcName})
})

t.Run("shortcuts-and-patches", func(t *testing.T) {
r := runner(t, true, []string{staging, stable})
svcName := uniq()
domainName := uniq()
addedDomainName := uniq()

r.Run(t, []string{
"unikraft", "service", "create",
"--output", "quiet",
"--name", "test-" + svcName,
"--metro", r.Config.MetroName,
"--domains", domainName + ".unikraft.example",
"--services", "443:8080/tls+http",
})

r.Run(t, []string{
"unikraft", "service", "edit", "test-" + svcName,
"--output", "quiet",
"--add", "domains=fqdn=" + addedDomainName + ".unikraft.example",
"--add", "services=8443:8080/tls",
})
out := r.Run(t, []string{"unikraft", "service", "inspect", "test-" + svcName})
assert.Contains(t, out, domainName+".unikraft.example")
assert.Contains(t, out, addedDomainName+".unikraft.example")
assert.Regexp(t, `source:\s+8443`, out)

r.Run(t, []string{
"unikraft", "service", "edit", "test-" + svcName,
"--output", "quiet",
"--del", "domains=fqdn=" + addedDomainName + ".unikraft.example",
"--del", "services=8443:8080/tls",
})
out = r.Run(t, []string{"unikraft", "service", "inspect", "test-" + svcName})
assert.NotContains(t, out, addedDomainName+".unikraft.example")
assert.NotRegexp(t, `source:\s+8443`, out)

r.Run(t, []string{"unikraft", "service", "delete", "test-" + svcName})
})
}
Loading
Loading