From 2e333a4e2efb0dbf8d79665efadc0c3df57be7e8 Mon Sep 17 00:00:00 2001 From: Zachary Wasserman Date: Wed, 4 Nov 2020 12:03:06 -0800 Subject: [PATCH] Fix regression in list hosts (#6) Changes in https://github.com/kolide/fleet/pull/2327 broke the MySQL syntax for listing hosts with online status. This was not caught due to the lack of a unit test for the functionality. This PR adds a unit test and fixes the regression. --- server/datastore/datastore_hosts_test.go | 37 +++++++++++++++++++++++- server/datastore/datastore_test.go | 3 +- server/datastore/mysql/hosts.go | 8 ++--- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/server/datastore/datastore_hosts_test.go b/server/datastore/datastore_hosts_test.go index 67336975f..5bf61bafb 100644 --- a/server/datastore/datastore_hosts_test.go +++ b/server/datastore/datastore_hosts_test.go @@ -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{ @@ -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 diff --git a/server/datastore/datastore_test.go b/server/datastore/datastore_test.go index 482a68229..f94a17767 100644 --- a/server/datastore/datastore_test.go +++ b/server/datastore/datastore_test.go @@ -40,7 +40,8 @@ var testFunctions = [...]func(*testing.T, kolide.Datastore){ testListUniqueHostsInLabels, testSaveHosts, testDeleteHost, - testListHost, + testListHosts, + testListHostsStatus, testListHostsInPack, testListPacksForHost, testHostIDsByName, diff --git a/server/datastore/mysql/hosts.go b/server/datastore/mysql/hosts.go index ab1a328b7..efdcc6557 100644 --- a/server/datastore/mysql/hosts.go +++ b/server/datastore/mysql/hosts.go @@ -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)