Skip to content

Commit

Permalink
add comment to the exported functions
Browse files Browse the repository at this point in the history
Signed-off-by: Shahriyar Jalayeri <[email protected]>
  • Loading branch information
shjala committed Aug 21, 2024
1 parent e54b5d8 commit 7518ffd
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 38 deletions.
63 changes: 38 additions & 25 deletions pkg/remote/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,18 @@ import (
"golang.org/x/crypto/ssh"
)

// RemoteNode is a struct that holds the information about the remote node
type RemoteNode struct {

Check failure on line 23 in pkg/remote/utils.go

View workflow job for this annotation

GitHub Actions / yetus

revive: type name will be used as remote.RemoteNode by other packages, and that stutters; consider calling this Node https://revive.run/r#exported
Controler *openevec.OpenEVEC
Edgenode *device.Ctx
Tc *projects.TestContext
Ip string
SshPort string
SshUser string
SshPass string
Controller *openevec.OpenEVEC
Edgenode *device.Ctx
Tc *projects.TestContext
IP string
SSHPort string
SSHUser string
SSHPass string
}

// GetOpenEVEC returns the OpenEVEC controller
func GetOpenEVEC() *openevec.OpenEVEC {
edenConfigEnv := os.Getenv(defaults.DefaultConfigEnv)
configName := utils.GetConfig(edenConfigEnv)
Expand All @@ -41,15 +43,17 @@ func GetOpenEVEC() *openevec.OpenEVEC {
return openevec.CreateOpenEVEC(viperCfg)
}

// CreateRemoteNode creates a new RemoteNode struct
func CreateRemoteNode(node *device.Ctx, tc *projects.TestContext) *RemoteNode {
evec := GetOpenEVEC()
if evec == nil {
return nil
}

return &RemoteNode{Controler: evec, Edgenode: node, Tc: tc, Ip: "", SshPort: "22"}
return &RemoteNode{Controller: evec, Edgenode: node, Tc: tc, IP: "", SSHPort: "22"}
}

// RunEveCommand runs a command on the EVE node
func (node *RemoteNode) RunEveCommand(command string) ([]byte, error) {
realStdout := os.Stdout
r, w, err := os.Pipe()
Expand All @@ -60,7 +64,7 @@ func (node *RemoteNode) RunEveCommand(command string) ([]byte, error) {
os.Stdout = w

// unfortunately, we can't capture command return value from SSHEve
err = node.Controler.SSHEve(command)
err = node.Controller.SSHEve(command)

os.Stdout = realStdout
w.Close()
Expand All @@ -73,6 +77,7 @@ func (node *RemoteNode) RunEveCommand(command string) ([]byte, error) {
return out, nil
}

// FileExists checks if a file exists on EVE node
func (node *RemoteNode) FileExists(fileName string) (bool, error) {
command := fmt.Sprintf("if stat \"%s\"; then echo \"1\"; else echo \"0\"; fi", fileName)
out, err := node.RunEveCommand(command)
Expand All @@ -87,6 +92,7 @@ func (node *RemoteNode) FileExists(fileName string) (bool, error) {
return true, nil
}

// ReadFile reads a file from EVE node
func (node *RemoteNode) ReadFile(fileName string) ([]byte, error) {
exist, err := node.FileExists(fileName)
if err != nil {
Expand All @@ -101,6 +107,7 @@ func (node *RemoteNode) ReadFile(fileName string) ([]byte, error) {
return node.RunEveCommand(command)
}

// DeleteFile deletes a file from EVE node
func (node *RemoteNode) DeleteFile(fileName string) error {
exist, err := node.FileExists(fileName)
if err != nil {
Expand All @@ -116,6 +123,7 @@ func (node *RemoteNode) DeleteFile(fileName string) error {
return err
}

// WaitForAppStart waits for an app to start on the EVE node
func (node *RemoteNode) WaitForAppStart(appName string, timeoutSeconds uint) error {
start := time.Now()
for {
Expand All @@ -136,10 +144,12 @@ func (node *RemoteNode) WaitForAppStart(appName string, timeoutSeconds uint) err
}
}

func (node *RemoteNode) WaitForSsh(timeoutSeconds uint) error {
// WaitForSSH waits for the SSH connection to be established to the app VM that
// is running on the EVE node
func (node *RemoteNode) WaitForSSH(timeoutSeconds uint) error {
start := time.Now()
for {
_, err := node.AppSshExec("echo")
_, err := node.AppSSHExec("echo")
if err == nil {
return nil
}
Expand All @@ -152,18 +162,20 @@ func (node *RemoteNode) WaitForSsh(timeoutSeconds uint) error {
}
}

// StopAndRemoveApp stops and removes an app from the EVE node
func (node *RemoteNode) StopAndRemoveApp(appName string) error {
if err := node.Controler.PodStop(appName); err != nil {
if err := node.Controller.PodStop(appName); err != nil {
return err
}

if _, err := node.Controler.PodDelete(appName, true); err != nil {
if _, err := node.Controller.PodDelete(appName, true); err != nil {
return err
}

return nil
}

// GetNodeIP gets the IP address of the app running the EVE node
func (node *RemoteNode) GetNodeIP() error {
if node.Edgenode.GetRemoteAddr() == "" {
eveIPCIDR, err := node.Tc.GetState(node.Edgenode).LookUp("Dinfo.Network[0].IPAddrs[0]")
Expand All @@ -176,14 +188,15 @@ func (node *RemoteNode) GetNodeIP() error {
return fmt.Errorf("failed to parse IP address: %s", eveIPCIDR.String())
}

node.Ip = ip.To4().String()
node.IP = ip.To4().String()
return nil
}

node.Ip = node.Edgenode.GetRemoteAddr()
node.IP = node.Edgenode.GetRemoteAddr()
return nil
}

// GetAppState gets the state of an app running on the EVE node
func (node *RemoteNode) GetAppState(appName string) (string, error) {
ctrl, err := controller.CloudPrepare()
if err != nil {
Expand All @@ -208,13 +221,14 @@ func (node *RemoteNode) GetAppState(appName string) (string, error) {
return "", fmt.Errorf("app %s not found", appName)
}

func (node *RemoteNode) AppSshExec(command string) (string, error) {
host := fmt.Sprintf("%s:%s", node.Ip, node.SshPort) // Include port if necessary (default is 22)
// AppSSHExec executes a command on the app VM running on the EVE node
func (node *RemoteNode) AppSSHExec(command string) (string, error) {
host := fmt.Sprintf("%s:%s", node.IP, node.SSHPort)

config := &ssh.ClientConfig{
User: node.SshUser,
User: node.SSHUser,
Auth: []ssh.AuthMethod{
ssh.Password(node.SshPass),
ssh.Password(node.SSHPass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 30 * time.Second,
Expand All @@ -225,7 +239,6 @@ func (node *RemoteNode) AppSshExec(command string) (string, error) {
}
defer client.Close()

// Create a session
session, err := client.NewSession()
if err != nil {
return "", fmt.Errorf("failed to create session: %s", err)
Expand All @@ -240,13 +253,14 @@ func (node *RemoteNode) AppSshExec(command string) (string, error) {
return string(output), nil
}

func (node *RemoteNode) AppScpCopy(localFile, remoteFile string) error {
host := fmt.Sprintf("%s:%s", node.Ip, node.SshPort)
// AppSCPCopy copies a file from the local machine to the app VM running on the EVE node
func (node *RemoteNode) AppSCPCopy(localFile, remoteFile string) error {
host := fmt.Sprintf("%s:%s", node.IP, node.SSHPort)

config := &ssh.ClientConfig{
User: node.SshUser,
User: node.SSHUser,
Auth: []ssh.AuthMethod{
ssh.Password(node.SshPass),
ssh.Password(node.SSHPass),
},
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
Timeout: 30 * time.Second,
Expand All @@ -257,7 +271,6 @@ func (node *RemoteNode) AppScpCopy(localFile, remoteFile string) error {
}
defer client.Close()

// Create a session
session, err := client.NewSession()
if err != nil {
return fmt.Errorf("failed to create session: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion tests/vcom/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,5 @@ help:
@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."
@echo "You need access to docker socket and installed qemu packages."

6 changes: 1 addition & 5 deletions tests/vcom/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,7 @@ func deployApp(appLink, name, metadata string, portPub []string) error {
pc.StartDelay = 0
pc.PinCpus = false

if err := rnode.Controler.PodDeploy(appLink, pc, cfg); err != nil {
return err
}

return nil
return rnode.Controller.PodDeploy(appLink, pc, cfg)
}

func getAppName(prefix string) string {
Expand Down
14 changes: 7 additions & 7 deletions tests/vcom/vcom_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,29 +64,29 @@ func TestVcomLink(t *testing.T) {
t.Fatalf("Failed to wait for app to start: %v", err)
}

rnode.SshUser = vmUser
rnode.SshPass = vmPass
rnode.SshPort = sshPort
rnode.SSHUser = vmUser
rnode.SSHPass = vmPass
rnode.SSHPort = sshPort
err = rnode.GetNodeIP()
if err != nil {
t.Fatalf("Failed to get node IP: %v", err)
}

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

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

// send a request to vcomlink via vsock to get the host's TPM EK
command := "echo '{\"channel\":2,\"request\":1}' | socat - VSOCK-CONNECT:2:2000"
out, err := rnode.AppSshExec(command)
out, err := rnode.AppSSHExec(command)
if err != nil {
t.Fatalf("Failed to communicate with host via vsock: %v", err)
}
Expand Down

0 comments on commit 7518ffd

Please sign in to comment.