github.com/secure-build/gitlab-runner@v12.5.0+incompatible/executors/custom/config_test.go (about)

     1  package custom
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  
     9  	"gitlab.com/gitlab-org/gitlab-runner/common"
    10  )
    11  
    12  type getDurationTestCase struct {
    13  	source        *int
    14  	defaultValue  time.Duration
    15  	expectedValue time.Duration
    16  }
    17  
    18  func testGetDuration(t *testing.T, defaultValue time.Duration, assert func(*testing.T, getDurationTestCase)) {
    19  	tests := map[string]getDurationTestCase{
    20  		"source undefined": {
    21  			defaultValue:  defaultValue,
    22  			expectedValue: defaultValue,
    23  		},
    24  		"source value lower than zero": {
    25  			source:        func() *int { i := -10; return &i }(),
    26  			defaultValue:  defaultValue,
    27  			expectedValue: defaultValue,
    28  		},
    29  		"source value greater than zero": {
    30  			source:        func() *int { i := 10; return &i }(),
    31  			defaultValue:  defaultValue,
    32  			expectedValue: time.Duration(10) * time.Second,
    33  		},
    34  	}
    35  
    36  	for testName, tt := range tests {
    37  		t.Run(testName, func(t *testing.T) {
    38  			assert(t, tt)
    39  		})
    40  	}
    41  }
    42  
    43  func TestConfig_GetConfigExecTimeout(t *testing.T) {
    44  	testGetDuration(t, defaultConfigExecTimeout, func(t *testing.T, tt getDurationTestCase) {
    45  		c := &config{
    46  			CustomConfig: &common.CustomConfig{
    47  				ConfigExecTimeout: tt.source,
    48  			},
    49  		}
    50  
    51  		assert.Equal(t, tt.expectedValue, c.GetConfigExecTimeout())
    52  	})
    53  }
    54  
    55  func TestConfig_GetPrepareExecTimeout(t *testing.T) {
    56  	testGetDuration(t, defaultPrepareExecTimeout, func(t *testing.T, tt getDurationTestCase) {
    57  		c := &config{
    58  			CustomConfig: &common.CustomConfig{
    59  				PrepareExecTimeout: tt.source,
    60  			},
    61  		}
    62  
    63  		assert.Equal(t, tt.expectedValue, c.GetPrepareExecTimeout())
    64  	})
    65  }
    66  
    67  func TestConfig_GetCleanupExecTimeout(t *testing.T) {
    68  	testGetDuration(t, defaultCleanupExecTimeout, func(t *testing.T, tt getDurationTestCase) {
    69  		c := &config{
    70  			CustomConfig: &common.CustomConfig{
    71  				CleanupExecTimeout: tt.source,
    72  			},
    73  		}
    74  
    75  		assert.Equal(t, tt.expectedValue, c.GetCleanupScriptTimeout())
    76  	})
    77  }
    78  
    79  func TestConfig_GetTerminateTimeout(t *testing.T) {
    80  	testGetDuration(t, defaultGracefulKillTimeout, func(t *testing.T, tt getDurationTestCase) {
    81  		c := &config{
    82  			CustomConfig: &common.CustomConfig{
    83  				GracefulKillTimeout: tt.source,
    84  			},
    85  		}
    86  
    87  		assert.Equal(t, tt.expectedValue, c.GetGracefulKillTimeout())
    88  	})
    89  }
    90  
    91  func TestConfig_GetForceKillTimeout(t *testing.T) {
    92  	testGetDuration(t, defaultForceKillTimeout, func(t *testing.T, tt getDurationTestCase) {
    93  		c := &config{
    94  			CustomConfig: &common.CustomConfig{
    95  				ForceKillTimeout: tt.source,
    96  			},
    97  		}
    98  
    99  		assert.Equal(t, tt.expectedValue, c.GetForceKillTimeout())
   100  	})
   101  }