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.
This commit is contained in:
Zachary Wasserman 2020-11-04 12:03:06 -08:00 committed by GitHub
parent f06c3cff93
commit 2e333a4e2e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 42 additions and 6 deletions

View File

@ -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

View File

@ -40,7 +40,8 @@ var testFunctions = [...]func(*testing.T, kolide.Datastore){
testListUniqueHostsInLabels,
testSaveHosts,
testDeleteHost,
testListHost,
testListHosts,
testListHostsStatus,
testListHostsInPack,
testListPacksForHost,
testHostIDsByName,

View File

@ -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)