github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/systests/ctl_test.go (about) 1 // Copyright 2015 Keybase, Inc. All rights reserved. Use of 2 // this source code is governed by the included BSD license. 3 4 package systests 5 6 import ( 7 "fmt" 8 "regexp" 9 "testing" 10 11 "github.com/keybase/client/go/client" 12 "github.com/keybase/client/go/libkb" 13 keybase1 "github.com/keybase/client/go/protocol/keybase1" 14 "github.com/keybase/client/go/service" 15 context "golang.org/x/net/context" 16 ) 17 18 func (v *versionUI) GetDumbOutputUI() libkb.DumbOutputUI { 19 return v 20 } 21 22 func (v *versionUI) Printf(format string, args ...interface{}) (n int, err error) { 23 return v.PrintfUnescaped(format, args...) 24 } 25 26 func (v *versionUI) PrintfUnescaped(format string, args ...interface{}) (n int, err error) { 27 v.outbuf = append(v.outbuf, fmt.Sprintf(format, args...)) 28 return 0, nil 29 } 30 31 func (v *versionUI) PrintfStderr(format string, args ...interface{}) (n int, err error) { 32 return 0, nil 33 } 34 35 type versionUI struct { 36 baseNullUI 37 outbuf []string 38 libkb.Contextified 39 } 40 41 func (v *versionUI) checkVersionOutput(t *testing.T) { 42 rx := regexp.MustCompile(`:\s*`) 43 n := len(v.outbuf) 44 if n < 2 { 45 t.Fatalf("expected >= 2 lines of output; got %d\n", n) 46 } 47 s := rx.Split(v.outbuf[n-1], -1) 48 c := rx.Split(v.outbuf[n-2], -1) 49 if s[0] != "Service" { 50 t.Fatalf("%s != Service", s[0]) 51 } 52 if c[0] != "Client" { 53 t.Fatalf("%s != Client", c[0]) 54 } 55 if c[1] != s[1] { 56 t.Fatalf("version mismatch: %s != %s", c[1], s[1]) 57 } 58 } 59 60 func TestVersionAndStop(t *testing.T) { 61 62 tc := setupTest(t, "stop") 63 64 defer tc.Cleanup() 65 66 stopCh := make(chan error) 67 svc := service.NewService(tc.G, false) 68 startCh := svc.GetStartChannel() 69 go func() { 70 err := svc.Run() 71 if err != nil { 72 t.Logf("hit an error in Run, which might be masked: %v", err) 73 } 74 stopCh <- err 75 }() 76 77 tc2 := cloneContext(tc) 78 defer tc2.Cleanup() 79 80 vui := versionUI{ 81 Contextified: libkb.NewContextified(tc2.G), 82 } 83 tc2.G.SetUI(&vui) 84 85 <-startCh 86 version := client.NewCmdVersionRunner(tc2.G) 87 88 if err := version.Run(); err != nil { 89 t.Fatal(err) 90 } 91 92 vui.checkVersionOutput(t) 93 94 if err := CtlStop(tc2.G); err != nil { 95 t.Fatal(err) 96 } 97 98 // If the server failed, it's also an error 99 if err := <-stopCh; err != nil { 100 t.Fatal(err) 101 } 102 } 103 104 func CtlStop(g *libkb.GlobalContext) error { 105 mctx := libkb.NewMetaContextTODO(g) 106 if err := g.Shutdown(mctx); err != nil { 107 return err 108 } 109 cli, err := client.GetCtlClient(g) 110 if err != nil { 111 return err 112 } 113 return cli.StopService(context.TODO(), keybase1.StopServiceArg{ExitCode: keybase1.ExitCode_OK}) 114 }