github.com/nilium/gitlab-runner@v12.5.0+incompatible/commands/config_test.go (about)

     1  package commands
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  	"gitlab.com/gitlab-org/gitlab-runner/common"
     9  )
    10  
    11  type metricsServerTestExample struct {
    12  	address         string
    13  	setAddress      bool
    14  	expectedAddress string
    15  	errorIsExpected bool
    16  }
    17  
    18  type metricsServerConfigurationType string
    19  
    20  const (
    21  	configurationFromCli    metricsServerConfigurationType = "from-cli"
    22  	configurationFromConfig metricsServerConfigurationType = "from-config"
    23  )
    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  		cfg.config = &common.Config{}
    29  		if example.setAddress {
    30  			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  		assert.Equal(t, example.expectedAddress, address)
    39  		if example.errorIsExpected {
    40  			assert.Error(t, err)
    41  		} else {
    42  			assert.NoError(t, err)
    43  		}
    44  	})
    45  }
    46  
    47  func TestMetricsServer(t *testing.T) {
    48  	examples := map[string]metricsServerTestExample{
    49  		"address-set-without-port": {"localhost", true, "localhost:9252", false},
    50  		"port-set-without-address": {":1234", true, ":1234", false},
    51  		"address-set-with-port":    {"localhost:1234", true, "localhost:1234", false},
    52  		"address-is-empty":         {"", true, "", false},
    53  		"address-is-invalid":       {"localhost::1234", true, "", true},
    54  		"address-not-set":          {"", false, "", false},
    55  	}
    56  
    57  	for exampleName, example := range examples {
    58  		testListenAddressSetting(t, exampleName, example, configurationFromCli)
    59  		testListenAddressSetting(t, exampleName, example, configurationFromConfig)
    60  	}
    61  }