github.com/verrazzano/verrazzano@v1.7.0/build/coverage-number-comparison.sh (about)

     1  #!/bin/bash
     2  #
     3  # Copyright (c) 2022, Oracle and/or its affiliates.
     4  # Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     5  #
     6  
     7  #Get latest line-rate from master/release for comparison
     8  UNIT_TEST_TXT_FILE=unit-test-coverage-number.txt
     9  LOCAL_BRANCH_LINE_RATE=$(grep -i '<coverage' coverage.xml | awk -F' ' '{print $2}' | sed -E 's/line-rate=\"(.*)\"/\1/')
    10  LOCAL_BRANCH_VERSION=$(grep -i 'version=' .verrazzano-development-version | awk -F '=' '{print $2}' | head -c3)
    11  
    12  #Returns 1 if Local-line-rate is gte Remote-line-rate, otherwise returns 0
    13  compareCoverageNumbers() {
    14    REMOTE_LINE_RATE=$(cat "$UNIT_TEST_TXT_FILE")
    15    RATE="$LOCAL_BRANCH_LINE_RATE >= $REMOTE_LINE_RATE"
    16    RESULT=$(echo "$RATE" | bc)
    17    if [[ "$RESULT" -eq 1 ]]; then
    18      echo "PASS."
    19      echo "Branch-line-rate: $LOCAL_BRANCH_LINE_RATE is gte to Remote-line-rate: $REMOTE_LINE_RATE"
    20    else
    21      echo "WARNING: Unit Test coverage(line-rate) does NOT pass"
    22      echo "Branch-line-rate: $LOCAL_BRANCH_LINE_RATE is lte to Remote-line-rate: $REMOTE_LINE_RATE"
    23      if [[ "$FAIL_BUILD_COVERAGE" = true ]]; then
    24        echo "Job Failed."
    25        exit 1
    26      fi
    27    fi
    28  }
    29  
    30  #Only upload coverage number if it passes
    31  uploadToObjectStorage() {
    32    echo "Writing coverage number $LOCAL_BRANCH_LINE_RATE to $UNIT_TEST_TXT_FILE ..."
    33    echo "$LOCAL_BRANCH_LINE_RATE" > "$UNIT_TEST_TXT_FILE"
    34    echo "Putting in object storage at $CLEAN_BRANCH_NAME/$UNIT_TEST_TXT_FILE"
    35    oci --region us-phoenix-1 os object put --force --namespace "$OCI_OS_NAMESPACE" -bn "$OCI_OS_BUCKET" --name "$CLEAN_BRANCH_NAME"/"$UNIT_TEST_TXT_FILE" --file "$UNIT_TEST_TXT_FILE"
    36  }
    37  
    38  #Determine if on a release or master branch and download unit-test-coverage-number
    39  if [[ ! "$CLEAN_BRANCH_NAME" =~ ^release-[0-9]+\.[0-9]+$|^master$ ]]; then
    40    echo "Trying to download $UNIT_TEST_TXT_FILE from release-$LOCAL_BRANCH_VERSION..."
    41    oci --region us-phoenix-1 os object get --namespace "$OCI_OS_NAMESPACE" -bn "$OCI_OS_BUCKET" --name release-"$LOCAL_BRANCH_VERSION"/"$UNIT_TEST_TXT_FILE" --file "$UNIT_TEST_TXT_FILE"
    42  
    43    if [[ $? -gt 0 ]]; then
    44      echo "Trying to download $UNIT_TEST_TXT_FILE from master..."
    45      oci --region us-phoenix-1 os object get --namespace "$OCI_OS_NAMESPACE" -bn "$OCI_OS_BUCKET" --name master/"$UNIT_TEST_TXT_FILE" --file "$UNIT_TEST_TXT_FILE"
    46    fi
    47  
    48    #Runs when we are on a feature branch and determines if line coverage passes
    49    compareCoverageNumbers
    50  
    51  else
    52    echo "Is a release-* or master branch..."
    53    if [[ "$UPLOAD_UT_COVERAGE" = true ]]; then
    54      uploadToObjectStorage
    55    fi
    56  fi