github.com/ddnomad/packer@v1.3.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/hashicorp/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(packer.NoopUi)
    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 communicator struct{}
    97  
    98  func (c communicator) Start(*packer.RemoteCmd) error {
    99  	return errors.New("communicator not supported")
   100  }
   101  
   102  func (c communicator) Upload(string, io.Reader, *os.FileInfo) error {
   103  	return errors.New("communicator not supported")
   104  }
   105  
   106  func (c communicator) UploadDir(dst string, src string, exclude []string) error {
   107  	return errors.New("communicator not supported")
   108  }
   109  
   110  func (c communicator) Download(string, io.Writer) error {
   111  	return errors.New("communicator not supported")
   112  }
   113  
   114  func (c communicator) DownloadDir(src string, dst string, exclude []string) error {
   115  	return errors.New("communicator not supported")
   116  }