2023-05-23 01:29:15 +00:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
2022-11-27 18:20:29 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2021-06-29 21:00:02 +00:00
|
|
|
|
|
|
|
package install
|
|
|
|
|
|
|
|
import (
|
2024-08-05 06:04:39 +00:00
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2023-05-23 01:29:15 +00:00
|
|
|
"net/http/httptest"
|
2024-08-05 06:04:39 +00:00
|
|
|
"net/url"
|
|
|
|
"os"
|
2021-06-29 21:00:02 +00:00
|
|
|
"testing"
|
2024-08-05 06:04:39 +00:00
|
|
|
"time"
|
2021-06-29 21:00:02 +00:00
|
|
|
|
2023-05-23 01:29:15 +00:00
|
|
|
"code.gitea.io/gitea/models/unittest"
|
2024-08-05 06:04:39 +00:00
|
|
|
"code.gitea.io/gitea/modules/opentelemetry"
|
|
|
|
"code.gitea.io/gitea/modules/setting"
|
|
|
|
"code.gitea.io/gitea/modules/test"
|
2023-05-23 01:29:15 +00:00
|
|
|
|
2024-08-05 06:04:39 +00:00
|
|
|
"github.com/google/uuid"
|
2021-06-29 21:00:02 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2024-08-05 06:04:39 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2021-06-29 21:00:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestRoutes(t *testing.T) {
|
2023-05-23 01:29:15 +00:00
|
|
|
r := Routes()
|
|
|
|
assert.NotNil(t, r)
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.EqualValues(t, 200, w.Code)
|
|
|
|
assert.Contains(t, w.Body.String(), `class="page-content install"`)
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req = httptest.NewRequest("GET", "/no-such", nil)
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.EqualValues(t, 404, w.Code)
|
|
|
|
|
|
|
|
w = httptest.NewRecorder()
|
|
|
|
req = httptest.NewRequest("GET", "/assets/img/gitea.svg", nil)
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
assert.EqualValues(t, 200, w.Code)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestMain(m *testing.M) {
|
2023-09-28 01:38:53 +00:00
|
|
|
unittest.MainTest(m)
|
2021-06-29 21:00:02 +00:00
|
|
|
}
|
2024-08-05 06:04:39 +00:00
|
|
|
|
|
|
|
func TestOtelChi(t *testing.T) {
|
|
|
|
ServiceName := "forgejo-otelchi" + uuid.NewString()
|
|
|
|
|
|
|
|
otelURL, ok := os.LookupEnv("TEST_OTEL_URL")
|
|
|
|
if !ok {
|
|
|
|
t.Skip("TEST_OTEL_URL not set")
|
|
|
|
}
|
|
|
|
traceEndpoint, err := url.Parse(otelURL)
|
|
|
|
require.NoError(t, err)
|
|
|
|
config := &setting.OtelExporter{
|
|
|
|
Endpoint: traceEndpoint,
|
|
|
|
Protocol: "grpc",
|
|
|
|
}
|
|
|
|
|
|
|
|
defer test.MockVariableValue(&setting.OpenTelemetry.Enabled, true)()
|
|
|
|
defer test.MockVariableValue(&setting.OpenTelemetry.Traces, "otlp")() // Required due to lazy loading
|
|
|
|
defer test.MockVariableValue(&setting.OpenTelemetry.ServiceName, ServiceName)()
|
|
|
|
defer test.MockVariableValue(&setting.OpenTelemetry.OtelTraces, config)()
|
|
|
|
|
|
|
|
require.NoError(t, opentelemetry.Init(context.Background()))
|
|
|
|
r := Routes()
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
req := httptest.NewRequest("GET", "/e/img/gitea.svg", nil)
|
|
|
|
r.ServeHTTP(w, req)
|
|
|
|
|
|
|
|
traceEndpoint.Host = traceEndpoint.Hostname() + ":16686"
|
|
|
|
traceEndpoint.Path = "/api/services"
|
|
|
|
|
|
|
|
require.EventuallyWithT(t, func(collect *assert.CollectT) {
|
|
|
|
resp, err := http.Get(traceEndpoint.String())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
apiResponse, err := io.ReadAll(resp.Body)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Contains(collect, string(apiResponse), ServiceName)
|
|
|
|
}, 15*time.Second, 1*time.Second)
|
|
|
|
}
|