...

Text file src/github.com/sigstore/rekor/tests/rekor-harness.sh

Documentation: github.com/sigstore/rekor/tests

     1#!/bin/bash
     2#
     3# Copyright 2022 The Sigstore Authors.
     4#
     5# Licensed under the Apache License, Version 2.0 (the "License");
     6# you may not use this file except in compliance with the License.
     7# You may obtain a copy of the License at
     8#
     9#     http://www.apache.org/licenses/LICENSE-2.0
    10#
    11# Unless required by applicable law or agreed to in writing, software
    12# distributed under the License is distributed on an "AS IS" BASIS,
    13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14# See the License for the specific language governing permissions and
    15# limitations under the License.
    16set -e
    17
    18TREE_ID=""
    19
    20function start_server () {
    21    server_version=$1
    22    current_branch=$(git rev-parse --abbrev-ref HEAD)
    23    git checkout $server_version
    24    if [ $(docker-compose ps | grep -c "(healthy)") == 0 ]; then
    25        echo "starting services with version $server_version"
    26        docker-compose up -d --build
    27        sleep 30
    28        make rekor-cli
    29        export TREE_ID=$(./rekor-cli loginfo --format json --rekor_server http://localhost:3000 --store_tree_state=false | jq -r .TreeID)
    30    else
    31        echo "turning down rekor and restarting at version $server_version"
    32        docker stop $(docker ps --filter name=rekor-server --format {{.ID}})
    33
    34        # Replace log in docker-compose.yml with the Tree ID we want
    35        search="# Uncomment this for production logging"
    36        replace="\"--trillian_log_server.tlog_id=$TREE_ID\","
    37        sed -i "s/$search/$replace/" docker-compose.yml
    38
    39        docker-compose up -d --build rekor-server
    40    fi
    41
    42    count=0
    43    echo -n "waiting up to 60 sec for system to start"
    44    until [ $(docker-compose ps | grep -c "(healthy)") == 3 ];
    45    do
    46        if [ $count -eq 6 ]; then
    47            echo "! timeout reached"
    48            cat docker-compose.yml
    49            docker-compose logs --no-color > /tmp/docker-compose.log
    50            exit 1
    51        else
    52            echo -n "."
    53            sleep 10
    54            let 'count+=1'
    55        fi
    56    done
    57    git checkout $server_version .
    58    git checkout $current_branch
    59    echo
    60}
    61
    62function build_cli () {
    63    echo "Building CLI at version $cli_version"
    64    cli_version=$1
    65    current_branch=$(git rev-parse --abbrev-ref HEAD)
    66    git checkout $cli_version
    67    make rekor-cli
    68    git checkout $cli_version .
    69    git checkout $current_branch
    70}
    71
    72function run_tests () {
    73    REKORTMPDIR="$(mktemp -d -t rekor_test.XXXXXX)"
    74    touch $REKORTMPDIR.rekor.yaml
    75    trap "rm -rf $REKORTMPDIR" EXIT
    76
    77    go clean -testcache
    78    if ! REKORTMPDIR=$REKORTMPDIR SERVER_VERSION=$1 CLI_VERSION=$2 go test -run TestHarness -v -tags=e2e ./tests/ ; then
    79        docker-compose logs --no-color > /tmp/docker-compose.log
    80        exit 1
    81    fi
    82    if docker-compose logs --no-color | grep -q "panic: runtime error:" ; then
    83        # if we're here, we found a panic
    84        echo "Failing due to panics detected in logs"
    85        docker-compose logs --no-color > /tmp/docker-compose.log
    86        exit 1
    87    fi
    88}
    89
    90# Get last 2 server versions
    91git fetch --all --tags
    92NUM_VERSIONS_TO_TEST=2
    93# don't explicitly fetch RC builds; they'll be included below when we test at HEAD
    94VERSIONS=$(git tag --sort=-version:refname | grep -v "rc" | head -n $NUM_VERSIONS_TO_TEST | tac)
    95
    96# Also add the commit @ HEAD
    97HEAD=$(git log --pretty="%H" -n 1 )
    98echo "Also testing at HEAD at commit $HEAD"
    99
   100VERSIONS="$VERSIONS $HEAD"
   101
   102echo $VERSIONS
   103
   104export REKOR_HARNESS_TMPDIR="$(mktemp -d -t rekor_test_harness.XXXXXX)"
   105docker-compose down
   106
   107for server_version in $VERSIONS
   108do
   109    start_server $server_version
   110    for cli_version in $VERSIONS
   111    do
   112        echo "======================================================="
   113        echo "Running tests with server version $server_version and CLI version $cli_version"
   114
   115        build_cli $cli_version
   116        run_tests $server_version $cli_version
   117
   118        echo "Tests passed successfully."
   119        echo "======================================================="
   120    done
   121done
   122
   123# Since we add two entries to the log for every test, once all tests are run we should have 2*(($NUM_VERSIONS_TO_TEST+1)^2) entries
   124make rekor-cli
   125actual=$(./rekor-cli loginfo --rekor_server http://localhost:3000 --format json --store_tree_state=false | jq -r .ActiveTreeSize)
   126expected=$((2*(1+$NUM_VERSIONS_TO_TEST)*(1+$NUM_VERSIONS_TO_TEST)))
   127if [[ ! "$expected" == "$actual" ]]; then
   128    echo "ERROR: Log had $actual entries instead of expected $expected"
   129    exit 1
   130fi
   131
   132echo "Harness testing successful :)"

View as plain text