github.com/hashicorp/packer@v1.14.3/packer/client_test.go (about) 1 // Copyright (c) HashiCorp, Inc. 2 // SPDX-License-Identifier: BUSL-1.1 3 4 package packer 5 6 import ( 7 "bytes" 8 "os" 9 "strings" 10 "testing" 11 "time" 12 ) 13 14 func TestClient(t *testing.T) { 15 process := helperProcess("mock") 16 c := NewClient(&PluginClientConfig{Cmd: process}) 17 defer c.Kill() 18 19 // Test that it parses the proper address 20 addr, err := c.Start() 21 if err != nil { 22 t.Fatalf("err should be nil, got %s", err) 23 } 24 25 if addr.Network() != "tcp" { 26 t.Fatalf("bad: %#v", addr) 27 } 28 29 if addr.String() != ":1234" { 30 t.Fatalf("bad: %#v", addr) 31 } 32 33 // Test that it exits properly if killed 34 c.Kill() 35 36 if process.ProcessState == nil { 37 t.Fatal("should have process state") 38 } 39 40 // Test that it knows it is exited 41 if !c.Exited() { 42 t.Fatal("should say client has exited") 43 } 44 } 45 46 func TestClientStart_badVersion(t *testing.T) { 47 config := &PluginClientConfig{ 48 Cmd: helperProcess("bad-version"), 49 StartTimeout: 50 * time.Millisecond, 50 } 51 52 c := NewClient(config) 53 defer c.Kill() 54 55 _, err := c.Start() 56 if err == nil { 57 t.Fatal("err should not be nil") 58 } 59 } 60 61 func TestClient_Start_Timeout(t *testing.T) { 62 config := &PluginClientConfig{ 63 Cmd: helperProcess("start-timeout"), 64 StartTimeout: 50 * time.Millisecond, 65 } 66 67 c := NewClient(config) 68 defer c.Kill() 69 70 _, err := c.Start() 71 if err == nil { 72 t.Fatal("err should not be nil") 73 } 74 } 75 76 func TestClient_Stderr(t *testing.T) { 77 stderr := new(bytes.Buffer) 78 process := helperProcess("stderr") 79 c := NewClient(&PluginClientConfig{ 80 Cmd: process, 81 Stderr: stderr, 82 }) 83 defer c.Kill() 84 85 if _, err := c.Start(); err != nil { 86 t.Fatalf("err: %s", err) 87 } 88 89 for !c.Exited() { 90 time.Sleep(10 * time.Millisecond) 91 } 92 93 if !strings.Contains(stderr.String(), "HELLO\n") { 94 t.Fatalf("bad log data: '%s'", stderr.String()) 95 } 96 97 if !strings.Contains(stderr.String(), "WORLD\n") { 98 t.Fatalf("bad log data: '%s'", stderr.String()) 99 } 100 } 101 102 func TestClient_Stdin(t *testing.T) { 103 // Overwrite stdin for this test with a temporary file 104 tf, err := os.CreateTemp("", "packer") 105 if err != nil { 106 t.Fatalf("err: %s", err) 107 } 108 defer os.Remove(tf.Name()) 109 defer tf.Close() 110 111 if _, err = tf.WriteString("hello"); err != nil { 112 t.Fatalf("error: %s", err) 113 } 114 115 if err = tf.Sync(); err != nil { 116 t.Fatalf("error: %s", err) 117 } 118 119 if _, err = tf.Seek(0, 0); err != nil { 120 t.Fatalf("error: %s", err) 121 } 122 123 oldStdin := os.Stdin 124 defer func() { os.Stdin = oldStdin }() 125 os.Stdin = tf 126 127 process := helperProcess("stdin") 128 c := NewClient(&PluginClientConfig{Cmd: process}) 129 defer c.Kill() 130 131 _, err = c.Start() 132 if err != nil { 133 t.Fatalf("error: %s", err) 134 } 135 136 for { 137 if c.Exited() { 138 break 139 } 140 141 time.Sleep(50 * time.Millisecond) 142 } 143 144 if !process.ProcessState.Success() { 145 t.Fatal("process didn't exit cleanly") 146 } 147 }