github.com/kubeshop/testkube@v1.17.23/pkg/repository/result/mongo_output.go (about) 1 package result 2 3 import ( 4 "context" 5 "io" 6 "strings" 7 8 "go.mongodb.org/mongo-driver/bson" 9 "go.mongodb.org/mongo-driver/mongo" 10 ) 11 12 type ExecutionOutput struct { 13 Id string `json:"id"` 14 Name string `json:"name"` 15 TestName string `json:"testname,omitempty"` 16 TestSuiteName string `json:"testsuitename,omitempty"` 17 Output string `json:"output"` 18 } 19 20 const CollectionOutput = "output" 21 22 type MongoOutputRepository struct { 23 Coll *mongo.Collection 24 } 25 26 var _ OutputRepository = (*MongoOutputRepository)(nil) 27 28 func NewMongoOutputRepository(db *mongo.Database, opts ...MongoOutputRepositoryOpt) *MongoOutputRepository { 29 r := &MongoOutputRepository{ 30 Coll: db.Collection(CollectionOutput), 31 } 32 33 for _, opt := range opts { 34 opt(r) 35 } 36 37 return r 38 } 39 40 type MongoOutputRepositoryOpt func(*MongoOutputRepository) 41 42 func WithMongoOutputRepositoryCollection(collection *mongo.Collection) MongoOutputRepositoryOpt { 43 return func(r *MongoOutputRepository) { 44 r.Coll = collection 45 } 46 } 47 48 func (r *MongoOutputRepository) GetOutput(ctx context.Context, id, testName, testSuiteName string) (output string, err error) { 49 var eOutput ExecutionOutput 50 err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": id}, bson.M{"name": id}}}).Decode(&eOutput) 51 return eOutput.Output, err 52 } 53 54 func (r *MongoOutputRepository) InsertOutput(ctx context.Context, id, testName, testSuiteName, output string) error { 55 _, err := r.Coll.InsertOne(ctx, ExecutionOutput{Id: id, Name: id, TestName: testName, TestSuiteName: testSuiteName, Output: output}) 56 return err 57 } 58 59 func (r *MongoOutputRepository) UpdateOutput(ctx context.Context, id, testName, testSuiteName, output string) error { 60 _, err := r.Coll.UpdateOne(ctx, bson.M{"$or": bson.A{bson.M{"id": id}, bson.M{"name": id}}}, bson.M{"$set": bson.M{"output": output}}) 61 return err 62 } 63 64 func (r *MongoOutputRepository) DeleteOutput(ctx context.Context, id, testName, testSuiteName string) error { 65 _, err := r.Coll.DeleteOne(ctx, bson.M{"$or": bson.A{bson.M{"id": id}, bson.M{"name": id}}}) 66 return err 67 } 68 69 func (r *MongoOutputRepository) DeleteOutputByTest(ctx context.Context, testName string) error { 70 _, err := r.Coll.DeleteMany(ctx, bson.M{"testname": testName}) 71 return err 72 } 73 74 func (r *MongoOutputRepository) DeleteOutputForTests(ctx context.Context, testNames []string) error { 75 _, err := r.Coll.DeleteMany(ctx, bson.M{"testname": bson.M{"$in": testNames}}) 76 return err 77 } 78 79 func (r *MongoOutputRepository) DeleteOutputByTestSuite(ctx context.Context, testSuiteName string) error { 80 _, err := r.Coll.DeleteMany(ctx, bson.M{"testsuitename": testSuiteName}) 81 return err 82 } 83 84 func (r *MongoOutputRepository) DeleteOutputForTestSuites(ctx context.Context, testSuiteNames []string) error { 85 _, err := r.Coll.DeleteMany(ctx, bson.M{"testsuitename": bson.M{"$in": testSuiteNames}}) 86 return err 87 } 88 89 func (r *MongoOutputRepository) DeleteOutputForAllTestSuite(ctx context.Context) error { 90 _, err := r.Coll.DeleteMany(ctx, bson.M{"testsuitename": bson.M{"$ne": ""}}) 91 return err 92 } 93 94 func (r *MongoOutputRepository) DeleteAllOutput(ctx context.Context) error { 95 _, err := r.Coll.DeleteMany(ctx, bson.M{}) 96 return err 97 } 98 99 func (r *MongoOutputRepository) StreamOutput(ctx context.Context, executionID, testName, testSuiteName string) (reader io.Reader, err error) { 100 var eOutput ExecutionOutput 101 err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": executionID}, bson.M{"name": executionID}}}).Decode(&eOutput) 102 return strings.NewReader(eOutput.Output), err 103 } 104 105 func (r *MongoOutputRepository) GetOutputSize(ctx context.Context, executionID, testName, testSuiteName string) (size int, err error) { 106 var eOutput ExecutionOutput 107 err = r.Coll.FindOne(ctx, bson.M{"$or": bson.A{bson.M{"id": executionID}, bson.M{"name": executionID}}}).Decode(&eOutput) 108 return len(eOutput.Output), err 109 }