github.com/gavinw2006/hashicorp-terraform@v0.11.12-beta1/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  	CommandFunc      func(*remote.Cmd) error
    22  	DisconnectFunc   func() error
    23  	ConnTimeout      time.Duration
    24  }
    25  
    26  // Connect implementation of communicator.Communicator interface
    27  func (c *MockCommunicator) Connect(o terraform.UIOutput) error {
    28  	return nil
    29  }
    30  
    31  // Disconnect implementation of communicator.Communicator interface
    32  func (c *MockCommunicator) Disconnect() error {
    33  	if c.DisconnectFunc != nil {
    34  		return c.DisconnectFunc()
    35  	}
    36  	return nil
    37  }
    38  
    39  // Timeout implementation of communicator.Communicator interface
    40  func (c *MockCommunicator) Timeout() time.Duration {
    41  	if c.ConnTimeout != 0 {
    42  		return c.ConnTimeout
    43  	}
    44  	return time.Duration(5 * time.Second)
    45  }
    46  
    47  // ScriptPath implementation of communicator.Communicator interface
    48  func (c *MockCommunicator) ScriptPath() string {
    49  	return c.RemoteScriptPath
    50  }
    51  
    52  // Start implementation of communicator.Communicator interface
    53  func (c *MockCommunicator) Start(r *remote.Cmd) error {
    54  	r.Init()
    55  
    56  	if c.CommandFunc != nil {
    57  		return c.CommandFunc(r)
    58  	}
    59  
    60  	if !c.Commands[r.Command] {
    61  		return fmt.Errorf("Command not found!")
    62  	}
    63  
    64  	r.SetExitStatus(0, nil)
    65  
    66  	return nil
    67  }
    68  
    69  // Upload implementation of communicator.Communicator interface
    70  func (c *MockCommunicator) Upload(path string, input io.Reader) error {
    71  	f, ok := c.Uploads[path]
    72  	if !ok {
    73  		return fmt.Errorf("Path %q not found!", path)
    74  	}
    75  
    76  	var buf bytes.Buffer
    77  	buf.ReadFrom(input)
    78  	content := strings.TrimSpace(buf.String())
    79  
    80  	f = strings.TrimSpace(f)
    81  	if f != content {
    82  		return fmt.Errorf("expected: %q\n\ngot: %q\n", f, content)
    83  	}
    84  
    85  	return nil
    86  }
    87  
    88  // UploadScript implementation of communicator.Communicator interface
    89  func (c *MockCommunicator) UploadScript(path string, input io.Reader) error {
    90  	c.Uploads = c.UploadScripts
    91  	return c.Upload(path, input)
    92  }
    93  
    94  // UploadDir implementation of communicator.Communicator interface
    95  func (c *MockCommunicator) UploadDir(dst string, src string) error {
    96  	v, ok := c.UploadDirs[src]
    97  	if !ok {
    98  		return fmt.Errorf("Directory not found!")
    99  	}
   100  
   101  	if v != dst {
   102  		return fmt.Errorf("expected: %q\n\ngot: %q\n", v, dst)
   103  	}
   104  
   105  	return nil
   106  }