github.com/bkosm/gompose/v2@v2.3.1/up_test.go (about)

     1  package gompose
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  	"time"
    10  )
    11  
    12  func TestUp(t *testing.T) {
    13  	t.Run("up fails if there is no file", func(t *testing.T) {
    14  		t.Parallel()
    15  
    16  		err := Up()
    17  		assertError(t, err)
    18  	})
    19  
    20  	t.Run("up works with no arguments", func(t *testing.T) {
    21  		goBack := goIntoTestDataDir(t)
    22  		defer func() {
    23  			goBack()
    24  			testDown(t)
    25  		}()
    26  
    27  		err := Up()
    28  		assertNoError(t, err)
    29  		assertEventually(t, serviceIsUp, time.Second, 100*time.Millisecond)
    30  	})
    31  
    32  	t.Run("up works with options", func(t *testing.T) {
    33  		defer testDown(t)
    34  
    35  		err := Up(
    36  			Wait(ReadyOnLog(expectedLogLine, customFileOpt)),
    37  			CustomServices(customServiceName),
    38  			customFileOpt,
    39  		)
    40  		assertNoError(t, err)
    41  		assertServiceIsUp(t)
    42  	})
    43  
    44  	t.Run("intercepts os signals", func(t *testing.T) {
    45  		defer testDown(t)
    46  
    47  		c := 0
    48  		callback := func(s os.Signal) {
    49  			if s == os.Interrupt {
    50  				c += 1
    51  			}
    52  		}
    53  
    54  		err := Up(
    55  			Wait(ReadyOnLog(expectedLogLine, customFileOpt)),
    56  			SignalCallback(callback),
    57  			customFileOpt,
    58  		)
    59  		assertNoError(t, err)
    60  		assertServiceIsUp(t)
    61  
    62  		doSignal(t, syscall.SIGINT)
    63  
    64  		wasCalled := func() bool { return c == 1 }
    65  		assertEventually(t, wasCalled, 5*time.Second, 100*time.Millisecond)
    66  	})
    67  
    68  	t.Run("propagates wait channel errors", func(t *testing.T) {
    69  		defer testDown(t)
    70  		c, done := make(chan error), make(chan any)
    71  		expected := errors.New("whoops")
    72  
    73  		go func() {
    74  			err := Up(Wait(c), customFileOpt)
    75  			assertError(t, err, expected)
    76  			close(done)
    77  		}()
    78  
    79  		c <- expected
    80  		<-done
    81  	})
    82  }
    83  
    84  func ExampleUp() {
    85  	_ = Up(
    86  		Wait(ReadyOnLog(expectedLogLine, CustomFile("./testdata/docker-compose.yml"))),
    87  		CustomServices(customServiceName),
    88  		customFileOpt,
    89  	)
    90  
    91  	fmt.Println("the containers are ready to go!")
    92  	// Output:
    93  	// the containers are ready to go!
    94  
    95  	_ = Down(customFileOpt)
    96  }