1
0
Fork 0
mirror of https://code.forgejo.org/forgejo/runner.git synced 2025-09-15 18:57:01 +00:00

chore: use t.Context for tests, activate usetesting for lint + add t.TempDir and t.Chdir (#844)

<!--start release-notes-assistant-->
<!--URL:https://code.forgejo.org/forgejo/runner-->
- other
  - [PR](https://code.forgejo.org/forgejo/runner/pulls/844): <!--number 844 --><!--line 0 --><!--description Y2hvcmU6IHVzZSB0LkNvbnRleHQgZm9yIHRlc3RzLCBhY3RpdmF0ZSB1c2V0ZXN0aW5nIGZvciBsaW50ICsgYWRkIHQuVGVtcERpciBhbmQgdC5DaGRpciBbc2tpcCBjYXNjYWRlXQ==-->chore: use t.Context for tests, activate usetesting for lint + add t.TempDir and t.Chdir [skip cascade]<!--description-->
<!--end release-notes-assistant-->

Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/844
Reviewed-by: Gusted <gusted@noreply.code.forgejo.org>
Co-authored-by: Earl Warren <contact@earl-warren.org>
Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
Earl Warren 2025-08-11 13:21:42 +00:00 committed by earl-warren
parent 66bb63ea18
commit 0520ff4e05
No known key found for this signature in database
GPG key ID: F128CBE6AB3A7201
22 changed files with 126 additions and 153 deletions

View file

@ -1,7 +1,6 @@
package container
import (
"context"
"io"
"testing"
@ -19,7 +18,7 @@ func TestImageExistsLocally(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
ctx := t.Context()
// to help make this test reliable and not flaky, we need to have
// an image that will exist, and onew that won't exist
@ -36,7 +35,7 @@ func TestImageExistsLocally(t *testing.T) {
// pull an image
cli, err := client.NewClientWithOpts(client.FromEnv)
assert.Nil(t, err)
cli.NegotiateAPIVersion(context.Background())
cli.NegotiateAPIVersion(t.Context())
// Chose alpine latest because it's so small
// maybe we should build an image instead so that tests aren't reliable on dockerhub

View file

@ -1,7 +1,6 @@
package container
import (
"context"
"testing"
"github.com/docker/cli/cli/config"
@ -29,13 +28,13 @@ func TestCleanImage(t *testing.T) {
}
for _, table := range tables {
imageOut := cleanImage(context.Background(), table.imageIn)
imageOut := cleanImage(t.Context(), table.imageIn)
assert.Equal(t, table.imageOut, imageOut)
}
}
func TestGetImagePullOptions(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
config.SetDir("/non-existent/docker")

View file

@ -26,7 +26,7 @@ func TestDocker(t *testing.T) {
if testing.Short() {
t.Skip("skipping integration test")
}
ctx := context.Background()
ctx := t.Context()
client, err := GetDockerClient(ctx)
assert.NoError(t, err)
defer client.Close()
@ -152,7 +152,7 @@ func TestDockerExecAbort(t *testing.T) {
}
func TestDockerExecFailure(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
conn := &mockConn{}
@ -183,7 +183,7 @@ func TestDockerExecFailure(t *testing.T) {
}
func TestDockerCopyTarStream(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
conn := &mockConn{}
@ -205,7 +205,7 @@ func TestDockerCopyTarStream(t *testing.T) {
}
func TestDockerCopyTarStreamErrorInCopyFiles(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
conn := &mockConn{}
@ -230,7 +230,7 @@ func TestDockerCopyTarStreamErrorInCopyFiles(t *testing.T) {
}
func TestDockerCopyTarStreamErrorInMkdir(t *testing.T) {
ctx := context.Background()
ctx := t.Context()
conn := &mockConn{}
@ -318,7 +318,7 @@ func TestCheckVolumes(t *testing.T) {
for _, tc := range testCases {
t.Run(tc.desc, func(t *testing.T) {
logger, _ := test.NewNullLogger()
ctx := common.WithLogger(context.Background(), logger)
ctx := common.WithLogger(t.Context(), logger)
cr := &containerReference{
input: &NewContainerInput{
ValidVolumes: tc.validVolumes,
@ -379,7 +379,7 @@ func TestMergeJobOptions(t *testing.T) {
JobOptions: testCase.options,
},
}
config, hostConfig, err := cr.mergeJobOptions(context.Background(), &container.Config{}, &container.HostConfig{})
config, hostConfig, err := cr.mergeJobOptions(t.Context(), &container.Config{}, &container.HostConfig{})
require.NoError(t, err)
assert.EqualValues(t, testCase.config, config)
assert.EqualValues(t, testCase.hostConfig, hostConfig)
@ -394,7 +394,7 @@ func TestDockerRun_isHealthy(t *testing.T) {
NetworkAliases: []string{"servicename"},
},
}
ctx := context.Background()
ctx := t.Context()
makeInspectResponse := func(interval time.Duration, status container.HealthStatus, test []string) container.InspectResponse {
return container.InspectResponse{
Config: &container.Config{

View file

@ -2,7 +2,6 @@ package container
import (
"archive/tar"
"context"
"io"
"os"
"path"
@ -16,10 +15,8 @@ import (
var _ ExecutionsEnvironment = &HostEnvironment{}
func TestCopyDir(t *testing.T) {
dir, err := os.MkdirTemp("", "test-host-env-*")
assert.NoError(t, err)
defer os.RemoveAll(dir)
ctx := context.Background()
dir := t.TempDir()
ctx := t.Context()
e := &HostEnvironment{
Path: filepath.Join(dir, "path"),
TmpDir: filepath.Join(dir, "tmp"),
@ -32,15 +29,13 @@ func TestCopyDir(t *testing.T) {
_ = os.MkdirAll(e.TmpDir, 0o700)
_ = os.MkdirAll(e.ToolCache, 0o700)
_ = os.MkdirAll(e.ActPath, 0o700)
err = e.CopyDir(e.Workdir, e.Path, true)(ctx)
err := e.CopyDir(e.Workdir, e.Path, true)(ctx)
assert.NoError(t, err)
}
func TestGetContainerArchive(t *testing.T) {
dir, err := os.MkdirTemp("", "test-host-env-*")
assert.NoError(t, err)
defer os.RemoveAll(dir)
ctx := context.Background()
dir := t.TempDir()
ctx := t.Context()
e := &HostEnvironment{
Path: filepath.Join(dir, "path"),
TmpDir: filepath.Join(dir, "tmp"),
@ -54,7 +49,7 @@ func TestGetContainerArchive(t *testing.T) {
_ = os.MkdirAll(e.ToolCache, 0o700)
_ = os.MkdirAll(e.ActPath, 0o700)
expectedContent := []byte("sdde/7sh")
err = os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0o600)
err := os.WriteFile(filepath.Join(e.Path, "action.yml"), expectedContent, 0o600)
assert.NoError(t, err)
archive, err := e.GetContainerArchive(ctx, e.Path)
assert.NoError(t, err)

View file

@ -35,18 +35,13 @@ func TestContainerPath(t *testing.T) {
{fmt.Sprintf("/mnt/%v/act", rootDriveLetter), "act", fmt.Sprintf("%s\\", rootDrive)},
} {
if v.workDir != "" {
if err := os.Chdir(v.workDir); err != nil {
log.Error(err)
t.Fail()
}
t.Chdir(v.workDir)
}
assert.Equal(t, v.destinationPath, linuxcontainerext.ToContainerPath(v.sourcePath))
}
if err := os.Chdir(cwd); err != nil {
log.Error(err)
}
t.Chdir(cwd)
} else {
cwd, err := os.Getwd()
if err != nil {