2020-03-23 01:33:04 +00:00
|
|
|
package live_query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-10-18 13:32:17 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/datastore/redis/redistest"
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/test"
|
2020-03-23 01:33:04 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestRedisLiveQuery(t *testing.T) {
|
|
|
|
for _, f := range testFunctions {
|
2020-04-21 00:29:02 +00:00
|
|
|
t.Run(test.FunctionName(f), func(t *testing.T) {
|
2021-09-01 20:32:57 +00:00
|
|
|
t.Run("standalone", func(t *testing.T) {
|
2021-10-18 13:32:17 +00:00
|
|
|
store := setupRedisLiveQuery(t, false)
|
2021-09-01 20:32:57 +00:00
|
|
|
f(t, store)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("cluster", func(t *testing.T) {
|
2021-10-18 13:32:17 +00:00
|
|
|
store := setupRedisLiveQuery(t, true)
|
2021-09-01 20:32:57 +00:00
|
|
|
f(t, store)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-18 13:32:17 +00:00
|
|
|
func setupRedisLiveQuery(t *testing.T, cluster bool) *redisLiveQuery {
|
2022-01-19 15:52:14 +00:00
|
|
|
pool := redistest.SetupRedis(t, "*livequery", cluster, true, true)
|
2021-10-18 13:32:17 +00:00
|
|
|
return NewRedisLiveQuery(pool)
|
2020-03-23 01:33:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-01 17:51:34 +00:00
|
|
|
func TestMapBitfield(t *testing.T) {
|
|
|
|
// empty
|
|
|
|
assert.Equal(t, []byte{}, mapBitfield(nil))
|
|
|
|
assert.Equal(t, []byte{}, mapBitfield([]uint{}))
|
|
|
|
|
|
|
|
// one byte
|
|
|
|
assert.Equal(t, []byte("\x80"), mapBitfield([]uint{0}))
|
|
|
|
assert.Equal(t, []byte("\x40"), mapBitfield([]uint{1}))
|
|
|
|
assert.Equal(t, []byte("\xc0"), mapBitfield([]uint{0, 1}))
|
|
|
|
|
|
|
|
assert.Equal(t, []byte("\x08"), mapBitfield([]uint{4}))
|
|
|
|
assert.Equal(t, []byte("\xf8"), mapBitfield([]uint{0, 1, 2, 3, 4}))
|
|
|
|
assert.Equal(t, []byte("\xff"), mapBitfield([]uint{0, 1, 2, 3, 4, 5, 6, 7}))
|
|
|
|
|
|
|
|
// two bytes
|
|
|
|
assert.Equal(t, []byte("\x00\x80"), mapBitfield([]uint{8}))
|
|
|
|
assert.Equal(t, []byte("\xff\x80"), mapBitfield([]uint{0, 1, 2, 3, 4, 5, 6, 7, 8}))
|
|
|
|
|
|
|
|
// more bytes
|
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]byte("\xff\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00 "),
|
|
|
|
mapBitfield([]uint{0, 1, 2, 3, 4, 5, 6, 7, 8, 170}),
|
2020-03-23 01:33:04 +00:00
|
|
|
)
|
2020-07-01 17:51:34 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]byte("\xff\x80\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00@\x00\x00\x00\x00\x00\x00 "),
|
|
|
|
mapBitfield([]uint{0, 1, 2, 3, 4, 5, 6, 7, 8, 113, 170}),
|
2020-03-23 01:33:04 +00:00
|
|
|
)
|
2020-07-01 17:51:34 +00:00
|
|
|
assert.Equal(
|
|
|
|
t,
|
|
|
|
[]byte("\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01"),
|
|
|
|
mapBitfield([]uint{79}),
|
2020-03-23 01:33:04 +00:00
|
|
|
)
|
|
|
|
}
|