@@ -53,6 +53,7 @@ func addLineSSHProxyConf(line string) {
5353
5454func removeLineSSHProxyConf (line string ) {
5555 ctx := context .Background ()
56+ line = strings .ReplaceAll (line , "/" , "\\ /" )
5657 for _ , gateway := range gateways {
5758 _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , fmt .Sprintf ("sed -i 's/^%s$//' %s" , line , SSHPROXYCONFIG )}, nil , nil )
5859 if err != nil {
@@ -63,6 +64,7 @@ func removeLineSSHProxyConf(line string) {
6364
6465func updateLineSSHProxyConf (key string , value string ) {
6566 ctx := context .Background ()
67+ value = strings .ReplaceAll (value , "/" , "\\ /" )
6668 for _ , gateway := range gateways {
6769 _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , fmt .Sprintf ("sed -i '/%s:/s/: .*$/: %s/' %s" , key , value , SSHPROXYCONFIG )}, nil , nil )
6870 if err != nil {
@@ -243,6 +245,29 @@ func TestSimpleConnect(t *testing.T) {
243245 }
244246}
245247
248+ func TestBlockingCommand (t * testing.T ) {
249+ addLineSSHProxyConf ("blocking_command: /bin/true" )
250+ ctx , cancel := context .WithTimeout (context .Background (), 5 * time .Second )
251+ defer cancel ()
252+ args , cmd := prepareCommand ("gateway1" , 2023 , "hostname" )
253+ _ , stdout , stderr , err := runCommand (ctx , "ssh" , args , nil , nil )
254+ stdoutStr := strings .TrimSpace (string (stdout ))
255+ if err != nil {
256+ t .Errorf ("%s unexpected error: %v | stderr = %s" , cmd , err , string (stderr ))
257+ } else if stdoutStr != "server1" {
258+ t .Errorf ("%s hostname = %s, want server1" , cmd , stdoutStr )
259+ }
260+ updateLineSSHProxyConf ("blocking_command" , "/bin/false" )
261+ defer removeLineSSHProxyConf ("blocking_command: /bin/false" )
262+ args , _ = prepareCommand ("gateway1" , 2023 , "hostname" )
263+ rc , _ , _ , err := runCommand (ctx , "ssh" , args , nil , nil )
264+ if err == nil {
265+ t .Errorf ("got no error, expected error due to blocking_command" )
266+ } else if rc != 1 {
267+ t .Errorf ("blocking_command rc = %d, want 1" , rc )
268+ }
269+ }
270+
246271func TestNodesets (t * testing.T ) {
247272 disableHost ("server[1000-1002]" )
248273 checkHostState (t , "server1000:22" , "disabled" , true )
@@ -382,12 +407,12 @@ func TestEtcdConnections(t *testing.T) {
382407}
383408
384409func TestWithoutEtcd (t * testing.T ) {
385- updateLineSSHProxyConf ("endpoints" , "[\" https:\\ / \\ /void:2379\" ]" )
410+ updateLineSSHProxyConf ("endpoints" , "[\" https:/ /void:2379\" ]" )
386411 ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
387412 defer cancel ()
388413 args , cmdStr := prepareCommand ("gateway1" , 2023 , "hostname" )
389414 _ , stdout , _ , err := runCommand (ctx , "ssh" , args , nil , nil )
390- updateLineSSHProxyConf ("endpoints" , "[\" https:\\ / \\ /etcd:2379\" ]" )
415+ updateLineSSHProxyConf ("endpoints" , "[\" https:/ /etcd:2379\" ]" )
391416 if err != nil {
392417 log .Fatal (err )
393418 }
@@ -396,10 +421,10 @@ func TestWithoutEtcd(t *testing.T) {
396421 t .Errorf ("%s got %s, expected server1" , cmdStr , dest )
397422 }
398423
399- updateLineSSHProxyConf ("endpoints" , "[\" https:\\ / \\ /void:2379\" ]" )
424+ updateLineSSHProxyConf ("endpoints" , "[\" https:/ /void:2379\" ]" )
400425 updateLineSSHProxyConf ("mandatory" , "true" )
401426 _ , _ , _ , err = runCommand (ctx , "ssh" , args , nil , nil )
402- updateLineSSHProxyConf ("endpoints" , "[\" https:\\ / \\ /etcd:2379\" ]" )
427+ updateLineSSHProxyConf ("endpoints" , "[\" https:/ /etcd:2379\" ]" )
403428 updateLineSSHProxyConf ("mandatory" , "false" )
404429 if err == nil {
405430 t .Error ("the connection should have been rejected" )
0 commit comments