From a4e2c903af7715f4adb2ad465dbcdf15ed1e991a Mon Sep 17 00:00:00 2001 From: Abirdcfly Date: Wed, 4 Jan 2023 20:10:22 +0800 Subject: [PATCH] test: add sample test Signed-off-by: Abirdcfly --- .github/workflows/example-test.yaml | 28 ++ config/samples/example-test.sh | 581 ++++++++++++++++++++++++++++ 2 files changed, 609 insertions(+) create mode 100644 .github/workflows/example-test.yaml create mode 100755 config/samples/example-test.sh diff --git a/.github/workflows/example-test.yaml b/.github/workflows/example-test.yaml new file mode 100644 index 00000000..5b49e36b --- /dev/null +++ b/.github/workflows/example-test.yaml @@ -0,0 +1,28 @@ +name: Example Test + +on: + push: + branches: [main] + pull_request: + branches: [main] + workflow_dispatch: + +jobs: + example: + env: + LOG_DIR: "/tmp/fabric-operator-example-test/logs" + runs-on: ubuntu-22.04 + steps: + - uses: actions/checkout@v3 + - name: Build + run: | + scripts/install-tools.sh + make image + - name: Example Test + run: config/samples/example-test.sh + - name: Upload logs + if: failure() + uses: actions/upload-artifact@v3 + with: + name: ${{ env.GITHUB_SHA }}.kind.logs + path: ${{ env.LOG_DIR }} diff --git a/config/samples/example-test.sh b/config/samples/example-test.sh new file mode 100755 index 00000000..117a4281 --- /dev/null +++ b/config/samples/example-test.sh @@ -0,0 +1,581 @@ +#!/bin/bash +# +# Copyright contributors to the Hyperledger Fabric Operator project +# +# SPDX-License-Identifier: Apache-2.0 +# +# 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 -x +KindName=${KindName:-"fabric-example-test"} +TimeoutSeconds=${TimeoutSeconds:-"600"} +KindVersion=${KindVersion:-"v1.24.4"} +TempFilePath=${TempFilePath:-"/tmp/fabric-operator-example-test"} +KindConfigPath=${TempFilePath}/kind-config.yaml +InstallDirPath=${TempFilePath}/installer +DefaultPassWord=${DefaultPassWord:-'admiN$123'} +LOG_DIR=${LOG_DIR:-"/tmp/fabric-operator-example-test/logs"} + +Timeout="${TimeoutSeconds}s" +mkdir ${TempFilePath} || true + +function cleanup { + echo "cleanup start..." + echo "1. Try to get all pod" + kubectl get po -A -o wide + + echo "2. Try to describe all pod" + kubectl describe po -A + + echo "3. Try to export kind logs to $LOG_DIR..." + mkdir -p $LOG_DIR + kind export logs --name=${KindName} $LOG_DIR + sudo chown -R $USER:$USER $LOG_DIR + + echo "4. kind delete cluster ${KindName}" + kind delete cluster --name=${KindName} + + echo "5. docker network remove" + docker network rm kind + + echo "6. delete tmp file" + rm -rf ${TempFilePathTempFilePath} + + echo "cleanup finished !" +} +trap cleanup ERR + +echo "1. create kind cluster..." +cat >>${KindConfigPath} </${KindName}-worker/g" ${InstallDirPath}/u4a-component/charts/cluster-component/values.yaml +helm install --wait --timeout=${Timeout} cluster-component -n u4a-system ${InstallDirPath}/u4a-component/charts/cluster-component +if [[ $? -ne 0 ]]; then + exit $? +fi + +echo "2.2 install u4a services" +sed -i -e "s//${workerNode1IP}/g" ${InstallDirPath}/u4a-component/values.yaml +sed -i -e "s//${KindName}-worker2/g" ${InstallDirPath}/u4a-component/values.yaml +sed -i -e "s//${workerNode2IP}/g" ${InstallDirPath}/u4a-component/values.yaml +# TODO remove there change after https://github.com/bestchains/installer/pull/2 merged +echo "Temporary testing, Verify that the readinessProbe in oidc-server prevents the pod from being ready!" +cat >>${TempFilePath}/deploy.yaml </${Admin1Token}/g" config/samples/orgs/org1.yaml +sed -i -e "s//${Admin2Token}/g" config/samples/orgs/org2.yaml +sed -i -e "s//${Admin3Token}/g" config/samples/orgs/org3.yaml + +echo "4.1.1 create org=org1, wait for the relevant components to start up." +kubectl create -f config/samples/orgs/org1.yaml --dry-run=client --dry-run=client -o json | jq '.spec.caSpec.ingress.class = "'$IngressClassName'"' | jq '.spec.caSpec.storage.ca.class = "'$StorageClassName'"' | kubectl create --as=org1admin -f - +function waitOrgReady() { + orgName=$1 + START_TIME=$(date +%s) + while true; do + status=$(kubectl get org $orgName -o json | jq -r .status.type) + if [ "$status" == "Deployed" ]; then + break + fi + + # TODO after finish https://github.com/bestchains/fabric-operator/issues/79, remove check ca status + caStatus=$(kubectl get ibpca -n $orgName $orgName -o json | jq -r '.status.type') + if [ "$caStatus" == "Deployed" ]; then + break + fi + + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + kubectl describe pod -n $orgName + kubectl get org $orgName -oyaml + kubectl get ibpca -n $orgName $orgName -oyaml + exit 1 + fi + sleep 5 + done +} +waitOrgReady org1 + +echo "4.1.2 create org=org2, wait for the relevant components to start up." +kubectl create -f config/samples/orgs/org2.yaml --dry-run=client --dry-run=client -o json | jq '.spec.caSpec.ingress.class = "'$IngressClassName'"' | jq '.spec.caSpec.storage.ca.class = "'$StorageClassName'"' | kubectl create --as=org2admin -f - +waitOrgReady org2 + +echo "4.1.3 create org=org3, wait for the relevant components to start up." +kubectl create -f config/samples/orgs/org3.yaml --dry-run=client --dry-run=client -o json | jq '.spec.caSpec.ingress.class = "'$IngressClassName'"' | jq '.spec.caSpec.storage.ca.class = "'$StorageClassName'"' | kubectl create --as=org3admin -f - +waitOrgReady org3 + +echo "4.2 create federation resources: federation-sample" +kubectl create -f config/samples/ibp.com_v1beta1_federation.yaml --as=org1admin +function waitFedActivated() { + fedName=$1 + onlyCheckExist=$2 + START_TIME=$(date +%s) + while true; do + if [[ $onlyCheckExist ]]; then + kubectl get fed $fedName + if [[ $? -eq 0 ]]; then + break + fi + else + status=$(kubectl get fed $1 -o json | jq -r '.status.type') + if [ "$status" == "FederationActivated" ]; then + break + fi + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + exit 1 + fi + sleep 5 + done +} +waitFedActivated federation-sample "onlyCheckExist" + +echo "4.3 create federation create proposal for fed=federation-sample" + +echo "4.3.1 create proposal pro=create-federation-sample" +kubectl create -f config/samples/ibp.com_v1beta1_proposal_create_federation.yaml --as=org1admin + +echo "4.3.2 user=org2admin vote for pro=create-federation-sample" +function waitVoteExist() { + ns=$1 + proposalName=$2 + START_TIME=$(date +%s) + while true; do + kubectl get vote -n $ns -l "bestchains.vote.federation=$proposalName" + if [[ $? -eq 0 ]]; then + break + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + exit 1 + fi + sleep 5 + done +} +waitVoteExist org2 create-federation-sample +kubectl patch vote -n org2 vote-org2-create-federation-sample --type='json' -p='[{"op": "replace", "path": "/spec/decision", "value": true}]' --as=org2admin + +echo "4.3.3 pro=create-federation-sample become Succeeded" +function waitProposalSucceeded() { + proposalName=$1 + START_TIME=$(date +%s) + while true; do + Type=$(kubectl get pro $proposalName -o json | jq -r '.status.conditions[] | select(.status=="True") | .type') + if [[ $Type == "Succeeded" ]]; then + break + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + kubectl describe pro $proposalName + exit 1 + fi + sleep 5 + done +} +waitProposalSucceeded create-federation-sample + +echo "4.3.4 fed=federation-sample become Activated, federation create finish!" +waitFedActivated federation-sample "" + +echo "4.4 network management" +echo "4.4.1 create single orderer node network" +sed -i -e "s//${Admin1Token}/g" config/samples/ibp.com_v1beta1_network.yaml +kubectl create -f config/samples/ibp.com_v1beta1_network.yaml --dry-run=client --dry-run=client -o json | jq '.spec.orderSpec.ingress.class = "'$IngressClassName'"' | jq '.spec.orderSpec.storage.orderer.class = "'$StorageClassName'"' | kubectl create --as=org1admin -f - +function waitNetwork() { + networkName=$1 + orderNs=$2 + want=$3 + START_TIME=$(date +%s) + while true; do + if [[ $want == "NoExist" ]]; then + kubectl get network $networkName + if [[ $? -eq 1 ]]; then + break + fi + elif [[ $want == "Ready" ]]; then + #todo after https://github.com/bestchains/fabric-operator/issues/83 change these check + Type=$(kubectl get ibporderer -n $orderNs ${networkName} -o json | jq '.status.type') + if [[ $Type == "Deployed" ]]; then + break + fi + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + kubectl describe network $networkName + exit 1 + fi + sleep 5 + done +} +waitNetwork network-sample "org1" "Ready" + +echo "4.4.2 create 3 orderer node network" +sed -i -e "s//${Admin1Token}/g" config/samples/ibp.com_v1beta1_network_size_3.yaml +kubectl create -f config/samples/ibp.com_v1beta1_network_size_3.yaml --dry-run=client --dry-run=client -o json | jq '.spec.orderSpec.ingress.class = "'$IngressClassName'"' | jq '.spec.orderSpec.storage.orderer.class = "'$StorageClassName'"' | kubectl create --as=org1admin -f - +waitNetwork network-sample3 "org1" "Ready" + +echo "4.4.3 delete network need create a federation dissolve network proposal for fed=federation-sample network=network-sample" + +echo "4.4.3.1 create proposal pro=dissolve-network-sample" +kubectl create -f config/samples/ibp.com_v1beta1_proposal_dissolve_network.yaml --as=org1admin + +echo "4.4.3.2 user=org2admin vote for pro=dissolve-network-sample" +waitVoteExist org2 dissolve-network-sample +kubectl patch vote -n org2 vote-org2-dissolve-network-sample --type='json' -p='[{"op": "replace", "path": "/spec/decision", "value": true}]' --as=org2admin + +echo "4.4.3.3 pro=dissolve-network-sample become Activated" +waitProposalSucceeded dissolve-network-sample + +echo "4.4.3.4 network=network-sample cant find, deletion finished" +waitNetwork network-sample "" "NoExist" + +echo "4.7 channel management" +echo "4.7.1 create channel channel=channel-sample" +kubectl create -f config/samples/ibp.com_v1beta1_channel_create.yaml --as=org1admin +function waitChannelReady() { + channelName=$1 + want=$2 + START_TIME=$(date +%s) + while true; do + if [[ $want == "NoExist" ]]; then + kubectl get network $networkName + if [[ $? -eq 1 ]]; then + break + fi + elif [[ $want == "Ready" ]]; then + #todo + Type=$(kubectl get pro $proposalName -o json | jq -r '.status.conditions[] | select(.status=="True") | .type') + if [[ $Type == "Succeeded" ]]; then + break + fi + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + kubectl describe pro $proposalName + exit 1 + fi + sleep 5 + done +} +waitChannelReady channel-sample + +echo "4.7.2 create peer node peer=org1peer1" +Org1CaCert=$(kubectl get cm -norg1 org1-connection-profile -ojson | jq -r '.binaryData."profile.json"' | base64 -d | jq -r '.tls.cert') +Org1CaURI=$(kubectl get cm -norg1 org1-connection-profile -ojson | jq -r '.binaryData."profile.json"' | base64 -d | jq -r '.endpoints.api') +function parseURI() { + uri=$1 + # https://stackoverflow.com/a/6174447/5939892 + # extract the protocol + proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')" + # remove the protocol + url="$(echo ${1/$proto/})" + # extract the user (if any) + user="$(echo $url | grep @ | cut -d@ -f1)" + # extract the host and port + hostport="$(echo ${url/$user@/} | cut -d/ -f1)" + # by request host without port + host="$(echo $hostport | sed -e 's,:.*,,g')" + # by request - try to extract the port + port="$(echo $hostport | sed -e 's,^.*:,:,g' -e 's,.*:\([0-9]*\).*,\1,g' -e 's,[^0-9],,g')" + # extract the path (if any) + path="$(echo $url | grep / | cut -d/ -f2-)" + if [[ $port == "" && $proto == "http" ]]; then + port="80" + elif [[ $port == "" && $proto == "https" ]]; then + port="443" + fi +} +parseURI ${Org1CaURI} +Org1CaHost=${host} +Org1CaPort=${port} +sed -i -e "s//${Admin1Token}/g" config/samples/peers/ibp.com_v1beta1_peer_org1peer1.yaml +sed -i -e "s//${Org1CaCert}/g" config/samples/peers/ibp.com_v1beta1_peer_org1peer1.yaml +kubectl create -f config/samples/peers/ibp.com_v1beta1_peer_org1peer1.yaml --dry-run=client --dry-run=client -o json | + jq '.spec.ingress.class = "'$IngressClassName'"' | + jq '.spec.storage.peer.class = "'$StorageClassName'"' | jq '.spec.storage.statedb.class = "'$StorageClassName'"' | + jq '.spec.secret.enrollment.component.cahost = "'$Org1CaHost'"' | jq '.spec.secret.enrollment.tls.cahost = "'$Org1CaHost'"' | + jq '.spec.secret.enrollment.component.caport = "'$Org1CaPort'"' | jq '.spec.secret.enrollment.tls.caport = "'$Org1CaPort'"' | + kubectl create --as=org1admin -f - +function waitPeerReady() { + peerName=$1 + want=$2 + START_TIME=$(date +%s) + while true; do + if [[ $want == "NoExist" ]]; then + kubectl get network $networkName + if [[ $? -eq 1 ]]; then + break + fi + elif [[ $want == "todo" ]]; then + #todo + Type=$(kubectl get pro $proposalName -o json | jq -r '.status.conditions[] | select(.status=="True") | .type') + if [[ $Type == "Succeeded" ]]; then + break + fi + fi + CURRENT_TIME=$(date +%s) + ELAPSED_TIME=$((CURRENT_TIME - START_TIME)) + if [ $ELAPSED_TIME -gt $TimeoutSeconds ]; then + echo "Timeout reached" + kubectl describe pro $proposalName + exit 1 + fi + sleep 5 + done +} +#todo +waitPeerReady + +echo "4.7.3 create peer node peer=org2peer1" +Org2CaCert=$(kubectl get cm -norg2 org2-connection-profile -ojson | jq -r '.binaryData."profile.json"' | base64 -d | jq -r '.tls.cert') +Org2CaURI=$(kubectl get cm -norg2 org2-connection-profile -ojson | jq -r '.binaryData."profile.json"' | base64 -d | jq -r '.endpoints.api') +parseURI ${Org2CaURI} +Org2CaHost=${host} +Org2CaPort=${port} +sed -i -e "s//${Admin2Token}/g" config/samples/peers/ibp.com_v1beta1_peer_org2peer1.yaml +sed -i -e "s//${Org1CaCert}/g" config/samples/peers/ibp.com_v1beta1_peer_org2peer1.yaml +kubectl create -f config/samples/peers/ibp.com_v1beta1_peer_org2peer1.yaml --dry-run=client --dry-run=client -o json | + jq '.spec.ingress.class = "'$IngressClassName'"' | + jq '.spec.storage.peer.class = "'$StorageClassName'"' | jq '.spec.storage.statedb.class = "'$StorageClassName'"' | + jq '.spec.secret.enrollment.component.cahost = "'$Org2CaHost'"' | jq '.spec.secret.enrollment.tls.cahost = "'$Org2CaHost'"' | + jq '.spec.secret.enrollment.component.caport = "'$Org2CaPort'"' | jq '.spec.secret.enrollment.tls.caport = "'$Org2CaPort'"' | + kubectl create --as=org2admin -f - +#todo +waitPeerReady + +echo "4.4.1 create proposal pro=add-member-federation-sample" +kubectl create -f config/samples/ibp.com_v1beta1_proposal_add_member.yaml --as=org1admin +waitVoteExist org2 add-member-federation-sample +waitVoteExist org3 add-member-federation-sample + +echo "4.4.2 user=org2admin vote for pro=create-federation-sample" +kubectl patch vote -n org2 vote-org2-add-member-federation-sample --type='json' -p='[{"op": "replace", "path": "/spec/decision", "value": true}]' --as=org2admin + +echo "4.4.3 add peer node to channel peer=org1peer1 peer=org2peer1 channel=channel-sample" +kubectl create -f config/samples/ibp.com_v1beta1_channel_join.yaml --as=org1admin +# todo Verify that peers successfully join channel + +echo "all sample test finish!" +exit 0 + +echo "4.5 create federation delete member proposal for fed=federation-sample" +echo "4.5.1 create proposal pro=delete-member-federation-sample" +kubectl create -f config/samples/ibp.com_v1beta1_proposal_delete_member.yaml --as=org1admin + +echo "4.5.2 user=org3admin vote for pro=delete-member-federation-sample" +waitVoteExist org3 delete-member-federation-sample +echo "4.5.3 pro=create-federation-sample become xxx" +echo "4.5.4 fed=federation-sample org2 will be removed from spec.Members " +# todo +waitFedActivated federation-sample ""