github.com/defang-io/defang/src@v0.0.0-20240505002154-bdf411911834/pkg/clouds/aws/ecs/cfn/setup_test.go (about)

     1  //go:build integration
     2  
     3  package cfn
     4  
     5  import (
     6  	"context"
     7  	"io"
     8  	"os"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/defang-io/defang/src/pkg/clouds/aws/region"
    13  	"github.com/defang-io/defang/src/pkg/types"
    14  )
    15  
    16  func TestCloudFormation(t *testing.T) {
    17  	if testing.Short() {
    18  		t.Skip("skipping slow integration test")
    19  	}
    20  
    21  	retainBucket = false // delete bucket after test
    22  
    23  	user := os.Getenv("USER") // avoid conflict with other users in the same account
    24  	aws := New("crun-test-"+user, region.Region("us-west-2"))
    25  	if aws == nil {
    26  		t.Fatal("aws is nil")
    27  	}
    28  
    29  	ctx := context.Background()
    30  
    31  	t.Run("SetUp", func(t *testing.T) {
    32  		containers := []types.Container{{
    33  			Image:    "public.ecr.aws/docker/library/alpine:latest",
    34  			Memory:   512_000_000,
    35  			Platform: "linux/amd64",
    36  		}}
    37  		err := aws.SetUp(ctx, containers)
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  		if aws.BucketName == "" {
    42  			t.Error("bucket name is empty")
    43  		}
    44  	})
    45  
    46  	var taskid types.TaskID
    47  	t.Run("Run", func(t *testing.T) {
    48  		var err error
    49  		taskid, err = aws.Run(ctx, nil, "echo", "hello")
    50  		if err != nil {
    51  			t.Fatal(err)
    52  		}
    53  		if taskid == nil || *taskid == "" {
    54  			t.Error("task id is empty")
    55  		}
    56  	})
    57  
    58  	t.Run("Tail", func(t *testing.T) {
    59  		if taskid == nil {
    60  			t.Skip("task id is empty")
    61  		}
    62  		ctx, cancel := context.WithTimeout(ctx, time.Minute)
    63  		defer cancel()
    64  		err := aws.Tail(ctx, taskid)
    65  		if err != nil && err != io.EOF {
    66  			t.Fatal(err)
    67  		}
    68  	})
    69  
    70  	t.Run("Stop", func(t *testing.T) {
    71  		if taskid == nil {
    72  			t.Skip("task id is empty")
    73  		}
    74  		err := aws.Stop(ctx, taskid)
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  	})
    79  
    80  	t.Run("Teardown", func(t *testing.T) {
    81  		// This will fail if the task is still running
    82  		err := aws.TearDown(ctx)
    83  		if err != nil {
    84  			t.Fatal(err)
    85  		}
    86  	})
    87  }