File: serverless_test.go

package info (click to toggle)
golang-github-newrelic-go-agent 3.15.2-9
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 8,356 kB
  • sloc: sh: 65; makefile: 6
file content (113 lines) | stat: -rw-r--r-- 2,737 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2020 New Relic Corporation. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package internal

import (
	"bytes"
	"strings"
	"testing"
	"time"

	"github.com/newrelic/go-agent/internal/logger"
)

func serverlessGetenvShim(s string) string {
	if s == "AWS_EXECUTION_ENV" {
		return "the-execution-env"
	}
	return ""
}

func TestServerlessHarvest(t *testing.T) {
	// Test the expected ServerlessHarvest use.
	sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim)
	event, err := CreateCustomEvent("myEvent", nil, time.Now())
	if nil != err {
		t.Fatal(err)
	}
	sh.Consume(event)
	buf := &bytes.Buffer{}
	sh.Write("arn", buf)
	metadata, data, err := ParseServerlessPayload(buf.Bytes())
	if nil != err {
		t.Fatal(err)
	}
	if v := string(metadata["metadata_version"]); v != `2` {
		t.Error(v)
	}
	if v := string(metadata["arn"]); v != `"arn"` {
		t.Error(v)
	}
	if v := string(metadata["protocol_version"]); v != `17` {
		t.Error(v)
	}
	if v := string(metadata["execution_environment"]); v != `"the-execution-env"` {
		t.Error(v)
	}
	if v := string(metadata["agent_version"]); v != `"the-version"` {
		t.Error(v)
	}
	if v := string(metadata["agent_language"]); v != `"go"` {
		t.Error(v)
	}
	eventData := string(data["custom_event_data"])
	if !strings.Contains(eventData, `"type":"myEvent"`) {
		t.Error(eventData)
	}
	if len(data) != 1 {
		t.Fatal(data)
	}
	// Test that the harvest was replaced with a new harvest.
	buf = &bytes.Buffer{}
	sh.Write("arn", buf)
	if 0 != buf.Len() {
		t.Error(buf.String())
	}
}

func TestServerlessHarvestNil(t *testing.T) {
	// The public ServerlessHarvest methods should not panic if the
	// receiver is nil.
	var sh *ServerlessHarvest
	event, err := CreateCustomEvent("myEvent", nil, time.Now())
	if nil != err {
		t.Fatal(err)
	}
	sh.Consume(event)
	buf := &bytes.Buffer{}
	sh.Write("arn", buf)
}

func TestServerlessHarvestEmpty(t *testing.T) {
	// Test that ServerlessHarvest.Write doesn't do anything if the harvest
	// is empty.
	sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim)
	buf := &bytes.Buffer{}
	sh.Write("arn", buf)
	if 0 != buf.Len() {
		t.Error(buf.String())
	}
}

func BenchmarkServerless(b *testing.B) {
	// The JSON creation in ServerlessHarvest.Write has not been optimized.
	// This benchmark would be useful for doing so.
	sh := NewServerlessHarvest(logger.ShimLogger{}, "the-version", serverlessGetenvShim)
	event, err := CreateCustomEvent("myEvent", nil, time.Now())
	if nil != err {
		b.Fatal(err)
	}

	b.ResetTimer()
	b.ReportAllocs()

	for i := 0; i < b.N; i++ {
		sh.Consume(event)
		buf := &bytes.Buffer{}
		sh.Write("arn", buf)
		if buf.Len() == 0 {
			b.Fatal(buf.String())
		}
	}
}