Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 36 additions & 1 deletion server/datastore/datastore_hosts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func testDeleteHost(t *testing.T, ds kolide.Datastore) {
assert.NotNil(t, err)
}

func testListHost(t *testing.T, ds kolide.Datastore) {
func testListHosts(t *testing.T, ds kolide.Datastore) {
hosts := []*kolide.Host{}
for i := 0; i < 10; i++ {
host, err := ds.NewHost(&kolide.Host{
Expand Down Expand Up @@ -155,6 +155,41 @@ func testListHost(t *testing.T, ds kolide.Datastore) {
require.Equal(t, hosts[0].ID, hosts2[0].ID)
}

func testListHostsStatus(t *testing.T, ds kolide.Datastore) {
for i := 0; i < 10; i++ {
_, err := ds.NewHost(&kolide.Host{
DetailUpdateTime: time.Now(),
LabelUpdateTime: time.Now(),
SeenTime: time.Now().Add(-time.Duration(i) *time.Minute),
OsqueryHostID: strconv.Itoa(i),
NodeKey: fmt.Sprintf("%d", i),
UUID: fmt.Sprintf("%d", i),
HostName: fmt.Sprintf("foo.local%d", i),
})
assert.Nil(t, err)
if err != nil {
return
}
}

hosts, err := ds.ListHosts(kolide.HostListOptions{StatusFilter: "online"})
require.Nil(t, err)
assert.Equal(t, 1, len(hosts))

hosts, err = ds.ListHosts(kolide.HostListOptions{StatusFilter: "offline"})
require.Nil(t, err)
assert.Equal(t, 9, len(hosts))

hosts, err = ds.ListHosts(kolide.HostListOptions{StatusFilter: "mia"})
require.Nil(t, err)
assert.Equal(t, 0, len(hosts))

hosts, err = ds.ListHosts(kolide.HostListOptions{StatusFilter: "new"})
require.Nil(t, err)
assert.Equal(t, 10, len(hosts))
}


func testEnrollHost(t *testing.T, ds kolide.Datastore) {
test.AddAllHostsLabel(t, ds)
var hosts []*kolide.Host
Expand Down
3 changes: 2 additions & 1 deletion server/datastore/datastore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ var testFunctions = [...]func(*testing.T, kolide.Datastore){
testListUniqueHostsInLabels,
testSaveHosts,
testDeleteHost,
testListHost,
testListHosts,
testListHostsStatus,
testListHostsInPack,
testListPacksForHost,
testHostIDsByName,
Expand Down
8 changes: 4 additions & 4 deletions server/datastore/mysql/hosts.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,16 @@ func (d *Datastore) ListHosts(opt kolide.HostListOptions) ([]*kolide.Host, error
var params []interface{}
switch opt.StatusFilter {
case "new":
sql += "AND DATE_ADD(created_at, INTERVAL 1 DAY) >= ?"
sql += "WHERE DATE_ADD(created_at, INTERVAL 1 DAY) >= ?"
params = append(params, time.Now())
case "online":
sql += fmt.Sprintf("AND DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) > ?", kolide.OnlineIntervalBuffer)
sql += fmt.Sprintf("WHERE DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) > ?", kolide.OnlineIntervalBuffer)
params = append(params, time.Now())
case "offline":
sql += fmt.Sprintf("AND DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) <= ? AND DATE_ADD(seen_time, INTERVAL 30 DAY) >= ?", kolide.OnlineIntervalBuffer)
sql += fmt.Sprintf("WHERE DATE_ADD(seen_time, INTERVAL LEAST(distributed_interval, config_tls_refresh) + %d SECOND) <= ? AND DATE_ADD(seen_time, INTERVAL 30 DAY) >= ?", kolide.OnlineIntervalBuffer)
params = append(params, time.Now(), time.Now())
case "mia":
sql += "AND DATE_ADD(seen_time, INTERVAL 30 DAY) <= ?"
sql += "WHERE DATE_ADD(seen_time, INTERVAL 30 DAY) <= ?"
params = append(params, time.Now())
}
sql = appendListOptionsToSQL(sql, opt.ListOptions)
Expand Down