Skip to content

Commit

Permalink
Certification test for Azure CosmosDB Statestore (dapr#1972)
Browse files Browse the repository at this point in the history
* Certification test for Azure CosmosDB

Signed-off-by: shivam <shivamkm07@gmail.com>

* Updating go.mod

Signed-off-by: shivam <shivamkm07@gmail.com>

* make motidy-all

Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>

* CosmosDB Cert Test: Apply Dapr Runtime updates, Go version update etc

Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>

Signed-off-by: shivam <shivamkm07@gmail.com>
Signed-off-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
Co-authored-by: Bernd Verst <4535280+berndverst@users.noreply.github.com>
  • Loading branch information
2 people authored and shubham1172 committed Sep 20, 2022
1 parent b194ed2 commit f32866e
Show file tree
Hide file tree
Showing 10 changed files with 1,717 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/certification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ jobs:
required-secrets: AzureBlobStorageAccount, AzureBlobStorageAccessKey, AzureCertificationTenantId, AzureCertificationServicePrincipalClientId, AzureCertificationServicePrincipalClientSecret
- component: state.azure.blobstorage
required-secrets: AzureBlobStorageContainer,AzureBlobStorageAccount, AzureBlobStorageAccessKey, AzureCertificationTenantId, AzureCertificationServicePrincipalClientId, AzureCertificationServicePrincipalClientSecret
- component: state.azure.cosmosdb
required-secrets: AzureCosmosDBMasterKey, AzureCosmosDBUrl, AzureCosmosDB, AzureCosmosDBCollection, AzureCertificationTenantId, AzureCertificationServicePrincipalClientId, AzureCertificationServicePrincipalClientSecret
EOF
)
echo "::set-output name=cloud-components::$CRON_COMPONENTS"
Expand Down
18 changes: 18 additions & 0 deletions tests/certification/state/azure/cosmosdb/README.md
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
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
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:
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
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:
6 changes: 6 additions & 0 deletions tests/certification/state/azure/cosmosdb/config.yaml
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 tests/certification/state/azure/cosmosdb/cosmosdb_test.go
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),
}
}
Loading

0 comments on commit f32866e

Please sign in to comment.