From 5cd81f39dfe24bac2ed949c654c22264f2bddc08 Mon Sep 17 00:00:00 2001 From: Vishwanath Hiremath <100623239+vishwahiremat@users.noreply.github.com> Date: Thu, 7 Dec 2023 11:24:42 -0800 Subject: [PATCH] Adding test workflow (#52) * adding tests for recipes Signed-off-by: Vishwanath Hiremath * testing to run test.yaml Signed-off-by: Vishwanath Hiremath * fix syntax Signed-off-by: Vishwanath Hiremath * fixing test.yaml Signed-off-by: Vishwanath Hiremath * fixing test.yaml Signed-off-by: Vishwanath Hiremath * setting output variables Signed-off-by: Vishwanath Hiremath * add install radius script Signed-off-by: Vishwanath Hiremath * fix test.yaml Signed-off-by: Vishwanath Hiremath * rename file Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * install edge version Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * adding checkout step Signed-off-by: Vishwanath Hiremath * adding execute permissions Signed-off-by: Vishwanath Hiremath * path fix Signed-off-by: Vishwanath Hiremath * fixing k3d create command Signed-off-by: Vishwanath Hiremath * adding mongo test Signed-off-by: Vishwanath Hiremath * fix syntax error Signed-off-by: Vishwanath Hiremath * fix syntax error Signed-off-by: Vishwanath Hiremath * remove provider Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * adding all tests Signed-off-by: Vishwanath Hiremath * adding matrix Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * test Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * test sql Signed-off-by: Vishwanath Hiremath * test-statestore Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * adding magpie Signed-off-by: Vishwanath Hiremath * fix errors Signed-off-by: Vishwanath Hiremath * enable all tests Signed-off-by: Vishwanath Hiremath * testSecretStores Signed-off-by: Vishwanath Hiremath * fix sescret store test Signed-off-by: Vishwanath Hiremath * adding pod logs Signed-off-by: Vishwanath Hiremath * test uploading logs Signed-off-by: Vishwanath Hiremath * fix upload logs error Signed-off-by: Vishwanath Hiremath * fix test error Signed-off-by: Vishwanath Hiremath * updating magpie to ghcr Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * testing Signed-off-by: Vishwanath Hiremath * remove build magpie Signed-off-by: Vishwanath Hiremath * have single app to deploy all recipes Signed-off-by: Vishwanath Hiremath * fix syntax error Signed-off-by: Vishwanath Hiremath * remove RUN_TEST Signed-off-by: Vishwanath Hiremath * addressing comments Signed-off-by: Vishwanath Hiremath * addressing review comments Signed-off-by: Vishwanath Hiremath * remove sql test Signed-off-by: Vishwanath Hiremath * fixing sql test Signed-off-by: Vishwanath Hiremath * updating wait for pods Signed-off-by: Vishwanath Hiremath * upgrading dapr version Signed-off-by: Vishwanath Hiremath * adressing comments Signed-off-by: Vishwanath Hiremath --------- Signed-off-by: Vishwanath Hiremath --- .github/scripts/install-radius.sh | 38 ++++++ .github/workflows/test.yaml | 102 ++++++++++++++ local-dev/pubsubbrokers.bicep | 2 +- tests/test-local-dev-recipes.bicep | 207 +++++++++++++++++++++++++++++ 4 files changed, 348 insertions(+), 1 deletion(-) create mode 100644 .github/scripts/install-radius.sh create mode 100644 .github/workflows/test.yaml create mode 100644 tests/test-local-dev-recipes.bicep diff --git a/.github/scripts/install-radius.sh b/.github/scripts/install-radius.sh new file mode 100644 index 0000000..23af31e --- /dev/null +++ b/.github/scripts/install-radius.sh @@ -0,0 +1,38 @@ +# ------------------------------------------------------------ +# Copyright 2023 The Radius 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. +# ------------------------------------------------------------ + +set -xe + +VERSION=$1 +RAD_CLI_URL=https://raw.githubusercontent.com/radius-project/radius/main/deploy/install.sh +RAD_CLI_EDGE_URL=ghcr.io/radius-project/rad/linux-amd64:latest + +if [[ $VERSION == "edge" ]]; then + echo Downloading rad CLI edge version + oras pull $RAD_CLI_EDGE_URL + chmod +x ./rad + mv ./rad /usr/local/bin/rad +elif [[ -n $VERSION && $VERSION != *"rc"* ]]; then + INPUT_CHANNEL=$(echo $VERSION | cut -d '.' -f 1,2) + echo "Downloading rad CLI version $INPUT_CHANNEL" + wget -q $RAD_CLI_URL -O - | /bin/bash -s $INPUT_CHANNEL +elif [[ -n $VERSION ]]; then + echo Downloading rad CLI version $VERSION + wget -q $RAD_CLI_URL -O - | /bin/bash -s $VERSION +else + echo Downloading latest rad CLI + wget -q $RAD_CLI_URL -O - | /bin/bash +fi diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml new file mode 100644 index 0000000..f41f4be --- /dev/null +++ b/.github/workflows/test.yaml @@ -0,0 +1,102 @@ +name: Test Recipes + +on: + workflow_dispatch: + inputs: + version: + description: 'Radius version number to use (e.g. 0.1.0, 0.1.0-rc1, edge). Defaults to edge.' + required: false + default: 'edge' + type: string + push: + branches: + - v*.* + - edge + paths: + - "local-dev/**" + - ".github/workflows/**" + pull_request: + types: [opened, synchronize, reopened] +env: + RUN_IDENTIFIER: recipestest-${{ github.run_id }}-${{ github.run_attempt }} + APP_NAME: local-dev-recipe-app + APP_NAMESPACE: local-dev-recipe-app +jobs: + test: + name: "Recipe tests" + runs-on: [ubuntu-latest] + steps: + - name: Generate output variables + id: gen-id + run: | + # Set output variables to be used in the other jobs + echo "RUN_IDENTIFIER=${RUN_IDENTIFIER}" >> $GITHUB_OUTPUT + - name: Checkout code + uses: actions/checkout@v3 + - name: Setup Node + uses: actions/setup-node@v3 + with: + node-version: 16 + - name: Download k3d + run: wget -q -O - https://raw.githubusercontent.com/k3d-io/k3d/main/install.sh | bash + - name: Create k3d cluster + run: k3d cluster create --agents 2 -p "80:80@loadbalancer" --k3s-arg "--disable=traefik@server:0" --registry-create reciperegistry:51351 + - name: Install Dapr + run: | + helm repo add dapr https://dapr.github.io/helm-charts/ + helm install dapr dapr/dapr --version=1.12 --namespace dapr-system --create-namespace --wait + - name: Download rad CLI + run: | + RADIUS_VERSION="${{ inputs.version }}" + if [[ -z "${{ inputs.version }}" ]]; then + RADIUS_VERSION=edge + fi + chmod +x ./.github/scripts/install-radius.sh + ./.github/scripts/install-radius.sh $RADIUS_VERSION + - name: Publish Recipes + run: | + files_list=$(ls "local-dev") + for file in $files_list; do + echo "Publishing recipe: local-dev/$file" + recipeName="${file%.*}" + rad bicep publish --file local-dev/$file --target br:localhost:51351/recipes/local-dev/$recipeName:latest --plain-http + done + - name: Initialize default environment + run: | + rad install kubernetes --set rp.publicEndpointOverride=localhost + rad group create default + rad workspace create kubernetes default --group default + rad group switch default + rad env create default + rad env switch default + - name: Deploy app + id: deploy-app + run: rad deploy ./tests/test-local-dev-recipes.bicep --parameters magpieimage="ghcr.io/radius-project/magpiego:latest" --parameters registry="reciperegistry:5000" --parameters version="latest" + - name: Wait for all pods to be ready + id: wait-for-pods + run: | + label="radapp.io/application=${APP_NAME}" + kubectl rollout status deployment -l $label -n ${APP_NAMESPACE} --timeout=90s + - name: Get Pod logs for failed tests + id: get-pod-logs + if: failure() && (steps.deploy-app.outcome == 'failure' || steps.wait-for-pods.outcome == 'failure') + run: | + # Create pod-logs directory + mkdir -p recipe-tests/pod-logs/local_dev_recipe_test_container_logs + # Get pod logs and save to file + label="radapp.io/application=${APP_NAME}" + pod_names=($(kubectl get pods -l $label -n ${APP_NAMESPACE} -o jsonpath='{.items[*].metadata.name}')) + for pod_name in "${pod_names[@]}"; do + kubectl logs $pod_name -n ${APP_NAMESPACE} > recipe-tests/pod-logs/local_dev_recipe_test_container_logs/${pod_name}.txt + done + echo "Pod logs saved to recipe-tests/pod-logs/local_dev_recipe_test_container_logs/" + # Get kubernetes events and save to file + kubectl get events -n ${APP_NAMESPACE} > recipe-tests/pod-logs/local_dev_recipe_test_container_logs/events.txt + - name: Upload Pod logs for failed tests + uses: actions/upload-artifact@v3 + if: failure() && steps.get-pod-logs.outcome == 'success' + with: + name: local_dev_recipe_test_container_logs-pod-logs + path: recipe-tests/pod-logs/local_dev_recipe_test_container_logs + retention-days: 30 + if-no-files-found: error diff --git a/local-dev/pubsubbrokers.bicep b/local-dev/pubsubbrokers.bicep index b22033b..609af5a 100644 --- a/local-dev/pubsubbrokers.bicep +++ b/local-dev/pubsubbrokers.bicep @@ -112,7 +112,7 @@ resource daprComponent 'dapr.io/Component@v1alpha1' = { metadata: [ { name: 'redisHost' - value: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local' + value: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local:${port}' } { name: 'redisPassword' diff --git a/tests/test-local-dev-recipes.bicep b/tests/test-local-dev-recipes.bicep new file mode 100644 index 0000000..74602e1 --- /dev/null +++ b/tests/test-local-dev-recipes.bicep @@ -0,0 +1,207 @@ +import radius as radius + +@description('Specifies the image for the container resource.') +param magpieimage string + +@description('The OCI registry for test Bicep recipes.') +param registry string + +@description('The OCI tag for test Bicep recipes.') +param version string + +import kubernetes as kubernetes { + namespace: 'daprrp-rs-secretstore-recipe' + kubeConfig: '' +} + +resource env 'Applications.Core/environments@2023-10-01-preview' = { + name: 'local-dev-recipe-env' + location: 'global' + properties: { + compute: { + kind: 'kubernetes' + resourceId: 'self' + namespace: 'local-dev-recipe-env' + } + recipes: { + 'Applications.Datastores/mongoDatabases':{ + mongoazure: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/mongodatabases:${version}' + } + } + 'Applications.Dapr/pubSubBrokers': { + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/pubsubbrokers:${version}' + } + } + 'Applications.Messaging/rabbitMQQueues': { + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/rabbitmqqueues:${version}' + } + } + 'Applications.Datastores/redisCaches':{ + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/rediscaches:${version}' + } + } + 'Applications.Dapr/secretStores': { + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/secretstores:${version}' + } + } + 'Applications.Datastores/sqlDatabases': { + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/sqldatabases:${version}' + } + } + 'Applications.Dapr/stateStores': { + default: { + templateKind: 'bicep' + plainHTTP: true + templatePath: '${registry}/recipes/local-dev/statestores:${version}' + } + } + } + } +} + +resource app 'Applications.Core/applications@2023-10-01-preview' = { + name: 'local-dev-recipe-app' + location: 'global' + properties: { + environment: env.id + extensions: [ + { + kind: 'kubernetesNamespace' + namespace: 'local-dev-recipe-app' + } + ] + } +} + +resource webapp 'Applications.Core/containers@2023-10-01-preview' = { + name: 'mongodb-app-ctnr' + location: 'global' + properties: { + application: app.id + connections: { + mongodb: { + source: recipedb.id + } + daprpubsub: { + source: pubsubBroker.id + } + rabbitmq: { + source: rabbitmq.id + } + redis: { + source: redis.id + } + daprsecretstore: { + source: secretstore.id + } + sql: { + source: db.id + disableDefaultEnvVars: true + } + daprstatestore: { + source: statestore.id + } + } + container: { + image: magpieimage + readinessProbe:{ + kind:'httpGet' + containerPort:3000 + path: '/healthz' + } + } + extensions: [ + { + kind: 'daprSidecar' + appId: 'dpsb-recipe-app-ctnr' + appPort: 3000 + } + ] + } +} + +resource recipedb 'Applications.Datastores/mongoDatabases@2023-10-01-preview' = { + name: 'mongodb-db' + properties: { + application: app.id + environment: env.id + recipe: { + name: 'mongoazure' + } + } +} + +resource pubsubBroker 'Applications.Dapr/pubSubBrokers@2023-10-01-preview' = { + name: 'dpsb-recipe' + properties: { + application: app.id + environment: env.id + } +} + +resource rabbitmq 'Applications.Messaging/rabbitMQQueues@2023-10-01-preview' = { + name: 'rmq-recipe-resource' + properties: { + application: app.id + environment: env.id + } +} + +resource redis 'Applications.Datastores/redisCaches@2023-10-01-preview' = { + name: 'rds-recipe' + properties: { + environment: env.id + application: app.id + } +} + +resource secretstore 'Applications.Dapr/secretStores@2023-10-01-preview' = { + name: 'gnrc-scs-recipe' + properties: { + environment: env.id + application: app.id + } +} + +resource mysecret 'core/Secret@v1' = { + metadata: { + name: 'mysecret' + namespace: 'local-dev-recipe-app' + } + type: 'opaque' + data: {} +} + +resource db 'Applications.Datastores/sqlDatabases@2023-10-01-preview' = { + name: 'sql-db-recipe' + properties: { + application: app.id + environment: env.id + } +} + +resource statestore 'Applications.Dapr/stateStores@2023-10-01-preview' = { + name: 'dapr-sts-recipe' + properties: { + application: app.id + environment: env.id + } +}