2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-06 21:28:07 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
2016-09-06 21:28:07 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-11-11 17:59:12 +00:00
|
|
|
"github.com/fleetdm/fleet/server/config"
|
|
|
|
"github.com/fleetdm/fleet/server/datastore/inmem"
|
|
|
|
"github.com/fleetdm/fleet/server/kolide"
|
2016-09-06 21:28:07 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func TestListHosts(t *testing.T) {
|
2016-11-25 18:08:22 +00:00
|
|
|
ds, err := inmem.New(config.TestConfig())
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-23 01:33:04 +00:00
|
|
|
svc, err := newTestService(ds, nil, nil)
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
hosts, err := svc.ListHosts(ctx, kolide.HostListOptions{})
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
|
|
|
|
_, err = ds.NewHost(&kolide.Host{
|
|
|
|
HostName: "foo",
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
hosts, err = svc.ListHosts(ctx, kolide.HostListOptions{})
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, hosts, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetHost(t *testing.T) {
|
2016-11-25 18:08:22 +00:00
|
|
|
ds, err := inmem.New(config.TestConfig())
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-23 01:33:04 +00:00
|
|
|
svc, err := newTestService(ds, nil, nil)
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
host, err := ds.NewHost(&kolide.Host{
|
|
|
|
HostName: "foo",
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, host.ID)
|
|
|
|
|
|
|
|
hostVerify, err := svc.GetHost(ctx, host.ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, host.ID, hostVerify.ID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteHost(t *testing.T) {
|
2016-11-25 18:08:22 +00:00
|
|
|
ds, err := inmem.New(config.TestConfig())
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-23 01:33:04 +00:00
|
|
|
svc, err := newTestService(ds, nil, nil)
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
host, err := ds.NewHost(&kolide.Host{
|
|
|
|
HostName: "foo",
|
|
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.NotZero(t, host.ID)
|
|
|
|
|
|
|
|
err = svc.DeleteHost(ctx, host.ID)
|
|
|
|
assert.Nil(t, err)
|
|
|
|
|
2020-03-30 02:19:54 +00:00
|
|
|
hosts, err := ds.ListHosts(kolide.HostListOptions{})
|
2016-09-06 21:28:07 +00:00
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.Len(t, hosts, 0)
|
|
|
|
|
|
|
|
}
|