Skip to content

Commit 73bd871

Browse files
committed
add unit tests and simplify
we use SendError where we would have previously called reportPluginError (no log emitted) we don't set error message in cni report because the error message and event message fields both end up in the Message field in the cni telemetry service
1 parent 787eaef commit 73bd871

File tree

4 files changed

+58
-15
lines changed

4 files changed

+58
-15
lines changed

cni/network/plugin/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,7 @@ func printVersion() {
5353
}
5454

5555
func rootExecute() error {
56-
var (
57-
config common.PluginConfig
58-
)
56+
var config common.PluginConfig
5957

6058
config.Version = version
6159

cni/network/stateless/main.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,7 @@ func printVersion() {
5151
}
5252

5353
func rootExecute() error {
54-
var (
55-
config common.PluginConfig
56-
)
54+
var config common.PluginConfig
5755

5856
log.SetName(name)
5957
log.SetLevel(log.LevelInfo)

cni/telemetry/client/telemetry_client.go

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,32 @@ type TelemetryClient struct {
2121
lock sync.Mutex
2222
}
2323

24-
var Telemetry = NewTelemetryClient(&telemetry.CNIReport{})
24+
type TelemetryInterface interface {
25+
// Settings gets a pointer to the cni report struct, used to modify individual fields
26+
Settings() *telemetry.CNIReport
27+
// SetSettings REPLACES the pointer to the cni report struct and should only be used on startup
28+
SetSettings(settings *telemetry.CNIReport)
29+
IsConnected() bool
30+
ConnectTelemetry(logger *zap.Logger)
31+
StartAndConnectTelemetry(logger *zap.Logger)
32+
DisconnectTelemetry()
33+
SendEvent(msg string)
34+
SendError(err error)
35+
SendMetric(cniMetric *telemetry.AIMetric)
36+
}
37+
38+
var Telemetry TelemetryInterface = NewTelemetryClient()
2539

26-
func NewTelemetryClient(report *telemetry.CNIReport) *TelemetryClient {
40+
func NewTelemetryClient() *TelemetryClient {
2741
return &TelemetryClient{
28-
cniReportSettings: report,
42+
cniReportSettings: &telemetry.CNIReport{},
2943
}
3044
}
3145

3246
func (c *TelemetryClient) Settings() *telemetry.CNIReport {
3347
return c.cniReportSettings
3448
}
49+
3550
func (c *TelemetryClient) SetSettings(settings *telemetry.CNIReport) {
3651
c.cniReportSettings = settings
3752
}
@@ -59,15 +74,14 @@ func (c *TelemetryClient) DisconnectTelemetry() {
5974
c.tb.Close()
6075
}
6176

62-
func (c *TelemetryClient) sendTelemetry(msg string, errMsg string) {
77+
func (c *TelemetryClient) sendEvent(msg string) {
6378
if c.tb == nil {
6479
return
6580
}
6681
c.lock.Lock()
6782
defer c.lock.Unlock()
6883
eventMsg := fmt.Sprintf("[%d] %s", os.Getpid(), msg)
6984
c.cniReportSettings.EventMessage = eventMsg
70-
c.cniReportSettings.ErrorMessage = errMsg
7185
telemetry.SendCNIEvent(c.tb, c.cniReportSettings)
7286
}
7387

@@ -80,14 +94,19 @@ func (c *TelemetryClient) sendLog(msg string) {
8094

8195
func (c *TelemetryClient) SendEvent(msg string) {
8296
c.sendLog(msg)
83-
c.sendTelemetry(msg, "")
97+
c.sendEvent(msg)
8498
}
99+
85100
func (c *TelemetryClient) SendError(err error) {
86101
if err == nil {
87102
return
88103
}
89-
c.sendTelemetry("", err.Error())
104+
// when the cni report reaches the telemetry service, the ai log message
105+
// is set to either the cni report's event message or error message,
106+
// whichever is not empty, so we can always just set the event message
107+
c.sendEvent(err.Error())
90108
}
109+
91110
func (c *TelemetryClient) SendMetric(cniMetric *telemetry.AIMetric) {
92111
if c.tb == nil || cniMetric == nil {
93112
return
Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
package telemetryclient
22

33
import (
4+
"errors"
5+
"regexp"
46
"testing"
57

8+
"github.com/Azure/azure-container-networking/telemetry"
69
"github.com/stretchr/testify/require"
710
"go.uber.org/zap"
811
)
912

13+
var errMockTelemetryClient = errors.New("mock telemetry client error")
14+
1015
func TestClient(t *testing.T) {
11-
emptyClient := NewTelemetryClient(nil)
16+
allowedErrorMsg := regexp.MustCompile(`^\[\d+\] mock telemetry client error`)
17+
allowedEventMsg := regexp.MustCompile(`^\[\d+\] telemetry event`)
18+
19+
emptyClient := NewTelemetryClient()
1220

1321
// an empty client should not cause panics
1422
require.NotPanics(t, func() { emptyClient.SendEvent("no errors") })
1523

24+
require.NotPanics(t, func() { emptyClient.SendError(errMockTelemetryClient) })
25+
1626
require.NotPanics(t, func() { emptyClient.DisconnectTelemetry() })
1727

1828
require.NotPanics(t, func() { emptyClient.sendLog("no errors") })
1929

20-
require.NotPanics(t, func() { emptyClient.sendTelemetry("no errors", "") })
30+
require.NotPanics(t, func() { emptyClient.sendEvent("no errors") })
2131

2232
logger, err := zap.NewDevelopment()
2333
require.NoError(t, err)
@@ -27,4 +37,22 @@ func TestClient(t *testing.T) {
2737

2838
// should set logger during connection
2939
require.Equal(t, logger, emptyClient.logger)
40+
41+
// for testing, we create a new telemetry buffer and assign it
42+
emptyClient.tb = &telemetry.TelemetryBuffer{}
43+
44+
// test sending error, event is empty
45+
require.NotPanics(t, func() { emptyClient.SendError(errMockTelemetryClient) })
46+
require.Regexp(t, allowedErrorMsg, emptyClient.Settings().EventMessage)
47+
48+
// test sending event, error is empty
49+
require.NotPanics(t, func() { emptyClient.SendEvent("telemetry event") })
50+
require.Regexp(t, allowedEventMsg, emptyClient.Settings().EventMessage)
51+
require.Equal(t, "", emptyClient.Settings().ErrorMessage)
52+
53+
// test sending aimetrics doesn't panic...
54+
require.NotPanics(t, func() { emptyClient.SendMetric(&telemetry.AIMetric{}) })
55+
// ...and doesn't affect the cni report
56+
require.Regexp(t, allowedEventMsg, emptyClient.Settings().EventMessage)
57+
require.Equal(t, "", emptyClient.Settings().ErrorMessage)
3058
}

0 commit comments

Comments
 (0)