-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
146 changed files
with
14,564 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.