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

     1  // (c) Copyright IBM Corp. 2022
     2  
     3  //go:build azure && integration
     4  // +build azure,integration
     5  
     6  package instana_test
     7  
     8  import (
     9  	"context"
    10  	"encoding/json"
    11  	"log"
    12  	"os"
    13  	"testing"
    14  	"time"
    15  
    16  	instana "github.com/instana/go-sensor"
    17  	"github.com/stretchr/testify/assert"
    18  	"github.com/stretchr/testify/require"
    19  )
    20  
    21  var agent *serverlessAgent
    22  
    23  func TestMain(m *testing.M) {
    24  	teardownAzureEnv := setupAzureFunctionEnv()
    25  	defer teardownAzureEnv()
    26  
    27  	teardownInstanaEnv := setupInstanaEnv()
    28  	defer teardownInstanaEnv()
    29  
    30  	var err error
    31  	agent, err = setupServerlessAgent()
    32  	if err != nil {
    33  		log.Fatalf("failed to initialize serverless agent: %s", err)
    34  	}
    35  
    36  	os.Exit(m.Run())
    37  }
    38  
    39  func TestAzureAgent_SendSpans(t *testing.T) {
    40  	defer agent.Reset()
    41  
    42  	tracer := instana.NewTracer()
    43  	sensor := instana.NewSensorWithTracer(tracer)
    44  	defer instana.ShutdownSensor()
    45  
    46  	sp := sensor.Tracer().StartSpan("azf")
    47  	sp.Finish()
    48  
    49  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    50  	defer cancel()
    51  
    52  	require.NoError(t, tracer.Flush(ctx))
    53  	require.Len(t, agent.Bundles, 1)
    54  
    55  	var spans []map[string]json.RawMessage
    56  	for _, bundle := range agent.Bundles {
    57  		var payload struct {
    58  			Spans []map[string]json.RawMessage `json:"spans"`
    59  		}
    60  
    61  		require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
    62  		spans = append(spans, payload.Spans...)
    63  	}
    64  
    65  	require.Len(t, spans, 1)
    66  	assert.JSONEq(t, `{"hl": true, "cp": "azure", "e": "/subscriptions/testgh05-3f0d-4bf9-8f53-209408003632/resourceGroups/test-resourcegroup/providers/Microsoft.Web/sites/test-funcname"}`, string(spans[0]["f"]))
    67  }
    68  
    69  func TestAzureAgent_SpanDetails(t *testing.T) {
    70  	defer agent.Reset()
    71  
    72  	tracer := instana.NewTracer()
    73  	sensor := instana.NewSensorWithTracer(tracer)
    74  	defer instana.ShutdownSensor()
    75  
    76  	sp := sensor.Tracer().StartSpan("azf")
    77  	sp.SetTag("azf.triggername", "HTTP")
    78  	sp.SetTag("azf.functionname", "testfunction")
    79  	sp.SetTag("azf.name", "testapp")
    80  	sp.SetTag("azf.methodname", "testmethod")
    81  	sp.SetTag("azf.runtime", "custom")
    82  	sp.Finish()
    83  
    84  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
    85  	defer cancel()
    86  
    87  	require.NoError(t, tracer.Flush(ctx))
    88  	require.Len(t, agent.Bundles, 1)
    89  
    90  	var spans []map[string]json.RawMessage
    91  	for _, bundle := range agent.Bundles {
    92  		var payload struct {
    93  			Spans []map[string]json.RawMessage `json:"spans"`
    94  		}
    95  
    96  		require.NoError(t, json.Unmarshal(bundle.Body, &payload), "%s", string(bundle.Body))
    97  		spans = append(spans, payload.Spans...)
    98  	}
    99  
   100  	require.Len(t, spans, 1)
   101  	assert.JSONEq(t, `{"hl": true, "cp": "azure", "e": "/subscriptions/testgh05-3f0d-4bf9-8f53-209408003632/resourceGroups/test-resourcegroup/providers/Microsoft.Web/sites/test-funcname"}`, string(spans[0]["f"]))
   102  	assert.JSONEq(t, ` {
   103          "azf": {
   104            "name": "testapp",
   105            "methodname" : "testmethod",
   106            "functionname": "testfunction",
   107            "triggername": "HTTP",
   108            "runtime": "custom"
   109          }}`, string(spans[0]["data"]))
   110  }
   111  
   112  func TestAzureAgent_SendSpans_Error(t *testing.T) {
   113  	defer agent.Reset()
   114  
   115  	tracer := instana.NewTracer()
   116  	sensor := instana.NewSensorWithTracer(tracer)
   117  	defer instana.ShutdownSensor()
   118  
   119  	sp := sensor.Tracer().StartSpan("azf")
   120  	sp.SetTag("returnError", "true")
   121  	sp.Finish()
   122  
   123  	ctx, cancel := context.WithTimeout(context.Background(), time.Second)
   124  	defer cancel()
   125  
   126  	require.NoError(t, tracer.Flush(ctx))
   127  	require.Len(t, agent.Bundles, 0)
   128  }
   129  
   130  func setupAzureFunctionEnv() func() {
   131  	var teardownFuncs []func()
   132  
   133  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("FUNCTIONS_WORKER_RUNTIME"))
   134  	os.Setenv("FUNCTIONS_WORKER_RUNTIME", "custom")
   135  
   136  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("WEBSITE_OWNER_NAME"))
   137  	os.Setenv("WEBSITE_OWNER_NAME", "testgh05-3f0d-4bf9-8f53-209408003632+testresourcegroup-GermanyWestCentralwebspace")
   138  
   139  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("WEBSITE_RESOURCE_GROUP"))
   140  	os.Setenv("WEBSITE_RESOURCE_GROUP", "test-resourcegroup")
   141  
   142  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("APPSETTING_WEBSITE_SITE_NAME"))
   143  	os.Setenv("APPSETTING_WEBSITE_SITE_NAME", "test-funcname")
   144  
   145  	return func() {
   146  		for _, f := range teardownFuncs {
   147  			f()
   148  		}
   149  	}
   150  }
   151  
   152  func setupInstanaEnv() func() {
   153  	var teardownFuncs []func()
   154  
   155  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_AGENT_KEY"))
   156  	os.Setenv("INSTANA_AGENT_KEY", "testkey1")
   157  
   158  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_ZONE"))
   159  	os.Setenv("INSTANA_ZONE", "testzone")
   160  
   161  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_TAGS"))
   162  	os.Setenv("INSTANA_TAGS", "key1=value1,key2")
   163  
   164  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("INSTANA_SECRETS"))
   165  	os.Setenv("INSTANA_SECRETS", "contains-ignore-case:key,password,secret,classified")
   166  
   167  	teardownFuncs = append(teardownFuncs, restoreEnvVarFunc("CLASSIFIED_DATA"))
   168  	os.Setenv("CLASSIFIED_DATA", "classified")
   169  
   170  	return func() {
   171  		for _, f := range teardownFuncs {
   172  			f()
   173  		}
   174  	}
   175  }