diff options
| author | Clément Lassieur <clement@lassieur.org> | 2017-03-19 13:18:37 +0100 |
|---|---|---|
| committer | Clément Lassieur <clement@lassieur.org> | 2017-03-21 20:49:26 +0100 |
| commit | cfaf4d11659a6b78ef35676b4e37d6da179e5b51 (patch) | |
| tree | 9ed1b354001e1bb9c097f4a297ff0b09be95e23c /gnu/tests/ssh.scm | |
| parent | 12723370e5a780b18eae4c44ab9634adaff927ea (diff) | |
tests: ssh: Abstract session connection and authentication.
* gnu/tests/ssh.scm (run-ssh-test): Introduce make-session-for-test,
call-with-connected-session and call-with-connected-session/auth.
(run-ssh-test)["connect"]: Rename to "shell command". Abstract its session
connection and authentication work into the above three functions.
Diffstat (limited to 'gnu/tests/ssh.scm')
| -rw-r--r-- | gnu/tests/ssh.scm | 82 |
1 files changed, 53 insertions, 29 deletions
diff --git a/gnu/tests/ssh.scm b/gnu/tests/ssh.scm index 456476e69d4..7779b71561a 100644 --- a/gnu/tests/ssh.scm +++ b/gnu/tests/ssh.scm | |||
| @@ -1,5 +1,6 @@ | |||
| 1 | ;;; GNU Guix --- Functional package management for GNU | 1 | ;;; GNU Guix --- Functional package management for GNU |
| 2 | ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org> | 2 | ;;; Copyright © 2016 Ludovic Courtès <ludo@gnu.org> |
| 3 | ;;; Copyright © 2017 Clément Lassieur <clement@lassieur.org> | ||
| 3 | ;;; | 4 | ;;; |
| 4 | ;;; This file is part of GNU Guix. | 5 | ;;; This file is part of GNU Guix. |
| 5 | ;;; | 6 | ;;; |
| @@ -101,6 +102,47 @@ empty-password logins." | |||
| 101 | (error "file didn't show up" ,file)))) | 102 | (error "file didn't show up" ,file)))) |
| 102 | marionette)) | 103 | marionette)) |
| 103 | 104 | ||
| 105 | (define (make-session-for-test) | ||
| 106 | "Make a session with predefined parameters for a test." | ||
| 107 | (make-session #:user "root" | ||
| 108 | #:port 2222 | ||
| 109 | #:host "localhost" | ||
| 110 | #:log-verbosity 'protocol)) | ||
| 111 | |||
| 112 | (define (call-with-connected-session proc) | ||
| 113 | "Call the one-argument procedure PROC with a freshly created and | ||
| 114 | connected SSH session object, return the result of the procedure call. The | ||
| 115 | session is disconnected when the PROC is finished." | ||
| 116 | (let ((session (make-session-for-test))) | ||
| 117 | (dynamic-wind | ||
| 118 | (lambda () | ||
| 119 | (let ((result (connect! session))) | ||
| 120 | (unless (equal? result 'ok) | ||
| 121 | (error "Could not connect to a server" | ||
| 122 | session result)))) | ||
| 123 | (lambda () (proc session)) | ||
| 124 | (lambda () (disconnect! session))))) | ||
| 125 | |||
| 126 | (define (call-with-connected-session/auth proc) | ||
| 127 | "Make an authenticated session. We should be able to connect as | ||
| 128 | root with an empty password." | ||
| 129 | (call-with-connected-session | ||
| 130 | (lambda (session) | ||
| 131 | ;; Try the simple authentication methods. Dropbear requires | ||
| 132 | ;; 'none' when there are no passwords, whereas OpenSSH accepts | ||
| 133 | ;; 'password' with an empty password. | ||
| 134 | (let loop ((methods (list (cut userauth-password! <> "") | ||
| 135 | (cut userauth-none! <>)))) | ||
| 136 | (match methods | ||
| 137 | (() | ||
| 138 | (error "all the authentication methods failed")) | ||
| 139 | ((auth rest ...) | ||
| 140 | (match (pk 'auth (auth session)) | ||
| 141 | ('success | ||
| 142 | (proc session)) | ||
| 143 | ('denied | ||
| 144 | (loop rest))))))))) | ||
| 145 | |||
| 104 | (mkdir #$output) | 146 | (mkdir #$output) |
| 105 | (chdir #$output) | 147 | (chdir #$output) |
| 106 | 148 | ||
| @@ -131,37 +173,19 @@ empty-password logins." | |||
| 131 | (current-services)))) | 173 | (current-services)))) |
| 132 | marionette)) | 174 | marionette)) |
| 133 | 175 | ||
| 134 | ;; Connect to the guest over SSH. We should be able to connect as | 176 | ;; Connect to the guest over SSH. Make sure we can run a shell |
| 135 | ;; "root" with an empty password. Make sure we can run a shell | ||
| 136 | ;; command there. | 177 | ;; command there. |
| 137 | (test-equal "connect" | 178 | (test-equal "shell command" |
| 138 | 'hello | 179 | 'hello |
| 139 | (let* ((session (make-session #:user "root" | 180 | (call-with-connected-session/auth |
| 140 | #:port 2222 #:host "localhost" | 181 | (lambda (session) |
| 141 | #:log-verbosity 'protocol))) | 182 | ;; FIXME: 'get-server-public-key' segfaults. |
| 142 | (match (connect! session) | 183 | ;; (get-server-public-key session) |
| 143 | ('ok | 184 | (let ((channel (make-channel session))) |
| 144 | ;; Try the simple authentication methods. Dropbear | 185 | (channel-open-session channel) |
| 145 | ;; requires 'none' when there are no passwords, whereas | 186 | (channel-request-exec channel "echo hello > /root/witness") |
| 146 | ;; OpenSSH accepts 'password' with an empty password. | 187 | (and (zero? (channel-get-exit-status channel)) |
| 147 | (let loop ((methods (list (cut userauth-password! <> "") | 188 | (wait-for-file "/root/witness")))))) |
| 148 | (cut userauth-none! <>)))) | ||
| 149 | (match methods | ||
| 150 | (() | ||
| 151 | (error "all the authentication methods failed")) | ||
| 152 | ((auth rest ...) | ||
| 153 | (match (pk 'auth (auth session)) | ||
| 154 | ('success | ||
| 155 | ;; FIXME: 'get-server-public-key' segfaults. | ||
| 156 | ;; (get-server-public-key session) | ||
| 157 | (let ((channel (make-channel session))) | ||
| 158 | (channel-open-session channel) | ||
| 159 | (channel-request-exec channel | ||
| 160 | "echo hello > /root/witness") | ||
| 161 | (and (zero? (channel-get-exit-status channel)) | ||
| 162 | (wait-for-file "/root/witness")))) | ||
| 163 | ('denied | ||
| 164 | (loop rest)))))))))) | ||
| 165 | 189 | ||
| 166 | (test-end) | 190 | (test-end) |
| 167 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) | 191 | (exit (= (test-runner-fail-count (test-runner-current)) 0))))) |
