fleet/server/datastore/mysql/locks_test.go
Tomas Touceda 46b0b7765b
Issue 1435 software to cpe (#1488)
* WIP

* WIP

* Make path optional and fix tests

* Add first generate

* Move to nvd package

* remove replace

* Re-add replace

* It's path, not file name

* Change how db path is set and use etag

* Fix typos

* Make db generation faster

* Remove quotes

* Doesn't like comments

* Samitize etag and save to file

* Refactor some things and improve writing of etagenv

* Compress file and truncate amount of items for faster testing

* Remove quotes

* Try to improve performance

* Ignore truncate error if not exists

* Minor cleanup and make sqlite have cpe prefix

* Simplify code and test sync

* Add VCR for sync test

* Check for nvdRelease nil

* Add test for the actual translation

* Address review comments

* Rename generate command because we'll have a cve one too

* Move to its own dir

* Address review comments
2021-07-29 13:10:34 -03:00

60 lines
1.4 KiB
Go

package mysql
import (
"testing"
"time"
"github.com/fleetdm/fleet/v4/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestLocks(t *testing.T) {
ds := CreateMySQLDS(t)
defer ds.Close()
owner1, err := server.GenerateRandomText(64)
require.NoError(t, err)
owner2, err := server.GenerateRandomText(64)
require.NoError(t, err)
// get first lock
locked, err := ds.Lock("test", owner1, 1*time.Minute)
require.NoError(t, err)
assert.True(t, locked)
// renew current lock
locked, err = ds.Lock("test", owner1, 1*time.Minute)
require.NoError(t, err)
assert.True(t, locked)
// owner2 tries to get the lock but fails
locked, err = ds.Lock("test", owner2, 1*time.Minute)
require.NoError(t, err)
assert.False(t, locked)
// owner2 gets a new lock that expires quickly
locked, err = ds.Lock("test-expired", owner2, 1*time.Second)
require.NoError(t, err)
assert.True(t, locked)
time.Sleep(3 * time.Second)
// owner1 gets the same lock because it's now expired
locked, err = ds.Lock("test-expired", owner1, 1*time.Minute)
require.NoError(t, err)
assert.True(t, locked)
// unlocking clears the lock
locked, err = ds.Lock("test", owner1, 1*time.Minute)
require.NoError(t, err)
assert.True(t, locked)
err = ds.Unlock("test", owner1)
require.NoError(t, err)
// owner2 tries to get the lock but fails
locked, err = ds.Lock("test", owner2, 1*time.Minute)
require.NoError(t, err)
assert.True(t, locked)
}