github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/cortex/modules_test.go (about)

     1  package cortex
     2  
     3  import (
     4  	"net/http/httptest"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/gorilla/mux"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"github.com/weaveworks/common/server"
    12  )
    13  
    14  func changeTargetConfig(c *Config) {
    15  	c.Target = []string{"all", "ruler"}
    16  }
    17  
    18  func TestAPIConfig(t *testing.T) {
    19  	actualCfg := newDefaultConfig()
    20  
    21  	cortex := &Cortex{
    22  		Server: &server.Server{},
    23  	}
    24  
    25  	for _, tc := range []struct {
    26  		name               string
    27  		path               string
    28  		actualCfg          func(*Config)
    29  		expectedStatusCode int
    30  		expectedBody       func(*testing.T, string)
    31  	}{
    32  		{
    33  			name:               "running with default config",
    34  			path:               "/config",
    35  			expectedStatusCode: 200,
    36  		},
    37  		{
    38  			name:               "defaults with default config",
    39  			path:               "/config?mode=defaults",
    40  			expectedStatusCode: 200,
    41  		},
    42  		{
    43  			name:               "diff with default config",
    44  			path:               "/config?mode=diff",
    45  			expectedStatusCode: 200,
    46  			expectedBody: func(t *testing.T, body string) {
    47  				assert.Equal(t, "{}\n", body)
    48  			},
    49  		},
    50  		{
    51  			name:               "running with changed target config",
    52  			path:               "/config",
    53  			actualCfg:          changeTargetConfig,
    54  			expectedStatusCode: 200,
    55  			expectedBody: func(t *testing.T, body string) {
    56  				assert.Contains(t, body, "target: all,ruler\n")
    57  			},
    58  		},
    59  		{
    60  			name:               "defaults with changed target config",
    61  			path:               "/config?mode=defaults",
    62  			actualCfg:          changeTargetConfig,
    63  			expectedStatusCode: 200,
    64  			expectedBody: func(t *testing.T, body string) {
    65  				assert.Contains(t, body, "target: all\n")
    66  			},
    67  		},
    68  		{
    69  			name:               "diff with changed target config",
    70  			path:               "/config?mode=diff",
    71  			actualCfg:          changeTargetConfig,
    72  			expectedStatusCode: 200,
    73  			expectedBody: func(t *testing.T, body string) {
    74  				assert.Equal(t, "target: all,ruler\n", body)
    75  			},
    76  		},
    77  	} {
    78  		t.Run(tc.name, func(t *testing.T) {
    79  			cortex.Server.HTTP = mux.NewRouter()
    80  
    81  			cortex.Cfg = *actualCfg
    82  			if tc.actualCfg != nil {
    83  				tc.actualCfg(&cortex.Cfg)
    84  			}
    85  
    86  			_, err := cortex.initAPI()
    87  			require.NoError(t, err)
    88  
    89  			req := httptest.NewRequest("GET", tc.path, nil)
    90  			resp := httptest.NewRecorder()
    91  
    92  			cortex.Server.HTTP.ServeHTTP(resp, req)
    93  
    94  			assert.Equal(t, tc.expectedStatusCode, resp.Code)
    95  
    96  			if tc.expectedBody != nil {
    97  				tc.expectedBody(t, resp.Body.String())
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestCortex_InitRulerStorage(t *testing.T) {
   104  	tests := map[string]struct {
   105  		config       *Config
   106  		expectedInit bool
   107  	}{
   108  		"should init the ruler storage with target=ruler": {
   109  			config: func() *Config {
   110  				cfg := newDefaultConfig()
   111  				cfg.Target = []string{"ruler"}
   112  				cfg.RulerStorage.Backend = "local"
   113  				cfg.RulerStorage.Local.Directory = os.TempDir()
   114  				return cfg
   115  			}(),
   116  			expectedInit: true,
   117  		},
   118  		"should not init the ruler storage on default config with target=all": {
   119  			config: func() *Config {
   120  				cfg := newDefaultConfig()
   121  				cfg.Target = []string{"all"}
   122  				return cfg
   123  			}(),
   124  			expectedInit: false,
   125  		},
   126  		"should init the ruler storage on legacy ruler storage config with target=all": {
   127  			config: func() *Config {
   128  				cfg := newDefaultConfig()
   129  				cfg.Target = []string{"all"}
   130  				cfg.Ruler.StoreConfig.Type = "local"
   131  				cfg.Ruler.StoreConfig.Local.Directory = os.TempDir()
   132  				return cfg
   133  			}(),
   134  			expectedInit: true,
   135  		},
   136  		"should init the ruler storage on ruler storage config with target=all": {
   137  			config: func() *Config {
   138  				cfg := newDefaultConfig()
   139  				cfg.Target = []string{"all"}
   140  				cfg.RulerStorage.Backend = "local"
   141  				cfg.RulerStorage.Local.Directory = os.TempDir()
   142  				return cfg
   143  			}(),
   144  			expectedInit: true,
   145  		},
   146  	}
   147  
   148  	for testName, testData := range tests {
   149  		t.Run(testName, func(t *testing.T) {
   150  			cortex := &Cortex{
   151  				Server: &server.Server{},
   152  				Cfg:    *testData.config,
   153  			}
   154  
   155  			_, err := cortex.initRulerStorage()
   156  			require.NoError(t, err)
   157  
   158  			if testData.expectedInit {
   159  				assert.NotNil(t, cortex.RulerStorage)
   160  			} else {
   161  				assert.Nil(t, cortex.RulerStorage)
   162  			}
   163  		})
   164  	}
   165  }