mirror of
https://github.com/fleetdm/fleet
synced 2026-05-24 09:28:54 +00:00
Adding new hosts sql tests. (#16826)
# Checklist for submitter If some of the following don't apply, delete the relevant line. <!-- Note that API documentation changes are now addressed by the product design team. --> - [x] Added/updated tests
This commit is contained in:
parent
a72d85602e
commit
98e9de6861
1 changed files with 71 additions and 2 deletions
|
|
@ -99,6 +99,7 @@ func TestHosts(t *testing.T) {
|
|||
{"IDsByName", testHostsIDsByName},
|
||||
{"Additional", testHostsAdditional},
|
||||
{"ByIdentifier", testHostsByIdentifier},
|
||||
{"HostLiteByIdentifierAndID", testHostLiteByIdentifierAndID},
|
||||
{"AddToTeam", testHostsAddToTeam},
|
||||
{"SaveUsers", testHostsSaveUsers},
|
||||
{"SaveHostUsers", testHostsSaveHostUsers},
|
||||
|
|
@ -2483,8 +2484,76 @@ func testHostsByIdentifier(t *testing.T, ds *Datastore) {
|
|||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
h, err = ds.HostByIdentifier(context.Background(), "foobar")
|
||||
require.Error(t, err)
|
||||
require.Nil(t, h)
|
||||
assert.ErrorIs(t, err, sql.ErrNoRows)
|
||||
assert.Nil(t, h)
|
||||
}
|
||||
|
||||
func testHostLiteByIdentifierAndID(t *testing.T, ds *Datastore) {
|
||||
now := time.Now().UTC().Truncate(time.Second)
|
||||
for i := 1; i <= 10; i++ {
|
||||
_, err := ds.NewHost(
|
||||
context.Background(), &fleet.Host{
|
||||
DetailUpdatedAt: now,
|
||||
LabelUpdatedAt: now,
|
||||
PolicyUpdatedAt: now,
|
||||
SeenTime: now,
|
||||
OsqueryHostID: ptr.String(fmt.Sprintf("osquery_host_id_%d", i)),
|
||||
NodeKey: ptr.String(fmt.Sprintf("node_key_%d", i)),
|
||||
UUID: fmt.Sprintf("uuid_%d", i),
|
||||
Hostname: fmt.Sprintf("hostname_%d", i),
|
||||
HardwareSerial: fmt.Sprintf("serial_%d", i),
|
||||
},
|
||||
)
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
var (
|
||||
h *fleet.HostLite
|
||||
err error
|
||||
)
|
||||
identifier := "uuid_1"
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), identifier)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint(1), h.ID)
|
||||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
// Also test fetching host by ID
|
||||
h, err = ds.HostLiteByID(context.Background(), h.ID)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, identifier, h.UUID)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "osquery_host_id_2")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint(2), h.ID)
|
||||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "node_key_4")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint(4), h.ID)
|
||||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "hostname_7")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint(7), h.ID)
|
||||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "serial_9")
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, uint(9), h.ID)
|
||||
assert.Equal(t, now.UTC(), h.SeenTime)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "foobar")
|
||||
assert.ErrorIs(t, err, sql.ErrNoRows)
|
||||
assert.Nil(t, h)
|
||||
|
||||
h, err = ds.HostLiteByIdentifier(context.Background(), "")
|
||||
assert.ErrorIs(t, err, sql.ErrNoRows)
|
||||
assert.Nil(t, h)
|
||||
|
||||
h, err = ds.HostLiteByID(context.Background(), 0)
|
||||
assert.ErrorIs(t, err, sql.ErrNoRows)
|
||||
assert.Nil(t, h)
|
||||
|
||||
}
|
||||
|
||||
func testHostsAddToTeam(t *testing.T, ds *Datastore) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue