Skip to content

Commit 712c92f

Browse files
committed
Start adding integration tests
1 parent 9da9735 commit 712c92f

File tree

146 files changed

+14564
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

146 files changed

+14564
-0
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
1010
github.com/oklog/run v1.1.0
1111
github.com/prometheus/client_golang v1.19.0
12+
github.com/stretchr/testify v1.8.4
1213
github.com/xanzy/go-gitlab v0.102.0
1314
go.opentelemetry.io/proto/otlp v1.2.0
1415
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
@@ -21,11 +22,13 @@ require (
2122
require (
2223
github.com/beorn7/perks v1.0.1 // indirect
2324
github.com/cespare/xxhash/v2 v2.3.0 // indirect
25+
github.com/davecgh/go-spew v1.1.1 // indirect
2426
github.com/google/go-querystring v1.1.0 // indirect
2527
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
2628
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
2729
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
2830
github.com/kr/text v0.2.0 // indirect
31+
github.com/pmezard/go-difflib v1.0.0 // indirect
2932
github.com/prometheus/client_model v0.6.1 // indirect
3033
github.com/prometheus/common v0.52.3 // indirect
3134
github.com/prometheus/procfs v0.13.0 // indirect

test/integration/conftest.go

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package integration_test
2+
3+
import (
4+
"context"
5+
"net"
6+
"net/http"
7+
"net/http/httptest"
8+
"testing"
9+
10+
"github.com/cluttrdev/gitlab-exporter/internal/exporter"
11+
"github.com/cluttrdev/gitlab-exporter/internal/gitlab"
12+
"google.golang.org/grpc"
13+
"google.golang.org/grpc/credentials/insecure"
14+
"google.golang.org/grpc/test/bufconn"
15+
16+
"github.com/cluttrdev/gitlab-exporter/test/mock/recorder"
17+
)
18+
19+
func setupGitLab(t *testing.T) (*http.ServeMux, *gitlab.Client) {
20+
mux := http.NewServeMux()
21+
srv := httptest.NewServer(mux)
22+
t.Cleanup(srv.Close)
23+
24+
client, err := gitlab.NewGitLabClient(gitlab.ClientConfig{
25+
URL: srv.URL,
26+
})
27+
if err != nil {
28+
t.Fatalf("failed to create client: %v", err)
29+
}
30+
31+
return mux, client
32+
}
33+
34+
func setupExporter(t *testing.T) (*exporter.Exporter, *recorder_mock.Recorder) {
35+
rec := recorder_mock.New()
36+
t.Cleanup(rec.GracefulStop)
37+
38+
const bufSize int = 4 * 1024 * 1024
39+
lis := bufconn.Listen(bufSize)
40+
go func() {
41+
if err := rec.Serve(lis); err != nil {
42+
t.Log(err)
43+
}
44+
}()
45+
46+
exp, err := exporter.New([]exporter.EndpointConfig{
47+
{
48+
Address: "bufnet",
49+
Options: []grpc.DialOption{
50+
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
51+
return lis.Dial()
52+
}),
53+
grpc.WithTransportCredentials(insecure.NewCredentials()),
54+
},
55+
},
56+
})
57+
if err != nil {
58+
t.Fatalf("failed to create exporter: %v", err)
59+
}
60+
61+
return exp, rec
62+
}

test/integration/main_test.go

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package integration_test
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"net/http"
7+
"net/http/httptest"
8+
"os"
9+
"strings"
10+
"testing"
11+
12+
gitlab_mock "github.com/cluttrdev/gitlab-exporter/test/mock/gitlab"
13+
)
14+
15+
const testSet string = "default"
16+
17+
func TestMain(m *testing.M) {
18+
fh := &gitlab_mock.FileHandler{Root: http.Dir("testdata/gitlab.com")}
19+
srv := httptest.NewServer(fh)
20+
defer srv.Close()
21+
22+
testEnv := GitLabExporterTestEnvironment{
23+
URL: srv.URL,
24+
}
25+
26+
SetTestEnvironment(testSet, testEnv)
27+
28+
os.Exit(m.Run())
29+
}
30+
31+
type GitLabExporterTestEnvironment struct {
32+
URL string
33+
}
34+
35+
func SetTestEnvironment(name string, env GitLabExporterTestEnvironment) {
36+
bytes, err := json.Marshal(env)
37+
if err != nil {
38+
panic(err)
39+
}
40+
41+
os.Setenv(fmt.Sprintf("GLE_TEST_ENV_%s", strings.ToUpper(testSet)), string(bytes))
42+
}
43+
44+
func GetTestEnvironment(name string) (GitLabExporterTestEnvironment, error) {
45+
sEnv := os.Getenv(fmt.Sprintf("GLE_TEST_ENV_%s", strings.ToUpper(testSet)))
46+
if sEnv == "" {
47+
return GitLabExporterTestEnvironment{}, fmt.Errorf("environment not found: %s", name)
48+
}
49+
50+
var env GitLabExporterTestEnvironment
51+
if err := json.Unmarshal([]byte(sEnv), &env); err != nil {
52+
return GitLabExporterTestEnvironment{}, err
53+
}
54+
55+
return env, nil
56+
}

test/integration/tasks_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package integration_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
10+
"github.com/cluttrdev/gitlab-exporter/internal/gitlab"
11+
"github.com/cluttrdev/gitlab-exporter/internal/tasks"
12+
)
13+
14+
func Test_ExportPipelineHierarchy(t *testing.T) {
15+
env, err := GetTestEnvironment(testSet)
16+
if err != nil {
17+
t.Error(err)
18+
}
19+
20+
glc, err := gitlab.NewGitLabClient(gitlab.ClientConfig{
21+
URL: fmt.Sprintf("%s/api/v4", env.URL),
22+
})
23+
if err != nil {
24+
t.Fatalf("failed to create client: %v", err)
25+
}
26+
27+
exp, rec := setupExporter(t)
28+
29+
var (
30+
projectID int64 = 50817395 // cluttrdev/gitlab-exporter
31+
pipelineID int64 = 1252248442
32+
)
33+
34+
opts := tasks.ExportPipelineHierarchyOptions{
35+
ProjectID: projectID,
36+
PipelineID: pipelineID,
37+
38+
ExportSections: false,
39+
ExportTestReports: true,
40+
ExportTraces: true,
41+
ExportMetrics: false,
42+
}
43+
44+
if err := tasks.ExportPipelineHierarchy(context.Background(), glc, exp, opts); err != nil {
45+
t.Error(err)
46+
}
47+
48+
assert.Equal(t, 1, len(rec.Datastore().ListProjectPipelines(projectID)))
49+
50+
p := rec.Datastore().GetPipeline(pipelineID)
51+
if p == nil {
52+
t.Fatalf("pipeline not recorded: %v", pipelineID)
53+
}
54+
55+
assert.Equal(t, 4, len(rec.Datastore().ListPipelineJobs(projectID, pipelineID)))
56+
assert.Equal(t, int64(13), rec.Datastore().GetPipelineTestReport(pipelineID).TotalCount)
57+
}

0 commit comments

Comments
 (0)