@@ -42,31 +42,23 @@ var (
4242)
4343
4444func addLineSSHProxyConf (line string ) {
45- ctx := context .Background ()
46- for _ , gateway := range gateways {
47- _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , fmt .Sprintf ("echo \" %s\" >> %s" , line , SSHPROXYCONFIG )}, nil , nil )
48- if err != nil {
49- log .Fatal (err )
50- }
51- }
45+ runRootCommand (fmt .Sprintf ("echo \" %s\" >> %s" , line , SSHPROXYCONFIG ))
5246}
5347
5448func removeLineSSHProxyConf (line string ) {
55- ctx := context .Background ()
5649 line = strings .ReplaceAll (line , "/" , "\\ /" )
57- for _ , gateway := range gateways {
58- _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , fmt .Sprintf ("sed -i 's/^%s$//' %s" , line , SSHPROXYCONFIG )}, nil , nil )
59- if err != nil {
60- log .Fatal (err )
61- }
62- }
50+ runRootCommand (fmt .Sprintf ("sed -i 's/^%s$//' %s" , line , SSHPROXYCONFIG ))
6351}
6452
6553func updateLineSSHProxyConf (key string , value string ) {
66- ctx := context .Background ()
6754 value = strings .ReplaceAll (value , "/" , "\\ /" )
55+ runRootCommand (fmt .Sprintf ("sed -i '/%s:/s/: .*$/: %s/' %s" , key , value , SSHPROXYCONFIG ))
56+ }
57+
58+ func runRootCommand (cmd string ) {
59+ ctx := context .Background ()
6860 for _ , gateway := range gateways {
69- _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , fmt . Sprintf ( "sed -i '/%s:/s/: .*$/: %s/' %s" , key , value , SSHPROXYCONFIG ) }, nil , nil )
61+ _ , _ , _ , err := runCommand (ctx , "ssh" , []string {fmt .Sprintf ("root@%s" , gateway ), "--" , cmd }, nil , nil )
7062 if err != nil {
7163 log .Fatal (err )
7264 }
@@ -662,6 +654,33 @@ func TestForgetPersist(t *testing.T) {
662654 }
663655}
664656
657+ func TestMissingUser (t * testing.T ) {
658+ ctx , cancel := context .WithTimeout (context .Background (), 30 * time .Second )
659+ defer cancel ()
660+ args , _ := prepareCommand ("tmpuser@gateway1" , 2022 , "sleep 20" )
661+ ch := make (chan * os.Process )
662+ go func () {
663+ runCommand (ctx , "ssh" , args , nil , ch )
664+ }()
665+ process1 := <- ch
666+
667+ time .Sleep (time .Second )
668+ users , _ := getEtcdAllUsers ()
669+ if len (users ) != 1 {
670+ t .Errorf ("Want 1 user, got %d" , len (users ))
671+ } else if users [0 ].Groups != "user1" {
672+ t .Errorf ("Want Groups=\" user1\" , got \" %s\" " , users [0 ].Groups )
673+ }
674+ runRootCommand ("userdel tmpuser" )
675+ users , _ = getEtcdAllUsers ()
676+ process1 .Kill ()
677+ if len (users ) != 1 {
678+ t .Errorf ("Want 1 user, got %d" , len (users ))
679+ } else if users [0 ].Groups != "" {
680+ t .Errorf ("Want Groups=\" \" , got \" %s\" " , users [0 ].Groups )
681+ }
682+ }
683+
665684func TestBalancedConnections (t * testing.T ) {
666685 // remove old connections stored in etcd
667686 time .Sleep (4 * time .Second )
0 commit comments