stages:
- build
- test
- package

variables:
  GIT_DEPTH: 1
  GIT_SUBMODULE_STRATEGY: recursive
  # pin to specific docker images for build repeatability
  #points registry.gitlab.syncad.com/hive/hive/runtime:ubuntu-20.04-3
  RUNTIME_IMAGE_TAG: "@sha256:58b2ef26ece0463bad9ba61c665ffb9dc4255a4d65b27f56048786e3fae7b2e5"
  #points registry.gitlab.syncad.com/hive/hive/builder:ubuntu-20.04-3
  BUILDER_IMAGE_TAG: "@sha256:590750387c74c470ee30910502b4f855122eae9ef9c6e7ad0841985abdc3938c"
  #points registry.gitlab.syncad.com/hive/hive/test:ubuntu-20.04-3
  TEST_IMAGE_TAG: "@sha256:991f5c225ea66dccabd43ae18ebe9cc4c90dc2b14d80625202f96e041c832ac6"

testnet_node_build:
  stage: build
  image: "$CI_REGISTRY_IMAGE/builder$BUILDER_IMAGE_TAG"
  script:
    # TESTNET=ON HIVE_LINT=OFF
    - ./ciscripts/build.sh ON OFF
    - mkdir -p "$CI_JOB_NAME"/tests/unit
    - mv build/install-root "$CI_JOB_NAME"
    - mv contrib/hived.run "$CI_JOB_NAME"
    - mv contrib/config-for-docker.ini "$CI_JOB_NAME"
    - mv build/tests/unit/chain_test build/tests/unit/plugin_test "$CI_JOB_NAME"/tests/unit
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
    paths:
    - "$CI_JOB_NAME"
    expire_in: 6 hours
  tags:
     - public-runner-docker
     - hived-for-tests
     - hived

consensus_build:
  stage: build
  image: "$CI_REGISTRY_IMAGE/builder$BUILDER_IMAGE_TAG"
  script:
    # TESTNET=OFF HIVE_LINT=ON
    - ./ciscripts/build.sh OFF ON
    - mkdir "$CI_JOB_NAME"
    - mv build/install-root "$CI_JOB_NAME"
    - mv contrib/hived.run "$CI_JOB_NAME"
    - mv contrib/config-for-docker.ini "$CI_JOB_NAME"
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
    paths:
    - "$CI_JOB_NAME"
    expire_in: 6 hours
  tags:
    - public-runner-docker
    - hived

chain_test:
  stage: test
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/runtime$RUNTIME_IMAGE_TAG"
  variables:
    GIT_STRATEGY: none
  script:
    - ./testnet_node_build/tests/unit/chain_test --log_format=JUNIT --log_sink=chain_test_results.xml --log_level=error > /dev/null 2>&1
  artifacts:
    reports:
      junit: chain_test_results.xml
    when: always
    expire_in: 6 months
  tags:
    - public-runner-docker

plugin_test:
  stage: test
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/runtime$RUNTIME_IMAGE_TAG"
  variables:
    GIT_STRATEGY: none
  script:
    # note: consider using --result_code=no to force exit code 0 even when tests fail because they currently fail and I want a nice green checkmark
    - ./testnet_node_build/tests/unit/plugin_test --log_format=JUNIT --log_sink=plugin_test_results.xml --log_level=error > /dev/null 2>&1
  artifacts:
    reports:
      junit: plugin_test_results.xml
    when: always
    expire_in: 6 months
  tags:
    - public-runner-docker

.beem_setup : &beem_setup
  - git clone --depth=1 --single-branch --branch master https://gitlab.syncad.com/hive/beem.git
  - python3 -m venv .python_tests
  - source .python_tests/bin/activate
  - python3 -m pip install --upgrade pip
  - pip3 install junit-xml
  - pip3 install python-dateutil
  - cd beem
  - python3 -m pip install --upgrade -r requirements-test.txt
  - python3 setup.py install
  - cd ..
  - mkdir -p build/tests/hive-node-data

beem_tests:
  stage: test
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  variables:
    PYTHONPATH: $CI_PROJECT_DIR/tests/functional
  script:
    - *beem_setup
    - cd tests/functional/python_tests/dhf_tests
    - "python3 run_proposal_tests.py initminer hive.fund 5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n --run-hived $CI_PROJECT_DIR/testnet_node_build/install-root/bin/hived --working-dir=$CI_PROJECT_DIR/build/tests/hive-node-data"
    - rm -rf $CI_PROJECT_DIR/build/tests/hive-node-data
    - mkdir -p $CI_PROJECT_DIR/build/tests/hive-node-data
    - "python3 beem_dhf_tests.py initminer initminer 5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n --run-hived $CI_PROJECT_DIR/testnet_node_build/install-root/bin/hived --working-dir=$CI_PROJECT_DIR/build/tests/hive-node-data --no-erase-proposal --junit-output=beem_dhf_test.xml"
    - cd ../hived
    - "python3 hived_test_hybrid_operations.py initminer 5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n --run-hived $CI_PROJECT_DIR/testnet_node_build/install-root/bin/hived --working-dir=$CI_PROJECT_DIR/build/tests/hive-node-data"

  artifacts:
    paths:
    - tests/functional/python_tests/dhf_tests/artifacts/
    - tests/functional/python_tests/dhf_tests/beem_dhf_tests.log
    - tests/functional/python_tests/hived/hived_hybrid_operations_test.log
    reports:
      junit: tests/functional/python_tests/dhf_tests/beem_dhf_test.xml
    when: always
    expire_in: 6 months
  tags:
    - public-runner-docker

list_proposals_tests:
  stage: test
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  variables:
    PYTHONPATH: $CI_PROJECT_DIR/tests/functional
  script:
    - *beem_setup
    - cd tests/functional/python_tests/dhf_tests
    - "python3 list_proposals_tests.py initminer initminer 5JNHfZYKGaomSFvd4NUdQ9qMcEAC43kujbfjueTHpVapX1Kzq2n --run-hived $CI_PROJECT_DIR/testnet_node_build/install-root/bin/hived --working-dir=$CI_PROJECT_DIR/build/tests/hive-node-data --junit-output=list_proposals_tests.xml"

  artifacts:
    paths:
    - tests/functional/python_tests/dhf_tests/hdf_list_proposal.log
    reports:
      junit: tests/functional/python_tests/dhf_tests/list_proposals_tests.xml
    when: always
    expire_in: 6 months
  tags:
    - public-runner-docker

.test_tools_based:
  variables:
    HIVED_PATH: "$CI_PROJECT_DIR/testnet_node_build/install-root/bin/hived"
    CLI_WALLET_PATH: "$CI_PROJECT_DIR/testnet_node_build/install-root/bin/cli_wallet"
    GET_DEV_KEY_PATH: "$CI_PROJECT_DIR/testnet_node_build/install-root/bin/get_dev_key"
  before_script:
    - apt-get update -y && apt-get install -y python3 python3-pip python3-dev
    - export NUMBER_OF_PROCESSES=$(nproc)
    - pip3 install pytest pytest-xdist "$CI_PROJECT_DIR/tests/test_tools"
  artifacts:
    name: "$CI_JOB_NAME-$CI_COMMIT_REF_NAME"
    paths:
    - "**/generated_during_*"
    - "**/generated_by_package_fixtures"
    when: always
    expire_in: 1 week

cli_wallet_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/cli_wallet
    - pytest --junitxml="./report.xml"
  tags:
    - public-runner-docker
  artifacts:
    reports:
      junit: tests/functional/python_tests/cli_wallet/report.xml

hived_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/hived
    - pytest --junitxml="./report.xml"
  tags:
    - public-runner-docker
  artifacts:
    reports:
      junit: tests/functional/python_tests/hived/report.xml

cli_wallet_extended_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/cli_wallet_extended_tests
    - pytest -n ${NUMBER_OF_PROCESSES} --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/functional/python_tests/cli_wallet_extended_tests/report.xml
  tags:
    - public-runner-docker

fork_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/fork_tests
    - pytest --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/functional/python_tests/fork_tests/report.xml
  tags:
    - public-runner-docker

synchronization_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/synchronization_tests
    - pytest --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/functional/python_tests/synchronization_tests/report.xml
  tags:
    - public-runner-docker

test_tools_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/test_tools/tests
    - pytest -n ${NUMBER_OF_PROCESSES} --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/test_tools/tests/report.xml
  tags:
    - public-runner-docker

rc_direct_delegations_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/functional/python_tests/direct_rc_delegations_tests
    - pytest -n ${NUMBER_OF_PROCESSES} --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/functional/python_tests/direct_rc_delegations_tests/report.xml
  tags:
    - public-runner-docker

message_format_tests:
  stage: test
  extends: .test_tools_based
  needs:
    - job: testnet_node_build
      artifacts: true
  image: "$CI_REGISTRY_IMAGE/test$TEST_IMAGE_TAG"
  script:
    - cd tests/api_tests/message_format_tests
    - pytest -n ${NUMBER_OF_PROCESSES} --junitxml="./report.xml"
  artifacts:
    reports:
      junit: tests/api_tests/message_format_tests/report.xml
  tags:
    - public-runner-docker

