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

     1  package cli
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"slices"
     9  	"testing"
    10  
    11  	"github.com/defang-io/defang/src/pkg/cli/client"
    12  	defangv1 "github.com/defang-io/defang/src/protos/io/defang/v1"
    13  )
    14  
    15  func TestComposeStart(t *testing.T) {
    16  	DoDryRun = true
    17  	defer func() { DoDryRun = false }()
    18  
    19  	loader := ComposeLoader{"../../tests/testproj/compose.yaml"}
    20  	proj, err := loader.LoadWithProjectName("tenant-id")
    21  	if err != nil {
    22  		t.Fatalf("LoadComposeWithProjectName() failed: %v", err)
    23  	}
    24  
    25  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    26  		w.WriteHeader(http.StatusNoContent)
    27  	}))
    28  	defer server.Close()
    29  
    30  	_, err = ComposeStart(context.Background(), client.MockClient{UploadUrl: server.URL + "/", Project: proj}, false)
    31  	if !errors.Is(err, ErrDryRun) {
    32  		t.Fatalf("ComposeStart() failed: %v", err)
    33  	}
    34  }
    35  
    36  func TestComposeFixupEnv(t *testing.T) {
    37  	loader := ComposeLoader{"../../tests/fixupenv/compose.yaml"}
    38  	proj, err := loader.LoadWithProjectName("tenant-id")
    39  	if err != nil {
    40  		t.Fatalf("LoadComposeWithProjectName() failed: %v", err)
    41  	}
    42  
    43  	services, err := convertServices(context.Background(), client.MockClient{}, proj.Services, false)
    44  	if err != nil {
    45  		t.Fatalf("convertServices() failed: %v", err)
    46  	}
    47  	ui := slices.IndexFunc(services, func(s *defangv1.Service) bool { return s.Name == "ui" })
    48  	if ui < 0 {
    49  		t.Fatalf("convertServices() failed: expected service named 'ui'")
    50  	}
    51  
    52  	const expected = "http://mistral:8000"
    53  	got := services[ui].Environment["API_URL"]
    54  	if got != expected {
    55  		t.Errorf("convertServices() failed: expected API_URL=%s, got %s", expected, got)
    56  	}
    57  
    58  	const sensitiveKey = "SENSITIVE_DATA"
    59  	_, ok := services[ui].Environment[sensitiveKey]
    60  	if ok {
    61  		t.Errorf("convertServices() failed: , %s found in environment map but should not be.", sensitiveKey)
    62  	}
    63  
    64  	found := false
    65  	for _, value := range services[ui].Secrets {
    66  		if value.Source == sensitiveKey {
    67  			found = true
    68  			break
    69  		}
    70  	}
    71  
    72  	if !found {
    73  		t.Errorf("convertServices() failed: unable to find sensitive config variable %s", sensitiveKey)
    74  	}
    75  }