-
Notifications
You must be signed in to change notification settings - Fork 169
Expand file tree
/
Copy pathconnection_identifier_shape_telemetry_test.go
More file actions
114 lines (104 loc) · 4.26 KB
/
Copy pathconnection_identifier_shape_telemetry_test.go
File metadata and controls
114 lines (104 loc) · 4.26 KB
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
package gosnowflake
import (
"context"
"sync"
"testing"
sfconfig "github.com/snowflakedb/gosnowflake/v2/internal/config"
)
// newTestShapeConn returns a snowflakeConn with an enabled in-memory telemetry
// client suitable for unit-testing connectionIdentifierShapeTelemetry without
// requiring a real Snowflake account or HTTP plumbing. The returned conn's
// telemetry.sr is nil, which is safe because the tests only exercise addLog
// (which appends to st.logs without touching st.sr).
func newTestShapeConn() *snowflakeConn {
return &snowflakeConn{
ctx: context.Background(),
telemetry: &snowflakeTelemetry{
mutex: &sync.Mutex{},
enabled: true,
flushSize: defaultFlushSize,
},
}
}
func TestConnectionIdentifierShapeTelemetryEmitsExpectedPayload(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, "")
cfg := &Config{}
sfconfig.SetInputShape(cfg, &sfconfig.ConnectionIdentifierShape{
AccountProvided: true,
AccountWithRegion: true,
AccountOrgProvided: true,
RegionProvided: false,
HostProvided: false,
})
sc := newTestShapeConn()
sc.connectionIdentifierShapeTelemetry(cfg)
assertEqualF(t, len(sc.telemetry.logs), 1, "expected exactly one telemetry record")
msg := sc.telemetry.logs[0].Message
assertEqualE(t, msg[typeKey], connectionIdentifierShape)
assertEqualE(t, msg[sourceKey], telemetrySource)
assertEqualE(t, msg[driverTypeKey], "Go")
assertEqualE(t, msg[accountProvidedKey], "true")
assertEqualE(t, msg[accountWithRegionKey], "true")
assertEqualE(t, msg[accountOrgProvidedKey], "true")
assertEqualE(t, msg[regionProvidedKey], "false")
assertEqualE(t, msg[hostProvidedKey], "false")
assertNotEqualE(t, msg[driverVersionKey], "",
"driver version field should carry SnowflakeGoDriverVersion")
assertNotEqualE(t, msg[golangVersionKey], "",
"golang version field should carry runtime.Version()")
}
func TestConnectionIdentifierShapeTelemetrySkipsWhenInputShapeNil(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, "")
sc := newTestShapeConn()
// A freshly-constructed Config has no shape set (SetInputShape not called).
sc.connectionIdentifierShapeTelemetry(&Config{})
assertEmptyE(t, sc.telemetry.logs,
"expected no telemetry record when shape is nil")
}
func TestConnectionIdentifierShapeTelemetrySkipsWhenCfgNil(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, "")
sc := newTestShapeConn()
sc.connectionIdentifierShapeTelemetry(nil)
assertEmptyE(t, sc.telemetry.logs,
"expected no telemetry record when cfg is nil")
}
func TestConnectionIdentifierShapeTelemetryHonorsEnvKillSwitch(t *testing.T) {
// Only case-insensitive "true" disables, matching SF_DISABLE_MINICORE
// and the CHANGELOG documentation.
for _, v := range []string{"true", "True", "TRUE"} {
t.Run("disabled_by_"+v, func(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, v)
cfg := &Config{}
sfconfig.SetInputShape(cfg, &sfconfig.ConnectionIdentifierShape{AccountProvided: true})
sc := newTestShapeConn()
sc.connectionIdentifierShapeTelemetry(cfg)
assertEmptyE(t, sc.telemetry.logs,
"expected no telemetry record when "+disableConnectionShapeEnv+"="+v)
})
}
}
func TestConnectionIdentifierShapeTelemetryEnvVarOffByDefault(t *testing.T) {
// Anything other than case-insensitive "true" (including former truthy
// aliases like "1" / "yes") must leave emission enabled.
for _, v := range []string{"", "0", "1", "yes", "Yes", "false", "no", "anything-else"} {
t.Run("not_disabled_by_"+v, func(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, v)
cfg := &Config{}
sfconfig.SetInputShape(cfg, &sfconfig.ConnectionIdentifierShape{AccountProvided: true})
sc := newTestShapeConn()
sc.connectionIdentifierShapeTelemetry(cfg)
assertEqualF(t, len(sc.telemetry.logs), 1,
"expected exactly one telemetry record when "+disableConnectionShapeEnv+"="+v)
})
}
}
func TestConnectionIdentifierShapeTelemetryRespectsTelemetryDisabled(t *testing.T) {
t.Setenv(disableConnectionShapeEnv, "")
cfg := &Config{}
sfconfig.SetInputShape(cfg, &sfconfig.ConnectionIdentifierShape{AccountProvided: true})
sc := newTestShapeConn()
sc.telemetry.enabled = false // server said CLIENT_TELEMETRY_ENABLED=false
sc.connectionIdentifierShapeTelemetry(cfg)
assertEmptyE(t, sc.telemetry.logs,
"expected no telemetry record when sc.telemetry.enabled=false")
}