fleet/server/service/service_hosts_test.go

211 lines
5.6 KiB
Go
Raw Normal View History

2016-09-26 18:48:55 +00:00
package service
import (
"testing"
"time"
"github.com/WatchBeam/clock"
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
2021-06-26 04:46:51 +00:00
"github.com/fleetdm/fleet/v4/server/fleet"
"github.com/fleetdm/fleet/v4/server/mock"
"github.com/fleetdm/fleet/v4/server/ptr"
"github.com/fleetdm/fleet/v4/server/test"
"github.com/stretchr/testify/assert"
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
"github.com/stretchr/testify/require"
)
2016-10-07 17:26:48 +00:00
func TestListHosts(t *testing.T) {
ds := mysql.CreateMySQLDS(t)
defer ds.Close()
svc := newTestService(ds, nil, nil)
hosts, err := svc.ListHosts(test.UserContext(test.UserAdmin), fleet.HostListOptions{})
assert.Nil(t, err)
assert.Len(t, hosts, 0)
storedTime := time.Now().UTC()
_, err = ds.NewHost(&fleet.Host{
Hostname: "foo",
SeenTime: storedTime,
DetailUpdatedAt: time.Now(),
LabelUpdatedAt: time.Now(),
})
require.NoError(t, err)
hosts, err = svc.ListHosts(test.UserContext(test.UserAdmin), fleet.HostListOptions{})
require.NoError(t, err)
require.Len(t, hosts, 1)
format := "%Y-%m-%d %HH:%MM:%SS %Z"
assert.Equal(t, storedTime.Format(format), hosts[0].SeenTime.Format(format))
}
func TestDeleteHost(t *testing.T) {
ds := mysql.CreateMySQLDS(t)
defer ds.Close()
svc := newTestService(ds, nil, nil)
mockClock := clock.NewMockClock()
host := test.NewHost(t, ds, "foo", "192.168.1.10", "1", "1", mockClock.Now())
assert.NotZero(t, host.ID)
err := svc.DeleteHost(test.UserContext(test.UserAdmin), host.ID)
assert.Nil(t, err)
filter := fleet.TeamFilter{User: test.UserAdmin}
hosts, err := ds.ListHosts(filter, fleet.HostListOptions{})
assert.Nil(t, err)
assert.Len(t, hosts, 0)
}
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
func TestHostDetails(t *testing.T) {
ds := new(mock.Store)
svc := &Service{ds: ds}
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
host := &fleet.Host{ID: 3}
expectedLabels := []*fleet.Label{
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
{
Name: "foobar",
Description: "the foobar label",
},
}
ds.ListLabelsForHostFunc = func(hid uint) ([]*fleet.Label, error) {
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
return expectedLabels, nil
}
expectedPacks := []*fleet.Pack{
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
{
Name: "pack1",
},
{
Name: "pack2",
},
}
ds.ListPacksForHostFunc = func(hid uint) ([]*fleet.Pack, error) {
return expectedPacks, nil
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
}
ds.LoadHostSoftwareFunc = func(host *fleet.Host) error {
return nil
}
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
hostDetail, err := svc.getHostDetails(test.UserContext(test.UserAdmin), host)
Add host details in API responses (#223) Add label and pack information for the returned hosts in the single-host API endpoints. Example: ``` curl -k 'https://localhost:8080/api/v1/kolide/hosts/7' -H 'Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzZXNzaW9uX2tleSI6Ii9oNEZ4MUpEVmlvQWhtMC8wNUJKbzZpdldsUDZpMDhjQVBuZXRLeFIvWjNOUGgvMW9VdCsxQnFlNU1CVDVsMlU3ckVGMm5Sb1VxS3ZSUllzSmJJR2lBPT0ifQ.GQQsJgBU3JA1H1o4Y8fPjyfF78F_VY4c9AbrP5k0sCg' { "host": { "created_at": "2021-01-16T00:22:33Z", "updated_at": "2021-01-16T00:22:51Z", "id": 7, "detail_updated_at": "1970-01-02T00:00:00Z", "label_updated_at": "1970-01-02T00:00:00Z", "last_enrolled_at": "2021-01-16T00:22:33Z", "seen_time": "2021-01-16T00:22:51Z", "hostname": "55d91fc9c303", "uuid": "853a4588-0000-0000-a061-7d494d04e9c4", "platform": "ubuntu", "osquery_version": "4.6.0", "os_version": "Ubuntu 20.04.0", "build": "", "platform_like": "debian", "code_name": "", "uptime": 0, "memory": 16794206208, "cpu_type": "x86_64", "cpu_subtype": "158", "cpu_brand": "Intel(R) Core(TM) i9-9980HK CPU @ 2.40GHz\u0000\u0000\u0000\u0000\u0000\u0000\u0000", "cpu_physical_cores": 8, "cpu_logical_cores": 8, "hardware_vendor": "", "hardware_model": "", "hardware_version": "", "hardware_serial": "", "computer_name": "55d91fc9c303", "primary_ip": "", "primary_mac": "", "distributed_interval": 10, "config_tls_refresh": 0, "logger_tls_period": 10, "enroll_secret_name": "default", "labels": [ { "created_at": "2020-12-22T01:22:47Z", "updated_at": "2020-12-22T01:22:47Z", "id": 6, "name": "All Hosts", "description": "All hosts which have enrolled in Fleet", "query": "select 1;", "label_type": "builtin", "label_membership_type": "dynamic" } ], "packs": [ { "created_at": "2021-01-20T16:36:42Z", "updated_at": "2021-01-20T16:36:42Z", "id": 2, "name": "test" } ], "status": "offline", "display_text": "55d91fc9c303" } } ```
2021-01-25 21:05:02 +00:00
require.NoError(t, err)
assert.Equal(t, expectedLabels, hostDetail.Labels)
assert.Equal(t, expectedPacks, hostDetail.Packs)
}
func TestRefetchHost(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
host := &fleet.Host{ID: 3}
ds.HostFunc = func(hid uint) (*fleet.Host, error) {
return host, nil
}
ds.SaveHostFunc = func(host *fleet.Host) error {
assert.True(t, host.RefetchRequested)
return nil
}
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserAdmin), host.ID))
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserObserver), host.ID))
require.NoError(t, svc.RefetchHost(test.UserContext(test.UserMaintainer), host.ID))
}
func TestRefetchHostUserInTeams(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
host := &fleet.Host{ID: 3, TeamID: ptr.Uint(4)}
ds.HostFunc = func(hid uint) (*fleet.Host, error) {
return host, nil
}
ds.SaveHostFunc = func(host *fleet.Host) error {
assert.True(t, host.RefetchRequested)
return nil
}
maintainer := &fleet.User{
Teams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 4},
Role: fleet.RoleMaintainer,
},
}}
require.NoError(t, svc.RefetchHost(test.UserContext(maintainer), host.ID))
observer := &fleet.User{
Teams: []fleet.UserTeam{
{
Team: fleet.Team{ID: 4},
Role: fleet.RoleObserver,
},
}}
require.NoError(t, svc.RefetchHost(test.UserContext(observer), host.ID))
}
func TestAddHostsToTeamByFilter(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
expectedHostIDs := []uint{1, 2, 4}
expectedTeam := (*uint)(nil)
ds.ListHostsFunc = func(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) {
var hosts []*fleet.Host
for _, id := range expectedHostIDs {
hosts = append(hosts, &fleet.Host{ID: id})
}
return hosts, nil
}
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
assert.Equal(t, expectedTeam, teamID)
assert.Equal(t, expectedHostIDs, hostIDs)
return nil
}
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), expectedTeam, fleet.HostListOptions{}, nil))
}
func TestAddHostsToTeamByFilterLabel(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
expectedHostIDs := []uint{6}
expectedTeam := ptr.Uint(1)
expectedLabel := ptr.Uint(2)
ds.ListHostsInLabelFunc = func(filter fleet.TeamFilter, lid uint, opt fleet.HostListOptions) ([]*fleet.Host, error) {
assert.Equal(t, *expectedLabel, lid)
var hosts []*fleet.Host
for _, id := range expectedHostIDs {
hosts = append(hosts, &fleet.Host{ID: id})
}
return hosts, nil
}
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
assert.Equal(t, expectedHostIDs, hostIDs)
return nil
}
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), expectedTeam, fleet.HostListOptions{}, expectedLabel))
}
func TestAddHostsToTeamByFilterEmptyHosts(t *testing.T) {
ds := new(mock.Store)
svc := newTestService(ds, nil, nil)
ds.ListHostsFunc = func(filter fleet.TeamFilter, opt fleet.HostListOptions) ([]*fleet.Host, error) {
return []*fleet.Host{}, nil
}
ds.AddHostsToTeamFunc = func(teamID *uint, hostIDs []uint) error {
t.Error("add hosts func should not have been called")
return nil
}
require.NoError(t, svc.AddHostsToTeamByFilter(test.UserContext(test.UserAdmin), nil, fleet.HostListOptions{}, nil))
}