2023-03-31 12:31:54 +00:00
|
|
|
package mos
|
2022-08-12 13:55:47 +00:00
|
|
|
|
|
|
|
import (
|
2022-10-13 09:12:51 +00:00
|
|
|
"context"
|
2022-08-12 13:55:47 +00:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2023-03-31 12:31:54 +00:00
|
|
|
"net"
|
2022-08-12 13:55:47 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
2023-03-31 12:31:54 +00:00
|
|
|
"strconv"
|
2022-11-26 14:05:00 +00:00
|
|
|
"strings"
|
2022-08-12 13:55:47 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-31 12:31:54 +00:00
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/kairos-io/kairos-sdk/utils"
|
2022-10-13 09:12:51 +00:00
|
|
|
process "github.com/mudler/go-processmanager"
|
2022-08-12 13:55:47 +00:00
|
|
|
. "github.com/onsi/ginkgo/v2"
|
|
|
|
. "github.com/onsi/gomega"
|
2022-10-13 09:12:51 +00:00
|
|
|
. "github.com/spectrocloud/peg/matcher"
|
2023-03-31 12:31:54 +00:00
|
|
|
"github.com/spectrocloud/peg/pkg/machine"
|
2022-10-13 09:12:51 +00:00
|
|
|
"github.com/spectrocloud/peg/pkg/machine/types"
|
2022-08-12 13:55:47 +00:00
|
|
|
)
|
|
|
|
|
2022-11-30 11:31:41 +00:00
|
|
|
var kubectl = func(s string) (string, error) {
|
|
|
|
return Sudo("k3s kubectl " + s)
|
|
|
|
}
|
|
|
|
|
2022-08-12 13:55:47 +00:00
|
|
|
func TestSuite(t *testing.T) {
|
|
|
|
RegisterFailHandler(Fail)
|
2022-09-16 15:42:45 +00:00
|
|
|
RunSpecs(t, "kairos Test Suite")
|
2022-08-12 13:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-11-26 14:05:00 +00:00
|
|
|
func isFlavor(flavor string) bool {
|
|
|
|
return strings.Contains(os.Getenv("FLAVOR"), flavor)
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
func screenshot() (string, error) {
|
|
|
|
vbox, ok := Machine.(*machine.VBox)
|
|
|
|
if ok {
|
|
|
|
return vbox.Screenshot()
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("screenshot not implemented")
|
|
|
|
}
|
|
|
|
func detachAndReboot() {
|
|
|
|
vbox, ok := Machine.(*machine.VBox)
|
|
|
|
if ok {
|
|
|
|
vbox.DetachCD()
|
|
|
|
vbox.Restart()
|
|
|
|
} else {
|
|
|
|
Reboot()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-12 13:55:47 +00:00
|
|
|
var sshPort string
|
|
|
|
|
|
|
|
var _ = AfterSuite(func() {
|
|
|
|
if os.Getenv("CREATE_VM") == "true" {
|
2022-10-13 09:12:51 +00:00
|
|
|
if Machine != nil {
|
|
|
|
Machine.Stop()
|
|
|
|
Machine.Clean()
|
2022-08-12 13:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
2022-10-13 09:12:51 +00:00
|
|
|
if !CurrentSpecReport().Failure.IsZero() {
|
|
|
|
gatherLogs()
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
})
|
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
func user() string {
|
|
|
|
user := os.Getenv("SSH_USER")
|
|
|
|
if user == "" {
|
|
|
|
user = "kairos"
|
|
|
|
}
|
|
|
|
return user
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
func pass() string {
|
|
|
|
pass := os.Getenv("SSH_PASS")
|
|
|
|
if pass == "" {
|
|
|
|
pass = "kairos"
|
2022-08-12 13:55:47 +00:00
|
|
|
}
|
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
return pass
|
|
|
|
}
|
|
|
|
|
|
|
|
var _ = BeforeSuite(func() {
|
|
|
|
|
2023-03-31 12:31:54 +00:00
|
|
|
machineID := uuid.New().String()
|
2022-08-12 13:55:47 +00:00
|
|
|
|
|
|
|
if os.Getenv("ISO") == "" && os.Getenv("CREATE_VM") == "true" {
|
|
|
|
fmt.Println("ISO missing")
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.Getenv("CREATE_VM") == "true" {
|
|
|
|
t, err := ioutil.TempDir("", "")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
2023-03-31 12:31:54 +00:00
|
|
|
p, _ := getFreePort()
|
|
|
|
sshPort = strconv.Itoa(p)
|
2022-08-12 13:55:47 +00:00
|
|
|
if os.Getenv("SSH_PORT") != "" {
|
|
|
|
sshPort = os.Getenv("SSH_PORT")
|
|
|
|
}
|
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
opts := []types.MachineOption{
|
2022-12-01 12:58:09 +00:00
|
|
|
types.WithMemory("9000"),
|
2022-10-13 09:12:51 +00:00
|
|
|
types.WithISO(os.Getenv("ISO")),
|
|
|
|
types.WithSSHPort(sshPort),
|
|
|
|
types.WithID(machineID),
|
|
|
|
types.WithSSHUser(user()),
|
|
|
|
types.WithSSHPass(pass()),
|
|
|
|
types.OnFailure(func(p *process.Process) {
|
|
|
|
out, _ := ioutil.ReadFile(p.StdoutPath())
|
|
|
|
err, _ := ioutil.ReadFile(p.StderrPath())
|
|
|
|
status, _ := p.ExitCode()
|
2023-03-31 12:31:54 +00:00
|
|
|
fmt.Printf("VM Aborted: %s %s Exit status: %s", out, err, status)
|
2022-10-13 09:12:51 +00:00
|
|
|
Fail(fmt.Sprintf("VM Aborted: %s %s Exit status: %s", out, err, status))
|
|
|
|
}),
|
|
|
|
types.WithStateDir(t),
|
|
|
|
types.WithDataSource(os.Getenv("DATASOURCE")),
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
if os.Getenv("USE_QEMU") == "true" {
|
|
|
|
opts = append(opts, types.QEMUEngine)
|
|
|
|
} else {
|
|
|
|
opts = append(opts, types.VBoxEngine)
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
m, err := machine.New(opts...)
|
|
|
|
if err != nil {
|
|
|
|
Fail(err.Error())
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
Machine = m
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2023-03-31 12:31:54 +00:00
|
|
|
if _, err := Machine.Create(context.Background()); err != nil {
|
2022-10-13 09:12:51 +00:00
|
|
|
Fail(err.Error())
|
|
|
|
}
|
2022-08-12 13:55:47 +00:00
|
|
|
}
|
2022-10-13 09:12:51 +00:00
|
|
|
})
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2023-03-31 12:31:54 +00:00
|
|
|
func getFreePort() (port int, err error) {
|
|
|
|
var a *net.TCPAddr
|
|
|
|
if a, err = net.ResolveTCPAddr("tcp", "localhost:0"); err == nil {
|
|
|
|
var l *net.TCPListener
|
|
|
|
if l, err = net.ListenTCP("tcp", a); err == nil {
|
|
|
|
defer l.Close()
|
|
|
|
return l.Addr().(*net.TCPAddr).Port, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-12 13:55:47 +00:00
|
|
|
func gatherLogs() {
|
2022-12-01 15:15:53 +00:00
|
|
|
Machine.SendFile("assets/kubernetes_logs.sh", "/tmp/logs.sh", "0770")
|
2022-12-03 22:37:50 +00:00
|
|
|
Sudo("cat /oem/* > /run/oem.yaml")
|
2022-12-03 22:45:17 +00:00
|
|
|
Sudo("cat /etc/resolv.conf > /run/resolv.conf")
|
2022-10-13 09:12:51 +00:00
|
|
|
Sudo("k3s kubectl get pods -A -o json > /run/pods.json")
|
|
|
|
Sudo("k3s kubectl get events -A -o json > /run/events.json")
|
|
|
|
Sudo("cat /proc/cmdline > /run/cmdline")
|
|
|
|
Sudo("chmod 777 /run/events.json")
|
2022-12-01 15:15:53 +00:00
|
|
|
Sudo("sh /tmp/logs.sh > /run/kube_logs")
|
2022-10-13 09:12:51 +00:00
|
|
|
Sudo("df -h > /run/disk")
|
|
|
|
Sudo("mount > /run/mounts")
|
|
|
|
Sudo("blkid > /run/blkid")
|
2022-12-03 22:45:17 +00:00
|
|
|
Sudo("dmesg > /run/dmesg.log")
|
2022-08-12 13:55:47 +00:00
|
|
|
|
2022-10-13 09:12:51 +00:00
|
|
|
GatherAllLogs(
|
2022-08-12 13:55:47 +00:00
|
|
|
[]string{
|
2022-09-16 15:42:45 +00:00
|
|
|
"edgevpn@kairos",
|
|
|
|
"kairos-agent",
|
2022-08-12 13:55:47 +00:00
|
|
|
"cos-setup-boot",
|
|
|
|
"cos-setup-network",
|
2022-12-03 22:45:17 +00:00
|
|
|
"cos-setup-initramfs",
|
|
|
|
"cos-setup-reconcile",
|
2022-09-16 15:42:45 +00:00
|
|
|
"kairos",
|
2022-08-12 13:55:47 +00:00
|
|
|
"k3s",
|
2022-12-03 22:45:17 +00:00
|
|
|
"k3s-agent",
|
2022-08-12 13:55:47 +00:00
|
|
|
},
|
|
|
|
[]string{
|
|
|
|
"/var/log/edgevpn.log",
|
2022-09-16 15:42:45 +00:00
|
|
|
"/var/log/kairos/agent.log",
|
2022-08-12 13:55:47 +00:00
|
|
|
"/run/pods.json",
|
|
|
|
"/run/disk",
|
|
|
|
"/run/mounts",
|
2022-12-01 15:15:53 +00:00
|
|
|
"/run/kube_logs",
|
2022-08-12 13:55:47 +00:00
|
|
|
"/run/blkid",
|
|
|
|
"/run/events.json",
|
|
|
|
"/run/cmdline",
|
2022-12-03 22:37:50 +00:00
|
|
|
"/run/oem.yaml",
|
2022-12-03 22:45:17 +00:00
|
|
|
"/run/resolv.conf",
|
|
|
|
"/run/dmesg.log",
|
2022-08-12 13:55:47 +00:00
|
|
|
})
|
|
|
|
}
|
2022-10-13 09:12:51 +00:00
|
|
|
|
|
|
|
func download(s string) {
|
|
|
|
f2, err := ioutil.TempFile("", "fff")
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
defer os.RemoveAll(f2.Name())
|
|
|
|
|
|
|
|
resp, err := http.Get(s)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
defer resp.Body.Close()
|
|
|
|
_, err = io.Copy(f2, resp.Body)
|
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
|
|
|
|
|
out, err := utils.SH("tar xvf " + f2.Name())
|
|
|
|
fmt.Println(out)
|
|
|
|
Expect(err).ToNot(HaveOccurred(), out)
|
|
|
|
}
|