Fetch all data for a host when listing it (#1293)

This commit is contained in:
Tomas Touceda 2021-07-02 12:59:42 -03:00 committed by GitHub
parent 8384034e04
commit 05ba38ce2f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 36 deletions

View File

@ -255,6 +255,13 @@ func testListHosts(t *testing.T, ds fleet.Datastore) {
require.Nil(t, err)
assert.Equal(t, len(hosts), len(hosts2))
for _, host := range hosts2 {
i, err := strconv.Atoi(host.UUID)
assert.Nil(t, err)
assert.True(t, i >= 0)
assert.True(t, strings.HasPrefix(host.Hostname, "foo.local"))
}
// Test with logic for only a few hosts
hosts2, err = ds.ListHosts(filter, fleet.HostListOptions{ListOptions: fleet.ListOptions{PerPage: 4, Page: 0}})
require.Nil(t, err)
@ -422,6 +429,13 @@ func testEnrollHost(t *testing.T, ds fleet.Datastore) {
team, err := ds.NewTeam(&fleet.Team{Name: "team1"})
require.NoError(t, err)
filter := fleet.TeamFilter{User: test.UserAdmin}
hosts, err := ds.ListHosts(filter, fleet.HostListOptions{})
require.Nil(t, err)
for _, host := range hosts {
assert.Zero(t, host.LastEnrolledAt)
}
for _, tt := range enrollTests {
h, err := ds.EnrollHost(tt.uuid, tt.nodeKey, &team.ID, 0)
require.Nil(t, err)
@ -437,6 +451,13 @@ func testEnrollHost(t *testing.T, ds fleet.Datastore) {
_, err = ds.EnrollHost(tt.uuid, tt.nodeKey+"new", nil, 10*time.Second)
require.Error(t, err)
}
hosts, err = ds.ListHosts(filter, fleet.HostListOptions{})
require.Nil(t, err)
for _, host := range hosts {
assert.NotZero(t, host.LastEnrolledAt)
}
}
func testAuthenticateHost(t *testing.T, ds fleet.Datastore) {

View File

@ -283,42 +283,7 @@ func (d *Datastore) Host(id uint) (*fleet.Host, error) {
func (d *Datastore) ListHosts(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) {
sql := `SELECT
h.id,
h.osquery_host_id,
h.created_at,
h.updated_at,
h.detail_updated_at,
h.node_key,
h.hostname,
h.uuid,
h.platform,
h.osquery_version,
h.os_version,
h.build,
h.platform_like,
h.code_name,
h.uptime,
h.memory,
h.cpu_type,
h.cpu_subtype,
h.cpu_brand,
h.cpu_physical_cores,
h.cpu_logical_cores,
h.hardware_vendor,
h.hardware_model,
h.hardware_version,
h.hardware_serial,
h.computer_name,
h.primary_ip_id,
h.seen_time,
h.distributed_interval,
h.logger_tls_period,
h.config_tls_refresh,
h.primary_ip,
h.primary_mac,
h.label_updated_at,
h.team_id,
h.refetch_requested,
h.*,
t.name AS team_name
`

View File

@ -2,6 +2,7 @@ package service
import (
"testing"
"time"
"github.com/fleetdm/fleet/v4/server/config"
"github.com/fleetdm/fleet/v4/server/datastore/inmem"
@ -23,14 +24,18 @@ func TestListHosts(t *testing.T) {
assert.Nil(t, err)
assert.Len(t, hosts, 0)
storedTime := time.Now()
_, err = ds.NewHost(&fleet.Host{
Hostname: "foo",
LastEnrolledAt: storedTime,
})
assert.Nil(t, err)
hosts, err = svc.ListHosts(test.UserContext(test.UserAdmin), fleet.HostListOptions{})
assert.Nil(t, err)
assert.Len(t, hosts, 1)
assert.Equal(t, storedTime, hosts[0].LastEnrolledAt)
}
func TestDeleteHost(t *testing.T) {