github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/communicator/communicator_mock.go (about) 1 package communicator 2 3 import ( 4 "bytes" 5 "fmt" 6 "io" 7 "strings" 8 "time" 9 10 "github.com/hashicorp/terraform/communicator/remote" 11 "github.com/hashicorp/terraform/terraform" 12 ) 13 14 // MockCommunicator is an implementation of Communicator that can be used for tests. 15 type MockCommunicator struct { 16 RemoteScriptPath string 17 Commands map[string]bool 18 Uploads map[string]string 19 UploadScripts map[string]string 20 UploadDirs map[string]string 21 } 22 23 // Connect implementation of communicator.Communicator interface 24 func (c *MockCommunicator) Connect(o terraform.UIOutput) error { 25 return nil 26 } 27 28 // Disconnect implementation of communicator.Communicator interface 29 func (c *MockCommunicator) Disconnect() error { 30 return nil 31 } 32 33 // Timeout implementation of communicator.Communicator interface 34 func (c *MockCommunicator) Timeout() time.Duration { 35 return time.Duration(5 * time.Second) 36 } 37 38 // ScriptPath implementation of communicator.Communicator interface 39 func (c *MockCommunicator) ScriptPath() string { 40 return c.RemoteScriptPath 41 } 42 43 // Start implementation of communicator.Communicator interface 44 func (c *MockCommunicator) Start(r *remote.Cmd) error { 45 if !c.Commands[r.Command] { 46 return fmt.Errorf("Command not found!") 47 } 48 49 r.SetExited(0) 50 51 return nil 52 } 53 54 // Upload implementation of communicator.Communicator interface 55 func (c *MockCommunicator) Upload(path string, input io.Reader) error { 56 f, ok := c.Uploads[path] 57 if !ok { 58 return fmt.Errorf("Path %q not found!", path) 59 } 60 61 var buf bytes.Buffer 62 buf.ReadFrom(input) 63 content := strings.TrimSpace(buf.String()) 64 65 f = strings.TrimSpace(f) 66 if f != content { 67 return fmt.Errorf("expected: %q\n\ngot: %q\n", f, content) 68 } 69 70 return nil 71 } 72 73 // UploadScript implementation of communicator.Communicator interface 74 func (c *MockCommunicator) UploadScript(path string, input io.Reader) error { 75 c.Uploads = c.UploadScripts 76 return c.Upload(path, input) 77 } 78 79 // UploadDir implementation of communicator.Communicator interface 80 func (c *MockCommunicator) UploadDir(dst string, src string) error { 81 v, ok := c.UploadDirs[src] 82 if !ok { 83 return fmt.Errorf("Directory not found!") 84 } 85 86 if v != dst { 87 return fmt.Errorf("expected: %q\n\ngot: %q\n", v, dst) 88 } 89 90 return nil 91 }