# E2E test pipeline - performs end-to-end tests on the following platforms
#  - Ubuntu 20.04

resources:
    containers:
        - container: ubuntu
          image: ubuntu:20.04
    repositories:
        - repository: 1ESPipelineTemplates
          type: git
          name: 1ESPipelineTemplates/1ESPipelineTemplates
          ref: refs/tags/release

parameters:
    - name: SKIP_TEARDOWN
      displayName: "Leave test infrastructure in-place"
      type: boolean
      default: false

    - name: TEST_ARTIFACTS_UPLOADCONTAINER
      displayName: "The name of the container to upload the test artifacts"
      type: string
      default: "gen1-test-artifacts"

variables:
    SKIP_TEARDOWN: ${{ parameters.SKIP_TEARDOWN }}
    E2E_WORKING_DIR: $(Build.SourcesDirectory)/azurepipelines/e2e_test

    Adu_Arm_Service_Connection: "adu_subscr_servc_connection"

    Test_Vm_Ssh_PublicKey: ""
    Test_Vm_Ssh_PrivateKeyFile: "adu_e2e_test_ssh_key"
    Test_Vm_Ssh_PublicKeyFile: "adu_e2e_test_ssh_key.pub"
    Test_VmAdmin: "azureuser"
    Test_ResourceGroupname: "e2e-rg-$(Build.BuildId)" # Composed of e2e-rg-$(Build.BuildId)
    Test_VmName: "e2e-vm-$(Build.BuildId)"            # Composed of e2e-vm-$(Build.BuildId)
    Test_InfraDeploymentName: "e2e-deployment-$(Build.BuildId)" #Composed of e2e-deployment-$(Build.BuildId)
    Test_VmHostname: "" # Set after the VM is created
    Test_Location: "westus3"
    Test_DeviceName: "ubuntu-2004-x509-test-device"
    Test_Distro: "ubuntu-20.04"
    Test_Arch: "amd64"
    Test_ArtifactUploadContainer: "gen1-debian-packages"
    Test_Artifacts_Folder: "adu-client-ubuntu-2004-amd64"

    Test_DeviceGroup: "gen1-test-devices"
    Test_DeploymentId: "aptdeployment-$(Build.BuildId)"
    Test_UpdateName: "Virtual"
    Test_UpdateProvider: "Contoso1"
    Test_UpdateVersion: "1.0.2"

name: "E2E Automated Test Run"
extends:
  template: v1/1ES.Unofficial.PipelineTemplate.yml@1ESPipelineTemplates
  parameters:
    pool:
      name: aduc_1es_client_pool
      os: linux

    sdl:
      sourceAnalysisPool:
        name: 1es_hosted_pool_windows
        os: windows
      suppression:
        suppressionFile: $(Build.SourcesDirectory)\guardian\SDL\.gdnsuppress
    customBuildTags:
    - ES365AIMigrationTooling

    stages:
        - stage: PerformNativeBuilds
          displayName: Builds all the native architecture builds
          jobs:
              - job: BuildUbuntu_2004_AMD64
                displayName: Building the Device Update Package for Ubuntu 20.04 AMD64
                continueOnError: False
                pool: aduc_1es_client_pool
                steps:
                    - template: /azurepipelines/build/templates/adu-native-build-steps.yml@self
                      parameters:
                          targetOs: ubuntu2004
                          targetArch: amd64
        - stage: PublishPackageUnderTest
          displayName: Publish the Package under Test to the Storage
          jobs:
              - job: PublishUbuntu2004_AMD64_Package
                displayName: Publish the Ubuntu 20.04 AMD64 Package to the Storage
                continueOnError: False
                pool: aduc_1es_client_pool
                steps:
                    - task: DownloadPipelineArtifact@2
                      displayName: "Download DeviceUpdate Package from pipeline"
                      inputs:
                          source: "current"
                          project: "adu-linux-client"
                          pipeline: "Azure.adu-private-preview.e2e-test"
                          itemPattern: "*ubuntu2004-amd64*/*.deb"
                          path: $(Build.ArtifactStagingDirectory)
                    - bash: |
                        ls -al $(Build.ArtifactStagingDirectory)
                        pPath=$(find $(Build.ArtifactStagingDirectory) -type f -name '*.deb')
                        echo "##vso[task.setvariable variable=packagePath;isOutput=true]$pPath"
                        echo "Path to package is $pPath"
                      displayName: "Get the path to the package under test"
                      name: pathToPackageUnderTest
                    - task: AzureCLI@2
                      displayName: "Publish the Ubuntu 20.04 AMD64 Package to the Storage"
                      inputs:
                          azureSubscription: $(Adu_Arm_Service_Connection)
                          scriptType: bash
                          scriptLocation: inlineScript
                          inlineScript: |
                              echo "Publishing the Ubuntu 20.04 AMD64 Package to the Storage"
                              az storage blob upload --auth-mode login --account-name $(AZURE_STORAGE_ACCOUNT_NAME) --container-name $(Test_ArtifactUploadContainer) --file $(pathToPackageUnderTest.packagePath) --name deviceupdate-$(Build.BuildId).deb --no-progress
                          failOnStandardError: true
                    - bash: |
                        echo "The Package under Test is $(pathToPackageUnderTest.packagePath)"
                        echo "Uploaded to storage as deviceupdate-$(Build.BuildId).deb"
                        echo "##vso[task.setvariable variable=packageUnderTest;isOutput=true]deviceupdate-$(Build.BuildId).deb"
                      displayName: "Display the Package under Test"
                      name: package_under_test_publisher
        - stage: InitializeTestInfra
          displayName: Initializes the Infrastructure for the Automated Tests
          variables:
              Test_PackageUnderTestName: $[ stageDependencies.PublishPackageUnderTest.PublishUbuntu2004_AMD64_Package.outputs['package_under_test_publisher.packageUnderTest'] ]
          pool: aduc_1es_client_pool
          jobs:

              - job: SetupUbuntu2004_AMD64_VM
                displayName: "Setting up VM for Ubuntu 20.04 AMD64 Test VM"
                timeoutInMinutes: 360
                cancelTimeoutInMinutes: 360
                pool: aduc_1es_client_pool
                steps:

                    - task: DownloadSecureFile@1
                      displayName: "Download the SSH Key for the VM"
                      name: public_vm_key
                      inputs:
                          secureFile: $(Test_Vm_Ssh_PublicKeyFile)
                          retryCount: 3
                          retryIntervalInSeconds: 5
                    - task: DownloadSecureFile@1
                      displayName: "Download the SSH Key for the VM"
                      name: private_vm_key
                      inputs:
                          secureFile: $(Test_Vm_Ssh_PrivateKeyFile)
                          retryCount: 3
                          retryIntervalInSeconds: 5
                    - bash: |
                         curl -s http://ipinfo.io/json | jq '.ip'
                      displayName: "Get the Public IP Address of the Agent"
                    - bash: |
                        key_contents=$(cat $(public_vm_key.secureFilePath))
                        echo "##vso[task.setvariable variable=Test_Vm_Ssh_PublicKey;isOutput=true;]$key_contents"
                      displayName: "Set the VM SSH Key variable for reading into the Azure VM Template"
                      name: set_ssh_key
                      workingDirectory: $(Agent.TempDirectory)
                    - bash: |
                        echo "The Test_InfraDeploymentName is $(Test_InfraDeploymentName)"
                      displayName: "Display the Infra Deployment's Name"

                    - task: AzureCLI@2
                      displayName: "Create the RG for the E2E Test Infrastructure"
                      inputs:
                          azureSubscription: $(Adu_Arm_Service_Connection)
                          scriptType: bash
                          scriptLocation: inlineScript
                          inlineScript: |
                              echo "Creating the RG for Test Infrastructure"
                              az group create --name $(Test_ResourceGroupname) --location $(Test_Location)
                          failOnStandardError: true

                    - task: AzureCLI@2
                      displayName: "Create the Ubuntu 20.04 VM for the E2E Test"
                      inputs:
                          azureSubscription: $(Adu_Arm_Service_Connection)
                          scriptType: bash
                          scriptLocation: inlineScript
                          inlineScript: |
                              echo "Creating the VM for Test"
                              az deployment group create --name $(Test_InfraDeploymentName) --resource-group "$(Test_ResourceGroupname)" --template-file ./azurepipelines/e2e_test/templates/az_vm.template.json --parameters adminUsername="$(Test_VmAdmin)" adminKey="$(set_ssh_key.Test_Vm_Ssh_PublicKey)" adminPassword="$(VM_ADMIN_PASS)" vmName="$(Test_VmName)" vmSize=Standard_DS1_v2 location=$(Test_Location) msi_resource_id="$(MSI_RESOURCE_ID)" msi_client_id="$(MSI_CLIENT_ID)" duSetupScriptFileUri="$(VM_SETUP_SCRIPT_URI)" duSetupScriptFileName="$(VM_SETUP_SCRIPT_FILE_NAME)" duSetupScriptFileArgs="--distro $(Test_Distro) --architecture $(Test_Arch) -s $(AZURE_STORAGE_ACCOUNT_NAME) -p $(Test_PackageUnderTestName)"
                          failOnStandardError: true
                    - task: AzureCLI@2
                      displayName: "Get the Public Hostname of the VM"
                      inputs:
                        azureSubscription: $(Adu_Arm_Service_Connection)
                        scriptType: bash
                        scriptLocation: inlineScript
                        inlineScript: |
                            echo "Getting the Public IP Address of the VM"
                            hostname=$(az deployment group show -g $(Test_ResourceGroupname) -n $(Test_InfraDeploymentName) --query properties.outputs.hostname.value)
                            echo "##vso[task.setvariable variable=Test_VmHostname]$hostname"
                        failOnStandardError: true

                    - task: AzureCLI@2
                      displayName: "Attempt an update to the VM"
                      inputs:
                        azureSubscription: $(Adu_Arm_Service_Connection)
                        scriptType: bash
                        scriptLocation: scriptPath
                        scriptPath: "$(E2E_WORKING_DIR)/scripts/start_and_check_deployment.sh"
                        arguments: -g "$(Test_DeviceGroup)" -d "$(Test_DeploymentId)" -a "$(ADU_ACCOUNT_NAME)" -i "$(ADU_INSTANCE_ID)" -n "$(Test_UpdateName)" -p "$(Test_UpdateProvider)" -v "$(Test_UpdateVersion)"
                        failOnStandardError: false
                        continueOnError: false

                    - task: AzureCLI@2
                      displayName: "Cleanup the Test Infrastructure"
                      inputs:
                        azureSubscription: $(Adu_Arm_Service_Connection)
                        scriptType: bash
                        scriptLocation: inlineScript
                        inlineScript: |
                            az group delete --name $(Test_ResourceGroupname) --yes
                        failOnStandardError: true
