Skip to content

Commit

Permalink
Add gitlab-exporter recorder mock
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed May 16, 2024
1 parent 294b529 commit 9da9735
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 0 deletions.
66 changes: 66 additions & 0 deletions test/mock/recorder/datastore.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package recorder_mock

import (
"github.com/cluttrdev/gitlab-exporter/protobuf/typespb"
)

type Datastore interface {
ListProjectPipelines(projectID int64) []*typespb.Pipeline
GetPipeline(id int64) *typespb.Pipeline

ListPipelineJobs(projectID int64, pipelineID int64) []*typespb.Job

GetPipelineTestReport(pipelineID int64) *typespb.TestReport
}

type datastore struct {
pipelines []*typespb.Pipeline
jobs []*typespb.Job
sections []*typespb.Section
bridges []*typespb.Bridge

testreports []*typespb.TestReport
testsuites []*typespb.TestSuite
testcases []*typespb.TestCase

traces []*typespb.Trace
metrics []*typespb.Metric
}

func (d *datastore) ListProjectPipelines(projectID int64) []*typespb.Pipeline {
var ps []*typespb.Pipeline
for _, p := range d.pipelines {
if p.ProjectId == projectID {
ps = append(ps, p)
}
}
return ps
}

func (d *datastore) GetPipeline(id int64) *typespb.Pipeline {
for _, p := range d.pipelines {
if p.GetId() == id {
return p
}
}
return nil
}

func (d *datastore) ListPipelineJobs(projectID int64, pipelineID int64) []*typespb.Job {
var js []*typespb.Job
for _, j := range d.jobs {
if j.Pipeline.ProjectId == projectID && j.Pipeline.Id == pipelineID {
js = append(js, j)
}
}
return js
}

func (d *datastore) GetPipelineTestReport(pipelineID int64) *typespb.TestReport {
for _, tr := range d.testreports {
if tr.PipelineId == pipelineID {
return tr
}
}
return nil
}
99 changes: 99 additions & 0 deletions test/mock/recorder/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package recorder_mock

import (
"context"
"net"

"google.golang.org/grpc"

"github.com/cluttrdev/gitlab-exporter/protobuf/servicepb"
)

type Recorder struct {
servicepb.UnimplementedGitLabExporterServer

server *grpc.Server
datastore *datastore
}

func New() *Recorder {
return &Recorder{
server: grpc.NewServer(),
datastore: &datastore{},
}
}

func (s *Recorder) Datastore() Datastore {
return s.datastore
}

func (r *Recorder) Reset() {
r.datastore = &datastore{}
}

func (r *Recorder) Serve(lis net.Listener) error {
servicepb.RegisterGitLabExporterServer(r.server, r)
return r.server.Serve(lis)
}

func (r *Recorder) GracefulStop() {
r.server.GracefulStop()
}

func (r *Recorder) Stop() {
r.server.Stop()
}

func (r *Recorder) RecordPipelines(ctx context.Context, req *servicepb.RecordPipelinesRequest) (*servicepb.RecordSummary, error) {
r.datastore.pipelines = append(r.datastore.pipelines, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordJobs(ctx context.Context, req *servicepb.RecordJobsRequest) (*servicepb.RecordSummary, error) {
r.datastore.jobs = append(r.datastore.jobs, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordSections(ctx context.Context, req *servicepb.RecordSectionsRequest) (*servicepb.RecordSummary, error) {
r.datastore.sections = append(r.datastore.sections, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordBridges(ctx context.Context, req *servicepb.RecordBridgesRequest) (*servicepb.RecordSummary, error) {
r.datastore.bridges = append(r.datastore.bridges, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordTraces(ctx context.Context, req *servicepb.RecordTracesRequest) (*servicepb.RecordSummary, error) {
r.datastore.traces = append(r.datastore.traces, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordMetrics(ctx context.Context, req *servicepb.RecordMetricsRequest) (*servicepb.RecordSummary, error) {
r.datastore.metrics = append(r.datastore.metrics, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordTestReports(ctx context.Context, req *servicepb.RecordTestReportsRequest) (*servicepb.RecordSummary, error) {
r.datastore.testreports = append(r.datastore.testreports, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordTestSuites(ctx context.Context, req *servicepb.RecordTestSuitesRequest) (*servicepb.RecordSummary, error) {
r.datastore.testsuites = append(r.datastore.testsuites, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

func (r *Recorder) RecordTestCases(ctx context.Context, req *servicepb.RecordTestCasesRequest) (*servicepb.RecordSummary, error) {
r.datastore.testcases = append(r.datastore.testcases, req.Data...)
var count int32 = int32(len(req.Data))
return &servicepb.RecordSummary{RecordedCount: count}, nil
}

0 comments on commit 9da9735

Please sign in to comment.