Skip to content

Commit

Permalink
Linux GCS tests and benchmarks (#1352)
Browse files Browse the repository at this point in the history
* Added GCS tests and benchmarks

Added testing suite that can built and run directly on the Linux uVM
by sharing or adding it to the rootfs.
It primarily focuses on container (standalone and CRI) management.

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

* PR: rebase, comments, bugs, cleanup, security policy, linting

Fixed bug with calling `*hcsv2.Host.GetContainer` instead of
`*hcsv2.Host.GetCreatedContainer`.

Removed left over comments, added clarifying comments to
`assertNumberContainers` and `listContaienrStates` interactions.

Reordered namespace and rootfs cleanup.

Removed underscore from consts
Removed unneeded constants
Added flag to test-lcow-uvm script to change boot type from vhd to
initrd.

Update security policy code to use enforcer.
Updated script for changes to `uvmboot` and to use default executable
name (`gcs.test`), as produced by `go test -c`.

Linting issues:
 - `switch` to `if`
 - unused `getContainer()`
 - unused receivers

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>

Signed-off-by: Hamza El-Saawy <hamzaelsaawy@microsoft.com>
  • Loading branch information
helsaawy committed Sep 16, 2022
1 parent 0e5cdff commit ed32773
Show file tree
Hide file tree
Showing 13 changed files with 1,877 additions and 3 deletions.
148 changes: 148 additions & 0 deletions scripts/Test-LCOW-UVM.ps1
@@ -0,0 +1,148 @@
#ex: .\scripts\Test-LCOW-UVM.ps1 -vb -Action Bench -BootFilesPath C:\ContainerPlat\LinuxBootFiles\ -MountGCSTest -Count 2 -Benchtime '3s'
# benchstat via `go install golang.org/x/perf/cmd/benchstat@latest`

[CmdletBinding()]
param (
[ValidateSet('Test', 'Bench', 'List', 'Shell')]
[alias('a')]
[string]
$Action = 'Bench',

[string]
$Note = '',

# test parameters
[int]
$Count = 1,

[string]
$BenchTime = '5s',

[string]
$Timeout = '10m',

[alias('tv')]
[switch]
$TestVerbose,

[string]
$Run = '',

[string]
$CodePath = '.',

[string]
$OutDirectory = '.\test\results',

# uvm parameters

[string]
$BootFilesPath = 'C:\ContainerPlat\LinuxBootFiles',

[ValidateSet('vhd', 'initrd')]
[string]
$BootFSType = 'vhd',

[switch]
$DisableTimeSync,

# gcs test/container options

[string]
$ContainerRootFSMount = '/run/rootfs',

[string]
$ContainerRootFSPath = (Join-Path $BootFilesPath 'rootfs.vhd'),

[string]
$GCSTestMount = '/run/bin',

[string]
$GCSTestPath = '.\bin\test\gcs.test',

[switch]
$MountGCSTest,

[string]
$Feature = ''
)

Import-Module ( Join-Path $PSScriptRoot Testing.psm1 ) -Force

$CodePath = Resolve-Path $CodePath
$OutDirectory = Resolve-Path $OutDirectory
$BootFilesPath = Resolve-Path $BootFilesPath
$ContainerRootFSPath = Resolve-Path $ContainerRootFSPath
$GCSTestPath = Resolve-Path $GCSTestPath

$shell = ( $Action -eq 'Shell' )

if ( $shell ) {
$cmd = 'ash'
} else {
$date = Get-Date
$waitfiles = "$ContainerRootFSMount"
$gcspath = 'gcs.test'
if ( $MountGCSTest ) {
$waitfiles += ",$GCSTestMount"
$gcspath = "$GCSTestMount/gcs.test"
}

$pre = "wait-paths -p $waitfiles -t 5 ; " + `
'echo nproc: `$(nproc) ; ' + `
'echo kernel: `$(uname -a) ; ' + `
'echo gcs.commit: `$(cat /info/gcs.commit 2>/dev/null) ; ' + `
'echo gcs.branch: `$(cat /info/gcs.branch 2>/dev/null) ; ' + `
'echo tar.date: `$(cat /info/tar.date 2>/dev/null) ; ' + `
'echo image.name: `$(cat /info/image.name 2>/dev/null) ; ' + `
'echo build.date: `$(cat /info/build.date 2>/dev/null) ; '

$testcmd, $out = New-TestCommand `
-Action $Action `
-Path $gcspath `
-Name gcstest `
-OutDirectory $OutDirectory `
-Date $date `
-Note $Note `
-TestVerbose:$TestVerbose `
-Count $Count `
-BenchTime $BenchTime `
-Timeout $Timeout `
-Run $Run `
-Feature $Feature `
-Verbose:$Verbose

$testcmd += " `'-rootfs-path=$ContainerRootFSMount`' "
$cmd = $pre + $testcmd
}

$boot = '.\bin\tool\uvmboot.exe -gcs lcow ' + `
'-fwd-stdout -fwd-stderr -output-handling stdout ' + `
"-boot-files-path $BootFilesPath " + `
"-root-fs-type $BootFSType " + `
'-kernel-file vmlinux ' + `
"-mount-scsi `"$ContainerRootFSPath,$ContainerRootFSMount`" "

if ( $MountGCSTest ) {
$boot += "-share `"$GCSTestPath,$GCSTestMount`" "
}

if ( $DisableTimeSync ) {
$boot += ' -disable-time-sync '
}

if ( $shell ) {
$boot += ' -t '
}

$boot += " -exec `"$cmd`" "

Invoke-TestCommand `
-TestCmd $boot `
-TestCmdPreamble $testcmd `
-OutputFile (&{ if ( $Action -ne 'Shell' ) { $out } }) `
-OutputCmd (&{ if ( $Action -eq 'Bench' ) { 'benchstat' } }) `
-Preamble `
-Date $Date `
-Note $Note `
-Verbose:$Verbose
226 changes: 226 additions & 0 deletions test/gcs/container_bench_test.go
@@ -0,0 +1,226 @@
//go:build linux

package gcs

import (
"context"
"testing"

"github.com/Microsoft/hcsshim/internal/guest/prot"
"github.com/Microsoft/hcsshim/internal/guest/runtime/hcsv2"
"github.com/Microsoft/hcsshim/internal/guest/stdio"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/oci"
cri_util "github.com/containerd/containerd/pkg/cri/util"

testoci "github.com/Microsoft/hcsshim/test/internal/oci"
)

func BenchmarkContainerCreate(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := namespaces.WithNamespace(context.Background(), testoci.DefaultNamespace)
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id := b.Name() + cri_util.GenerateID()
scratch, rootfs := mountRootfs(ctx, b, host, id)

s := testoci.CreateLinuxSpec(ctx, b, id,
testoci.DefaultLinuxSpecOpts(id,
oci.WithRootFSPath(rootfs),
oci.WithProcessArgs("/bin/sh", "-c", tailNull),
)...,
)
r := &prot.VMHostedContainerSettingsV2{
OCIBundlePath: scratch,
OCISpecification: s,
}

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
b.StopTimer()

// create launches background go-routines
// so kill container to end those and avoid future perf hits
killContainer(ctx, b, c)
deleteContainer(ctx, b, c)
removeContainer(ctx, b, host, id)
unmountRootfs(ctx, b, scratch)
}
}

func BenchmarkContainerStart(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)

c := createContainer(ctx, b, host, id, r)

b.StartTimer()
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
b.StopTimer()

killContainer(ctx, b, c)
waitContainer(ctx, b, c, p, true)
cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerKill(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})

b.StartTimer()
killContainer(ctx, b, c)
_, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtForcedExit:
default:
b.Fatalf("container exit was %s", n)
}

cleanupContainer(ctx, b, host, c)
cleanup()
}
}

// benchmark container create through wait until exit.
func BenchmarkContainerCompleteExit(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host, oci.WithProcessArgs("/bin/sh", "-c", "true"))

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
e, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtGracefulExit, prot.NtUnexpectedExit:
default:
b.Fatalf("container exit was %s", n)
}

if e != 0 {
b.Fatalf("container exit code was %d", e)
}

killContainer(ctx, b, c)
c.Wait()
cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerCompleteKill(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := context.Background()
host, _ := getTestState(ctx, b)

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
id, r, cleanup := standaloneContainerRequest(ctx, b, host)

b.StartTimer()
c := createContainer(ctx, b, host, id, r)
p := startContainer(ctx, b, c, stdio.ConnectionSettings{})
killContainer(ctx, b, c)
_, n := waitContainerRaw(c, p)
b.StopTimer()

switch n {
case prot.NtForcedExit:
default:
b.Fatalf("container exit was %s", n)
}

cleanupContainer(ctx, b, host, c)
cleanup()
}
}

func BenchmarkContainerExec(b *testing.B) {
requireFeatures(b, featureStandalone)
ctx := namespaces.WithNamespace(context.Background(), testoci.DefaultNamespace)
host, _ := getTestState(ctx, b)

id := b.Name()
c := createStandaloneContainer(ctx, b, host, id)
ip := startContainer(ctx, b, c, stdio.ConnectionSettings{})

b.StopTimer()
b.ResetTimer()
for i := 0; i < b.N; i++ {
ps := testoci.CreateLinuxSpec(ctx, b, id,
oci.WithDefaultPathEnv,
oci.WithProcessArgs("/bin/sh", "-c", "true"),
).Process

b.StartTimer()
p := execProcess(ctx, b, c, ps, stdio.ConnectionSettings{})
exch, dch := p.Wait()
if e := <-exch; e != 0 {
b.Errorf("process exited with error code %d", e)
}
b.StopTimer()

dch <- true
close(dch)
}

killContainer(ctx, b, c)
waitContainer(ctx, b, c, ip, true)
cleanupContainer(ctx, b, host, c)
}

func standaloneContainerRequest(
ctx context.Context,
t testing.TB,
host *hcsv2.Host,
extra ...oci.SpecOpts,
) (string, *prot.VMHostedContainerSettingsV2, func()) {
ctx = namespaces.WithNamespace(ctx, testoci.DefaultNamespace)
id := t.Name() + cri_util.GenerateID()
scratch, rootfs := mountRootfs(ctx, t, host, id)

opts := testoci.DefaultLinuxSpecOpts(id,
oci.WithRootFSPath(rootfs),
oci.WithProcessArgs("/bin/sh", "-c", tailNull),
)
opts = append(opts, extra...)
s := testoci.CreateLinuxSpec(ctx, t, id, opts...)
r := &prot.VMHostedContainerSettingsV2{
OCIBundlePath: scratch,
OCISpecification: s,
}
f := func() {
unmountRootfs(ctx, t, scratch)
}

return id, r, f
}

0 comments on commit ed32773

Please sign in to comment.