github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/segment/tracing/handler/tracehandler_test.go (about)

     1  package handler
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	pipesearch "github.com/siglens/siglens/pkg/ast/pipesearch"
     8  	"github.com/stretchr/testify/assert"
     9  	"github.com/valyala/fasthttp"
    10  )
    11  
    12  func TestExtractTraceID(t *testing.T) {
    13  	// Test case 1: Valid trace ID
    14  	isTraceID, traceID := ExtractTraceID("trace_id=abc123")
    15  	assert.True(t, isTraceID)
    16  	assert.Equal(t, "abc123", traceID)
    17  
    18  	// Test case 2: Invalid trace ID
    19  	isTraceID, traceID = ExtractTraceID("trace_id=invalid*id")
    20  	assert.False(t, isTraceID)
    21  	assert.Equal(t, "", traceID)
    22  
    23  	// Test case 3: Empty input
    24  	isTraceID, traceID = ExtractTraceID("")
    25  	assert.False(t, isTraceID)
    26  	assert.Equal(t, "", traceID)
    27  
    28  	// Test case 4: No trace ID in input
    29  	isTraceID, traceID = ExtractTraceID("no_trace_id_here")
    30  	assert.False(t, isTraceID)
    31  	assert.Equal(t, "", traceID)
    32  }
    33  
    34  func TestParseAndValidateRequestBody(t *testing.T) {
    35  	// Test case 1: Valid request body
    36  	ctx := &fasthttp.RequestCtx{
    37  		Request: fasthttp.Request{},
    38  	}
    39  	ctx.Request.SetBody([]byte(`{
    40          "searchText": "*",
    41          "startEpoch": "now-90d",
    42          "endEpoch": "now",
    43          "queryLanguage": "Splunk QL",
    44          "page": 1
    45      }`))
    46  	body, readJSON, err := ParseAndValidateRequestBody(ctx)
    47  	assert.Nil(t, err)
    48  	assert.Equal(t, "*", body.SearchText)
    49  	assert.Equal(t, "now-90d", body.StartEpoch)
    50  	assert.Equal(t, "now", body.EndEpoch)
    51  	assert.Equal(t, "Splunk QL", body.QueryLanguage)
    52  	assert.Equal(t, "traces", body.IndexName)
    53  	page, err := readJSON["page"].(json.Number).Int64()
    54  	assert.Nil(t, err)
    55  	assert.Equal(t, int64(1), page)
    56  	assert.Equal(t, "*", readJSON["searchText"])
    57  	assert.Equal(t, "now-90d", readJSON["startEpoch"])
    58  	assert.Equal(t, "now", readJSON["endEpoch"])
    59  	assert.Equal(t, "Splunk QL", readJSON["queryLanguage"])
    60  	assert.Equal(t, json.Number("1"), readJSON["page"])
    61  
    62  	// Test case 2: Invalid JSON
    63  	ctx.Request.SetBody([]byte(`{
    64          searchText: *,
    65          "startEpoch": "now-90d",
    66          "endEpoch": "now",
    67          "queryLanguage": "Splunk QL"
    68          "page": 2
    69      }`))
    70  	_, _, err = ParseAndValidateRequestBody(ctx)
    71  	assert.NotNil(t, err)
    72  
    73  	ctx.Request.SetBody([]byte(``))
    74  	body, readJSON, err = ParseAndValidateRequestBody(ctx)
    75  	assert.NotNil(t, err)
    76  	assert.Nil(t, body)
    77  	assert.Nil(t, readJSON)
    78  }
    79  
    80  func TestGetTotalUniqueTraceIds(t *testing.T) {
    81  	//Non-empty PipeSearchResponseOuter
    82  	pipeSearchResponseOuter := &pipesearch.PipeSearchResponseOuter{
    83  		Aggs: map[string]pipesearch.AggregationResults{
    84  			"": {
    85  				Buckets: []map[string]interface{}{
    86  					{"key": "trace1"},
    87  					{"key": "trace2"},
    88  					{"key": "trace3"},
    89  				},
    90  			},
    91  		},
    92  	}
    93  	totalTraces := GetTotalUniqueTraceIds(pipeSearchResponseOuter)
    94  	assert.Equal(t, 3, totalTraces)
    95  
    96  	//Empty PipeSearchResponseOuter
    97  	pipeSearchResponseOuter = &pipesearch.PipeSearchResponseOuter{}
    98  	totalTraces = GetTotalUniqueTraceIds(pipeSearchResponseOuter)
    99  	assert.Equal(t, 0, totalTraces)
   100  }
   101  
   102  func TestGetUniqueTraceIds(t *testing.T) {
   103  	// Non-empty PipeSearchResponseOuter
   104  	pipeSearchResponseOuter := &pipesearch.PipeSearchResponseOuter{
   105  		Aggs: map[string]pipesearch.AggregationResults{
   106  			"": {
   107  				Buckets: []map[string]interface{}{
   108  					{"key": "trace1"},
   109  					{"key": "trace2"},
   110  					{"key": "trace3"},
   111  				},
   112  			},
   113  		},
   114  	}
   115  	traceIds := GetUniqueTraceIds(pipeSearchResponseOuter, 0, 0, 1)
   116  	assert.Equal(t, []string{"trace1", "trace2", "trace3"}, traceIds)
   117  
   118  	// Empty PipeSearchResponseOuter
   119  	pipeSearchResponseOuter = &pipesearch.PipeSearchResponseOuter{}
   120  	traceIds = GetUniqueTraceIds(pipeSearchResponseOuter, 0, 0, 1)
   121  	assert.Equal(t, []string{}, traceIds)
   122  }