github.com/muhammadn/cortex@v1.9.1-0.20220510110439-46bb7000d03d/pkg/configs/client/configs_test.go (about) 1 package client 2 3 import ( 4 "net/http" 5 "net/http/httptest" 6 "testing" 7 "time" 8 9 "github.com/cortexproject/cortex/pkg/configs/userconfig" 10 11 "github.com/stretchr/testify/require" 12 13 "github.com/stretchr/testify/assert" 14 ) 15 16 var response = `{ 17 "configs": { 18 "2": { 19 "id": 1, 20 "config": { 21 "rules_files": { 22 "recording.rules": "groups:\n- name: demo-service-alerts\n interval: 15s\n rules:\n - alert: SomethingIsUp\n expr: up == 1\n" 23 }, 24 "rule_format_version": "2" 25 } 26 } 27 } 28 } 29 ` 30 31 func TestDoRequest(t *testing.T) { 32 server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 33 _, err := w.Write([]byte(response)) 34 require.NoError(t, err) 35 })) 36 defer server.Close() 37 38 resp, err := doRequest(server.URL, 1*time.Second, nil, 0) 39 assert.Nil(t, err) 40 41 expected := ConfigsResponse{Configs: map[string]userconfig.View{ 42 "2": { 43 ID: 1, 44 Config: userconfig.Config{ 45 RulesConfig: userconfig.RulesConfig{ 46 Files: map[string]string{ 47 "recording.rules": "groups:\n- name: demo-service-alerts\n interval: 15s\n rules:\n - alert: SomethingIsUp\n expr: up == 1\n", 48 }, 49 FormatVersion: userconfig.RuleFormatV2, 50 }, 51 }, 52 }, 53 }} 54 assert.Equal(t, &expected, resp) 55 }