github.com/instana/go-sensor@v1.62.2-0.20240520081010-4919868049e1/agent_test.go (about)

     1  // (c) Copyright IBM Corp. 2022
     2  
     3  package instana
     4  
     5  import (
     6  	"bytes"
     7  	"context"
     8  	"encoding/json"
     9  	"io/ioutil"
    10  	"net/http"
    11  	"strings"
    12  	"testing"
    13  
    14  	"github.com/instana/go-sensor/acceptor"
    15  	"github.com/instana/go-sensor/autoprofile"
    16  
    17  	"github.com/stretchr/testify/assert"
    18  )
    19  
    20  func Test_agentS_SendSpans(t *testing.T) {
    21  	tests := []struct {
    22  		name  string
    23  		spans []Span
    24  	}{
    25  		{
    26  			name: "big span",
    27  			spans: []Span{
    28  				{
    29  					Data: HTTPSpanData{
    30  						Tags: HTTPSpanTags{
    31  							URL: strings.Repeat("1", maxContentLength),
    32  						},
    33  					},
    34  				},
    35  			},
    36  		},
    37  		{
    38  			name: "multiple big span",
    39  			spans: []Span{
    40  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    41  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    42  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    43  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    44  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    45  				{Data: HTTPSpanData{Tags: HTTPSpanTags{URL: strings.Repeat("1", maxContentLength)}}},
    46  			},
    47  		},
    48  		{
    49  			name: "not really a big span",
    50  			spans: []Span{
    51  				{
    52  					Data: HTTPSpanData{
    53  						Tags: HTTPSpanTags{
    54  							URL: strings.Repeat("1", maxContentLength/2),
    55  						},
    56  					},
    57  				},
    58  			},
    59  		},
    60  	}
    61  
    62  	for _, tt := range tests {
    63  		t.Run(tt.name, func(t *testing.T) {
    64  			ad := &agentCommunicator{
    65  				host: "", from: &fromS{},
    66  				client: &httpClientMock{
    67  					resp: &http.Response{
    68  						StatusCode: 200,
    69  						Body:       ioutil.NopCloser(bytes.NewReader([]byte("{}"))),
    70  					},
    71  				},
    72  			}
    73  			agent := &agentS{agentComm: ad, logger: defaultLogger}
    74  			err := agent.SendSpans(tt.spans)
    75  
    76  			assert.NoError(t, err)
    77  		})
    78  	}
    79  }
    80  
    81  type httpClientMock struct {
    82  	resp *http.Response
    83  	err  error
    84  }
    85  
    86  func (h httpClientMock) Do(req *http.Request) (*http.Response, error) {
    87  	return h.resp, h.err
    88  }
    89  
    90  func Test_agentResponse_getExtraHTTPHeaders(t *testing.T) {
    91  
    92  	tests := []struct {
    93  		name         string
    94  		originalJSON string
    95  		want         []string
    96  	}{
    97  		{
    98  			name:         "old config",
    99  			originalJSON: `{"pid":37808,"agentUuid":"88:66:5a:ff:fe:05:a5:f0","extraHeaders":["expected-value"],"secrets":{"matcher":"contains-ignore-case","list":["key","pass","secret"]}}`,
   100  			want:         []string{"expected-value"},
   101  		},
   102  		{
   103  			name:         "new config",
   104  			originalJSON: `{"pid":38381,"agentUuid":"88:66:5a:ff:fe:05:a5:f0","tracing":{"extra-http-headers":["expected-value"]},"extraHeaders":["non-expected-value"],"secrets":{"matcher":"contains-ignore-case","list":["key","pass","secret"]}}`,
   105  			want:         []string{"expected-value"},
   106  		},
   107  	}
   108  
   109  	for _, tt := range tests {
   110  		t.Run(tt.name, func(t *testing.T) {
   111  			r := &agentResponse{}
   112  			json.Unmarshal([]byte(tt.originalJSON), r)
   113  			assert.Equalf(t, tt.want, r.getExtraHTTPHeaders(), "getExtraHTTPHeaders()")
   114  		})
   115  	}
   116  }
   117  
   118  func Test_agentApplyHostSettings(t *testing.T) {
   119  	fsm := &fsmS{
   120  		agentComm: &agentCommunicator{
   121  			host: "",
   122  			from: &fromS{},
   123  		},
   124  	}
   125  
   126  	response := agentResponse{
   127  		Pid:    37892,
   128  		HostID: "myhost",
   129  		Tracing: struct {
   130  			ExtraHTTPHeaders []string `json:"extra-http-headers"`
   131  		}{
   132  			ExtraHTTPHeaders: []string{"my-unwanted-custom-headers"},
   133  		},
   134  	}
   135  
   136  	opts := &Options{
   137  		Service: "test_service",
   138  		Tracer: TracerOptions{
   139  			CollectableHTTPHeaders: []string{"x-custom-header-1", "x-custom-header-2"},
   140  		},
   141  		AgentClient: alwaysReadyClient{},
   142  	}
   143  
   144  	sensor = newSensor(opts)
   145  	defer func() {
   146  		sensor = nil
   147  	}()
   148  
   149  	fsm.applyHostAgentSettings(response)
   150  
   151  	assert.NotContains(t, sensor.options.Tracer.CollectableHTTPHeaders, "my-unwanted-custom-headers")
   152  }
   153  
   154  type alwaysReadyClient struct{}
   155  
   156  func (alwaysReadyClient) Ready() bool                                       { return true }
   157  func (alwaysReadyClient) SendMetrics(data acceptor.Metrics) error           { return nil }
   158  func (alwaysReadyClient) SendEvent(event *EventData) error                  { return nil }
   159  func (alwaysReadyClient) SendSpans(spans []Span) error                      { return nil }
   160  func (alwaysReadyClient) SendProfiles(profiles []autoprofile.Profile) error { return nil }
   161  func (alwaysReadyClient) Flush(context.Context) error                       { return nil }