|
| 1 | +package cli |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http/httptest" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + |
| 9 | + cliClient "github.com/DefangLabs/defang/src/pkg/cli/client" |
| 10 | + defangv1 "github.com/DefangLabs/defang/src/protos/io/defang/v1" |
| 11 | + "github.com/DefangLabs/defang/src/protos/io/defang/v1/defangv1connect" |
| 12 | + "github.com/bufbuild/connect-go" |
| 13 | + compose "github.com/compose-spec/compose-go/v2/types" |
| 14 | + "google.golang.org/protobuf/types/known/emptypb" |
| 15 | +) |
| 16 | + |
| 17 | +type grpcDestroyMockHandler struct { |
| 18 | + defangv1connect.UnimplementedFabricControllerHandler |
| 19 | +} |
| 20 | + |
| 21 | +func (g *grpcDestroyMockHandler) Delete(context.Context, *connect.Request[defangv1.DeleteRequest]) (*connect.Response[defangv1.DeleteResponse], error) { |
| 22 | + return connect.NewResponse(&defangv1.DeleteResponse{ |
| 23 | + Etag: "test-etag", |
| 24 | + }), nil |
| 25 | +} |
| 26 | + |
| 27 | +func (g *grpcDestroyMockHandler) GetServices(context.Context, *connect.Request[emptypb.Empty]) (*connect.Response[defangv1.ListServicesResponse], error) { |
| 28 | + return connect.NewResponse(&defangv1.ListServicesResponse{ |
| 29 | + Project: "tenantx", |
| 30 | + Services: []*defangv1.ServiceInfo{ |
| 31 | + { |
| 32 | + Service: &defangv1.Service{ |
| 33 | + Name: "test-service", |
| 34 | + }, |
| 35 | + }, |
| 36 | + }, |
| 37 | + }), nil |
| 38 | +} |
| 39 | + |
| 40 | +func TestDestroy(t *testing.T) { |
| 41 | + fabricServer := &grpcDestroyMockHandler{} |
| 42 | + _, handler := defangv1connect.NewFabricControllerHandler(fabricServer) |
| 43 | + |
| 44 | + server := httptest.NewServer(handler) |
| 45 | + defer server.Close() |
| 46 | + |
| 47 | + ctx := context.Background() |
| 48 | + url := strings.TrimPrefix(server.URL, "http://") |
| 49 | + loader := FakeLoader{ProjectName: "test-project"} |
| 50 | + grpcClient := Connect(url, loader) |
| 51 | + client := cliClient.PlaygroundClient{GrpcClient: grpcClient} |
| 52 | + |
| 53 | + etag, err := client.Destroy(ctx) |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
| 57 | + |
| 58 | + if etag != "test-etag" { |
| 59 | + t.Fatalf("expected etag %q, got %q", "test-etag", etag) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +type FakeLoader struct { |
| 64 | + ProjectName string |
| 65 | +} |
| 66 | + |
| 67 | +func (f FakeLoader) LoadProject(ctx context.Context) (*compose.Project, error) { |
| 68 | + return &compose.Project{Name: f.ProjectName}, nil |
| 69 | +} |
| 70 | + |
| 71 | +func (f FakeLoader) LoadProjectName(ctx context.Context) (string, error) { |
| 72 | + return f.ProjectName, nil |
| 73 | +} |
0 commit comments