mirror of
https://gitea.com/gitea/act_runner.git
synced 2026-08-07 09:24:22 +02:00
Gitea needs the workflow model and the expression evaluator to parse workflows and to build the task payload this runner consumes, so it had to depend on gitea.com/gitea/runner for it. Both packages now live in gitea.dev/actionslib, the module both sides already share, and this repository consumes them from there. The GithubContext helpers that need a git checkout on disk (SetRef, SetSha and SetRepositoryAndOwner) are not moved: they are runner only and would drag a git client and the act context logger into the shared module. They become functions of the new act/ghcontext package, together with their tests. act/common.CartesianProduct moved to the shared model package as well, act/model was its only user. act/model/testdata/container-volumes is now act/runner/testdata/container-volumes, its only user is runner_test.go. The x-runner-uuid and x-runner-token header names come from actionslib too, so the runner and Gitea cannot drift apart. The generated runner API code moved along with the repository, from gitea.dev/actions-proto-go to gitea.dev/actionslib, so the imports of ping/v1 and runner/v1 follow the new module path. Both cannot be used at once: the generated types would no longer be the same types. Signed-off-by: Lunny Xiao <xiaolunwen@gmail.com>
96 lines
3.0 KiB
Go
96 lines
3.0 KiB
Go
// Copyright 2026 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package client
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"connectrpc.com/connect"
|
|
pingv1 "gitea.dev/actionslib/ping/v1"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestGetHTTPClientUsesProxyFromEnvironment(t *testing.T) {
|
|
t.Setenv("HTTP_PROXY", "http://proxy.example.com:8080")
|
|
|
|
client := getHTTPClient("http://gitea.example.com", false)
|
|
transport, ok := client.Transport.(*http.Transport)
|
|
require.True(t, ok)
|
|
|
|
req, err := http.NewRequest(http.MethodGet, "http://gitea.example.com/api/actions/ping", nil)
|
|
require.NoError(t, err)
|
|
|
|
proxyURL, err := transport.Proxy(req)
|
|
require.NoError(t, err)
|
|
require.NotNil(t, proxyURL)
|
|
require.Equal(t, "http://proxy.example.com:8080", proxyURL.String())
|
|
}
|
|
|
|
func TestGetHTTPClientInsecureTLS(t *testing.T) {
|
|
// insecure only takes effect for https endpoints
|
|
httpsInsecure := getHTTPClient("https://gitea.example.com", true)
|
|
transport, ok := httpsInsecure.Transport.(*http.Transport)
|
|
require.True(t, ok)
|
|
require.NotNil(t, transport.TLSClientConfig)
|
|
require.True(t, transport.TLSClientConfig.InsecureSkipVerify)
|
|
|
|
for _, tc := range []struct {
|
|
name string
|
|
endpoint string
|
|
insecure bool
|
|
}{
|
|
{"https secure", "https://gitea.example.com", false},
|
|
{"http insecure ignored", "http://gitea.example.com", true},
|
|
} {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
c := getHTTPClient(tc.endpoint, tc.insecure)
|
|
tr, ok := c.Transport.(*http.Transport)
|
|
require.True(t, ok)
|
|
require.Nil(t, tr.TLSClientConfig)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestNewSetsBaseURLAndHeaders(t *testing.T) {
|
|
var gotPath string
|
|
gotHeaders := make(http.Header)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotPath = r.URL.Path
|
|
gotHeaders = r.Header.Clone()
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer server.Close()
|
|
|
|
// trailing slash must be trimmed before "/api/actions" is appended
|
|
c := New(server.URL+"/", false, "the-uuid", "the-token")
|
|
// Address returns the endpoint as supplied (untrimmed)
|
|
require.Equal(t, server.URL+"/", c.Address())
|
|
require.False(t, c.Insecure())
|
|
|
|
// the call is expected to fail (server returns 500), we only assert what was sent
|
|
_, _ = c.Ping(t.Context(), connect.NewRequest(&pingv1.PingRequest{Data: "hi"}))
|
|
|
|
require.True(t, strings.HasPrefix(gotPath, "/api/actions/"), "unexpected path %q", gotPath)
|
|
require.Equal(t, "the-uuid", gotHeaders.Get(UUIDHeader))
|
|
require.Equal(t, "the-token", gotHeaders.Get(TokenHeader))
|
|
}
|
|
|
|
func TestNewOmitsEmptyHeaders(t *testing.T) {
|
|
gotHeaders := make(http.Header)
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
gotHeaders = r.Header.Clone()
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
}))
|
|
defer server.Close()
|
|
|
|
c := New(server.URL, false, "", "")
|
|
_, _ = c.Ping(t.Context(), connect.NewRequest(&pingv1.PingRequest{Data: "hi"}))
|
|
|
|
require.Empty(t, gotHeaders.Get(UUIDHeader))
|
|
require.Empty(t, gotHeaders.Get(TokenHeader))
|
|
}
|