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

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/defang-io/defang/src/pkg/types"
     9  	v1 "github.com/opencontainers/image-spec/specs-go/v1"
    10  )
    11  
    12  func TestRun(t *testing.T) {
    13  	if testing.Short() {
    14  		t.Skip("skipping test in short mode.")
    15  	}
    16  
    17  	d := New()
    18  
    19  	err := d.SetUp(context.Background(), []types.Container{{Image: "alpine:latest", Platform: d.platform}})
    20  
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer d.TearDown(context.Background())
    25  
    26  	id, err := d.Run(context.Background(), nil, "sh", "-c", "echo hello world")
    27  	if err != nil {
    28  		t.Fatal(err)
    29  	}
    30  	if id == nil || *id == "" {
    31  		t.Fatal("id is empty")
    32  	}
    33  
    34  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    35  	defer cancel()
    36  	err = d.Tail(ctx, id)
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  }
    41  
    42  func TestParsePlatform(t *testing.T) {
    43  	tdt := []struct {
    44  		platform string
    45  		expected v1.Platform
    46  	}{
    47  		{
    48  			platform: "linux/amd64",
    49  			expected: v1.Platform{
    50  				Architecture: "amd64",
    51  				OS:           "linux",
    52  			},
    53  		},
    54  		{
    55  			platform: "linux/arm64/v8",
    56  			expected: v1.Platform{
    57  				Architecture: "arm64",
    58  				OS:           "linux",
    59  				Variant:      "v8",
    60  			},
    61  		},
    62  		{
    63  			platform: "linux/arm64",
    64  			expected: v1.Platform{
    65  				Architecture: "arm64",
    66  				OS:           "linux",
    67  			},
    68  		},
    69  		{
    70  			platform: "arm64",
    71  			expected: v1.Platform{
    72  				Architecture: "arm64",
    73  			},
    74  		},
    75  	}
    76  	for _, tt := range tdt {
    77  		t.Run(tt.platform, func(t *testing.T) {
    78  			p := parsePlatform(tt.platform)
    79  			if p.Architecture != tt.expected.Architecture {
    80  				t.Errorf("expected architecture %q, got %q", tt.expected.Architecture, p.Architecture)
    81  			}
    82  			if p.OS != tt.expected.OS {
    83  				t.Errorf("expected OS %q, got %q", tt.expected.OS, p.OS)
    84  			}
    85  			if p.Variant != tt.expected.Variant {
    86  				t.Errorf("expected variant %q, got %q", tt.expected.Variant, p.Variant)
    87  			}
    88  		})
    89  	}
    90  }