diff --git a/cmd/testops/result/upload/upload.go b/cmd/testops/result/upload/upload.go index 8bee341..bf3cfde 100644 --- a/cmd/testops/result/upload/upload.go +++ b/cmd/testops/result/upload/upload.go @@ -80,7 +80,10 @@ func Command() *cobra.Command { Suite: suite, } - s.Upload(cmd.Context(), param) + err := s.Upload(cmd.Context(), param) + if err != nil { + return err + } logger.Info("Results uploaded successfully") diff --git a/internal/parsers/junit/junit_test.go b/internal/parsers/junit/junit_test.go new file mode 100644 index 0000000..770abab --- /dev/null +++ b/internal/parsers/junit/junit_test.go @@ -0,0 +1 @@ +package junit diff --git a/internal/service/result/result.go b/internal/service/result/result.go index 1cbcf1c..bd06946 100644 --- a/internal/service/result/result.go +++ b/internal/service/result/result.go @@ -2,6 +2,7 @@ package result import ( "context" + "fmt" models "github.com/qase-tms/qasectl/internal/models/result" "log/slog" ) @@ -35,29 +36,27 @@ func NewService(client client, parser Parser, rs runService) *Service { } // Upload imports the data -func (s *Service) Upload(ctx context.Context, p UploadParams) { +func (s *Service) Upload(ctx context.Context, p UploadParams) error { const op = "result.parser.import" logger := slog.With("op", op) results, err := s.parser.Parse() if err != nil { - logger.Error("failed to parse results", "error", err) - return + return fmt.Errorf("failed to parse results: %w", err) } logger.Info("number of results found", "count", len(results)) if len(results) == 0 { - logger.Info("no results to upload") - return + return fmt.Errorf("no results to upload") } + runID := p.RunID isTestRunCreated := false if runID == 0 { ID, err := s.rs.CreateRun(ctx, p.Project, p.Title, p.Description, "", 0, 0) if err != nil { - logger.Error("failed to create run", "error", err) - return + return err } runID = ID isTestRunCreated = true @@ -78,7 +77,7 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) { if int64(len(results)) < p.Batch { err := s.client.UploadData(ctx, p.Project, runID, results) if err != nil { - logger.Error("failed to upload results", "error", err) + return fmt.Errorf("failed to upload results: %w", err) } } else { for i := int64(0); i < int64(len(results)); i += p.Batch { @@ -90,7 +89,7 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) { err := s.client.UploadData(ctx, p.Project, runID, results[i:end]) if err != nil { - logger.Error("failed to upload results", "error", err) + return fmt.Errorf("failed to upload results: %w", err) } } } @@ -98,7 +97,9 @@ func (s *Service) Upload(ctx context.Context, p UploadParams) { if isTestRunCreated { err := s.rs.CompleteRun(ctx, p.Project, runID) if err != nil { - logger.Error("failed to complete run", "error", err) + return err } } + + return nil } diff --git a/internal/service/result/result_test.go b/internal/service/result/result_test.go index 7ce6872..f84be78 100644 --- a/internal/service/result/result_test.go +++ b/internal/service/result/result_test.go @@ -168,8 +168,8 @@ func TestService_Upload(t *testing.T) { err: nil, isUsed: false, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "failed to parse results: failed parser", }, { name: "empty results", @@ -201,8 +201,8 @@ func TestService_Upload(t *testing.T) { err: nil, isUsed: false, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "no results to upload", }, { name: "failed create test run", @@ -234,8 +234,8 @@ func TestService_Upload(t *testing.T) { err: nil, isUsed: false, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "failed create test run", }, { name: "use batch", @@ -283,7 +283,7 @@ func TestService_Upload(t *testing.T) { }, err: errors.New("failed upload data"), isUsed: true, - count: 2, + count: 1, runID: 1, }, pArgs: pArgs{ @@ -298,10 +298,10 @@ func TestService_Upload(t *testing.T) { }, cArgs: cArgs{ err: nil, - isUsed: true, + isUsed: false, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "failed to upload results: failed upload data", }, { name: "failed upload data", @@ -331,10 +331,10 @@ func TestService_Upload(t *testing.T) { }, cArgs: cArgs{ err: nil, - isUsed: true, + isUsed: false, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "failed to upload results: failed upload data", }, { name: "failed complete run", @@ -366,8 +366,8 @@ func TestService_Upload(t *testing.T) { err: errors.New("failed complete run"), isUsed: true, }, - wantErr: false, - errMessage: "", + wantErr: true, + errMessage: "failed complete run", }, } for _, tt := range tests { @@ -387,15 +387,17 @@ func TestService_Upload(t *testing.T) { } if tt.args.isUsed { - if tt.args.count != 1 { - f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, gomock.Any()).Return(tt.args.err).Times(tt.args.count) - } else { - f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, tt.pArgs.models).Return(tt.args.err).Times(tt.args.count) - } + f.client.EXPECT().UploadData(gomock.Any(), tt.args.p.Project, tt.args.runID, gomock.Any()).Return(tt.args.err).Times(tt.args.count) } s := NewService(f.client, f.parser, f.rs) - s.Upload(context.Background(), tt.args.p) + err := s.Upload(context.Background(), tt.args.p) + if (err != nil) != tt.wantErr { + t.Errorf("Service.Upload() error = %v, wantErr %v", err, tt.wantErr) + } + if err != nil && err.Error() != tt.errMessage { + t.Errorf("Service.Upload() error = %v, wantErr %v", err, tt.errMessage) + } }) } }