2018-07-19 20:41:29 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
package telemetry
|
|
|
|
|
|
|
|
import (
|
2018-09-26 15:00:54 +01:00
|
|
|
"context"
|
2018-07-19 20:41:29 +01:00
|
|
|
"os"
|
|
|
|
"testing"
|
2018-09-26 15:00:54 +01:00
|
|
|
"time"
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2018-09-26 15:00:54 +01:00
|
|
|
"github.com/zeebo/admission/admmonkit"
|
2018-07-19 20:41:29 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestNewClient_IntervalIsZero(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { assert.NoError(t, s.Close()) }()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "testapp",
|
|
|
|
Instance: "testinst",
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
assert.Equal(t, client.interval, DefaultInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_ApplicationAndArgsAreEmpty(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
oldArgs := os.Args
|
|
|
|
|
|
|
|
defer func() {
|
2018-09-11 14:13:25 +01:00
|
|
|
assert.NoError(t, s.Close())
|
2018-07-19 20:41:29 +01:00
|
|
|
os.Args = oldArgs
|
|
|
|
}()
|
|
|
|
|
|
|
|
os.Args = nil
|
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "",
|
|
|
|
Instance: "testinst",
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
assert.Equal(t, DefaultApplication, client.opts.Application)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_ApplicationIsEmpty(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { assert.NoError(t, s.Close()) }()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "",
|
|
|
|
Instance: "testinst",
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
assert.Equal(t, client.opts.Application, os.Args[0])
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_InstanceIsEmpty(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { assert.NoError(t, s.Close()) }()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "qwe",
|
|
|
|
Instance: "",
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
|
|
|
|
assert.Equal(t, client.opts.Application, "qwe")
|
|
|
|
assert.Equal(t, client.interval, DefaultInterval)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_RegistryIsNil(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { assert.NoError(t, s.Close()) }()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "qwe",
|
|
|
|
Instance: "",
|
|
|
|
Interval: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
|
|
|
|
assert.Equal(t, client.opts.Application, "qwe")
|
|
|
|
assert.Equal(t, client.interval, DefaultInterval)
|
|
|
|
assert.Equal(t, client.opts.Registry, monkit.Default)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestNewClient_PacketSizeIsZero(t *testing.T) {
|
|
|
|
s, err := Listen("127.0.0.1:0")
|
|
|
|
assert.NoError(t, err)
|
2018-09-11 14:13:25 +01:00
|
|
|
defer func() { assert.NoError(t, s.Close()) }()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
|
|
|
client, err := NewClient(s.Addr(), ClientOpts{
|
|
|
|
Application: "qwe",
|
|
|
|
Instance: "",
|
|
|
|
Interval: 0,
|
|
|
|
PacketSize: 0,
|
|
|
|
})
|
|
|
|
|
|
|
|
assert.NotNil(t, client)
|
|
|
|
|
|
|
|
assert.Equal(t, client.opts.InstanceId, []byte(DefaultInstanceID()))
|
2018-07-27 18:01:21 +01:00
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
assert.Equal(t, client.opts.Application, "qwe")
|
|
|
|
assert.Equal(t, client.interval, DefaultInterval)
|
|
|
|
assert.Equal(t, client.opts.Registry, monkit.Default)
|
|
|
|
assert.Equal(t, client.opts.PacketSize, DefaultPacketSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRun_ReportNoCalled(t *testing.T) {
|
2018-09-26 15:00:54 +01:00
|
|
|
client, err := NewClient("127.0.0.1:0", ClientOpts{
|
|
|
|
Application: "qwe",
|
|
|
|
Instance: "",
|
|
|
|
Interval: time.Millisecond,
|
|
|
|
PacketSize: 0,
|
|
|
|
})
|
|
|
|
assert.NoError(t, err)
|
2018-07-19 20:41:29 +01:00
|
|
|
|
2018-09-26 15:00:54 +01:00
|
|
|
client.send = func(context.Context, admmonkit.Options) error {
|
|
|
|
t.Fatal("shouldn't be called")
|
|
|
|
return nil
|
|
|
|
}
|
2018-07-19 20:41:29 +01:00
|
|
|
|
2018-09-26 15:00:54 +01:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
cancel()
|
2018-07-19 20:41:29 +01:00
|
|
|
|
2018-09-26 15:00:54 +01:00
|
|
|
client.Run(ctx)
|
2018-07-19 20:41:29 +01:00
|
|
|
}
|