Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added conformance tests for lock store components #3045

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .github/scripts/test-info.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ const components = {
'crypto.jwks': {
conformance: true,
},
'lock.redis.v6': {
conformance: true,
conformanceSetup: 'docker-compose.sh redisjson redis',
sourcePkg: ['lock/redis', 'internal/component/redis'],
},
'lock.redis.v7': {
conformance: true,
conformanceSetup: 'docker-compose.sh redis7 redis',
sourcePkg: ['lock/redis', 'internal/component/redis'],
},
'middleware.http.bearer': {
certification: true,
},
Expand Down
12 changes: 12 additions & 0 deletions tests/config/lock/redis/v6/statestore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: lockstore
spec:
type: lock.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
12 changes: 12 additions & 0 deletions tests/config/lock/redis/v7/statestore.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: lockstore
spec:
type: lock.redis
version: v1
metadata:
- name: redisHost
value: localhost:6380
- name: redisPassword
value: ""
7 changes: 7 additions & 0 deletions tests/config/lock/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Supported additional operations: (none)
componentType: lock
components:
- component: redis.v6
operations: []
- component: redis.v7
operations: []
6 changes: 3 additions & 3 deletions tests/conformance/bindings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package conformance
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBindingsConformance(t *testing.T) {
tc, err := NewTestConfiguration("../config/bindings/tests.yml")
assert.NoError(t, err)
assert.NotNil(t, tc)
require.NoError(t, err)
require.NotNil(t, tc)
tc.Run(t)
}
24 changes: 24 additions & 0 deletions tests/conformance/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import (
"github.com/dapr/components-contrib/bindings"
"github.com/dapr/components-contrib/configuration"
contribCrypto "github.com/dapr/components-contrib/crypto"
"github.com/dapr/components-contrib/lock"
"github.com/dapr/components-contrib/pubsub"
"github.com/dapr/components-contrib/secretstores"
"github.com/dapr/components-contrib/state"
Expand Down Expand Up @@ -63,6 +64,7 @@ import (
cr_azurekeyvault "github.com/dapr/components-contrib/crypto/azure/keyvault"
cr_jwks "github.com/dapr/components-contrib/crypto/jwks"
cr_localstorage "github.com/dapr/components-contrib/crypto/localstorage"
l_redis "github.com/dapr/components-contrib/lock/redis"
p_snssqs "github.com/dapr/components-contrib/pubsub/aws/snssqs"
p_eventhubs "github.com/dapr/components-contrib/pubsub/azure/eventhubs"
p_servicebusqueues "github.com/dapr/components-contrib/pubsub/azure/servicebus/queues"
Expand Down Expand Up @@ -105,6 +107,7 @@ import (
conf_bindings "github.com/dapr/components-contrib/tests/conformance/bindings"
conf_configuration "github.com/dapr/components-contrib/tests/conformance/configuration"
conf_crypto "github.com/dapr/components-contrib/tests/conformance/crypto"
conf_lock "github.com/dapr/components-contrib/tests/conformance/lock"
conf_pubsub "github.com/dapr/components-contrib/tests/conformance/pubsub"
conf_secret "github.com/dapr/components-contrib/tests/conformance/secretstores"
conf_state "github.com/dapr/components-contrib/tests/conformance/state"
Expand Down Expand Up @@ -402,6 +405,15 @@ func (tc *TestConfiguration) Run(t *testing.T) {
wf := loadWorkflow(comp)
wfConfig := conf_workflows.NewTestConfig(comp.Component, comp.Operations, comp.Config)
conf_workflows.ConformanceTests(t, props, wf, wfConfig)
case "lock":
filepath := fmt.Sprintf("../config/lock/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
require.NoErrorf(t, err, "error running conformance test for component %s", comp.Component)
component := loadLockStore(comp)
require.NotNil(t, component, "error running conformance test for component %s", comp.Component)
lockConfig, err := conf_lock.NewTestConfig(comp.Component, comp.Operations, comp.Config)
require.NoErrorf(t, err, "error running conformance test for component %s", comp.Component)
conf_lock.ConformanceTests(t, props, component, lockConfig)
case "crypto":
filepath := fmt.Sprintf("../config/crypto/%s", componentConfigPath)
props, err := tc.loadComponentsAndProperties(t, filepath)
Expand Down Expand Up @@ -525,6 +537,18 @@ func loadCryptoProvider(tc TestComponent) contribCrypto.SubtleCrypto {
return component
}

func loadLockStore(tc TestComponent) lock.Store {
var component lock.Store
switch tc.Component {
case redisv6:
component = l_redis.NewStandaloneRedisLock(testLogger)
case redisv7:
component = l_redis.NewStandaloneRedisLock(testLogger)
}

return component
}

func loadStateStore(tc TestComponent) state.Store {
var store state.Store
switch tc.Component {
Expand Down
6 changes: 3 additions & 3 deletions tests/conformance/configuration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package conformance
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigurationConformance(t *testing.T) {
tc, err := NewTestConfiguration("../config/configuration/tests.yml")
assert.NoError(t, err)
assert.NotNil(t, tc)
require.NoError(t, err)
require.NotNil(t, tc)
tc.Run(t)
}
6 changes: 3 additions & 3 deletions tests/conformance/crypto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package conformance
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCryptoConformance(t *testing.T) {
tc, err := NewTestConfiguration("../config/crypto/tests.yml")
assert.NoError(t, err)
assert.NotNil(t, tc)
require.NoError(t, err)
require.NotNil(t, tc)
tc.Run(t)
}
180 changes: 180 additions & 0 deletions tests/conformance/lock/lock.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,180 @@
/*
Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package state

import (
"context"
"strings"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/dapr/components-contrib/lock"
"github.com/dapr/components-contrib/metadata"
"github.com/dapr/components-contrib/tests/conformance/utils"
"github.com/dapr/kit/config"
)

type TestConfig struct {
utils.CommonConfig
}

func NewTestConfig(component string, operations []string, configMap map[string]interface{}) (TestConfig, error) {
testConfig := TestConfig{
CommonConfig: utils.CommonConfig{
ComponentType: "lock",
ComponentName: component,
Operations: utils.NewStringSet(operations...),
},
}

err := config.Decode(configMap, &testConfig)
if err != nil {
return testConfig, err
}

return testConfig, nil
}

// ConformanceTests runs conf tests for lock stores.
func ConformanceTests(t *testing.T, props map[string]string, lockstore lock.Store, config TestConfig) {
// Test vars
key := strings.ReplaceAll(uuid.New().String(), "-", "")
t.Logf("Base key for test: %s", key)

t.Run("init", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
err := lockstore.InitLockStore(ctx, lock.Metadata{Base: metadata.Base{
Properties: props,
}})
require.NoError(t, err)
})

// Don't run more tests if init failed
if t.Failed() {
t.Fatal("Init failed, stopping further tests")
}

const lockOwner = "conftest"
lockKey1 := key + "-1"
lockKey2 := key + "-2"

var expirationCh *time.Timer

t.Run("TryLock", func(t *testing.T) {
// Acquire a lock
t.Run("acquire lock1", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
ExpiryInSeconds: 15,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.True(t, res.Success)
})

// Acquire a second lock (with a shorter expiration)
t.Run("acquire lock2", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey2,
LockOwner: lockOwner,
ExpiryInSeconds: 3,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.True(t, res.Success)

// Set expirationCh to when lock2 expires
expirationCh = time.NewTimer(3 * time.Second)
})

// Acquiring the same lock again should fail
t.Run("fails to acquire existing lock", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
ExpiryInSeconds: 15,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.False(t, res.Success)
})
})

t.Run("Unlock", func(t *testing.T) {
t.Run("fails to unlock with nonexistent resource ID", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: "nonexistent",
LockOwner: lockOwner,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.LockDoesNotExist, res.Status)
})

t.Run("fails to unlock with wrong owner", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: lockKey1,
LockOwner: "nonowner",
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.LockBelongsToOthers, res.Status)
})

t.Run("unlocks successfully", func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.Unlock(ctx, &lock.UnlockRequest{
ResourceID: lockKey1,
LockOwner: lockOwner,
})
require.NoError(t, err)
require.NotNil(t, res)
assert.Equal(t, lock.Success, res.Status)
})
})

t.Run("lock expires", func(t *testing.T) {
// Wait until the lock is supposed to expire
<-expirationCh.C

// Assert that the lock doesn't exist anymore - we should be able to re-acquire it
assert.Eventually(t, func() bool {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
res, err := lockstore.TryLock(ctx, &lock.TryLockRequest{
ResourceID: lockKey2,
LockOwner: lockOwner,
ExpiryInSeconds: 3,
})
return err == nil && res != nil && res.Success
}, 5*time.Second, 100*time.Millisecond, "Lock 2 was not released in time after its scheduled expiration")
})
}
30 changes: 30 additions & 0 deletions tests/conformance/lock_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//go:build conftests
// +build conftests

/*
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package conformance

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestLockConformance(t *testing.T) {
tc, err := NewTestConfiguration("../config/lock/tests.yml")
require.NoError(t, err)
require.NotNil(t, tc)
tc.Run(t)
}
6 changes: 3 additions & 3 deletions tests/conformance/pubsub_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ package conformance
import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestPubsubConformance(t *testing.T) {
tc, err := NewTestConfiguration("../config/pubsub/tests.yml")
assert.NoError(t, err)
assert.NotNil(t, tc)
require.NoError(t, err)
require.NotNil(t, tc)
tc.Run(t)
}
Loading
Loading