-
Notifications
You must be signed in to change notification settings - Fork 242
/
Copy pathtelemetrywrapper_test.go
178 lines (151 loc) · 4.23 KB
/
telemetrywrapper_test.go
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package aitelemetry
import (
"fmt"
"net/http"
"net/url"
"os"
"path/filepath"
"runtime"
"testing"
"github.com/Azure/azure-container-networking/common"
"github.com/Azure/azure-container-networking/log"
"github.com/Azure/azure-container-networking/platform"
)
var (
th TelemetryHandle
hostAgentUrl = "localhost:3501"
getCloudResponse = "AzurePublicCloud"
httpURL = "http://" + hostAgentUrl
)
func TestMain(m *testing.M) {
log.SetName("testaitelemetry")
log.SetLevel(log.LevelInfo)
err := log.SetTargetLogDirectory(log.TargetLogfile, "/var/log/")
if err == nil {
fmt.Printf("TestST LogDir configuration succeeded\n")
}
p := platform.NewExecClient(nil)
if runtime.GOOS == "linux" {
//nolint:errcheck // initial test setup
p.ExecuteCommand("cp metadata_test.json /tmp/azuremetadata.json")
} else {
metadataFile := filepath.FromSlash(os.Getenv("TEMP")) + "\\azuremetadata.json"
cmd := fmt.Sprintf("copy metadata_test.json %s", metadataFile)
//nolint:errcheck // initial test setup
p.ExecuteCommand(cmd)
}
hostu, _ := url.Parse("tcp://" + hostAgentUrl)
hostAgent, err := common.NewListener(hostu)
if err != nil {
fmt.Printf("Failed to create agent, err:%v.\n", err)
return
}
hostAgent.AddHandler("/", handleGetCloud)
err = hostAgent.Start(make(chan error, 1))
if err != nil {
fmt.Printf("Failed to start agent, err:%v.\n", err)
return
}
exitCode := m.Run()
if runtime.GOOS == "linux" {
//nolint:errcheck // test cleanup
p.ExecuteCommand("rm /tmp/azuremetadata.json")
} else {
metadataFile := filepath.FromSlash(os.Getenv("TEMP")) + "\\azuremetadata.json"
cmd := fmt.Sprintf("del %s", metadataFile)
//nolint:errcheck // initial test cleanup
p.ExecuteCommand(cmd)
}
log.Close()
hostAgent.Stop()
os.Exit(exitCode)
}
func handleGetCloud(w http.ResponseWriter, req *http.Request) {
w.Write([]byte(getCloudResponse))
}
func TestEmptyAIKey(t *testing.T) {
var err error
aiConfig := AIConfig{
AppName: "testapp",
AppVersion: "v1.0.26",
BatchSize: 4096,
BatchInterval: 2,
RefreshTimeout: 10,
DebugMode: true,
DisableMetadataRefreshThread: true,
}
_, err = NewAITelemetry(httpURL, "", aiConfig)
if err == nil {
t.Errorf("Error intializing AI telemetry:%v", err)
}
}
func TestNewAITelemetry(t *testing.T) {
var err error
aiConfig := AIConfig{
AppName: "testapp",
AppVersion: "v1.0.26",
BatchSize: 4096,
BatchInterval: 2,
RefreshTimeout: 10,
GetEnvRetryCount: 1,
GetEnvRetryWaitTimeInSecs: 2,
DebugMode: true,
DisableMetadataRefreshThread: true,
}
th, err = NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
if th == nil {
t.Errorf("Error intializing AI telemetry: %v", err)
}
}
func TestTrackMetric(t *testing.T) {
metric := Metric{
Name: "test",
Value: 1.0,
CustomDimensions: make(map[string]string),
}
metric.CustomDimensions["dim1"] = "col1"
th.TrackMetric(metric)
}
func TestTrackLog(t *testing.T) {
report := Report{
Message: "test",
Context: "10a",
CustomDimensions: make(map[string]string),
}
report.CustomDimensions["dim1"] = "col1"
th.TrackLog(report)
}
func TestTrackEvent(t *testing.T) {
event := Event{
EventName: "testEvent",
ResourceID: "SomeResourceId",
Properties: make(map[string]string),
}
event.Properties["P1"] = "V1"
event.Properties["P2"] = "V2"
th.TrackEvent(event)
}
func TestFlush(t *testing.T) {
th.Flush()
}
func TestClose(t *testing.T) {
th.Close(10)
}
func TestClosewithoutSend(t *testing.T) {
var err error
aiConfig := AIConfig{
AppName: "testapp",
AppVersion: "v1.0.26",
BatchSize: 4096,
BatchInterval: 2,
DisableMetadataRefreshThread: true,
RefreshTimeout: 10,
GetEnvRetryCount: 1,
GetEnvRetryWaitTimeInSecs: 2,
}
thtest, err := NewAITelemetry(httpURL, "00ca2a73-c8d6-4929-a0c2-cf84545ec225", aiConfig)
if thtest == nil {
t.Errorf("Error intializing AI telemetry:%v", err)
}
thtest.Close(10)
}