Skip to content

Commit

Permalink
Start adding integration tests
Browse files Browse the repository at this point in the history
  • Loading branch information
cluttrdev committed May 16, 2024
1 parent 9da9735 commit 712c92f
Show file tree
Hide file tree
Showing 146 changed files with 14,564 additions and 0 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
github.com/grpc-ecosystem/go-grpc-middleware/providers/prometheus v1.0.0
github.com/oklog/run v1.1.0
github.com/prometheus/client_golang v1.19.0
github.com/stretchr/testify v1.8.4
github.com/xanzy/go-gitlab v0.102.0
go.opentelemetry.io/proto/otlp v1.2.0
golang.org/x/exp v0.0.0-20240409090435-93d18d7e34b8
Expand All @@ -21,11 +22,13 @@ require (
require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/grpc-ecosystem/go-grpc-middleware/v2 v2.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.5 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.6.1 // indirect
github.com/prometheus/common v0.52.3 // indirect
github.com/prometheus/procfs v0.13.0 // indirect
Expand Down
62 changes: 62 additions & 0 deletions test/integration/conftest.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package integration_test

import (
"context"
"net"
"net/http"
"net/http/httptest"
"testing"

"github.com/cluttrdev/gitlab-exporter/internal/exporter"
"github.com/cluttrdev/gitlab-exporter/internal/gitlab"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/test/bufconn"

"github.com/cluttrdev/gitlab-exporter/test/mock/recorder"
)

func setupGitLab(t *testing.T) (*http.ServeMux, *gitlab.Client) {
mux := http.NewServeMux()
srv := httptest.NewServer(mux)
t.Cleanup(srv.Close)

client, err := gitlab.NewGitLabClient(gitlab.ClientConfig{
URL: srv.URL,
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}

return mux, client
}

func setupExporter(t *testing.T) (*exporter.Exporter, *recorder_mock.Recorder) {
rec := recorder_mock.New()
t.Cleanup(rec.GracefulStop)

const bufSize int = 4 * 1024 * 1024
lis := bufconn.Listen(bufSize)
go func() {
if err := rec.Serve(lis); err != nil {
t.Log(err)
}
}()

exp, err := exporter.New([]exporter.EndpointConfig{
{
Address: "bufnet",
Options: []grpc.DialOption{
grpc.WithContextDialer(func(context.Context, string) (net.Conn, error) {
return lis.Dial()
}),
grpc.WithTransportCredentials(insecure.NewCredentials()),
},
},
})
if err != nil {
t.Fatalf("failed to create exporter: %v", err)
}

return exp, rec
}
56 changes: 56 additions & 0 deletions test/integration/main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package integration_test

import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"os"
"strings"
"testing"

gitlab_mock "github.com/cluttrdev/gitlab-exporter/test/mock/gitlab"
)

const testSet string = "default"

func TestMain(m *testing.M) {
fh := &gitlab_mock.FileHandler{Root: http.Dir("testdata/gitlab.com")}
srv := httptest.NewServer(fh)
defer srv.Close()

testEnv := GitLabExporterTestEnvironment{
URL: srv.URL,
}

SetTestEnvironment(testSet, testEnv)

os.Exit(m.Run())
}

type GitLabExporterTestEnvironment struct {
URL string
}

func SetTestEnvironment(name string, env GitLabExporterTestEnvironment) {
bytes, err := json.Marshal(env)
if err != nil {
panic(err)
}

os.Setenv(fmt.Sprintf("GLE_TEST_ENV_%s", strings.ToUpper(testSet)), string(bytes))
}

func GetTestEnvironment(name string) (GitLabExporterTestEnvironment, error) {
sEnv := os.Getenv(fmt.Sprintf("GLE_TEST_ENV_%s", strings.ToUpper(testSet)))
if sEnv == "" {
return GitLabExporterTestEnvironment{}, fmt.Errorf("environment not found: %s", name)
}

var env GitLabExporterTestEnvironment
if err := json.Unmarshal([]byte(sEnv), &env); err != nil {
return GitLabExporterTestEnvironment{}, err
}

return env, nil
}
57 changes: 57 additions & 0 deletions test/integration/tasks_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package integration_test

import (
"context"
"fmt"
"testing"

"github.com/stretchr/testify/assert"

"github.com/cluttrdev/gitlab-exporter/internal/gitlab"
"github.com/cluttrdev/gitlab-exporter/internal/tasks"
)

func Test_ExportPipelineHierarchy(t *testing.T) {
env, err := GetTestEnvironment(testSet)
if err != nil {
t.Error(err)
}

glc, err := gitlab.NewGitLabClient(gitlab.ClientConfig{
URL: fmt.Sprintf("%s/api/v4", env.URL),
})
if err != nil {
t.Fatalf("failed to create client: %v", err)
}

exp, rec := setupExporter(t)

var (
projectID int64 = 50817395 // cluttrdev/gitlab-exporter
pipelineID int64 = 1252248442
)

opts := tasks.ExportPipelineHierarchyOptions{
ProjectID: projectID,
PipelineID: pipelineID,

ExportSections: false,
ExportTestReports: true,
ExportTraces: true,
ExportMetrics: false,
}

if err := tasks.ExportPipelineHierarchy(context.Background(), glc, exp, opts); err != nil {
t.Error(err)
}

assert.Equal(t, 1, len(rec.Datastore().ListProjectPipelines(projectID)))

p := rec.Datastore().GetPipeline(pipelineID)
if p == nil {
t.Fatalf("pipeline not recorded: %v", pipelineID)
}

assert.Equal(t, 4, len(rec.Datastore().ListPipelineJobs(projectID, pipelineID)))
assert.Equal(t, int64(13), rec.Datastore().GetPipelineTestReport(pipelineID).TotalCount)
}
Loading

0 comments on commit 712c92f

Please sign in to comment.