Skip to content

Commit

Permalink
Fix #16: Increase memory in VM
Browse files Browse the repository at this point in the history
This adds support for configuring memory of the VM and also increases
the default memory.
  • Loading branch information
jacob-carlborg committed Feb 7, 2023
1 parent 3042801 commit 4dba297
Show file tree
Hide file tree
Showing 12 changed files with 200 additions and 20 deletions.
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ inputs:
A list of environment variables to forward to the virtual machine.
The list should be separated with spaces.
default: ''
memory:
required: false
description: The amout of memory for the virtual machine

runs:
using: node16
Expand Down
5 changes: 5 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]
### Added
- Support for configuring memory ([#16](https://github.com/cross-platform-actions/action/issues/16))

### Changed
- Increased default memory to 13GB on macOS runner and to 6GB on Linux runners ([#16](https://github.com/cross-platform-actions/action/issues/16))

### Fixed
- Action doesn't terminate when command fails ([#21](https://github.com/cross-platform-actions/action/issues/21))
Expand Down
39 changes: 28 additions & 11 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ jobs:
architecture: ${{ matrix.os.architecture }}
version: ${{ matrix.os.version }}
shell: bash
memory: 5G
run: |
uname -a
echo $SHELL
Expand All @@ -82,12 +83,13 @@ Different platforms need to run on different runners, see the
This section lists the available inputs for the action.
| Input | Required | Default Value | Description |
| ----------------------- | -------- | ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------ |
|-------------------------|----------|---------------|--------------------------------------------------------------------------------------------------------------------------------------------------------|
| `run` | ✓ | ✗ | Runs command-line programs using the operating system's shell. This will be executed inside the virtual machine. |
| `operating_system` | ✓ | ✗ | The type of operating system to run the job on. See [Supported Platforms](#supported-platforms). |
| `version` | ✓ | ✗ | The version of the operating system to use. See [Supported Platforms](#supported-platforms). |
| `shell` | ✗ | `default` | The shell to use to execute the commands. Defaults to the default shell for the given operating system. Allowed values are: `default`, `sh` and `bash` |
| `environment_variables` | ✗ | `""` | A list of environment variables to forward to the virtual machine. The list should be separated with spaces. |
| `memory` | ✗ | `6G` or `13G` | The amount of memory for the virtual machine. The default value is `6G` for Linux runners and `13G` for macOS runners. |

All inputs are expected to be strings. It's important that especially the
`version` is explicitly specified as a string, using single or double quotes.
Expand Down
86 changes: 86 additions & 0 deletions spec/operating_systems/freebsd/freebsd.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import FreeBsd from '../../../src/operating_systems/freebsd/freebsd'
import * as hostModule from '../../../src/host'
import * as arch from '../../../src/architecture'
import * as os from '../../../src/operating_systems/kind'
import {Accelerator} from '../../../src/vm'
import HostQemu from '../../../src/host_qemu'
import * as hypervisor from '../../../src/hypervisor'
import * as qemu from '../../../src/qemu_vm'
import * as xhyve from '../../../src/xhyve_vm'

describe('FreeBSD OperatingSystem', () => {
class MockHost extends hostModule.Host {
get workDirectory(): string {
return '/home/runner/work'
}

get vmModule(): typeof xhyve | typeof qemu {
return qemu
}

override get qemu(): HostQemu {
return new HostQemu.LinuxHostQemu()
}

override get hypervisor(): hypervisor.Hypervisor {
return new hypervisor.Qemu()
}

override get efiHypervisor(): hypervisor.Hypervisor {
return new hypervisor.QemuEfi()
}

override get defaultMemory(): string {
return '6G'
}
}

let host = new MockHost()
let osKind = os.Kind.for('freebsd')
let architecture = arch.Architecture.for(arch.Kind.x86_64, host, osKind)
let freebsd = new FreeBsd(architecture, '0.0.0')
let hypervisorDirectory = 'hypervisor/directory'
let resourcesDirectory = 'resources/directory'
let firmwareDirectory = 'firmware/directory'

let config = {
memory: '4G',
cpuCount: 0,
diskImage: '',
resourcesDiskImage: '',
userboot: ''
}

describe('createVirtualMachine', () => {
let vmModule = jasmine.createSpy('vmModule')

beforeEach(() => {
spyOn(hostModule, 'getHost').and.returnValue(host)
spyOn(host.vmModule, 'resolve').and.returnValue(vmModule)
})

it('creates a virtual machine with the correct configuration', () => {
freebsd.createVirtualMachine(
hypervisorDirectory,
resourcesDirectory,
firmwareDirectory,
config
)

expect(vmModule).toHaveBeenCalledOnceWith(
hypervisorDirectory,
resourcesDirectory,
architecture,
{
...config,
ssHostPort: 2847,
cpu: 'qemu64',
accelerator: Accelerator.tcg,
machineType: 'pc',
uuid: '864ED7F0-7876-4AA7-8511-816FABCFA87F',
firmware: `${firmwareDirectory}/share/qemu/bios-256k.bin`
}
)
})
})
})
36 changes: 36 additions & 0 deletions spec/operating_systems/freebsd/qemu_vm.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {QemuVm} from '../../../src/operating_systems/freebsd/qemu_vm'
import * as arch from '../../../src/architecture'
import {host} from '../../../src/host'
import * as os from '../../../src/operating_systems/kind'
import {Accelerator} from '../../../src/vm'
import '../../../src/operating_systems/freebsd/freebsd'

describe('FreeBSD QemuVm', () => {
let memory = '5G'

let osKind = os.Kind.for('freebsd')
let architecture = arch.Architecture.for(arch.Kind.x86_64, host, osKind)
let config = {
memory: memory,
cpuCount: 0,
diskImage: '',
ssHostPort: 0,
cpu: '',
accelerator: Accelerator.tcg,
machineType: '',
uuid: '',
resourcesDiskImage: '',
userboot: '',
firmware: ''
}
let vm = new QemuVm('', '', architecture, config)

let getFlagValue = (flag: string) => vm.command[vm.command.indexOf(flag) + 1]
let actualMemory = () => getFlagValue('-m')

describe('command', () => {
it('constucts a command with the correct memory configuration', () => {
expect(actualMemory()).toEqual(memory)
})
})
})
10 changes: 7 additions & 3 deletions src/action/action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ export class Action {
hypervisorDirectory,
firmwareDirectory,
resourcesDirectory,
diskImagePath
diskImagePath,
{
memory: this.input.memory
}
)

const excludes = [
Expand Down Expand Up @@ -148,7 +151,8 @@ export class Action {
hypervisorDirectory: string,
firmwareDirectory: string,
resourcesDirectory: string,
diskImagePath: string
diskImagePath: string,
config: os.ExternalVmConfiguration
): Promise<vmModule.Vm> {
await this.operatingSystem.prepareDisk(
diskImagePath,
Expand All @@ -161,7 +165,7 @@ export class Action {
resourcesDirectory,
firmwareDirectory,
{
memory: '4G',
...config,
cpuCount: 2,
diskImage: path.join(resourcesDirectory, this.targetDiskName),

Expand Down
13 changes: 13 additions & 0 deletions src/action/input.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import * as core from '@actions/core'
import * as architecture from '../architecture'
import {Shell, toShell} from './shell'
import * as os from '../operating_systems/kind'
import {host} from '../host'

export class Input {
private run_?: string
Expand All @@ -10,6 +11,7 @@ export class Input {
private shell_?: Shell
private environmentVariables_?: string
private architecture_?: architecture.Kind
private memory_?: string

get version(): string {
if (this.version_ !== undefined) return this.version_
Expand Down Expand Up @@ -61,4 +63,15 @@ export class Input {

return (this.architecture_ = kind)
}

get memory(): string {
if (this.memory_ !== undefined) return this.memory_

const memory = core.getInput('memory')
core.debug(`memory input: '${memory}'`)
if (memory === undefined || memory === '')
return (this.memory_ = host.defaultMemory)

return (this.memory_ = memory)
}
}
12 changes: 12 additions & 0 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export abstract class Host {
abstract get qemu(): HostQemu
abstract get hypervisor(): hypervisor.Hypervisor
abstract get efiHypervisor(): hypervisor.Hypervisor
abstract get defaultMemory(): string

resolve<T>(implementation: Record<string, T>): T {
return getImplementation(this, implementation)
Expand Down Expand Up @@ -53,6 +54,10 @@ class MacOs extends Host {
override get efiHypervisor(): hypervisor.Hypervisor {
return this.hypervisor
}

override get defaultMemory(): string {
return '13G'
}
}

class Linux extends Host {
Expand All @@ -75,6 +80,13 @@ class Linux extends Host {
override get efiHypervisor(): hypervisor.Hypervisor {
return new hypervisor.QemuEfi()
}

override get defaultMemory(): string {
return '6G'
}
}

export const host = Host.create()
export function getHost() {
return host
}
6 changes: 4 additions & 2 deletions src/operating_system.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@ import {host} from './host'
import {ResourceUrls} from './operating_systems/resource_urls'
import {LinuxDiskFileCreator, LinuxDiskDeviceCreator} from './resource_disk'

export interface VmConfiguration {
export interface ExternalVmConfiguration {
memory: string
}

export interface VmConfiguration extends ExternalVmConfiguration {
cpuCount: number
diskImage: fs.PathLike

resourcesDiskImage: fs.PathLike
userboot: fs.PathLike
}
Expand Down
4 changes: 2 additions & 2 deletions src/operating_systems/freebsd/freebsd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import * as architecture from '../../architecture'
import * as action from '../../action/action'
import {operatingSystem} from '../factory'
import * as vmModule from '../../vm'
import {host} from '../../host'
import {getHost} from '../../host'
import {QemuVm} from './qemu_vm'
import * as os from '../../operating_system'
import {LinuxDiskFileCreator, LinuxDiskDeviceCreator} from '../../resource_disk'
Expand Down Expand Up @@ -87,7 +87,7 @@ export default class FreeBsd extends os.OperatingSystem {
uuid: this.uuid
}

const cls = host.vmModule.resolve({qemu: QemuVm, xhyve: XhyveVm})
const cls = getHost().vmModule.resolve({qemu: QemuVm, xhyve: XhyveVm})
return new cls(
hypervisorDirectory,
resourcesDirectory,
Expand Down

0 comments on commit 4dba297

Please sign in to comment.