github.com/grafana/pyroscope@v1.18.0/pkg/scheduler/schedulerdiscovery/config_test.go (about)

     1  // SPDX-License-Identifier: AGPL-3.0-only
     2  
     3  package schedulerdiscovery
     4  
     5  import (
     6  	"testing"
     7  
     8  	"github.com/grafana/dskit/flagext"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestConfig_Validate(t *testing.T) {
    14  	tests := map[string]struct {
    15  		setup       func(cfg *Config)
    16  		expectedErr string
    17  	}{
    18  		"should pass with default config": {
    19  			setup: func(cfg *Config) {},
    20  		},
    21  		"should fail if service discovery mode is invalid": {
    22  			setup: func(cfg *Config) {
    23  				cfg.Mode = "xxx"
    24  			},
    25  			expectedErr: "unsupported query-scheduler service discovery mode",
    26  		},
    27  		"should fail if service discovery mode is set to DNS and max used instances has been configured": {
    28  			setup: func(cfg *Config) {
    29  				cfg.Mode = ModeDNS
    30  				cfg.MaxUsedInstances = 1
    31  			},
    32  			expectedErr: "the query-scheduler max used instances can be set only when -query-scheduler.service-discovery-mode is set to 'ring'",
    33  		},
    34  		"should pass if service discovery mode is set to ring and max used instances has been configured": {
    35  			setup: func(cfg *Config) {
    36  				cfg.Mode = ModeRing
    37  				cfg.MaxUsedInstances = 1
    38  			},
    39  		},
    40  		"should fail if service discovery mode is set to ring but max used instances is negative": {
    41  			setup: func(cfg *Config) {
    42  				cfg.Mode = ModeRing
    43  				cfg.MaxUsedInstances = -1
    44  			},
    45  			expectedErr: "the query-scheduler max used instances can't be negative",
    46  		},
    47  	}
    48  
    49  	for testName, testData := range tests {
    50  		t.Run(testName, func(t *testing.T) {
    51  			cfg := Config{}
    52  			flagext.DefaultValues(&cfg)
    53  			testData.setup(&cfg)
    54  
    55  			actualErr := cfg.Validate()
    56  			if testData.expectedErr == "" {
    57  				require.NoError(t, actualErr)
    58  			} else {
    59  				require.Error(t, actualErr)
    60  				assert.ErrorContains(t, actualErr, testData.expectedErr)
    61  			}
    62  		})
    63  	}
    64  }