github.com/kubeshop/testkube@v1.17.23/pkg/logs/logsserver_test.go (about) 1 package logs 2 3 import ( 4 "context" 5 "fmt" 6 "testing" 7 "time" 8 9 "github.com/stretchr/testify/assert" 10 11 "github.com/kubeshop/testkube/pkg/logs/client" 12 "github.com/kubeshop/testkube/pkg/logs/events" 13 "github.com/kubeshop/testkube/pkg/logs/repository" 14 "github.com/kubeshop/testkube/pkg/logs/state" 15 ) 16 17 const count = 10 18 19 func TestGRPC_Server(t *testing.T) { 20 // Contact the server and print out its response. 21 ctx, cancel := context.WithTimeout(context.Background(), time.Second) 22 defer cancel() 23 24 state := &StateMock{state: state.LogStatePending} 25 26 ls := NewLogsService(nil, nil, state, nil). 27 WithLogsRepositoryFactory(LogsFactoryMock{}). 28 WithRandomPort() 29 30 go ls.RunGRPCServer(ctx, nil) 31 32 // allow server to splin up 33 time.Sleep(time.Millisecond * 100) 34 35 expectedCount := 0 36 37 stream := client.NewGrpcClient(ls.grpcAddress, nil) 38 ch, err := stream.Get(ctx, "id1") 39 assert.NoError(t, err) 40 41 t.Log("waiting for logs") 42 43 for l := range ch { 44 t.Log(l) 45 expectedCount++ 46 } 47 48 assert.Equal(t, count, expectedCount) 49 } 50 51 type StateMock struct { 52 state state.LogState 53 } 54 55 func (s StateMock) Get(ctx context.Context, key string) (state.LogState, error) { 56 return s.state, nil 57 } 58 func (s *StateMock) Put(ctx context.Context, key string, state state.LogState) error { 59 s.state = state 60 return nil 61 } 62 63 type LogsFactoryMock struct { 64 } 65 66 func (l LogsFactoryMock) GetRepository(state state.LogState) (repository.LogsRepository, error) { 67 return LogsRepositoryMock{}, nil 68 } 69 70 type LogsRepositoryMock struct{} 71 72 func (l LogsRepositoryMock) Get(ctx context.Context, id string) (chan events.LogResponse, error) { 73 ch := make(chan events.LogResponse, 10) 74 defer close(ch) 75 76 for i := 0; i < count; i++ { 77 ch <- events.LogResponse{Log: events.Log{Time: time.Now(), Content: fmt.Sprintf("test %d", i), Error_: false, Type_: "test", Source: "test", Metadata: map[string]string{"test": "test"}}} 78 } 79 return ch, nil 80 }