Skip to content

Commit

Permalink
tests(e2e): Add barebones E2E test workflow (#818)
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielNagy authored Oct 26, 2023
2 parents 6b79e48 + 30ed844 commit fd4dd83
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 4 deletions.
93 changes: 93 additions & 0 deletions .github/workflows/e2e-tests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: E2E - Run tests

on:
workflow_dispatch:
# push:
# branches:
# - main
# tags:
# - "*"
# pull_request:

jobs:
supported-releases:
name: Build matrix for supported ADSys and Ubuntu releases
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.set-supported-releases.outputs.matrix }}
steps:
- name: Install needed binaries
run: |
sudo apt-get update
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y distro-info
- name: Build matrix
id: set-supported-releases
run: |
set -eu
all="$(distro-info --supported-esm) $(distro-info --supported)"
all="$(echo $all | tr ' ' '\n' | sort -u)"
releases=""
for r in ${all}; do
# Filter out unsupported LTS releases
if [ "${r}" = "trusty" -o "${r}" = "xenial" -o "${r}" = "bionic" ]; then
continue
fi
if [ -n "${releases}" ]; then
releases="${releases}, "
fi
releases="${releases}'${r}'"
done
echo matrix="${releases}" >> $GITHUB_OUTPUT
tests:
name: Tests
runs-on: ubuntu-latest
needs:
- supported-releases
strategy:
matrix:
codename: ${{ fromJSON(format('[{0}]', needs.supported-releases.outputs.matrix)) }}
fail-fast: false
env:
AD_PASSWORD: ${{ secrets.AD_PASSWORD }}
steps:
- uses: azure/login@v1
with:
creds: ${{ secrets.AZURE_CREDENTIALS }}
- uses: actions/checkout@v4
- uses: actions/setup-go@v4
with:
go-version-file: go.mod
- name: Set up SSH key
run: |
mkdir -p ~/.ssh
echo "${{ secrets.AZURE_SSH_KEY }}" > ~/.ssh/id_rsa-e2e
chmod 600 ~/.ssh/id_rsa-e2e
- name: Build adsys deb
run: |
go run ./e2e/cmd/provision_resources/00_build_adsys_deb --codename ${{ matrix.codename }}
- name: Set up VPN connection
uses: ./.github/actions/azure-sstpc-vpn
with:
gateway: ${{ secrets.VPN_GATEWAY }}
ca: ${{ secrets.VPN_CA }}
cert: ${{ secrets.VPN_CERT }}
key: ${{ secrets.VPN_KEY }}
- name: Provision client VM
run: |
go run ./e2e/cmd/provision_resources/01_provision_client --ssh-key ~/.ssh/id_rsa-e2e
- name: Deprovision resources
if: ${{ always() }}
run: |
# Check inventory status to see if we need to deprovision
if [ ! -f inventory.yaml ] || grep -q 'vmid: ""' inventory.yaml; then
echo "Inventory file not found, skipping deprovision"
exit 0
fi
go run ./e2e/cmd/provision_resources/99_deprovision
5 changes: 5 additions & 0 deletions e2e/cmd/provision_resources/01_provision_client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"os"
"path/filepath"
"strings"
"time"

"github.com/google/uuid"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -113,6 +114,7 @@ func action(ctx context.Context, cmd *command.Command) error {
"--subnet", "default",
"--nic-delete-option", "Delete",
"--public-ip-address", "",
"--ssh-key-name", "adsys-e2e",
"--storage-sku", "StandardSSD_LRS",
"--os-disk-delete-option", "Delete",
"--tags", "project=AD", "subproject=adsys-e2e-tests", "lifetime=6h",
Expand Down Expand Up @@ -147,6 +149,9 @@ func action(ctx context.Context, cmd *command.Command) error {
ipAddress := vm.IP
id := vm.ID

// Sleep for a bit to let the VM finish booting
time.Sleep(5 * time.Second)

client, err := remote.NewClient(ipAddress, "root", sshKey)
if err != nil {
return fmt.Errorf("failed to connect to VM: %w", err)
Expand Down
76 changes: 76 additions & 0 deletions e2e/cmd/provision_resources/99_deprovision/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Package main provides a script that deprovisions previously created resources.
// This currently consists of leaving the realm and deleting the client VM.
package main

import (
"context"
"fmt"
"os"
"path/filepath"

log "github.com/sirupsen/logrus"
"github.com/ubuntu/adsys/e2e/internal/az"
"github.com/ubuntu/adsys/e2e/internal/command"
"github.com/ubuntu/adsys/e2e/internal/inventory"
"github.com/ubuntu/adsys/e2e/internal/remote"
)

var adPassword string

func main() {
os.Exit(run())
}

func run() int {
cmd := command.New(action,
command.WithValidateFunc(validate),
command.WithStateTransition(inventory.ClientProvisioned, inventory.Deprovisioned),
)
cmd.Usage = fmt.Sprintf(`go run ./%s [options]
Deprovision and destroy previously created resources.
This will leave the realm, delete the computer object from the domain, and
destroy the Azure client VM.`, filepath.Base(os.Args[0]))

return cmd.Execute(context.Background())
}

func validate(_ context.Context, _ *command.Command) error {
adPassword = os.Getenv("AD_PASSWORD")
if adPassword == "" {
return fmt.Errorf("AD_PASSWORD environment variable must be set")
}

return nil
}

func action(ctx context.Context, cmd *command.Command) error {
ipAddress := cmd.Inventory.IP
sshKey := cmd.Inventory.SSHKeyPath
client, err := remote.NewClient(ipAddress, "root", sshKey)
if err != nil {
return fmt.Errorf("failed to connect to VM: %w", err)
}
defer client.Close()

// Leave realm and delete computer object
_, err = client.Run(ctx, fmt.Sprintf("realm leave --remove -U localadmin -v --unattended <<<'%s'", adPassword))
if err != nil {
return fmt.Errorf("failed to leave domain: %w", err)
}

// Destroy the client VM
log.Infof("Destroying client VM %q", cmd.Inventory.VMName)
_, _, err = az.RunCommand(ctx, "vm", "delete",
"--resource-group", "AD",
"--name", cmd.Inventory.VMName,
"--force-deletion", "true",
"--yes",
)
if err != nil {
return err
}

return nil
}
1 change: 1 addition & 0 deletions e2e/internal/inventory/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ const (
PackageBuilt State = "package_built"
ClientProvisioned State = "client_provisioned"
ADProvisioned State = "ad_provisioned"
Deprovisioned State = "deprovisioned"
)
13 changes: 9 additions & 4 deletions e2e/scripts/patches/jammy.patch
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
diff --git a/debian/control b/debian/control
index d6fb19f2..85eab447 100644
index d6fb19f2..93764223 100644
--- a/debian/control
+++ b/debian/control
@@ -5,7 +5,7 @@ Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Expand All @@ -12,16 +12,21 @@ index d6fb19f2..85eab447 100644
libdbus-1-dev,
libglib2.0-dev,
diff --git a/debian/rules b/debian/rules
index 43646c6a..b984a796 100755
index 43646c6a..403e7bb9 100755
--- a/debian/rules
+++ b/debian/rules
@@ -25,5 +25,8 @@ export DH_GOLANG_INSTALL_ALL := 1
@@ -25,9 +25,14 @@ export DH_GOLANG_INSTALL_ALL := 1
# Tests needing sudo will be skipped automatically
export ADSYS_SKIP_INTEGRATION_TESTS=1

+# Run with Go 1.20
+export PATH := /usr/lib/go-1.20/bin/:$(PATH)
+
%:
dh $@ --buildsystem=golang --with=golang,apport
dh $@ --buildsystem=golang --with=golang,apport

+override_dh_dwz:
+
override_dh_auto_clean:
dh_auto_clean
# Create the vendor directory when building the source package

0 comments on commit fd4dd83

Please sign in to comment.