File: vm_test.go

package info (click to toggle)
golang-opentelemetry-contrib 0.56.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 4,884 kB
  • sloc: makefile: 278; sh: 211; sed: 1
file content (114 lines) | stat: -rw-r--r-- 2,557 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
114
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package azurevm

import (
	"context"
	"fmt"
	"net/http"
	"net/http/httptest"
	"testing"

	"github.com/stretchr/testify/assert"

	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/sdk/resource"
	semconv "go.opentelemetry.io/otel/semconv/v1.26.0"
)

func TestDetect(t *testing.T) {
	type input struct {
		jsonMetadata string
		statusCode   int
	}
	type expected struct {
		resource *resource.Resource
		err      bool
	}
	type testCase struct {
		input    input
		expected expected
	}

	testTable := []testCase{
		{
			input: input{
				jsonMetadata: `{ 
					"location": "us-west3",
					"resourceId": "/subscriptions/sid/resourceGroups/rid/providers/pname/name",
					"vmId": "43f65c49-8715-4639-88a9-be6d7eb749a5",
					"name": "localhost-3",
					"vmSize": "Standard_D2s_v3",
					"osType": "linux",
					"version": "6.5.0-26-generic"
				}`,
				statusCode: http.StatusOK,
			},
			expected: expected{
				resource: resource.NewWithAttributes(semconv.SchemaURL, []attribute.KeyValue{
					semconv.CloudProviderAzure,
					semconv.CloudPlatformAzureVM,
					semconv.CloudRegion("us-west3"),
					semconv.CloudResourceID("/subscriptions/sid/resourceGroups/rid/providers/pname/name"),
					semconv.HostID("43f65c49-8715-4639-88a9-be6d7eb749a5"),
					semconv.HostName("localhost-3"),
					semconv.HostType("Standard_D2s_v3"),
					semconv.OSTypeKey.String("linux"),
					semconv.OSVersion("6.5.0-26-generic"),
				}...),
				err: false,
			},
		},
		{
			input: input{
				jsonMetadata: `{`,
				statusCode:   http.StatusOK,
			},
			expected: expected{
				resource: nil,
				err:      true,
			},
		},
		{
			input: input{
				jsonMetadata: "",
				statusCode:   http.StatusNotFound,
			},
			expected: expected{
				resource: resource.Empty(),
				err:      false,
			},
		},
		{
			input: input{
				jsonMetadata: "",
				statusCode:   http.StatusInternalServerError,
			},
			expected: expected{
				resource: nil,
				err:      true,
			},
		},
	}

	for _, tCase := range testTable {
		svr := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(tCase.input.statusCode)

			if r.Header.Get("Metadata") == "True" {
				fmt.Fprint(w, tCase.input.jsonMetadata)
			}
		}))

		detector := New()
		detector.endpoint = svr.URL

		azureResource, err := detector.Detect(context.Background())

		svr.Close()

		assert.Equal(t, err != nil, tCase.expected.err)
		assert.Equal(t, tCase.expected.resource, azureResource)
	}
}