Skip to content

Commit

Permalink
add vcomlink service test
Browse files Browse the repository at this point in the history
Signed-off-by: Shahriyar Jalayeri <[email protected]>
  • Loading branch information
shjala committed Aug 29, 2024
1 parent a499b32 commit e27c64d
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 1 deletion.
77 changes: 77 additions & 0 deletions tests/vcom/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
DEBUG ?= "debug"

# HOSTARCH is the host architecture
# ARCH is the target architecture
# we need to keep track of them separately
HOSTARCH ?= $(shell uname -m)
HOSTOS ?= $(shell uname -s | tr A-Z a-z)

# canonicalized names for host architecture
override HOSTARCH := $(subst aarch64,arm64,$(subst x86_64,amd64,$(HOSTARCH)))

# unless otherwise set, I am building for my own architecture, i.e. not cross-compiling
# and for my OS
ARCH ?= $(HOSTARCH)
OS ?= $(HOSTOS)

# canonicalized names for target architecture
override ARCH := $(subst aarch64,arm64,$(subst x86_64,amd64,$(ARCH)))

WORKDIR ?= $(CURDIR)/../../dist
TESTDIR := tests/$(shell basename $(CURDIR))
BINDIR := $(WORKDIR)/bin
DATADIR := $(WORKDIR)/$(TESTDIR)/
BIN := eden
LOCALBIN := $(BINDIR)/$(BIN)-$(OS)-$(ARCH)
TESTNAME := eden.vcom
TESTBIN := $(TESTNAME).test
TESTSCN := $(TESTNAME).tests.txt
LOCALTESTBIN := $(TESTBIN)-$(OS)-$(ARCH)
LINKDIR := ../../tests/vcom

.DEFAULT_GOAL := help

clean:
rm -rf $(LOCALTESTBIN) $(BINDIR)/$(TESTBIN) $(WORKDIR)/$(TESTSCN) $(CURDIR)/$(TESTBIN) $(BINDIR)/$(TESTBIN)

$(BINDIR):
mkdir -p $@
$(DATADIR):
mkdir -p $@

test:
$(LOCALBIN) test $(CURDIR) -v $(DEBUG)

build: setup

testbin: $(TESTBIN)
$(LOCALTESTBIN): $(BINDIR) *.go
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go test -c -ldflags "-s -w" -o $@ *.go

$(TESTBIN): $(LOCALTESTBIN)
ln -sf $(LOCALTESTBIN) $(CURDIR)/$(TESTBIN)

setup: testbin $(BINDIR) $(DATADIR)
cp -a $(LOCALTESTBIN) $(CURDIR)/$(TESTBIN) $(BINDIR)
cp -a *.yml $(TESTSCN) $(DATADIR)

debug:
CGO_ENABLED=0 GOOS=$(OS) GOARCH=$(ARCH) go test -c -gcflags "all=-N -l" -o $@ *.go
dlv dap --listen=:12345 --headless=true --api-version=2 exec ./debug -- -test.v

.PHONY: test build setup clean all testbin debug

help:
@echo "EDEN is the harness for testing EVE and ADAM"
@echo
@echo "This Makefile automates commons tasks of EDEN testing"
@echo
@echo "Commonly used maintenance and development targets:"
@echo " build build test-binary (OS and ARCH options supported, for ex. OS=linux ARCH=arm64)"
@echo " setup setup of test environment"
@echo " test run tests"
@echo " clean cleanup of test harness"
@echo
@echo "You need install requirements for EVE (look at https://github.com/lf-edge/eve#install-dependencies)."
@echo "You need access to docker socket and installed qemu packages."

7 changes: 7 additions & 0 deletions tests/vcom/eden-config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
eden:
# test binary
test-bin: "eden.vcom.test"

# test scenario
test-scenario: "eden.vcom.tests.txt"
1 change: 1 addition & 0 deletions tests/vcom/eden.vcom.tests.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eden.vcom.test
11 changes: 11 additions & 0 deletions tests/vcom/scripts/testvsock.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env python3

Check failure on line 1 in tests/vcom/scripts/testvsock.py

View workflow job for this annotation

GitHub Actions / yetus

pylint:[C0114(missing-module-docstring), ] Missing module docstring
import socket

CID = socket.VMADDR_CID_HOST
PORT = 2000
s = socket.socket(socket.AF_VSOCK, socket.SOCK_STREAM)
s.connect((CID, PORT))
s.sendall(b"{\"channel\":2,\"request\":1}")
response = s.recv(1024)
print(response.decode('utf-8'))
s.close()
107 changes: 107 additions & 0 deletions tests/vcom/vcom_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package vcom

import (
"os"
"strings"
"testing"
"time"

tk "github.com/lf-edge/eden/pkg/evetestkit"
log "github.com/sirupsen/logrus"
)

var eveNode *tk.EveNode

const (
sshPort = "8027"
appLink = "https://cloud-images.ubuntu.com/releases/22.04/release/ubuntu-22.04-server-cloudimg-amd64.img"
projectName = "vcomlink"
)

func TestMain(m *testing.M) {
log.Println("VCOM Test started")
defer log.Println("VCOM Test finished")

node, err := tk.InitilizeTest(projectName, tk.WithControllerVerbosity("debug"))
if err != nil {
log.Fatalf("Failed to initialize test: %v", err)
}

eveNode = node
res := m.Run()
os.Exit(res)
}

func TestVcomLink(t *testing.T) {
log.Println("TestVcomLink started")
defer log.Println("TestVcomLink finished")

if !eveNode.EveIsTpmEnabled() {
t.Skip("TPM is not enabled, skipping test")
}

t.Log("Checking if vcomlink is running on EVE")
stat, err := eveNode.EveRunCommand("eve exec pillar ss -l --vsock")
if err != nil {
t.Fatalf("Failed to check if vcomlink is running: %v", err)
}
// vcomlink listens on port 2000 and host cid is 2.
// this is hacky way to check it is running, but it works ¯\_(ツ)_/¯
if !strings.Contains(string(stat), "2:2000") {
t.Fatalf("vcomlink is not running, ss output :\n%s", stat)
}

appName := tk.GetRandomAppName(projectName)
pubPorts := []string{sshPort + ":22"}
pc := tk.GetDefaultVMConfig(appName, tk.AppDefaultCloudConfig, pubPorts)
err = eveNode.EveDeployApp(appLink, pc, tk.WithSSH(tk.AppDefaultSSHUser, tk.AppDefaultSSHPass, sshPort))
if err != nil {
t.Fatalf("Failed to deploy app: %v", err)
}
defer func() {
err = eveNode.AppStopAndRemove(appName)
if err != nil {
log.Errorf("Failed to stop and remove app: %v", err)
}
}()

// wait for the app to show up in the list
time.Sleep(10 * time.Second)

// wait 5 minutes for the app to start
t.Logf("Waiting for app %s to start...", appName)
err = eveNode.AppWaitForRunningState(appName, 60*5)
if err != nil {
t.Fatalf("Failed to wait for app to start: %v", err)
}

t.Logf("Waiting for ssh to be ready...")
err = eveNode.AppWaitForSSH(appName, 60*5)
if err != nil {
t.Fatalf("Failed to wait for ssh: %v", err)
}
t.Logf("SSH connection established")

t.Log("Installing socat on the vm")
_, err = eveNode.AppSSHExec(appName, "sudo apt-get -y install socat")
if err != nil {
t.Fatalf("Failed install socat on the vm: %v", err)
}

err = eveNode.AppSCPCopy(appName, "scripts/testvsock.py", "testvsock.py")
if err != nil {
t.Fatalf("Failed to copy testvsock.py to the vm: %v", err)
}

out, err := eveNode.AppSSHExec(appName, "python3 testvsock.py")
if err != nil {
t.Fatalf("Failed to communicate with host via vsock: %v", err)
}

// XXX : fix this by importing vcom from eve and unmrashal the output
// to TpmResponseEk
if strings.Contains(string(out), "error") {
t.Fatalf("Failed to communicate with host via vsock: %v", out)
}
t.Log("Successfully communicated from VM to vcomlink (host) via vsock")
}
5 changes: 4 additions & 1 deletion tests/workflow/smoke.tests.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Number of tests
{{$tests := 23}}
{{$tests := 24}}
# EDEN_TEST_SETUP env. var. -- "y"(default) performs the EDEN setup steps
{{$setup := "y"}}
{{$setup_env := EdenGetEnv "EDEN_TEST_SETUP"}}
Expand Down Expand Up @@ -81,3 +81,6 @@ eden.escript.test -test.run TestEdenScripts/eden_reset

/bin/echo EVE security tests (23/{{$tests}})
eden.escript.test -test.run TestEdenScripts/sec_eden

/bin/echo EVE VcomLink tests (24/{{$tests}})
eden.escript.test -test.run TestEdenScripts/eden_vcom
1 change: 1 addition & 0 deletions tests/workflow/testdata/eden_vcom.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test eden.vcom.test

0 comments on commit e27c64d

Please sign in to comment.