fleet/server/kolide/hosts_test.go
2017-03-09 11:36:24 -07:00

76 lines
1.9 KiB
Go

package kolide
import (
"testing"
"time"
"github.com/WatchBeam/clock"
"github.com/stretchr/testify/assert"
)
func TestResetHosts(t *testing.T) {
host := Host{}
result := host.ResetPrimaryNetwork()
assert.False(t, result)
host.NetworkInterfaces = []*NetworkInterface{
&NetworkInterface{
ID: 1,
IPAddress: "192.168.1.2",
},
&NetworkInterface{
ID: 2,
IPAddress: "192.168.1.3",
},
}
result = host.ResetPrimaryNetwork()
assert.True(t, result)
assert.Equal(t, uint(1), *host.PrimaryNetworkInterfaceID)
host.PrimaryNetworkInterfaceID = &host.NetworkInterfaces[1].ID
result = host.ResetPrimaryNetwork()
assert.False(t, result)
assert.Equal(t, uint(2), *host.PrimaryNetworkInterfaceID)
host.NetworkInterfaces = host.NetworkInterfaces[:1]
result = host.ResetPrimaryNetwork()
assert.True(t, result)
assert.Equal(t, uint(1), *host.PrimaryNetworkInterfaceID)
host.NetworkInterfaces = []*NetworkInterface{}
result = host.ResetPrimaryNetwork()
assert.True(t, result)
assert.Nil(t, host.PrimaryNetworkInterfaceID)
}
func TestHostStatus(t *testing.T) {
mockClock := clock.NewMockClock()
host := Host{}
host.SeenTime = mockClock.Now()
assert.Equal(t, StatusOnline, host.Status(mockClock.Now(), 60*time.Second))
host.SeenTime = mockClock.Now().Add(-1 * time.Minute)
assert.Equal(t, StatusOnline, host.Status(mockClock.Now(), 60*time.Second))
host.SeenTime = mockClock.Now().Add(-1 * time.Hour)
assert.Equal(t, StatusOffline, host.Status(mockClock.Now(), 60*time.Second))
host.SeenTime = mockClock.Now().Add(-35 * (24 * time.Hour)) // 35 days
assert.Equal(t, StatusMIA, host.Status(mockClock.Now(), 60*time.Second))
}
func TestHostIsNew(t *testing.T) {
mockClock := clock.NewMockClock()
host := Host{}
host.CreatedAt = mockClock.Now().AddDate(0, 0, -1)
assert.True(t, host.IsNew(mockClock.Now()))
host.CreatedAt = mockClock.Now().AddDate(0, 0, -2)
assert.False(t, host.IsNew(mockClock.Now()))
}