How to use Kill method of plugin Package

Best Gauge code snippet using plugin.Kill

client_test.go

Source:client_test.go Github

copy

Full Screen

...22 Cmd: process,23 HandshakeConfig: testHandshake,24 Plugins: testPluginMap,25 })26 defer c.Kill()27 // Test that it parses the proper address28 addr, err := c.Start()29 if err != nil {30 t.Fatalf("err should be nil, got %s", err)31 }32 if addr.Network() != "tcp" {33 t.Fatalf("bad: %#v", addr)34 }35 if addr.String() != ":1234" {36 t.Fatalf("bad: %#v", addr)37 }38 // Test that it exits properly if killed39 c.Kill()40 // Test that it knows it is exited41 if !c.Exited() {42 t.Fatal("should say client has exited")43 }44 // this test isn't expected to get a client45 if !c.killed() {46 t.Fatal("Client should have failed")47 }48}49// This tests a bug where Kill would start50func TestClient_killStart(t *testing.T) {51 // Create a temporary dir to store the result file52 td, err := ioutil.TempDir("", "plugin")53 if err != nil {54 t.Fatalf("err: %s", err)55 }56 defer os.RemoveAll(td)57 // Start the client58 path := filepath.Join(td, "booted")59 process := helperProcess("bad-version", path)60 c := NewClient(&ClientConfig{Cmd: process, HandshakeConfig: testHandshake})61 defer c.Kill()62 // Verify our path doesn't exist63 if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {64 t.Fatalf("bad: %s", err)65 }66 // Test that it parses the proper address67 if _, err := c.Start(); err == nil {68 t.Fatal("expected error")69 }70 // Verify we started71 if _, err := os.Stat(path); err != nil {72 t.Fatalf("bad: %s", err)73 }74 if err := os.Remove(path); err != nil {75 t.Fatalf("bad: %s", err)76 }77 // Test that Kill does nothing really78 c.Kill()79 // Test that it knows it is exited80 if !c.Exited() {81 t.Fatal("should say client has exited")82 }83 if !c.killed() {84 t.Fatal("process should have failed")85 }86 // Verify our path doesn't exist87 if _, err := os.Stat(path); err == nil || !os.IsNotExist(err) {88 t.Fatalf("bad: %s", err)89 }90}91func TestClient_testCleanup(t *testing.T) {92 // Create a temporary dir to store the result file93 td, err := ioutil.TempDir("", "plugin")94 if err != nil {95 t.Fatalf("err: %s", err)96 }97 defer os.RemoveAll(td)98 // Create a path that the helper process will write on cleanup99 path := filepath.Join(td, "output")100 // Test the cleanup101 process := helperProcess("cleanup", path)102 c := NewClient(&ClientConfig{103 Cmd: process,104 HandshakeConfig: testHandshake,105 Plugins: testPluginMap,106 })107 // Grab the client so the process starts108 if _, err := c.Client(); err != nil {109 c.Kill()110 t.Fatalf("err: %s", err)111 }112 // Kill it gracefully113 c.Kill()114 // Test for the file115 if _, err := os.Stat(path); err != nil {116 t.Fatalf("err: %s", err)117 }118}119func TestClient_testInterface(t *testing.T) {120 process := helperProcess("test-interface")121 c := NewClient(&ClientConfig{122 Cmd: process,123 HandshakeConfig: testHandshake,124 Plugins: testPluginMap,125 })126 defer c.Kill()127 // Grab the RPC client128 client, err := c.Client()129 if err != nil {130 t.Fatalf("err should be nil, got %s", err)131 }132 // Grab the impl133 raw, err := client.Dispense("test")134 if err != nil {135 t.Fatalf("err should be nil, got %s", err)136 }137 impl, ok := raw.(testInterface)138 if !ok {139 t.Fatalf("bad: %#v", raw)140 }141 result := impl.Double(21)142 if result != 42 {143 t.Fatalf("bad: %#v", result)144 }145 // Kill it146 c.Kill()147 // Test that it knows it is exited148 if !c.Exited() {149 t.Fatal("should say client has exited")150 }151 if c.killed() {152 t.Fatal("process failed to exit gracefully")153 }154}155func TestClient_grpc_servercrash(t *testing.T) {156 process := helperProcess("test-grpc")157 c := NewClient(&ClientConfig{158 Cmd: process,159 HandshakeConfig: testHandshake,160 Plugins: testGRPCPluginMap,161 AllowedProtocols: []Protocol{ProtocolGRPC},162 })163 defer c.Kill()164 if _, err := c.Start(); err != nil {165 t.Fatalf("err: %s", err)166 }167 if v := c.Protocol(); v != ProtocolGRPC {168 t.Fatalf("bad: %s", v)169 }170 // Grab the RPC client171 client, err := c.Client()172 if err != nil {173 t.Fatalf("err should be nil, got %s", err)174 }175 // Grab the impl176 raw, err := client.Dispense("test")177 if err != nil {178 t.Fatalf("err should be nil, got %s", err)179 }180 _, ok := raw.(testInterface)181 if !ok {182 t.Fatalf("bad: %#v", raw)183 }184 c.process.Kill()185 select {186 case <-c.doneCtx.Done():187 case <-time.After(time.Second * 2):188 t.Fatal("Context was not closed")189 }190}191func TestClient_grpc(t *testing.T) {192 process := helperProcess("test-grpc")193 c := NewClient(&ClientConfig{194 Cmd: process,195 HandshakeConfig: testHandshake,196 Plugins: testGRPCPluginMap,197 AllowedProtocols: []Protocol{ProtocolGRPC},198 })199 defer c.Kill()200 if _, err := c.Start(); err != nil {201 t.Fatalf("err: %s", err)202 }203 if v := c.Protocol(); v != ProtocolGRPC {204 t.Fatalf("bad: %s", v)205 }206 // Grab the RPC client207 client, err := c.Client()208 if err != nil {209 t.Fatalf("err should be nil, got %s", err)210 }211 // Grab the impl212 raw, err := client.Dispense("test")213 if err != nil {214 t.Fatalf("err should be nil, got %s", err)215 }216 impl, ok := raw.(testInterface)217 if !ok {218 t.Fatalf("bad: %#v", raw)219 }220 result := impl.Double(21)221 if result != 42 {222 t.Fatalf("bad: %#v", result)223 }224 // Kill it225 c.Kill()226 // Test that it knows it is exited227 if !c.Exited() {228 t.Fatal("should say client has exited")229 }230 if c.killed() {231 t.Fatal("process failed to exit gracefully")232 }233}234func TestClient_grpcNotAllowed(t *testing.T) {235 process := helperProcess("test-grpc")236 c := NewClient(&ClientConfig{237 Cmd: process,238 HandshakeConfig: testHandshake,239 Plugins: testPluginMap,240 })241 defer c.Kill()242 if _, err := c.Start(); err == nil {243 t.Fatal("should error")244 }245}246func TestClient_cmdAndReattach(t *testing.T) {247 config := &ClientConfig{248 Cmd: helperProcess("start-timeout"),249 Reattach: &ReattachConfig{},250 }251 c := NewClient(config)252 defer c.Kill()253 _, err := c.Start()254 if err == nil {255 t.Fatal("err should not be nil")256 }257}258func TestClient_reattach(t *testing.T) {259 process := helperProcess("test-interface")260 c := NewClient(&ClientConfig{261 Cmd: process,262 HandshakeConfig: testHandshake,263 Plugins: testPluginMap,264 })265 defer c.Kill()266 // Grab the RPC client267 _, err := c.Client()268 if err != nil {269 t.Fatalf("err should be nil, got %s", err)270 }271 // Get the reattach configuration272 reattach := c.ReattachConfig()273 // Create a new client274 c = NewClient(&ClientConfig{275 Reattach: reattach,276 HandshakeConfig: testHandshake,277 Plugins: testPluginMap,278 })279 // Grab the RPC client280 client, err := c.Client()281 if err != nil {282 t.Fatalf("err should be nil, got %s", err)283 }284 // Grab the impl285 raw, err := client.Dispense("test")286 if err != nil {287 t.Fatalf("err should be nil, got %s", err)288 }289 impl, ok := raw.(testInterface)290 if !ok {291 t.Fatalf("bad: %#v", raw)292 }293 result := impl.Double(21)294 if result != 42 {295 t.Fatalf("bad: %#v", result)296 }297 // Kill it298 c.Kill()299 // Test that it knows it is exited300 if !c.Exited() {301 t.Fatal("should say client has exited")302 }303 if c.killed() {304 t.Fatal("process failed to exit gracefully")305 }306}307func TestClient_reattachNoProtocol(t *testing.T) {308 process := helperProcess("test-interface")309 c := NewClient(&ClientConfig{310 Cmd: process,311 HandshakeConfig: testHandshake,312 Plugins: testPluginMap,313 })314 defer c.Kill()315 // Grab the RPC client316 _, err := c.Client()317 if err != nil {318 t.Fatalf("err should be nil, got %s", err)319 }320 // Get the reattach configuration321 reattach := c.ReattachConfig()322 reattach.Protocol = ""323 // Create a new client324 c = NewClient(&ClientConfig{325 Reattach: reattach,326 HandshakeConfig: testHandshake,327 Plugins: testPluginMap,328 })329 // Grab the RPC client330 client, err := c.Client()331 if err != nil {332 t.Fatalf("err should be nil, got %s", err)333 }334 // Grab the impl335 raw, err := client.Dispense("test")336 if err != nil {337 t.Fatalf("err should be nil, got %s", err)338 }339 impl, ok := raw.(testInterface)340 if !ok {341 t.Fatalf("bad: %#v", raw)342 }343 result := impl.Double(21)344 if result != 42 {345 t.Fatalf("bad: %#v", result)346 }347 // Kill it348 c.Kill()349 // Test that it knows it is exited350 if !c.Exited() {351 t.Fatal("should say client has exited")352 }353 if c.killed() {354 t.Fatal("process failed to exit gracefully")355 }356}357func TestClient_reattachGRPC(t *testing.T) {358 process := helperProcess("test-grpc")359 c := NewClient(&ClientConfig{360 Cmd: process,361 HandshakeConfig: testHandshake,362 Plugins: testGRPCPluginMap,363 AllowedProtocols: []Protocol{ProtocolGRPC},364 })365 defer c.Kill()366 // Grab the RPC client367 _, err := c.Client()368 if err != nil {369 t.Fatalf("err should be nil, got %s", err)370 }371 // Get the reattach configuration372 reattach := c.ReattachConfig()373 // Create a new client374 c = NewClient(&ClientConfig{375 Reattach: reattach,376 HandshakeConfig: testHandshake,377 Plugins: testGRPCPluginMap,378 AllowedProtocols: []Protocol{ProtocolGRPC},379 })380 // Grab the RPC client381 client, err := c.Client()382 if err != nil {383 t.Fatalf("err should be nil, got %s", err)384 }385 // Grab the impl386 raw, err := client.Dispense("test")387 if err != nil {388 t.Fatalf("err should be nil, got %s", err)389 }390 impl, ok := raw.(testInterface)391 if !ok {392 t.Fatalf("bad: %#v", raw)393 }394 result := impl.Double(21)395 if result != 42 {396 t.Fatalf("bad: %#v", result)397 }398 // Kill it399 c.Kill()400 // Test that it knows it is exited401 if !c.Exited() {402 t.Fatal("should say client has exited")403 }404 if c.killed() {405 t.Fatal("process failed to exit gracefully")406 }407}408func TestClient_reattachNotFound(t *testing.T) {409 // Find a bad pid410 var pid int = 5000411 for i := pid; i < 32000; i++ {412 if _, err := os.FindProcess(i); err != nil {413 pid = i414 break415 }416 }417 // Addr that won't work418 l, err := net.Listen("tcp", "127.0.0.1:0")419 if err != nil {420 t.Fatalf("err: %s", err)421 }422 addr := l.Addr()423 l.Close()424 // Reattach425 c := NewClient(&ClientConfig{426 Reattach: &ReattachConfig{427 Addr: addr,428 Pid: pid,429 },430 HandshakeConfig: testHandshake,431 Plugins: testPluginMap,432 })433 // Start shouldn't error434 if _, err := c.Start(); err == nil {435 t.Fatal("should error")436 } else if err != ErrProcessNotFound {437 t.Fatalf("err: %s", err)438 }439}440func TestClientStart_badVersion(t *testing.T) {441 config := &ClientConfig{442 Cmd: helperProcess("bad-version"),443 StartTimeout: 50 * time.Millisecond,444 HandshakeConfig: testHandshake,445 Plugins: testPluginMap,446 }447 c := NewClient(config)448 defer c.Kill()449 _, err := c.Start()450 if err == nil {451 t.Fatal("err should not be nil")452 }453}454func TestClientStart_badNegotiatedVersion(t *testing.T) {455 config := &ClientConfig{456 Cmd: helperProcess("test-versioned-plugins"),457 StartTimeout: 50 * time.Millisecond,458 // test-versioned-plugins only has version 2459 HandshakeConfig: testHandshake,460 Plugins: testPluginMap,461 }462 c := NewClient(config)463 defer c.Kill()464 _, err := c.Start()465 if err == nil {466 t.Fatal("err should not be nil")467 }468 fmt.Println(err)469}470func TestClient_Start_Timeout(t *testing.T) {471 config := &ClientConfig{472 Cmd: helperProcess("start-timeout"),473 StartTimeout: 50 * time.Millisecond,474 HandshakeConfig: testHandshake,475 Plugins: testPluginMap,476 }477 c := NewClient(config)478 defer c.Kill()479 _, err := c.Start()480 if err == nil {481 t.Fatal("err should not be nil")482 }483}484func TestClient_Stderr(t *testing.T) {485 stderr := new(bytes.Buffer)486 process := helperProcess("stderr")487 c := NewClient(&ClientConfig{488 Cmd: process,489 Stderr: stderr,490 HandshakeConfig: testHandshake,491 Plugins: testPluginMap,492 })493 defer c.Kill()494 if _, err := c.Start(); err != nil {495 t.Fatalf("err: %s", err)496 }497 for !c.Exited() {498 time.Sleep(10 * time.Millisecond)499 }500 if c.killed() {501 t.Fatal("process failed to exit gracefully")502 }503 if !strings.Contains(stderr.String(), "HELLO\n") {504 t.Fatalf("bad log data: '%s'", stderr.String())505 }506 if !strings.Contains(stderr.String(), "WORLD\n") {507 t.Fatalf("bad log data: '%s'", stderr.String())508 }509}510func TestClient_StderrJSON(t *testing.T) {511 stderr := new(bytes.Buffer)512 process := helperProcess("stderr-json")513 var logBuf bytes.Buffer514 mutex := new(sync.Mutex)515 // Custom hclog.Logger516 testLogger := hclog.New(&hclog.LoggerOptions{517 Name: "test-logger",518 Level: hclog.Trace,519 Output: &logBuf,520 Mutex: mutex,521 })522 c := NewClient(&ClientConfig{523 Cmd: process,524 Stderr: stderr,525 HandshakeConfig: testHandshake,526 Logger: testLogger,527 Plugins: testPluginMap,528 })529 defer c.Kill()530 if _, err := c.Start(); err != nil {531 t.Fatalf("err: %s", err)532 }533 for !c.Exited() {534 time.Sleep(10 * time.Millisecond)535 }536 if c.killed() {537 t.Fatal("process failed to exit gracefully")538 }539 logOut := logBuf.String()540 if !strings.Contains(logOut, "[\"HELLO\"]\n") {541 t.Fatalf("missing json list: '%s'", logOut)542 }543 if !strings.Contains(logOut, "12345\n") {544 t.Fatalf("missing line with raw number: '%s'", logOut)545 }546 if !strings.Contains(logOut, "{\"a\":1}") {547 t.Fatalf("missing json object: '%s'", logOut)548 }549}550func TestClient_textLogLevel(t *testing.T) {551 stderr := new(bytes.Buffer)552 process := helperProcess("level-warn-text")553 var logBuf bytes.Buffer554 mutex := new(sync.Mutex)555 // Custom hclog.Logger556 testLogger := hclog.New(&hclog.LoggerOptions{557 Name: "test-logger",558 Level: hclog.Warn,559 Output: &logBuf,560 Mutex: mutex,561 })562 c := NewClient(&ClientConfig{563 Cmd: process,564 Stderr: stderr,565 HandshakeConfig: testHandshake,566 Logger: testLogger,567 Plugins: testPluginMap,568 })569 defer c.Kill()570 if _, err := c.Start(); err != nil {571 t.Fatalf("err: %s", err)572 }573 for !c.Exited() {574 time.Sleep(10 * time.Millisecond)575 }576 if c.killed() {577 t.Fatal("process failed to exit gracefully")578 }579 logOut := logBuf.String()580 if !strings.Contains(logOut, "test line 98765") {581 log.Fatalf("test string not found in log: %q\n", logOut)582 }583}584func TestClient_Stdin(t *testing.T) {585 // Overwrite stdin for this test with a temporary file586 tf, err := ioutil.TempFile("", "terraform")587 if err != nil {588 t.Fatalf("err: %s", err)589 }590 defer os.Remove(tf.Name())591 defer tf.Close()592 if _, err = tf.WriteString("hello"); err != nil {593 t.Fatalf("error: %s", err)594 }595 if err = tf.Sync(); err != nil {596 t.Fatalf("error: %s", err)597 }598 if _, err = tf.Seek(0, 0); err != nil {599 t.Fatalf("error: %s", err)600 }601 oldStdin := os.Stdin602 defer func() { os.Stdin = oldStdin }()603 os.Stdin = tf604 process := helperProcess("stdin")605 c := NewClient(&ClientConfig{606 Cmd: process,607 HandshakeConfig: testHandshake,608 Plugins: testPluginMap,609 })610 defer c.Kill()611 _, err = c.Start()612 if err != nil {613 t.Fatalf("error: %s", err)614 }615 for {616 if c.Exited() {617 break618 }619 time.Sleep(50 * time.Millisecond)620 }621 if !process.ProcessState.Success() {622 t.Fatal("process didn't exit cleanly")623 }624}625func TestClient_SecureConfig(t *testing.T) {626 // Test failure case627 secureConfig := &SecureConfig{628 Checksum: []byte{'1'},629 Hash: sha256.New(),630 }631 process := helperProcess("test-interface")632 c := NewClient(&ClientConfig{633 Cmd: process,634 HandshakeConfig: testHandshake,635 Plugins: testPluginMap,636 SecureConfig: secureConfig,637 })638 // Grab the RPC client, should error639 _, err := c.Client()640 c.Kill()641 if err != ErrChecksumsDoNotMatch {642 t.Fatalf("err should be %s, got %s", ErrChecksumsDoNotMatch, err)643 }644 // Get the checksum of the executable645 file, err := os.Open(os.Args[0])646 if err != nil {647 t.Fatal(err)648 }649 defer file.Close()650 hash := sha256.New()651 _, err = io.Copy(hash, file)652 if err != nil {653 t.Fatal(err)654 }655 sum := hash.Sum(nil)656 secureConfig = &SecureConfig{657 Checksum: sum,658 Hash: sha256.New(),659 }660 process = helperProcess("test-interface")661 c = NewClient(&ClientConfig{662 Cmd: process,663 HandshakeConfig: testHandshake,664 Plugins: testPluginMap,665 SecureConfig: secureConfig,666 })667 defer c.Kill()668 // Grab the RPC client669 _, err = c.Client()670 if err != nil {671 t.Fatalf("err should be nil, got %s", err)672 }673}674func TestClient_TLS(t *testing.T) {675 // Test failure case676 process := helperProcess("test-interface-tls")677 cBad := NewClient(&ClientConfig{678 Cmd: process,679 HandshakeConfig: testHandshake,680 Plugins: testPluginMap,681 })682 defer cBad.Kill()683 // Grab the RPC client684 clientBad, err := cBad.Client()685 if err != nil {686 t.Fatalf("err should be nil, got %s", err)687 }688 // Grab the impl689 raw, err := clientBad.Dispense("test")690 if err == nil {691 t.Fatal("expected error, got nil")692 }693 cBad.Kill()694 // Add TLS config to client695 tlsConfig, err := helperTLSProvider()696 if err != nil {697 t.Fatalf("err should be nil, got %s", err)698 }699 process = helperProcess("test-interface-tls")700 c := NewClient(&ClientConfig{701 Cmd: process,702 HandshakeConfig: testHandshake,703 Plugins: testPluginMap,704 TLSConfig: tlsConfig,705 })706 defer c.Kill()707 // Grab the RPC client708 client, err := c.Client()709 if err != nil {710 t.Fatalf("err should be nil, got %s", err)711 }712 // Grab the impl713 raw, err = client.Dispense("test")714 if err != nil {715 t.Fatalf("err should be nil, got %s", err)716 }717 impl, ok := raw.(testInterface)718 if !ok {719 t.Fatalf("bad: %#v", raw)720 }721 result := impl.Double(21)722 if result != 42 {723 t.Fatalf("bad: %#v", result)724 }725 // Kill it726 c.Kill()727 // Test that it knows it is exited728 if !c.Exited() {729 t.Fatal("should say client has exited")730 }731 if c.killed() {732 t.Fatal("process failed to exit gracefully")733 }734}735func TestClient_TLS_grpc(t *testing.T) {736 // Add TLS config to client737 tlsConfig, err := helperTLSProvider()738 if err != nil {739 t.Fatalf("err should be nil, got %s", err)740 }741 process := helperProcess("test-grpc-tls")742 c := NewClient(&ClientConfig{743 Cmd: process,744 HandshakeConfig: testHandshake,745 Plugins: testGRPCPluginMap,746 TLSConfig: tlsConfig,747 AllowedProtocols: []Protocol{ProtocolGRPC},748 })749 defer c.Kill()750 // Grab the RPC client751 client, err := c.Client()752 if err != nil {753 t.Fatalf("err should be nil, got %s", err)754 }755 // Grab the impl756 raw, err := client.Dispense("test")757 if err != nil {758 t.Fatalf("err should be nil, got %s", err)759 }760 impl, ok := raw.(testInterface)761 if !ok {762 t.Fatalf("bad: %#v", raw)763 }764 result := impl.Double(21)765 if result != 42 {766 t.Fatalf("bad: %#v", result)767 }768 // Kill it769 c.Kill()770 if !c.Exited() {771 t.Fatal("should say client has exited")772 }773 if c.killed() {774 t.Fatal("process failed to exit gracefully")775 }776}777func TestClient_secureConfigAndReattach(t *testing.T) {778 config := &ClientConfig{779 SecureConfig: &SecureConfig{},780 Reattach: &ReattachConfig{},781 }782 c := NewClient(config)783 defer c.Kill()784 _, err := c.Start()785 if err != ErrSecureConfigAndReattach {786 t.Fatalf("err should not be %s, got %s", ErrSecureConfigAndReattach, err)787 }788}789func TestClient_ping(t *testing.T) {790 process := helperProcess("test-interface")791 c := NewClient(&ClientConfig{792 Cmd: process,793 HandshakeConfig: testHandshake,794 Plugins: testPluginMap,795 })796 defer c.Kill()797 // Get the client798 client, err := c.Client()799 if err != nil {800 t.Fatalf("err: %s", err)801 }802 // Ping, should work803 if err := client.Ping(); err != nil {804 t.Fatalf("err: %s", err)805 }806 // Kill it807 c.Kill()808 if err := client.Ping(); err == nil {809 t.Fatal("should error")810 }811}812func TestClient_wrongVersion(t *testing.T) {813 process := helperProcess("test-proto-upgraded-plugin")814 c := NewClient(&ClientConfig{815 Cmd: process,816 HandshakeConfig: testHandshake,817 Plugins: testGRPCPluginMap,818 AllowedProtocols: []Protocol{ProtocolGRPC},819 })820 defer c.Kill()821 // Get the client822 _, err := c.Client()823 if err == nil {824 t.Fatal("expected incorrect protocol version server")825 }826}827func TestClient_legacyClient(t *testing.T) {828 process := helperProcess("test-proto-upgraded-plugin")829 c := NewClient(&ClientConfig{830 Cmd: process,831 HandshakeConfig: testVersionedHandshake,832 VersionedPlugins: map[int]PluginSet{833 1: testPluginMap,834 },835 })836 defer c.Kill()837 // Get the client838 client, err := c.Client()839 if err != nil {840 t.Fatalf("err: %s", err)841 }842 if c.NegotiatedVersion() != 1 {843 t.Fatal("using incorrect version", c.NegotiatedVersion())844 }845 // Ping, should work846 if err := client.Ping(); err == nil {847 t.Fatal("expected error, should negotiate wrong plugin")848 }849}850func TestClient_legacyServer(t *testing.T) {851 // test using versioned plugins version when the server supports only852 // supports one853 process := helperProcess("test-proto-upgraded-client")854 c := NewClient(&ClientConfig{855 Cmd: process,856 HandshakeConfig: testVersionedHandshake,857 VersionedPlugins: map[int]PluginSet{858 2: testGRPCPluginMap,859 },860 AllowedProtocols: []Protocol{ProtocolGRPC},861 })862 defer c.Kill()863 // Get the client864 client, err := c.Client()865 if err != nil {866 t.Fatalf("err: %s", err)867 }868 if c.NegotiatedVersion() != 2 {869 t.Fatal("using incorrect version", c.NegotiatedVersion())870 }871 // Ping, should work872 if err := client.Ping(); err == nil {873 t.Fatal("expected error, should negotiate wrong plugin")874 }875}876func TestClient_versionedClient(t *testing.T) {877 process := helperProcess("test-versioned-plugins")878 c := NewClient(&ClientConfig{879 Cmd: process,880 HandshakeConfig: testVersionedHandshake,881 VersionedPlugins: map[int]PluginSet{882 2: testGRPCPluginMap,883 },884 AllowedProtocols: []Protocol{ProtocolGRPC},885 })886 defer c.Kill()887 if _, err := c.Start(); err != nil {888 t.Fatalf("err: %s", err)889 }890 if v := c.Protocol(); v != ProtocolGRPC {891 t.Fatalf("bad: %s", v)892 }893 // Grab the RPC client894 client, err := c.Client()895 if err != nil {896 t.Fatalf("err should be nil, got %s", err)897 }898 if c.NegotiatedVersion() != 2 {899 t.Fatal("using incorrect version", c.NegotiatedVersion())900 }901 // Grab the impl902 raw, err := client.Dispense("test")903 if err != nil {904 t.Fatalf("err should be nil, got %s", err)905 }906 _, ok := raw.(testInterface)907 if !ok {908 t.Fatalf("bad: %#v", raw)909 }910 c.process.Kill()911 select {912 case <-c.doneCtx.Done():913 case <-time.After(time.Second * 2):914 t.Fatal("Context was not closed")915 }916}917func TestClient_mtlsClient(t *testing.T) {918 process := helperProcess("test-mtls")919 c := NewClient(&ClientConfig{920 AutoMTLS: true,921 Cmd: process,922 HandshakeConfig: testVersionedHandshake,923 VersionedPlugins: map[int]PluginSet{924 2: testGRPCPluginMap,925 },926 AllowedProtocols: []Protocol{ProtocolGRPC},927 })928 defer c.Kill()929 if _, err := c.Start(); err != nil {930 t.Fatalf("err: %s", err)931 }932 if v := c.Protocol(); v != ProtocolGRPC {933 t.Fatalf("bad: %s", v)934 }935 // Grab the RPC client936 client, err := c.Client()937 if err != nil {938 t.Fatalf("err should be nil, got %s", err)939 }940 if c.NegotiatedVersion() != 2 {941 t.Fatal("using incorrect version", c.NegotiatedVersion())942 }943 // Grab the impl944 raw, err := client.Dispense("test")945 if err != nil {946 t.Fatalf("err should be nil, got %s", err)947 }948 tester, ok := raw.(testInterface)949 if !ok {950 t.Fatalf("bad: %#v", raw)951 }952 n := tester.Double(3)953 if n != 6 {954 t.Fatal("invalid response", n)955 }956 c.process.Kill()957 select {958 case <-c.doneCtx.Done():959 case <-time.After(time.Second * 2):960 t.Fatal("Context was not closed")961 }962}963func TestClient_mtlsNetRPCClient(t *testing.T) {964 process := helperProcess("test-interface-mtls")965 c := NewClient(&ClientConfig{966 AutoMTLS: true,967 Cmd: process,968 HandshakeConfig: testVersionedHandshake,969 Plugins: testPluginMap,970 AllowedProtocols: []Protocol{ProtocolNetRPC},971 })972 defer c.Kill()973 if _, err := c.Start(); err != nil {974 t.Fatalf("err: %s", err)975 }976 // Grab the RPC client977 client, err := c.Client()978 if err != nil {979 t.Fatalf("err should be nil, got %s", err)980 }981 // Grab the impl982 raw, err := client.Dispense("test")983 if err != nil {984 t.Fatalf("err should be nil, got %s", err)985 }986 tester, ok := raw.(testInterface)987 if !ok {988 t.Fatalf("bad: %#v", raw)989 }990 n := tester.Double(3)991 if n != 6 {992 t.Fatal("invalid response", n)993 }994 c.process.Kill()995 select {996 case <-c.doneCtx.Done():997 case <-time.After(time.Second * 2):998 t.Fatal("Context was not closed")999 }1000}1001func TestClient_logger(t *testing.T) {1002 t.Run("net/rpc", func(t *testing.T) { testClient_logger(t, "netrpc") })1003 t.Run("grpc", func(t *testing.T) { testClient_logger(t, "grpc") })1004}1005func testClient_logger(t *testing.T, proto string) {1006 var buffer bytes.Buffer1007 mutex := new(sync.Mutex)1008 stderr := io.MultiWriter(os.Stderr, &buffer)1009 // Custom hclog.Logger1010 clientLogger := hclog.New(&hclog.LoggerOptions{1011 Name: "test-logger",1012 Level: hclog.Trace,1013 Output: stderr,1014 Mutex: mutex,1015 })1016 process := helperProcess("test-interface-logger-" + proto)1017 c := NewClient(&ClientConfig{1018 Cmd: process,1019 HandshakeConfig: testHandshake,1020 Plugins: testGRPCPluginMap,1021 Logger: clientLogger,1022 AllowedProtocols: []Protocol{ProtocolNetRPC, ProtocolGRPC},1023 })1024 defer c.Kill()1025 // Grab the RPC client1026 client, err := c.Client()1027 if err != nil {1028 t.Fatalf("err should be nil, got %s", err)1029 }1030 // Grab the impl1031 raw, err := client.Dispense("test")1032 if err != nil {1033 t.Fatalf("err should be nil, got %s", err)1034 }1035 impl, ok := raw.(testInterface)1036 if !ok {1037 t.Fatalf("bad: %#v", raw)1038 }1039 {1040 // Discard everything else, and capture the output we care about1041 mutex.Lock()1042 buffer.Reset()1043 mutex.Unlock()1044 impl.PrintKV("foo", "bar")1045 time.Sleep(100 * time.Millisecond)1046 mutex.Lock()1047 line, err := buffer.ReadString('\n')1048 mutex.Unlock()1049 if err != nil {1050 t.Fatal(err)1051 }1052 if !strings.Contains(line, "foo=bar") {1053 t.Fatalf("bad: %q", line)1054 }1055 }1056 {1057 // Try an integer type1058 mutex.Lock()1059 buffer.Reset()1060 mutex.Unlock()1061 impl.PrintKV("foo", 12)1062 time.Sleep(100 * time.Millisecond)1063 mutex.Lock()1064 line, err := buffer.ReadString('\n')1065 mutex.Unlock()1066 if err != nil {1067 t.Fatal(err)1068 }1069 if !strings.Contains(line, "foo=12") {1070 t.Fatalf("bad: %q", line)1071 }1072 }1073 // Kill it1074 c.Kill()1075 // Test that it knows it is exited1076 if !c.Exited() {1077 t.Fatal("should say client has exited")1078 }1079 if c.killed() {1080 t.Fatal("process failed to exit gracefully")1081 }1082}1083// Test that we continue to consume stderr over long lines.1084func TestClient_logStderr(t *testing.T) {1085 orig := stdErrBufferSize1086 stdErrBufferSize = 321087 defer func() {1088 stdErrBufferSize = orig...

Full Screen

Full Screen

driver.go

Source:driver.go Github

copy

Full Screen

...16 // DatabaseName is the default database to connect.17 DatabaseName string18}19type PluginClient interface {20 Kill()21 Client() (goPlugin.ClientProtocol, error)22 RegisterPlugin(c PluginClient) error23}24type pluginClientOld struct {25 path string26 c *goPlugin.Client27}28func newOldClientFromFile(path string) *pluginClientOld {29 return &pluginClientOld{30 path: path,31 }32}33func (p *pluginClientOld) Kill() {34 p.c.Kill()35}36func (p *pluginClientOld) Client() (goPlugin.ClientProtocol, error) {37 p.resetClient()38 return p.c.Client()39}40func (p *pluginClientOld) RegisterPlugin(c PluginClient) error {41 _, err := registerAuditDriver(c)42 return err43}44func (p *pluginClientOld) resetClient() {45 if p.c != nil {46 p.c.Kill()47 }48 p.c = goPlugin.NewClient(&goPlugin.ClientConfig{49 HandshakeConfig: handshakeConfig,50 Plugins: goPlugin.PluginSet{51 PluginNameDriver: &auditDriverPlugin{},52 },53 Cmd: exec.Command(p.path),54 AllowedProtocols: []goPlugin.Protocol{goPlugin.ProtocolGRPC},55 })56}57type pluginClient struct {58 path string59 c *goPlugin.Client60}61func newClientFromFile(path string) *pluginClient {62 return &pluginClient{63 path: path,64 }65}66func (p *pluginClient) Kill() {67 p.c.Kill()68}69func (p *pluginClient) Client() (goPlugin.ClientProtocol, error) {70 p.resetClient()71 return p.c.Client()72}73func (p *pluginClient) RegisterPlugin(c PluginClient) error {74 pluginName, err := registerAuditDriver(c)75 if err != nil {76 return err77 }78 return registerPlugin(pluginName, c)79}80func (p *pluginClient) resetClient() {81 if p.c != nil {82 p.c.Kill()83 }84 p.c = goPlugin.NewClient(&goPlugin.ClientConfig{85 HandshakeConfig: handshakeConfig,86 VersionedPlugins: defaultPluginSet,87 Cmd: exec.Command(p.path),88 AllowedProtocols: []goPlugin.Protocol{goPlugin.ProtocolGRPC},89 })90}91func testConnClient(client PluginClient) bool {92 c, err := client.Client()93 if err != nil {94 return false95 }96 defer client.Kill()97 err = c.Ping()98 return err == nil99}100func RegisterDriverFromClient(client PluginClient) error {101 return client.RegisterPlugin(client)102}103func registerPlugin(pluginName string, c PluginClient) error {104 if err := registerQueryPlugin(pluginName, c); err != nil {105 log.Logger().WithFields(logrus.Fields{106 "plugin_name": pluginName,107 "plugin_type": PluginNameQueryDriver,108 }).Infoln("plugin not exist or failed to load")109 }110 return nil...

Full Screen

Full Screen

process.go

Source:process.go Github

copy

Full Screen

...6var (7 isStart bool8 WorkerProcessInstance *WorkerProcess9)10type ProcKill struct {11 Name string12 Properties string13}14type Msg struct{15 ProcessKill ProcKill16 Proc []plugin.Process17 Err []error18 Res chan interface{}19}20type WorkerProcess struct{21 PluginInstance plugin.Plugin22 GetDaemons func ()([]plugin.Process, error)23 GetServices func ()([]plugin.Process, error)24 GetDockers func ()([]plugin.Process, error)25 GetFindAndKill func ([]plugin.Process, string, string)([]plugin.Process, []error)26 ChRead chan *Msg27 ChKill chan *Msg28}29func NewWorkerProcess() *WorkerProcess {30 if !isStart {31 isStart = true32 WorkerProcessInstance = new(WorkerProcess)33 WorkerProcessInstance.ChRead = make(chan *Msg, 100)34 WorkerProcessInstance.ChKill = make(chan *Msg, 100)35 //TODO: change mesos config, set by command line36 WorkerProcessInstance.PluginInstance = mesos.NewMesosConfig()37 WorkerProcessInstance.ConfigManager(WorkerProcessInstance.PluginInstance)38 go WorkerProcessInstance.WorkerProcess(WorkerProcessInstance.PluginInstance)39 }40 return WorkerProcessInstance41}42func (p *WorkerProcess) ConfigManager(plug plugin.Plugin) {43 p.GetDaemons = plug.GetDaemons()44 p.GetServices = plug.GetServices()45 p.GetDockers = plug.GetDocker()46 p.GetFindAndKill = plug.FindAndKill()47}48func (p *WorkerProcess) LoadProcesses() (res []plugin.Process, err []error) {49 daemons, errDaemons := p.GetDaemons()50 if errDaemons != nil {51 err = append(err, errDaemons)52 }53 services, errServices := p.GetServices()54 if errServices != nil {55 err = append(err, errServices)56 }57 dockers, errDockers := p.GetDockers()58 if errDockers != nil {59 err = append(err, errDockers)60 }61 res = append(append(append(res, daemons...), services...), dockers...)62 return63}64func (p *WorkerProcess) WorkerProcess(plug plugin.Plugin){65 for{66 select {67 case msgRead := <-p.ChRead:68 msgRead.Proc, msgRead.Err = p.LoadProcesses()69 msgRead.Res <- true70 //. high concurrency purpose71 for {72 select {73 case msgNew := <-p.ChRead:74 msgNew.Proc = msgRead.Proc75 msgNew.Err = msgRead.Err76 msgNew.Res <- true77 default:78 break79 }80 if len(p.ChRead) < 1 {81 break82 }83 }84 case msgKill := <- p.ChKill:85 process, _ := p.LoadProcesses()86 msgKill.Proc, msgKill.Err = p.GetFindAndKill(process, msgKill.ProcessKill.Name, msgKill.ProcessKill.Properties)87 msgKill.Res <- true88 for {89 select{90 case msgNew := <- p.ChKill:91 msgNew.Proc, msgNew.Err = p.GetFindAndKill(process, msgNew.ProcessKill.Name, msgNew.ProcessKill.Properties)92 msgNew.Res <- true93 default:94 break95 }96 if len(p.ChKill) < 1 {97 break98 }99 }100 }101 }102}...

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p, err := plugin.Open("plugin.so")4 if err != nil {5 panic(err)6 }7 s, err := p.Lookup("Plugin")8 if err != nil {9 panic(err)10 }11 plugin := s.(*Plugin)12 plugin.Kill()13 fmt.Println("Killed")14}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p, err := plugin.Open("plugin.so")4 if err != nil {5 panic(err)6 }7 kill, err := p.Lookup("Kill")8 if err != nil {9 panic(err)10 }11 kill.(func())()12}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("notepad.exe")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 }8 time.Sleep(5 * time.Second)9 err = cmd.Process.Kill()10 if err != nil {11 fmt.Println(err)12 }13}14Recommended Posts: Go | exec.Command() function15Go | exec.CommandContext() function16Go | exec.Command() function with examples17Go | exec.CommandContext() function with examples18Go | exec.LookPath() function19Go | exec.Command() function with examples

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 client := plugin.NewClient(&plugin.ClientConfig{4 AllowedProtocols: []plugin.Protocol{5 },6 })7 defer client.Kill()8 rcpClient, err := client.Client()9 if err != nil {10 log.Fatal(err)11 }12 raw, err := rcpClient.Dispense("greeter")13 if err != nil {14 log.Fatal(err)15 }16 greeter := raw.(shared.Greeter)17 resp, err := greeter.Greet(context.Background(), &shared.GreetRequest{Name: "HashiCorp"})18 if err != nil {19 log.Fatal(err)20 }21 log.Println(resp.Greeting)22 client.Kill()23 client.Wait()24}25import (26func main() {27 client := plugin.NewClient(&plugin.ClientConfig{28 AllowedProtocols: []plugin.Protocol{29 },30 })31 defer client.Kill()

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 p, err := plugin.Open("plugin.so")4 if err != nil {5 fmt.Println(err)6 os.Exit(1)7 }8 f, err := p.Lookup("Kill")9 if err != nil {10 fmt.Println(err)11 os.Exit(1)12 }13 f.(func())()14}

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("go", "build", "-buildmode=plugin", "plugin.go")4 err := cmd.Run()5 if err != nil {6 fmt.Println("Error while compiling plugin")7 os.Exit(1)8 }9 p, err := plugin.Open("plugin.so")10 if err != nil {11 fmt.Println("Error while loading plugin")12 os.Exit(1)13 }14 symbol, err := p.Lookup("Kill")15 if err != nil {16 fmt.Println("Error while loading symbol")17 os.Exit(1)18 }19 kill, ok := symbol.(func())20 if !ok {21 fmt.Println("Could not convert to func()")22 os.Exit(1)23 }24 kill()25}26import "

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 cmd := exec.Command("C:\\Users\\Administrator\\Desktop\\test\\2.exe")4 err := cmd.Start()5 if err != nil {6 fmt.Println(err)7 }8 time.Sleep(10 * time.Second)9 cmd.Process.Kill()10}11import (12func main() {13 fmt.Println("hello world")14 time.Sleep(30 * time.Second)15 os.Exit(0)16}17import (18func main() {19 fmt.Println("hello world")20 time.Sleep(30 * time.Second)21 os.Exit(1)22}23import (24func main() {25 fmt.Println("hello world")26 time.Sleep(30 * time.Second)27 os.Exit(3)28}29import (30func main() {31 fmt.Println("hello world")32 time.Sleep(30 * time.Second)33 os.Exit(5)34}35import (36func main() {37 fmt.Println("hello world")38 time.Sleep(30 * time.Second)39 os.Exit(10)40}41import (42func main() {43 fmt.Println("hello world")44 time.Sleep(30 * time.Second)45 os.Exit(20)46}47import (48func main() {49 fmt.Println("hello world")50 time.Sleep(30 * time.Second)51 os.Exit(30)52}53import (54func main() {

Full Screen

Full Screen

Kill

Using AI Code Generation

copy

Full Screen

1import (2func Kill() {3 fmt.Println("Killing the process")4 os.Exit(0)5}6func main() {7 cmd := exec.Command("go", "run", "2.go")8 cmd.SysProcAttr = &syscall.SysProcAttr{HideWindow: true}9 stdout, err := cmd.StdoutPipe()10 if err != nil {11 fmt.Println(err)12 }13 defer stdout.Close()14 if err := cmd.Start(); err != nil {15 fmt.Println(err)16 }17 cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}18 ch := make(chan os.Signal)19 signal.Notify(ch, os.Interrupt)20 go func() {21 for {22 select {23 syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL)24 }25 }26 }()27 for {28 buf := make([]byte, 1024)29 n, err := stdout.Read(buf)30 if err != nil && err != io.EOF {31 fmt.Println(err)32 }33 if n == 0 {34 }35 fmt.Print(string(buf[:n]))36 }37 if err := cmd.Wait(); err != nil {38 fmt.Println(err)39 }40}41import (42func Kill() {43 fmt.Println("Killing the process")44 os.Exit(0)45}46func main() {47 for {48 time.Sleep(time.Second * 1)49 fmt.Println("Hello World")50 }51}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful