pool:
    name: ads-build-1es-hosted-pool
    demands:
    - ImageOverride -equals ADS-Linux_Image

steps:

  - task: DockerInstaller@0
    displayName: Installing Docker
    inputs:
      dockerVersion: 17.09.0-ce
      releaseType: stable

  - script: docker pull mcr.microsoft.com/mssql/server:2022-latest
    displayName: Pull MSSQL Docker Image

  - bash: echo "##vso[task.setvariable variable=sqlOnPrem_password;issecret=true]Test-$(Build.BuildNumber)-$(Get-Date -format yyyyMMdd-Hmmss)"
    displayName: Generate password for test server

  - script: 'docker run -e "ACCEPT_EULA=Y" -e "SA_PASSWORD=$(sqlOnPrem_password)" -e "MSSQL_AGENT_ENABLED=True" -p 1433:1433 --name sql1 -h sql1 -d mcr.microsoft.com/mssql/server:2022-latest'
    displayName: Starting Server in Docker Container

  - task: UseDotNet@2
    displayName: Use .NET Core SDK
    inputs:
      useGlobalJson: true

  - task: Bash@3
    displayName: Creating Loc Directories Expected By Dotnet Build
    inputs:
      filePath: ./azure-pipelines/createBuildDirectories.sh

  - task: NuGetAuthenticate@1

  - task: DotNetCoreCLI@2
    displayName: Building Test Environment
    inputs:
      projects: '**/Microsoft.SqlTools.ServiceLayer.TestEnvConfig.csproj'

  - bash: 'echo "<?xml version=\"1.0\" encoding=\"utf-8\"?>
        <Instances>
          <Instance VersionKey=\"sqlOnPrem\">
          <DataSource>localhost</DataSource>
          <UserId>sa</UserId>
          <Password></Password>
        </Instance>
      </Instances>" > test/Microsoft.SqlTools.ServiceLayer.TestEnvConfig/SQLConnectionInstances.xml'
    displayName: 'Writing test environment config file'

  - task: DotNetCoreCLI@2
    displayName: Setting Up Test Environment
    inputs:
      command: run
      projects: 'test/Microsoft.SqlTools.ServiceLayer.TestEnvConfig/Microsoft.SqlTools.ServiceLayer.TestEnvConfig.csproj'
      arguments: 'test/Microsoft.SqlTools.ServiceLayer.TestEnvConfig/SQLConnectionInstances.xml'
      testRunTitle: 'Setting tests'
    env:
      sqlOnPrem_password: '$(sqlOnPrem_password)'

  - task: AzureKeyVault@1
    displayName: 'Azure Key Vault: SqlToolsSecretStore'
    inputs:
      azureSubscription: 'ClientToolsInfra_670062 (88d5392f-a34f-4769-b405-f597fc533613)'
      KeyVaultName: SqlToolsSecretStore
      SecretsFilter: 'sqltools-backup-url-tests-blobcontaineruri,sqltools-backup-url-tests-storageaccountname,sqltools-backup-url-tests-storageaccountkey'

  - task: DotNetCoreCLI@2
    displayName: Building Integration Tests
    inputs:
      projects: '**/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj'

  - task: DotNetCoreCLI@2
    displayName: Running Integration Tests
    inputs:
      command: test
      projects: '**/Microsoft.SqlTools.ServiceLayer.IntegrationTests.csproj'
      arguments: '--no-build'
      testRunTitle: 'SqlToolsService Integration Tests'
    env:
      AzureStorageAccountKey: '$(sqltools-backup-url-tests-storageaccountkey)'
      AzureStorageAccountName: '$(sqltools-backup-url-tests-storageaccountname)'
      AzureBlobContainerUri: '$(sqltools-backup-url-tests-blobcontaineruri)'
