config_test.go 1.77 KB
Newer Older
1 2 3 4 5 6 7
package commands

import (
	"fmt"
	"testing"

	"github.com/stretchr/testify/assert"
8
	"gitlab.com/gitlab-org/gitlab-runner/common"
9 10
)

11 12 13 14 15
type metricsServerTestExample struct {
	address         string
	setAddress      bool
	expectedAddress string
	errorIsExpected bool
16 17
}

18
type metricsServerConfigurationType string
19

20 21 22 23
const (
	configurationFromCli    metricsServerConfigurationType = "from-cli"
	configurationFromConfig metricsServerConfigurationType = "from-config"
)
24

25
func testListenAddressSetting(t *testing.T, exampleName string, example metricsServerTestExample, testType metricsServerConfigurationType) {
26
	t.Run(fmt.Sprintf("%s-%s", exampleName, testType), func(t *testing.T) {
27
		cfg := configOptionsWithListenAddress{}
28 29 30
		cfg.config = &common.Config{}
		if example.setAddress {
			if testType == configurationFromCli {
31
				cfg.ListenAddress = example.address
32
			} else {
33
				cfg.config.ListenAddress = example.address
34 35 36
			}
		}

37
		address, err := cfg.listenAddress()
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
		assert.Equal(t, example.expectedAddress, address)
		if example.errorIsExpected {
			assert.Error(t, err)
		} else {
			assert.NoError(t, err)
		}
	})
}

func TestMetricsServer(t *testing.T) {
	examples := map[string]metricsServerTestExample{
		"address-set-without-port": {"localhost", true, "localhost:9252", false},
		"port-set-without-address": {":1234", true, ":1234", false},
		"address-set-with-port":    {"localhost:1234", true, "localhost:1234", false},
		"address-is-empty":         {"", true, "", false},
		"address-is-invalid":       {"localhost::1234", true, "", true},
		"address-not-set":          {"", false, "", false},
	}

	for exampleName, example := range examples {
58 59
		testListenAddressSetting(t, exampleName, example, configurationFromCli)
		testListenAddressSetting(t, exampleName, example, configurationFromConfig)
60
	}
61
}