github.com/dacamp/packer@v0.10.2/provisioner/ansible/adapter_test.go (about)

     1  package ansible
     2  
     3  import (
     4  	"errors"
     5  	"io"
     6  	"log"
     7  	"net"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/mitchellh/packer/packer"
    13  
    14  	"golang.org/x/crypto/ssh"
    15  )
    16  
    17  func TestAdapter_Serve(t *testing.T) {
    18  
    19  	// done signals the adapter that the provisioner is done
    20  	done := make(chan struct{})
    21  
    22  	acceptC := make(chan struct{})
    23  	l := listener{done: make(chan struct{}), acceptC: acceptC}
    24  
    25  	config := &ssh.ServerConfig{}
    26  
    27  	ui := new(ui)
    28  
    29  	sut := newAdapter(done, &l, config, "", newUi(ui), communicator{})
    30  	go func() {
    31  		i := 0
    32  		for range acceptC {
    33  			i++
    34  			if i == 4 {
    35  				close(done)
    36  				l.Close()
    37  			}
    38  		}
    39  	}()
    40  
    41  	sut.Serve()
    42  }
    43  
    44  type listener struct {
    45  	done    chan struct{}
    46  	acceptC chan<- struct{}
    47  	i       int
    48  }
    49  
    50  func (l *listener) Accept() (net.Conn, error) {
    51  	log.Println("Accept() called")
    52  	l.acceptC <- struct{}{}
    53  	select {
    54  	case <-l.done:
    55  		log.Println("done, serving an error")
    56  		return nil, errors.New("listener is closed")
    57  
    58  	case <-time.After(10 * time.Millisecond):
    59  		l.i++
    60  
    61  		if l.i%2 == 0 {
    62  			c1, c2 := net.Pipe()
    63  
    64  			go func(c net.Conn) {
    65  				<-time.After(100 * time.Millisecond)
    66  				log.Println("closing c")
    67  				c.Close()
    68  			}(c1)
    69  
    70  			return c2, nil
    71  		}
    72  	}
    73  
    74  	return nil, errors.New("accept error")
    75  }
    76  
    77  func (l *listener) Close() error {
    78  	close(l.done)
    79  	return nil
    80  }
    81  
    82  func (l *listener) Addr() net.Addr {
    83  	return addr{}
    84  }
    85  
    86  type addr struct{}
    87  
    88  func (a addr) Network() string {
    89  	return a.String()
    90  }
    91  
    92  func (a addr) String() string {
    93  	return "test"
    94  }
    95  
    96  type ui int
    97  
    98  func (u *ui) Ask(s string) (string, error) {
    99  	*u++
   100  	return s, nil
   101  }
   102  
   103  func (u *ui) Say(s string) {
   104  	*u++
   105  	log.Println(s)
   106  }
   107  
   108  func (u *ui) Message(s string) {
   109  	*u++
   110  	log.Println(s)
   111  }
   112  
   113  func (u *ui) Error(s string) {
   114  	*u++
   115  	log.Println(s)
   116  }
   117  
   118  func (u *ui) Machine(s1 string, s2 ...string) {
   119  	*u++
   120  	log.Println(s1)
   121  	for _, s := range s2 {
   122  		log.Println(s)
   123  	}
   124  }
   125  
   126  type communicator struct{}
   127  
   128  func (c communicator) Start(*packer.RemoteCmd) error {
   129  	return errors.New("communicator not supported")
   130  }
   131  
   132  func (c communicator) Upload(string, io.Reader, *os.FileInfo) error {
   133  	return errors.New("communicator not supported")
   134  }
   135  
   136  func (c communicator) UploadDir(dst string, src string, exclude []string) error {
   137  	return errors.New("communicator not supported")
   138  }
   139  
   140  func (c communicator) Download(string, io.Writer) error {
   141  	return errors.New("communicator not supported")
   142  }
   143  
   144  func (c communicator) DownloadDir(src string, dst string, exclude []string) error {
   145  	return errors.New("communicator not supported")
   146  }