# Copyright (c) Microsoft Corporation.
# Licensed under the MIT license.

jobs:
- job: 'integration_test_frameworkController'
  timeoutInMinutes: 120

  steps:
  - script: python3 -m pip install --upgrade pip setuptools --user
    displayName: 'Install python tools'

  - script: |
      set -e
      cd deployment/pypi
      if [ -d ./dist ]
      then
        rm -rf ./dist/*
      fi
      echo 'building prerelease package...'
      make build
      ls $(Build.SourcesDirectory)/deployment/pypi/dist/
    condition: eq( variables['build_docker_img'], 'true' )
    displayName: 'build nni bdsit_wheel'

  - script: |
      source install.sh
    displayName: 'Install nni toolkit via source code'

  - script: |
      set -e
      sudo apt-get install swig -y
      PATH=$HOME/.local/bin:$PATH nnictl package install --name=SMAC
      PATH=$HOME/.local/bin:$PATH nnictl package install --name=BOHB
    displayName: 'Install dependencies for integration tests in frameworkcontroller mode'

  - script: |
      set -e
      if [ $(build_docker_img) = 'true' ]
      then
        cd deployment/pypi
        docker login -u $(docker_hub_user) -p $(docker_hub_pwd)

        echo 'updating docker file for installing nni from local...'
        # update Dockerfile to install NNI in docker image from whl file built in last step
        sed -ie 's/RUN python3 -m pip --no-cache-dir install nni/COPY .\/dist\/* .\/\nRUN python3 -m pip install nni-*.whl/' ../docker/Dockerfile
        cat ../docker/Dockerfile
        export IMG_TAG=`date -u +%y%m%d%H%M`
        docker build -f ../docker/Dockerfile -t $(test_docker_img_name):$IMG_TAG .
        docker push $(test_docker_img_name):$IMG_TAG
        export TEST_IMG=$(test_docker_img_name):$IMG_TAG
        cd ../../
      else
        export TEST_IMG=$(existing_docker_img)
      fi
      echo "TEST_IMG:$TEST_IMG"
      cd test
      python3 nni_test/nnitest/generate_ts_config.py --ts frameworkcontroller --keyvault_vaultname $(keyVault_vaultName) --keyvault_name $(keyVault_name) \
      --azs_account $(azureStorage_accountName) --azs_share $(azureStorage_azureShare) --nni_docker_image $TEST_IMG --nni_manager_ip $(nni_manager_ip)

      cat config/training_service.yml
      PATH=$HOME/.local/bin:$PATH python3 nni_test/nnitest/run_tests.py --config config/integration_tests.yml --ts frameworkcontroller --exclude multi-phase
    displayName: 'integration test'
