Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add output stream functions and mocks #4431

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions pkg/repository/result/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package result

import (
"context"
"io"
"time"

"github.com/kubeshop/testkube/pkg/api/v1/testkube"
Expand Down Expand Up @@ -77,6 +78,7 @@ type Sequences interface {
GetNextExecutionNumber(ctx context.Context, testName string) (number int32, err error)
}

//go:generate mockgen -destination=./mock_output_repository.go -package=result "github.com/kubeshop/testkube/pkg/repository/result" OutputRepository
type OutputRepository interface {
// GetOutput gets execution output by id or name
GetOutput(ctx context.Context, id, testName, testSuiteName string) (output string, err error)
Expand All @@ -98,4 +100,8 @@ type OutputRepository interface {
DeleteAllOutput(ctx context.Context) error
// DeleteOutputForAllTestSuite deletes all execution output for test suite
DeleteOutputForAllTestSuite(ctx context.Context) error
// StreamOutput streams execution output by id or name
StreamOutput(ctx context.Context, executionID, testName, testSuiteName string) (reader io.Reader, err error)
// GetOutputSize gets execution output metadata by id or name
GetOutputSize(ctx context.Context, executionID, testName, testSuiteName string) (size int, err error)
}
15 changes: 15 additions & 0 deletions pkg/repository/result/minio_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand All @@ -15,6 +16,8 @@ import (
"github.com/kubeshop/testkube/pkg/storage/minio"
)

var _ OutputRepository = (*MinioRepository)(nil)

type MinioRepository struct {
storage storage.Client
executionCollection *mongo.Collection
Expand Down Expand Up @@ -175,3 +178,15 @@ func (m *MinioRepository) DeleteAllOutput(ctx context.Context) error {
}
return m.storage.CreateBucket(ctx, m.bucket)
}

func (m *MinioRepository) StreamOutput(ctx context.Context, executionID, testName, testSuiteName string) (reader io.Reader, err error) {
file, err := m.storage.DownloadFileFromBucket(ctx, m.bucket, "", executionID)
if err != nil {
return nil, err
}
return file, nil
}

func (m *MinioRepository) GetOutputSize(ctx context.Context, executionID, testName, testSuiteName string) (size int, err error) {
return 0, fmt.Errorf("not implemented")
nicufk marked this conversation as resolved.
Show resolved Hide resolved
}
207 changes: 207 additions & 0 deletions pkg/repository/result/mock_output_repository.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions pkg/repository/result/mongo_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package result

import (
"context"
"io"
"strings"

"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
Expand Down Expand Up @@ -93,3 +95,15 @@ func (r *MongoOutputRepository) DeleteAllOutput(ctx context.Context) error {
_, err := r.Coll.DeleteMany(ctx, bson.M{})
return err
}

func (r *MongoOutputRepository) StreamOutput(ctx context.Context, executionID, testName, testSuiteName string) (reader io.Reader, err error) {
var eOutput ExecutionOutput
err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": executionID}, bson.M{"name": executionID}}}).Decode(&eOutput)
return strings.NewReader(eOutput.Output), err
}

func (r *MongoOutputRepository) GetOutputSize(ctx context.Context, executionID, testName, testSuiteName string) (size int, err error) {
var eOutput ExecutionOutput
err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": executionID}, bson.M{"name": executionID}}}).Decode(&eOutput)
return len(eOutput.Output), err
}
Loading