forked from dapr/components-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Certification test for Azure CosmosDB Statestore (dapr#1972)
* Certification test for Azure CosmosDB Signed-off-by: shivam <[email protected]> * Updating go.mod Signed-off-by: shivam <[email protected]> * make motidy-all Signed-off-by: Bernd Verst <[email protected]> * CosmosDB Cert Test: Apply Dapr Runtime updates, Go version update etc Signed-off-by: Bernd Verst <[email protected]> Signed-off-by: shivam <[email protected]> Signed-off-by: Bernd Verst <[email protected]> Co-authored-by: Bernd Verst <[email protected]>
- Loading branch information
1 parent
b194ed2
commit f32866e
Showing
10 changed files
with
1,717 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Azure Cosmos DB certification testing | ||
|
||
This project aims to test the Azure Cosmos DB State Store component under various conditions. | ||
|
||
## Test plan | ||
|
||
### Basic Test using master key authentication | ||
1. Able to create and test connection. | ||
2. Able to do set, fetch and delete. | ||
|
||
### Test Partition Keys | ||
Ensure the following scenarios: | ||
1. In case of invalid partition key, the access fails | ||
2. In case of missing partition key, *key* property is used in place of it | ||
3. In case of correct partition key, the access succeeds | ||
|
||
### Authentication using Azure AD | ||
1. Save Data and retrieve data using AAD credentials |
35 changes: 35 additions & 0 deletions
35
tests/certification/state/azure/cosmosdb/components/aadtest/cosmosdb.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: statestore-aad | ||
spec: | ||
type: state.azure.cosmosdb | ||
version: v1 | ||
metadata: | ||
- name: url | ||
secretKeyRef: | ||
key: AzureCosmosDBUrl | ||
name: AzureCosmosDBUrl | ||
- name: database | ||
secretKeyRef: | ||
key: AzureCosmosDB | ||
name: AzureCosmosDB | ||
- name: collection | ||
secretKeyRef: | ||
key: AzureCosmosDBCollection | ||
name: AzureCosmosDBCollection | ||
- name: azureTenantId | ||
secretKeyRef: | ||
name: AzureCertificationTenantId | ||
key: AzureCertificationTenantId | ||
- name: azureClientId | ||
secretKeyRef: | ||
name: AzureCertificationServicePrincipalClientId | ||
key: AzureCertificationServicePrincipalClientId | ||
- name: azureClientSecret | ||
secretKeyRef: | ||
name: AzureCertificationServicePrincipalClientSecret | ||
key: AzureCertificationServicePrincipalClientSecret | ||
|
||
auth: | ||
secretStore: envvar-secret-store |
9 changes: 9 additions & 0 deletions
9
tests/certification/state/azure/cosmosdb/components/aadtest/localsecrets.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: envvar-secret-store | ||
namespace: default | ||
spec: | ||
type: secretstores.local.env | ||
version: v1 | ||
metadata: |
26 changes: 26 additions & 0 deletions
26
tests/certification/state/azure/cosmosdb/components/basictest/cosmosdb.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: statestore-basic | ||
spec: | ||
type: state.azure.cosmosdb | ||
version: v1 | ||
metadata: | ||
- name: url | ||
secretKeyRef: | ||
key: AzureCosmosDBUrl | ||
name: AzureCosmosDBUrl | ||
- name: masterKey | ||
secretKeyRef: | ||
key: AzureCosmosDBMasterKey | ||
name: AzureCosmosDBMasterKey | ||
- name: database | ||
secretKeyRef: | ||
key: AzureCosmosDB | ||
name: AzureCosmosDB | ||
- name: collection | ||
secretKeyRef: | ||
key: AzureCosmosDBCollection | ||
name: AzureCosmosDBCollection | ||
auth: | ||
secretStore: envvar-secret-store |
9 changes: 9 additions & 0 deletions
9
tests/certification/state/azure/cosmosdb/components/basictest/localsecrets.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Component | ||
metadata: | ||
name: envvar-secret-store | ||
namespace: default | ||
spec: | ||
type: secretstores.local.env | ||
version: v1 | ||
metadata: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
apiVersion: dapr.io/v1alpha1 | ||
kind: Configuration | ||
metadata: | ||
name: keyvaultconfig | ||
spec: | ||
features: |
174 changes: 174 additions & 0 deletions
174
tests/certification/state/azure/cosmosdb/cosmosdb_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,174 @@ | ||
/* | ||
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 cosmosDBStorage_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
cosmosdb "github.com/dapr/components-contrib/state/azure/cosmosdb" | ||
"github.com/dapr/components-contrib/tests/certification/embedded" | ||
"github.com/dapr/components-contrib/tests/certification/flow" | ||
"github.com/dapr/go-sdk/client" | ||
|
||
secretstore_env "github.com/dapr/components-contrib/secretstores/local/env" | ||
secretstores_loader "github.com/dapr/dapr/pkg/components/secretstores" | ||
|
||
"github.com/dapr/components-contrib/tests/certification/flow/sidecar" | ||
state_loader "github.com/dapr/dapr/pkg/components/state" | ||
"github.com/dapr/dapr/pkg/runtime" | ||
dapr_testing "github.com/dapr/dapr/pkg/testing" | ||
"github.com/dapr/kit/logger" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
const ( | ||
sidecarNamePrefix = "cosmosdb-sidecar-" | ||
certificationTestPrefix = "stable-certification-" | ||
) | ||
|
||
func TestAzureCosmosDBStorage(t *testing.T) { | ||
ports, err := dapr_testing.GetFreePorts(2) | ||
assert.NoError(t, err) | ||
|
||
currentGrpcPort := ports[0] | ||
currentHTTPPort := ports[1] | ||
|
||
basicTest := func(statestore string) flow.Runnable { | ||
return func(ctx flow.Context) error { | ||
client, err := client.NewClientWithPort(fmt.Sprint(currentGrpcPort)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Close() | ||
|
||
stateKey := certificationTestPrefix + "key" | ||
stateValue := "certificationdata" | ||
|
||
// save state, default options: strong, last-write | ||
err = client.SaveState(ctx, statestore, stateKey, []byte(stateValue), nil) | ||
assert.NoError(t, err) | ||
|
||
// get state | ||
item, err := client.GetState(ctx, statestore, stateKey, nil) | ||
assert.NoError(t, err) | ||
assert.Equal(t, stateValue, string(item.Value)) | ||
|
||
// delete state | ||
err = client.DeleteState(ctx, statestore, stateKey, nil) | ||
assert.NoError(t, err) | ||
|
||
return nil | ||
} | ||
} | ||
|
||
partitionTest := func(statestore string) flow.Runnable { | ||
return func(ctx flow.Context) error { | ||
client, err := client.NewClientWithPort(fmt.Sprint(currentGrpcPort)) | ||
if err != nil { | ||
panic(err) | ||
} | ||
defer client.Close() | ||
|
||
stateKey := certificationTestPrefix + "key" | ||
stateValue := "certificationdata" | ||
|
||
// The default value for partition key is <App ID>||<state key> | ||
meta1 := map[string]string{ | ||
"partitionKey": sidecarNamePrefix + "||" + stateKey, | ||
} | ||
|
||
// Specifying custom partition key | ||
meta2 := map[string]string{ | ||
"partitionKey": "mypartition", | ||
} | ||
|
||
test := func(setMeta, getMeta map[string]string, expectedValue string) { | ||
// save state, default options: strong, last-write | ||
err = client.SaveState(ctx, statestore, stateKey, []byte(stateValue), setMeta) | ||
assert.NoError(t, err) | ||
|
||
// get state | ||
item, err := client.GetState(ctx, statestore, stateKey, getMeta) | ||
assert.NoError(t, err) | ||
assert.Equal(t, expectedValue, string(item.Value)) | ||
|
||
// delete state | ||
err = client.DeleteState(ctx, statestore, stateKey, setMeta) | ||
assert.NoError(t, err) | ||
} | ||
|
||
// Test with no partition key | ||
test(nil, meta1, stateValue) | ||
|
||
// Test with specific partition key | ||
test(meta2, meta2, stateValue) | ||
|
||
// Test with incorrect partition key | ||
test(meta2, meta1, "") | ||
|
||
return nil | ||
} | ||
} | ||
|
||
flow.New(t, "Test basic operations"). | ||
// Run the Dapr sidecar with azure CosmosDB storage. | ||
Step(sidecar.Run(sidecarNamePrefix, | ||
embedded.WithoutApp(), | ||
embedded.WithDaprGRPCPort(currentGrpcPort), | ||
embedded.WithDaprHTTPPort(currentHTTPPort), | ||
embedded.WithComponentsPath("./components/basictest"), | ||
componentRuntimeOptions())). | ||
Step("Run basic test with master key", basicTest("statestore-basic")). | ||
Run() | ||
|
||
flow.New(t, "Test basic operations with different partition keys"). | ||
// Run the Dapr sidecar with azure CosmosDB storage. | ||
Step(sidecar.Run(sidecarNamePrefix, | ||
embedded.WithoutApp(), | ||
embedded.WithDaprGRPCPort(currentGrpcPort), | ||
embedded.WithDaprHTTPPort(currentHTTPPort), | ||
embedded.WithComponentsPath("./components/basictest"), | ||
componentRuntimeOptions())). | ||
Step("Run basic test with multiple parition keys", partitionTest("statestore-basic")). | ||
Run() | ||
|
||
flow.New(t, "Test AAD authentication"). | ||
// Run the Dapr sidecar with azure CosmosDB storage. | ||
Step(sidecar.Run(sidecarNamePrefix, | ||
embedded.WithoutApp(), | ||
embedded.WithDaprGRPCPort(currentGrpcPort), | ||
embedded.WithDaprHTTPPort(currentHTTPPort), | ||
embedded.WithComponentsPath("./components/aadtest"), | ||
componentRuntimeOptions())). | ||
Step("Run basic test with Azure AD Authentication", basicTest("statestore-aad")). | ||
Run() | ||
} | ||
|
||
func componentRuntimeOptions() []runtime.Option { | ||
log := logger.NewLogger("dapr.components") | ||
|
||
stateRegistry := state_loader.NewRegistry() | ||
stateRegistry.Logger = log | ||
stateRegistry.RegisterComponent(cosmosdb.NewCosmosDBStateStore, "azure.cosmosdb") | ||
|
||
secretstoreRegistry := secretstores_loader.NewRegistry() | ||
secretstoreRegistry.Logger = log | ||
secretstoreRegistry.RegisterComponent(secretstore_env.NewEnvSecretStore, "local.env") | ||
|
||
return []runtime.Option{ | ||
runtime.WithStates(stateRegistry), | ||
runtime.WithSecretStores(secretstoreRegistry), | ||
} | ||
} |
Oops, something went wrong.