Path: blob/main/components/content-service/pkg/service/headless-log-service_test.go
2499 views
// Copyright (c) 2020 Gitpod GmbH. All rights reserved.1// Licensed under the GNU Affero General Public License (AGPL).2// See License.AGPL.txt in the project root for license information.34package service56import (7"context"8"fmt"9"testing"1011"github.com/golang/mock/gomock"12"github.com/google/go-cmp/cmp"1314"github.com/gitpod-io/gitpod/content-service/api"15"github.com/gitpod-io/gitpod/content-service/api/config"16"github.com/gitpod-io/gitpod/content-service/pkg/storage"17storagemock "github.com/gitpod-io/gitpod/content-service/pkg/storage/mock"18)1920func TestListLogs(t *testing.T) {21cfg := config.StorageConfig{22Stage: config.StageProduction,23Kind: config.GCloudStorage, // dummy, mocked away24BlobQuota: 1073741824, // 1Gi25}2627OwnerId := "1234"28WorkspaceId := "amber-baboon-cij4wozf"29InstanceId := "958aff1c-849a-460f-8af4-5c5b1401a599"30logFile := func(name string) string {31return fmt.Sprintf("workspace/%s/instance/%s/logs/%s", WorkspaceId, InstanceId, name)32}33tests := []struct {34Name string35Files []string36ExpectedTaskIds []string37}{38{39Name: "one instance, three tasks",40Files: []string{41logFile("1"),42logFile("3"),43logFile("4"),44},45ExpectedTaskIds: []string{"1", "3", "4"},46},47}4849for _, test := range tests {50t.Run(test.Name, func(t *testing.T) {51ctrl := gomock.NewController(t)52defer ctrl.Finish()5354s := storagemock.NewMockPresignedAccess(ctrl)55da := storagemock.NewMockDirectAccess(ctrl)56daFactory := func(cfg *config.StorageConfig) (storage.DirectAccess, error) {57return da, nil58}59svc := HeadlessLogService{60cfg: cfg,61s: s,62daFactory: daFactory,63}6465s.EXPECT().InstanceObject(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).66Return("")67da.EXPECT().Init(gomock.Any(), gomock.Eq(OwnerId), gomock.Eq(WorkspaceId), gomock.Not(gomock.Eq(""))).68Times(1)69da.EXPECT().ListObjects(gomock.Any(), gomock.Any()).Return(test.Files, nil)7071ctx, cancel := context.WithCancel(context.Background())72defer cancel()7374req := &api.ListLogsRequest{75OwnerId: OwnerId,76WorkspaceId: WorkspaceId,77InstanceId: InstanceId,78}79resp, err := svc.ListLogs(ctx, req)80if err != nil {81t.Fatalf("ListLogs err: %v", err)82}83if diff := cmp.Diff(test.ExpectedTaskIds, resp.TaskId); diff != "" {84t.Errorf("unexpected TaskIds (-want +got):\n%s", diff)85}86})87}88}899091