diff options
-rw-r--r-- | test/config.go | 2 | ||||
-rw-r--r-- | test/fixtures/ssh/authorized_keys | 1 | ||||
-rw-r--r-- | test/fixtures/ssh/ssh | 27 | ||||
-rw-r--r-- | test/fixtures/ssh/ssh.pub | 1 | ||||
-rw-r--r-- | test/fixtures/ssh/ssh_host_rsa_key | 27 | ||||
-rw-r--r-- | test/fixtures/ssh/sshd_config | 116 | ||||
-rw-r--r-- | test/main_test.go | 4 | ||||
-rw-r--r-- | test/target_test.go | 263 |
8 files changed, 357 insertions, 84 deletions
diff --git a/test/config.go b/test/config.go index 4850919..0b3ce76 100644 --- a/test/config.go +++ b/test/config.go @@ -24,10 +24,12 @@ const ( prefixURL = "http://localhost:8000" logFileXdsServer = "xdsserver-test.log" logFileClient = "client-test.log" + logFileSSHd = "sshd.log" envXdtSdk = "XDT_SDK" envXdsServerWorkspaceDir = "XDS_SERVER_WORKSPACE_DIR" envXdsServerRootCfgDir = "XDS_SERVER_ROOT_CFG_DIR" sdkFileName = "poky-agl-glibc-x86_64-gcc_crosssdk-native_x86_64-toolchain-1.0.2.sh" sdkFile = "fixtures/" + sdkFileName helloworldFixturesDir = "fixtures/helloworld" + sshFixturesDir = "fixtures/ssh" ) diff --git a/test/fixtures/ssh/authorized_keys b/test/fixtures/ssh/authorized_keys new file mode 100644 index 0000000..42d67cb --- /dev/null +++ b/test/fixtures/ssh/authorized_keys @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5YA/PN5m6gOtjP98h75EcW3AYyRlcEYoJa2gRhv3iFE+An7LK42VhEJ7OeKV1vvEAKZWTbr67RAkdl6gl3sNmL6oNwYelUgLJ1OrvCbKdA9+aj6J3+ydBFtn/etyBYR+DTlsgZstO7jTZiXJBKy+ungZ0f6DbKUOqnU4OeiQhqz7wnm+jjiXeBlSeH/gy0eoadSkO2jiwP4X+0izSpTAxsfhkLU85rWMJWiCD7ZS8TauXNYhXQBt9yi9I2n7VYxsJU+yVuSum7SYMRbJuvWWpj8vMb07pS/XXg2RHcAMu3vBQtl5uisJ94vuL8xcIu+PQNAYawOYa1ijzRSec7fC5 test@clementMachine diff --git a/test/fixtures/ssh/ssh b/test/fixtures/ssh/ssh new file mode 100644 index 0000000..49d97dc --- /dev/null +++ b/test/fixtures/ssh/ssh @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAuWAPzzeZuoDrYz/fIe+RHFtwGMkZXBGKCWtoEYb94hRPgJ+y +yuNlYRCeznildb7xACmVk26+u0QJHZeoJd7DZi+qDcGHpVICydTq7wmynQPfmo+i +d/snQRbZ/3rcgWEfg05bIGbLTu402YlyQSsvrp4GdH+g2ylDqp1ODnokIas+8J5v +o44l3gZUnh/4MtHqGnUpDto4sD+F/tIs0qUwMbH4ZC1POa1jCVogg+2UvE2rlzWI +V0AbfcovSNp+1WMbCVPslbkrpu0mDEWybr1lqY/LzG9O6Uv114NkR3ADLt7wULZe +borCfeL7i/MXCLvj0DQGGsDmGtYo80UnnO3wuQIDAQABAoIBABatMqHlqk+PT7wv +ZwtOtBCk+tx9VTXXC2zFj9B9E/ehBmy0nHmHQbKTBuo5BfyWZALiE2MarAhadUcC +c1ZWns6UTBgssXw+wQqRmj+tjwT4IVVV9sj4lz+2HOrMy8aj6+fzIOCg0oQQTbTC +xJBGSSnyONXteu70aEupvI+2z06y34JdOjypeg6QuabfggglBXrNL6vZJ6V40kyP +yYOdXb44oSajbb2RzP9zl7wnOi1RgBpU2xxJLWCA5KXyuQvQQUeR4ApZNQLOcR5N +Q1oELr8NQbS6AkNdx24ErPe1I/NswQgWJjzr+A+MXqeLb4ttYC1225vYah3Co4yG +zgdLSyUCgYEA8i/mmQw0Gff/yNFnStcX1El8XdZA5CETqvfn9axjERLOPtRkmGSX +gL62flc6JjHVK1o84sSkMWKQ+sS8uZm12xDYK46tORuRyiq7/2vwW/rRSgUr6l6w +cp8JyOr/Kw1v20I6gYJv0V3pidkdaAj8KTYrrj/50s3e3EVHlZzv3AsCgYEAw/Kr +HCzFeaXhf3RshaVUK3jRX9029qijYojobMAsz7f12bVyCcrJOotAQTRuADKq6Iiv +B3CwGzppnANiztPbEc1m6i5Qt8HBOY/oOr7dPCjqnOvVrJmtl7o+b+OjVwLJ2cut +D19wAnq+ZSbnuO8tv221qySizTxmq0Hjj1CD3MsCgYEAx1PbU+2yWAlAMVT/0Z30 +U9ljjYvQfrImkNlWxWFra9rxes6uOKrZ8rGEW7mI93SeQP8VC4LSrH62Wl5gAdXb +0gAsGrVkJpNlS2IetttsyRdG98gH2+6xBLWWhRG8j/+QJ4J7yqnDRCsqEjVquXxj +pbua+eBXykN8gAhZIW84Dv8CgYEAh/S4BLSLz/vomiaNpa0w2XjazUkP+dTIiHW1 +zr8IWJerXYMnvOOW+6fizVszd6+wJeewidkGV94PqNAwnpEgTxnVbmb0Wot1JhtT +Zm9H0ZYSMw7vEvMOH1h6OegYTScCJrvLN/9gGFhSAp0b6pUJiOj2Q+kHjJ98HRd6 +bNXQqEECgYAtIo4t0RzfIYhNgQZ1sXxegvswfmUIghDEPmWMRxrkd7YzZrxgU/No +M3AqdFGZlfUh4os48w9HCX93zobNar0Z3RYKNuKNtVKvIppOx6PVPl3FSNpwMIbh +PXX7PftJOpqAWFDo0DqwL3rBg2eV177dwjetCSSXLTtMoRK1d02DUw== +-----END RSA PRIVATE KEY----- diff --git a/test/fixtures/ssh/ssh.pub b/test/fixtures/ssh/ssh.pub new file mode 100644 index 0000000..42d67cb --- /dev/null +++ b/test/fixtures/ssh/ssh.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC5YA/PN5m6gOtjP98h75EcW3AYyRlcEYoJa2gRhv3iFE+An7LK42VhEJ7OeKV1vvEAKZWTbr67RAkdl6gl3sNmL6oNwYelUgLJ1OrvCbKdA9+aj6J3+ydBFtn/etyBYR+DTlsgZstO7jTZiXJBKy+ungZ0f6DbKUOqnU4OeiQhqz7wnm+jjiXeBlSeH/gy0eoadSkO2jiwP4X+0izSpTAxsfhkLU85rWMJWiCD7ZS8TauXNYhXQBt9yi9I2n7VYxsJU+yVuSum7SYMRbJuvWWpj8vMb07pS/XXg2RHcAMu3vBQtl5uisJ94vuL8xcIu+PQNAYawOYa1ijzRSec7fC5 test@clementMachine diff --git a/test/fixtures/ssh/ssh_host_rsa_key b/test/fixtures/ssh/ssh_host_rsa_key new file mode 100644 index 0000000..df41d73 --- /dev/null +++ b/test/fixtures/ssh/ssh_host_rsa_key @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEogIBAAKCAQEAqDQ8m/TfF7ritC3DLzqhxXwhpvqGugmxMjuWpmgX1BHTp8OL +AXo5Wv0CMWx2gN7no0h7hAVDPcLFXUWmEU9SphsNLHB/EcE1LgZKhwno523dSXFI +W5abpW2gSbmq/zYQjBw2lxss4jM4hnkJaY+iP6D7CNo/hAvSn/LU7KwAvoIiymId +1/VzT8J8F+UvkWRFOBlO2wVHUvcKxw69M8nM/UmFmQL3AFc84Aw2XkXyBDqVQ38F +6pQE+ScOBTPEYqjaLRMqDUtm+DzhFNgE9iojY7cVZlNMFcrfWiXpwZWnYdDXP4nq +x1aFzEr87SUvLXS1kT7Sa95VlnXRiHWUFwl9PwIDAQABAoIBACOteNGnvg5dgKqx +Lj59VET8UHMHnr4lQiWiiBDh8XP1GAz0qC53C2VmVym+lOA+56k1vaVCLIIUJyLi +nBf92E0NXdz4O6u7bJlHklVUX8d/AToLwkpTBs6vExmwPToG8LmXXmjsxbdxH+M+ +FyYB5mtSrQyyplhO3a05p7pPvZSq08Aj1ch4D9GtnKWkY2VoeR8GeJsXwcBA7FHx +7MotipeBcWB3F6r2UmtuG0a45rSM8fVf7Rh8KAFBNxFuRfqbQohJleA3fDi+rFfL +fmWg0OQL8H/JV8UPqBQXuIP6RxNl/V2VuNyZ4OtP0nlmXYwSmGNvmBg45M0/6Lh7 +FGn1NOkCgYEA1Tmz9Yd8qHGhZU+NexoXCaQNtqhopBa8aKpUiLZXdv5G9EGnbjQW +5JTJTSYR3ZRPiVDJ7u0qJHSYWmXvTyNomeq6V4bJ8HV8NgLSpQ+dATWp4iez8j53 +vzzIMF60aAlAze62zrOu/U3hxMVYePZQ08EDxq5S9HJ/0VjF74kf5CUCgYEAyfJw +k3PjeH97VuuuoKLEBjMhNAXkKwQMScX72CaN2oQxh9Jne95tlfk6IcvlYXG/bag8 +Rcb4rwIYR/zLsnbAkhuibYk5gPd+Qjz9B1YJmOj1RI+Gz4eq/D69T2O/zZi9saB4 +05Xu/vEOFTKrAvcIGjPTCpCu9WeSOUS9PFZ8zJMCgYB8yQJKhPotUiLUOZRhIMmu +uLXV2gUzUqL81gIW6C+T0ggQVoP3+MAVEgYlAkyeUnehswgare1l3F51OQauV9iV +i0t2gcmY0gjZSxmfw91soc1EL1wePvSxlI7L0ADgidnFKxkNKxcT4wpnr96xG+X9 +pw3grpCZwpCPIdbk8mkZiQKBgAmONC9181R3S8HAIyIssXPNpYuNn889QMuY16LR +qDKnwKQ5m3Xr/2lyQoLo3/Ctbi5lIHz4vjmhsr5tQsIuBkyjjBr8vEDS9SFzO1HG +i2u10i0tUWaud7O50EFaae/zgz5NZoo0dPuRMmZkYF0ijTqYKys7z1uDTBLzorSh +LwSLAoGADYmpZzOAnUUFJmE9ldtIU1xmBSi/smKNjb4OdTh6wCUTYkCowLnzFCbH +yRyfe2m7r7GPDXWWk2cJXINW9gSlT6G2OLfcUxKuMDiIVfAGC1TIMDn+Xtd3ElHb +zTNgmqoE3ki2bYwByNgogipRO/Eyrkimpv439p9Y0psgvRVrc78= +-----END RSA PRIVATE KEY----- diff --git a/test/fixtures/ssh/sshd_config b/test/fixtures/ssh/sshd_config new file mode 100644 index 0000000..cb8bff9 --- /dev/null +++ b/test/fixtures/ssh/sshd_config @@ -0,0 +1,116 @@ +# $OpenBSD: sshd_config,v 1.102 2018/02/16 02:32:40 djm Exp $ + +# This is the sshd server system-wide configuration file. See +# sshd_config(5) for more information. + +# This sshd was compiled with PATH=/usr/local/sbin:/usr/local/bin:/usr/bin + +# The strategy used for options in the default sshd_config shipped with +# OpenSSH is to specify options with their default value where +# possible, but leave them commented. Uncommented options override the +# default value. + +#Port 22222 +#AddressFamily any +#ListenAddress 0.0.0.0 +#ListenAddress :: + +#HostKey /tmp/sshtest/ssh_host_rsa_key +#HostKey /tmp/sshtest/ssh_host_ecdsa_key +#HostKey /tmp/sshtest/ssh_host_ed25519_key + +# Ciphers and keying +#RekeyLimit default none + +# Logging +#SyslogFacility AUTH +#LogLevel INFO + +# Authentication: + +#LoginGraceTime 2m +#PermitRootLogin prohibit-password +StrictModes no +#MaxAuthTries 6 +#MaxSessions 10 + +PubkeyAuthentication yes + +# The default is to check both .ssh/authorized_keys and .ssh/authorized_keys2 +# but this is overridden so installations will only check .ssh/authorized_keys +#AuthorizedKeysFile /tmp/sshtest/authorized_keys + +#AuthorizedPrincipalsFile none + +#AuthorizedKeysCommand none +#AuthorizedKeysCommandUser nobody + +# For this to work you will also need host keys in /etc/ssh/ssh_known_hosts +HostbasedAuthentication no +# Change to yes if you don't trust ~/.ssh/known_hosts for +# HostbasedAuthentication +IgnoreUserKnownHosts yes +# Don't read the user's ~/.rhosts and ~/.shosts files +IgnoreRhosts yes + +# To disable tunneled clear text passwords, change to no here! +PasswordAuthentication yes +PermitEmptyPasswords yes + +# Change to no to disable s/key passwords +ChallengeResponseAuthentication no +# Kerberos options +#KerberosAuthentication no +#KerberosOrLocalPasswd yes +#KerberosTicketCleanup yes +#KerberosGetAFSToken no + +# GSSAPI options +#GSSAPIAuthentication no +#GSSAPICleanupCredentials yes + +# Set this to 'yes' to enable PAM authentication, account processing, +# and session processing. If this is enabled, PAM authentication will +# be allowed through the ChallengeResponseAuthentication and +# PasswordAuthentication. Depending on your PAM configuration, +# PAM authentication via ChallengeResponseAuthentication may bypass +# the setting of "PermitRootLogin without-password". +# If you just want the PAM account and session checks to run without +# PAM authentication, then enable this but set PasswordAuthentication +# and ChallengeResponseAuthentication to 'no'. +UsePAM yes + +#AllowAgentForwarding yes +#AllowTcpForwarding yes +#GatewayPorts no +#X11Forwarding no +#X11DisplayOffset 10 +#X11UseLocalhost yes +#PermitTTY yes +PrintMotd no # pam does that +#PrintLastLog yes +#TCPKeepAlive yes +#UseLogin no +PermitUserEnvironment yes +#Compression delayed +#ClientAliveInterval 0 +#ClientAliveCountMax 3 +#UseDNS no +#PidFile /run/sshd.pid +#MaxStartups 10:30:100 +#PermitTunnel no +#ChrootDirectory none +#VersionAddendum none + +# no default banner path +#Banner none + +# override default of no subsystems +Subsystem sftp /usr/lib/ssh/sftp-server + +# Example of overriding settings on a per-user basis +#Match User anoncvs +# X11Forwarding no +# AllowTcpForwarding no +# PermitTTY no +# ForceCommand cvs server diff --git a/test/main_test.go b/test/main_test.go index 68206a9..4889991 100644 --- a/test/main_test.go +++ b/test/main_test.go @@ -180,7 +180,9 @@ func NewIoSocketClient(url, clientID string) (*IOSockClient, error) { }) sCli.Conn.On("disconnection", func(err error) { - log.Printf("WS disconnection event with err: %v\n", err) + if err != nil { + log.Printf("WS disconnection event with err: %v\n", err) + } sCli.Connected = false }) diff --git a/test/target_test.go b/test/target_test.go index e89011b..d7576c6 100644 --- a/test/target_test.go +++ b/test/target_test.go @@ -17,23 +17,128 @@ package xdsservertest import ( + "bytes" + "fmt" + "log" + "os" + "os/exec" + "path" + "regexp" "strconv" "strings" "testing" "time" + common "gerrit.automotivelinux.org/gerrit/src/xds/xds-common.git/golib" "gerrit.automotivelinux.org/gerrit/src/xds/xds-server/lib/xsapiv1" "github.com/stretchr/testify/assert" ) -/*flush channel with timeout*/ -func flushChannelTerm(channel chan xsapiv1.TerminalOutMsg, ms time.Duration) { - timeoutB := false - for !timeoutB { +func launchSSHd(sshDir string, proc **os.Process, sshdCmd string) (*os.File, string) { + port := "22222" + argsProcessSSHd := []string{ + sshdCmd, + "-f", + sshDir + "/sshd_config", + "-D", + "-h", + sshDir + "/ssh_host_rsa_key", + "-o", + "AuthorizedKeysFile=" + sshDir + "/authorized_keys", + "-p", + port, + } + logFile := logDir + logFileSSHd + file, err := os.OpenFile(logFile, os.O_CREATE|os.O_WRONLY, 0644) + if err != nil { + log.Fatal(err) + } + defer file.Close() + tmpProc, err := os.StartProcess(argsProcessSSHd[0], argsProcessSSHd, &os.ProcAttr{ + Files: []*os.File{os.Stdin, file, file}, + }) + if err != nil { + log.Fatal(err) + } + *proc = tmpProc + return file, port +} + +func InitSSH(t *testing.T, procSSHd **os.Process) (string, string) { + Debug(t, "Initialise ssh with local user") + sshDir := path.Join(os.Getenv(envRootCfgDir), "ssh") + cmd := exec.Command("cp", "-r", sshFixturesDir, sshDir) + var out bytes.Buffer + cmd.Stdout = &out + assert.Nil(t, cmd.Run()) + + cmd = exec.Command("ls", sshDir) + cmd.Stdout = &out + assert.Nil(t, cmd.Run()) + + files := strings.Split(fmt.Sprint(cmd.Stdout), "\n") + + for _, f := range files { + if f != "" { + file := sshDir + "/" + f + cmd = exec.Command("chmod", "600", file) + cmd.Stdout = &out + assert.Nil(t, cmd.Run()) + } + } + + var outSSHd bytes.Buffer + var sshdCmd string + cmd = exec.Command("which", "sshd") + cmd.Stdout = &outSSHd + if cmd.Run() != nil { + if common.Exists("/usr/sbin/sshd") { + sshdCmd = "/usr/sbin/sshd" + } else if common.Exists("/usr/bin/sshd") { + sshdCmd = "/usr/sbin/sshd" + } else { + assert.FailNow(t, "Cannot find sshd command, please install it or set in your PATH") + } + } else { + sshdCmd = strings.TrimSpace(fmt.Sprint(cmd.Stdout)) + } + + var port string + _, port = launchSSHd(sshDir, procSSHd, sshdCmd) + go func(p *os.Process) { + Debug(t, "sshd is launching") + if status, err := p.Wait(); err != nil { + log.Fatalf("status=%v\n err=%v\n", status, err) + } + }(*procSSHd) + return sshDir, port +} + +/*wait for terminal prompt*/ +func waitForPrompt(t *testing.T, channel chan xsapiv1.TerminalOutMsg, prompt string) string { + step := 1 * time.Millisecond + timeout := 10 * time.Second + current := 0 * time.Second + out := "" + re := regexp.MustCompile("^" + prompt) + + for { select { - case <-channel: - case <-time.After(ms * time.Millisecond): - timeoutB = true + case outMsg := <-channel: + out += string(outMsg.Stdout) + if string(outMsg.Stderr) != "" { + out += string(outMsg.Stderr) + } + for _, line := range strings.Split(out, "\n") { + if re.MatchString(line) { + return line + } + } + case <-time.After(step): + current = current + step + if current >= timeout { + assert.FailNow(t, "Never received prompt message from terminal (output:"+out+")") + } } } } @@ -122,13 +227,21 @@ func AddTargets(t *testing.T, nbTargets int, chTarget chan xsapiv1.TargetConfig) return listID } -func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1.TerminalConfig) { +func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1.TerminalConfig, sshDir string, port string) { for j := 0; j < len(listID); j++ { listTermsID := make([]string, nbTerms) for i := 0; i < nbTerms; i++ { term := xsapiv1.TerminalConfig{ Name: "terminal" + strconv.Itoa(i), Type: xsapiv1.TypeTermSSH, + Options: []string{ + "-p", + port, + "-i", + sshDir + "/ssh", + "-o", + "StrictHostKeyChecking=no", + }, } /*add terminal on target*/ assert.Nil(t, HTTPCli.Post("/targets/"+listID[j]+"/terminals", term, &term)) @@ -146,7 +259,8 @@ func AddTerms(t *testing.T, nbTerms int, listID []string, chTermEvt chan xsapiv1 } } -func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig) { +func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig, chTerm chan xsapiv1.TerminalOutMsg, + prompt string) { var status string switch post { case "open": @@ -166,6 +280,11 @@ func PostTerms(t *testing.T, post string, chTermEvt chan xsapiv1.TerminalConfig) assert.Nil(t, HTTPCli.Post("/targets/"+targets[i].ID+"/terminals/"+terms[j].ID+"/"+post, terms[j], &term)) Debugf(t, "%v terminal %v", post, term.Name) termEvt := <-chTermEvt //waiting for event terminalStateChange + if post == "open" { + data := []byte("PS1=" + prompt + " bash -norc\n") + assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) + waitForPrompt(t, chTerm, prompt) + } assert.Equal(t, term.ID, termEvt.ID) assert.Equal(t, term.Status, status) assert.Equal(t, termEvt.Status, status) @@ -203,8 +322,13 @@ func RemoveTermsTargets(t *testing.T, chTarget chan xsapiv1.TargetConfig, chTerm } } func TestTarget(t *testing.T) { - nbTargets := 3 - nbTermsByTarget := 3 + prompt := "--PROMPT--" + var procSSHd *os.Process + sshDir, port := InitSSH(t, &procSSHd) + defer procSSHd.Kill() + + nbTargets := 2 + nbTermsByTarget := 2 /*channel for target events*/ chTarget := make(chan xsapiv1.TargetConfig) defer close(chTarget) @@ -221,78 +345,51 @@ func TestTarget(t *testing.T) { assert.Equal(t, len(targetArray), 0) listID := AddTargets(t, nbTargets, chTarget) - AddTerms(t, nbTermsByTarget, listID, chTermEvt) - - ///*channel for TerminalOutMsg*/ - //chTerm := make(chan xsapiv1.TerminalOutMsg) - //defer close(chTerm) - - ///*connect on terminalOutMsg event*/ - //sCli.Conn.On(xsapiv1.TerminalOutEvent, func(ev xsapiv1.TerminalOutMsg) { - // chTerm <- ev - //}) - - ///*just for the first term*/ - //var terms []xsapiv1.TerminalConfig - //var term xsapiv1.TerminalConfig - //assert.Nil(t, HTTPCli.Get("/targets/"+listID[0]+"/terminals", &terms)) - //assert.Nil(t, HTTPCli.Post("/targets/"+listID[0]+"/terminals/"+terms[0].ID+"/open", terms[0], &term)) - //<-chTermEvt //waiting for event terminalStateChange - //termOut := <-chTerm //waiting for terminalOutMsg - //flushChannelTerm(chTerm, 50) //flushing all terminalOutMsg - //stdoutMsg := string(termOut.Stdout) - //if strings.Contains(stdoutMsg, "Connection refused") { - // t.Fatalf("%vYou may have to launch ssh server", stdoutMsg) - //} else if strings.Contains(stdoutMsg, "password") { - // t.Fatalf("%vcopy your pub key in authorized_keys\ncat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys", stdoutMsg) - //} - //assert.True(t, strings.Contains(stdoutMsg, "Last login")) //first terminal msg should be Last Login - //assert.Nil(t, HTTPCli.Post("/targets/"+listID[0]+"/terminals/"+terms[0].ID+"/close", terms[0], &term)) - //<-chTermEvt //waiting for event terminalStateChange - - ///*open terminals*/ - //PostTerms(t, "open", chTermEvt) - //termOut = <-chTerm //waiting for terminalOutMsg - //flushChannelTerm(chTerm, 50) //flushing all terminalOutMsg - //stdoutMsg = string(termOut.Stdout) - //if strings.Contains(stdoutMsg, "Connection refused") { - // t.Fatalf("%vYou may have to launch ssh server", stdoutMsg) - //} else if strings.Contains(stdoutMsg, "password") { - // t.Fatalf("%vcopy your pub key in authorized_keys\ncat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys", stdoutMsg) - //} - //assert.True(t, strings.Contains(stdoutMsg, "Last login")) //first terminal msg should be Last Login - - ///*create toto file through terminals*/ - //rootCfgDir := os.Getenv(envRootCfgDir) - //totoFile := path.Join(rootCfgDir, "toto") - - ///*test with 2 terminals*/ - //for i := 0; i < 2; i++ { - // totoFileCurrent := totoFile + strconv.Itoa(i) - // /*send cmd though term*/ - // data := []byte("echo helloWorld" + strconv.Itoa(i) + " >> " + totoFileCurrent + "\n") - // Debugf(t, "send following command through terminal: %v", string(data)) - // assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) - // flushChannelTerm(chTerm, 50) //waiting for terminal msg - - // /*check that toto file is created*/ - // _, err := os.Stat(totoFileCurrent) - // assert.Nil(t, err) - - // /*send cmd though term*/ - // data = []byte("cat " + totoFileCurrent + "\n") - // Debugf(t, "send following command through terminal: %v", string(data)) - // assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) - - // <-chTerm //cmd sent - // termOut = <-chTerm //result of cat cmd - // flushChannelTerm(chTerm, 50) //flushing what remains - // /*check that terminal msg is what was written before*/ - // assert.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n") - // Debugf(t, "check terminal output msg: %v", string(termOut.Stdout)) - //} - - //PostTerms(t, "close", chTermEvt) + AddTerms(t, nbTermsByTarget, listID, chTermEvt, sshDir, port) + + /*channel for TerminalOutMsg*/ + chTerm := make(chan xsapiv1.TerminalOutMsg) + defer close(chTerm) + + /*connect on terminalOutMsg event*/ + sCli.Conn.On(xsapiv1.TerminalOutEvent, func(ev xsapiv1.TerminalOutMsg) { + chTerm <- ev + }) + + /*open terminals*/ + PostTerms(t, "open", chTermEvt, chTerm, prompt) + + /*create toto file through terminals*/ + rootCfgDir := os.Getenv(envRootCfgDir) + totoFile := path.Join(rootCfgDir, "toto") + + /*test with 2 terminals*/ + for i := 0; i < 2; i++ { + totoFileCurrent := totoFile + strconv.Itoa(i) + /*send cmd though term*/ + data := []byte("echo helloWorld" + strconv.Itoa(i) + " >> " + totoFileCurrent + "\n") + Debugf(t, "send following command through terminal: %v", string(data)) + assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) + waitForPrompt(t, chTerm, prompt) //waiting for terminal prompt + + /*check that toto file is created*/ + _, err := os.Stat(totoFileCurrent) + assert.Nil(t, err) + + /*send cmd though term*/ + data = []byte("cat " + totoFileCurrent + "\n") + Debugf(t, "send following command through terminal: %v", string(data)) + assert.Nil(t, sCli.Conn.Emit(xsapiv1.TerminalInEvent, data)) + + <-chTerm //cmd sent + termOut := <-chTerm //result of cat cmd + waitForPrompt(t, chTerm, prompt) //wait for terminal prompt + /*check that terminal msg is what was written before*/ + assert.Equal(t, string(termOut.Stdout), "helloWorld"+strconv.Itoa(i)+"\r\n") + Debugf(t, "check terminal output msg: %v", string(termOut.Stdout)) + } + + PostTerms(t, "close", chTermEvt, nil, prompt) /*remove targets and terms*/ RemoveTermsTargets(t, chTarget, chTermEvt) |