github.com/yankunsam/loki/v2@v2.6.3-0.20220817130409-389df5235c27/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/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/grafana/loki/pkg/configs/userconfig"
    13  )
    14  
    15  var response = `{
    16    "configs": {
    17      "2": {
    18        "id": 1,
    19        "config": {
    20          "rules_files": {
    21            "recording.rules": "groups:\n- name: demo-service-alerts\n  interval: 15s\n  rules:\n  - alert: SomethingIsUp\n    expr: up == 1\n"
    22  				},
    23  				"rule_format_version": "2"
    24        }
    25      }
    26    }
    27  }
    28  `
    29  
    30  func TestDoRequest(t *testing.T) {
    31  	server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    32  		_, err := w.Write([]byte(response))
    33  		require.NoError(t, err)
    34  	}))
    35  	defer server.Close()
    36  
    37  	resp, err := doRequest(server.URL, 1*time.Second, nil, 0)
    38  	assert.Nil(t, err)
    39  
    40  	expected := ConfigsResponse{Configs: map[string]userconfig.View{
    41  		"2": {
    42  			ID: 1,
    43  			Config: userconfig.Config{
    44  				RulesConfig: userconfig.RulesConfig{
    45  					Files: map[string]string{
    46  						"recording.rules": "groups:\n- name: demo-service-alerts\n  interval: 15s\n  rules:\n  - alert: SomethingIsUp\n    expr: up == 1\n",
    47  					},
    48  					FormatVersion: userconfig.RuleFormatV2,
    49  				},
    50  			},
    51  		},
    52  	}}
    53  	assert.Equal(t, &expected, resp)
    54  }