summaryrefslogtreecommitdiff
path: root/tests/lib/lib.ps1
blob: 58b1a424dae9396042ce5deb14b7c59cb5e81f1b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
if ($PSVersionTable.PSEdition -ne 'Core') {
  throw "The tests are not compatible with Windows PowerShell, please use PowerShell Core instead"
}

function Remove-Escapes {
  param(
    [parameter(ValueFromPipeline = $true)]
    [string[]]$InputObject
  )
  process {
    $InputObject | ForEach-Object { $_ -replace '\x1b(\[(\?..|.)|.)', '' }
  }
}

# Implementation-independent base class
class Distro {
  [string]$id

  [string]Launch() {
    throw "Not implemented"
  }

  [string]GetPath([string]$path) {
    throw "Not implemented"
  }

  [string]FindTarball() {
    # Check if a fresh tarball exists in result, otherwise try one in the current directory
    $tarball = "./result/tarball/nixos-wsl-installer.tar.gz"
    if (!(Test-Path $tarball)) {
      $tarball = "./nixos-wsl-installer.tar.gz"
      if (!(Test-Path $tarball)) {
        throw "Could not find the installer tarball! Run nix build first, or place one in the current directory."
      }
    }
    Write-Host "Using tarball: $tarball"
    return $tarball
  }

  [void]InstallConfig([string]$path) {
    Write-Host "Installing config: $path"

    # Move config out of the way
    $this.Launch("/bin/sh -c 'test -f /etc/nixos/base.nix || sudo mv /etc/nixos/configuration.nix /etc/nixos/base.nix'")
    $LASTEXITCODE | Should -Be 0

    # Copy the new config
    $this.Launch("sudo cp $($this.GetPath($path)) /etc/nixos/configuration.nix")
    $LASTEXITCODE | Should -Be 0

    # Rebuild
    $this.Launch("sudo nixos-rebuild switch")
    $LASTEXITCODE | Should -Be 0

    Write-Host "Config installed successfully"
  }

  [void]Shutdown() {
    throw "Not implemented"
  }

  [void]Uninstall() {
    throw "Not implemented"
  }
}

# Emulates a WSL distro using a Docker container (for Linux hosts)
class DockerDistro : Distro {
  static [string]$hostMount = "/mnt/c"
  static [string]$imageName = "local:nixos-wsl"

  static [bool]$imageCreated = $false

  DockerDistro() {
    $tarball = $this.FindTarball()

    if (!([DockerDistro]::imageCreated)) {
      # Build docker image from the installer tarball
      $tmpdir = $(mktemp -d)
      Copy-Item $PSScriptRoot/Dockerfile $tmpdir
      Copy-Item $tarball $tmpdir
      docker build -t $([DockerDistro]::imageName) $tmpdir | Write-Host
      Remove-Item $tmpdir -Recurse -Force

      [DockerDistro]::imageCreated = $true
    }

    $this.id = $(New-Guid).ToString()

    docker run -di --privileged --volume "/:$([DockerDistro]::hostMount)" --name $this.id $([DockerDistro]::imageName) "/bin/sh" | Out-Null
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to launch container"
    }
  }

  [Array]Launch([string]$command) {
    Write-Host "> $command"
    $result = @()
    docker exec -t $this.id /nix/nixos-wsl/entrypoint -c $command | Tee-Object -Variable result | Write-Host
    return $result | Remove-Escapes
  }

  [string]GetPath([string]$path) {
    return [DockerDistro]::hostMount + $(readlink -f $path)
  }

  [void]Shutdown() {
    docker restart $this.id # Restart instead of stop so that exec can still be used
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to stop container"
    }
  }

  [void]Uninstall() {
    docker rm -f $this.id
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to remove container"
    }
  }
}

# A real WSL distro (for Windows hosts)
class WslDistro : Distro {
  [string]$tempdir

  WslDistro() {
    $tarball = $this.FindTarball()

    $this.id = $(New-Guid).ToString()
    $this.tempdir = Join-Path $([System.IO.Path]::GetTempPath()) $this.id
    New-Item -ItemType Directory $this.tempdir

    & wsl.exe --import $this.id $this.tempdir $tarball --version 2 | Write-Host
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to import distro"
    }
    & wsl.exe --list | Should -Contain $this.id
  }

  [Array]Launch([string]$command) {
    Write-Host "> $command"
    $result = @()
    & wsl.exe -d $this.id -e /nix/nixos-wsl/entrypoint -c $command | Tee-Object -Variable result | Write-Host
    return $result | Remove-Escapes
  }

  [string]GetPath([string]$path) {
    return $this.Launch("wslpath $($path -replace "\\", "/")") | Select-Object -Last 1
  }

  [void]Shutdown() {
    & wsl.exe -t $this.id
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to stop distro"
    }
  }

  [void]Uninstall() {
    & wsl.exe --unregister $this.id | Write-Host
    if ($LASTEXITCODE -ne 0) {
      throw "Failed to unregister distro"
    }
    Remove-Item $this.tempdir -Recurse -Force
  }
}

# Auto-select the implementation to use
function Install-Distro() {
  if ($IsWindows) {
    Write-Host "Detected Windows host, using WSL2"
    return [WslDistro]::new()
  }
  else {
    Write-Host "Detected Linux host, using Docker"
    return [DockerDistro]::new()
  }
}