mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
8cdf61f8df
* Remove inmem store * Use full package name for db to avoid conflicts * Fix lint * Remove unneeded check/comment
81 lines
2.0 KiB
Go
81 lines
2.0 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/mysql"
|
|
"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"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestServiceListPacks(t *testing.T) {
|
|
ds := mysql.CreateMySQLDS(t)
|
|
defer ds.Close()
|
|
|
|
svc := newTestService(ds, nil, nil)
|
|
|
|
queries, err := svc.ListPacks(test.UserContext(test.UserAdmin), fleet.ListOptions{})
|
|
assert.Nil(t, err)
|
|
assert.Len(t, queries, 0)
|
|
|
|
_, err = ds.NewPack(&fleet.Pack{
|
|
Name: "foo",
|
|
})
|
|
assert.Nil(t, err)
|
|
|
|
queries, err = svc.ListPacks(test.UserContext(test.UserAdmin), fleet.ListOptions{})
|
|
assert.Nil(t, err)
|
|
assert.Len(t, queries, 1)
|
|
}
|
|
|
|
func TestGetPack(t *testing.T) {
|
|
ds := mysql.CreateMySQLDS(t)
|
|
defer ds.Close()
|
|
|
|
svc := newTestService(ds, nil, nil)
|
|
|
|
pack := &fleet.Pack{
|
|
Name: "foo",
|
|
}
|
|
_, err := ds.NewPack(pack)
|
|
assert.Nil(t, err)
|
|
assert.NotZero(t, pack.ID)
|
|
|
|
packVerify, err := svc.GetPack(test.UserContext(test.UserAdmin), pack.ID)
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, pack.ID, packVerify.ID)
|
|
}
|
|
|
|
func TestNewSavesTargets(t *testing.T) {
|
|
ds := new(mock.Store)
|
|
svc := newTestService(ds, nil, nil)
|
|
|
|
ds.NewPackFunc = func(pack *fleet.Pack, opts ...fleet.OptionalArg) (*fleet.Pack, error) {
|
|
return pack, nil
|
|
}
|
|
ds.NewActivityFunc = func(user *fleet.User, activityType string, details *map[string]interface{}) error {
|
|
return nil
|
|
}
|
|
|
|
packPayload := fleet.PackPayload{
|
|
Name: ptr.String("foo"),
|
|
HostIDs: &[]uint{123},
|
|
LabelIDs: &[]uint{456},
|
|
TeamIDs: &[]uint{789},
|
|
}
|
|
pack, _ := svc.NewPack(test.UserContext(test.UserAdmin), packPayload)
|
|
|
|
require.Len(t, pack.HostIDs, 1)
|
|
require.Len(t, pack.LabelIDs, 1)
|
|
require.Len(t, pack.TeamIDs, 1)
|
|
assert.Equal(t, uint(123), pack.HostIDs[0])
|
|
assert.Equal(t, uint(456), pack.LabelIDs[0])
|
|
assert.Equal(t, uint(789), pack.TeamIDs[0])
|
|
assert.True(t, ds.NewActivityFuncInvoked)
|
|
}
|