File: config_test.go

package info (click to toggle)
gitlab-ci-multi-runner 14.10.1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 31,248 kB
  • sloc: sh: 1,694; makefile: 384; asm: 79; ruby: 68
file content (101 lines) | stat: -rw-r--r-- 2,490 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
//go:build !integration
// +build !integration

package custom

import (
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"gitlab.com/gitlab-org/gitlab-runner/common"
	"gitlab.com/gitlab-org/gitlab-runner/helpers/process"
)

type getDurationTestCase struct {
	source        *int
	expectedValue time.Duration
}

func testGetDuration(t *testing.T, defaultValue time.Duration, assert func(*testing.T, getDurationTestCase)) {
	tests := map[string]getDurationTestCase{
		"source undefined": {
			expectedValue: defaultValue,
		},
		"source value lower than zero": {
			source:        func() *int { i := -10; return &i }(),
			expectedValue: defaultValue,
		},
		"source value greater than zero": {
			source:        func() *int { i := 10; return &i }(),
			expectedValue: time.Duration(10) * time.Second,
		},
	}

	for testName, tt := range tests {
		t.Run(testName, func(t *testing.T) {
			assert(t, tt)
		})
	}
}

func TestConfig_GetConfigExecTimeout(t *testing.T) {
	testGetDuration(t, defaultConfigExecTimeout, func(t *testing.T, tt getDurationTestCase) {
		c := &config{
			CustomConfig: &common.CustomConfig{
				ConfigExecTimeout: tt.source,
			},
		}

		assert.Equal(t, tt.expectedValue, c.GetConfigExecTimeout())
	})
}

func TestConfig_GetPrepareExecTimeout(t *testing.T) {
	testGetDuration(t, defaultPrepareExecTimeout, func(t *testing.T, tt getDurationTestCase) {
		c := &config{
			CustomConfig: &common.CustomConfig{
				PrepareExecTimeout: tt.source,
			},
		}

		assert.Equal(t, tt.expectedValue, c.GetPrepareExecTimeout())
	})
}

func TestConfig_GetCleanupExecTimeout(t *testing.T) {
	testGetDuration(t, defaultCleanupExecTimeout, func(t *testing.T, tt getDurationTestCase) {
		c := &config{
			CustomConfig: &common.CustomConfig{
				CleanupExecTimeout: tt.source,
			},
		}

		assert.Equal(t, tt.expectedValue, c.GetCleanupScriptTimeout())
	})
}

func TestConfig_GetTerminateTimeout(t *testing.T) {
	testGetDuration(t, process.GracefulTimeout, func(t *testing.T, tt getDurationTestCase) {
		c := &config{
			CustomConfig: &common.CustomConfig{
				GracefulKillTimeout: tt.source,
			},
		}

		assert.Equal(t, tt.expectedValue, c.GetGracefulKillTimeout())
	})
}

func TestConfig_GetForceKillTimeout(t *testing.T) {
	testGetDuration(t, process.KillTimeout, func(t *testing.T, tt getDurationTestCase) {
		c := &config{
			CustomConfig: &common.CustomConfig{
				ForceKillTimeout: tt.source,
			},
		}

		assert.Equal(t, tt.expectedValue, c.GetForceKillTimeout())
	})
}