github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/pkg/local/local_test.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  package local
     5  
     6  import (
     7  	"context"
     8  	"testing"
     9  	"time"
    10  
    11  	"github.com/defang-io/defang/src/pkg/types"
    12  )
    13  
    14  func TestLocal(t *testing.T) {
    15  	l := New()
    16  	ctx := context.Background()
    17  
    18  	t.Run("SetUp", func(t *testing.T) {
    19  		if err := l.SetUp(ctx, []types.Container{{EntryPoint: []string{"/bin/sh"}}}); err != nil {
    20  			t.Fatal(err)
    21  		}
    22  	})
    23  	defer l.TearDown(ctx)
    24  
    25  	var pid PID
    26  	t.Run("Run", func(t *testing.T) {
    27  		env := map[string]string{"FOO": "bar"}
    28  		var err error
    29  		pid, err = l.Run(ctx, env, "-c", "sleep 1 ; echo $FOO")
    30  		if err != nil {
    31  			t.Fatal(err)
    32  		}
    33  	})
    34  
    35  	t.Run("Tail", func(t *testing.T) {
    36  		ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
    37  		defer cancel()
    38  		// This should print "bar" to stdout
    39  		if err := l.Tail(ctx, pid); err != nil {
    40  			t.Error(err)
    41  		}
    42  	})
    43  
    44  	t.Run("TearDown", func(t *testing.T) {
    45  		if err := l.TearDown(ctx); err != nil {
    46  			t.Error(err)
    47  		}
    48  	})
    49  }