From ae5a16f18da3f49806491c9374dd8586f74239e9 Mon Sep 17 00:00:00 2001 From: Hamza El-Saawy Date: Thu, 4 Aug 2022 18:29:39 -0400 Subject: [PATCH] PR: refactor, updated image names in cri tests Signed-off-by: Hamza El-Saawy --- test/cri-containerd/main_test.go | 65 +++++--------------- test/functional/lcow_bench_test.go | 8 +-- test/functional/lcow_container_bench_test.go | 36 ++++------- test/functional/lcow_container_test.go | 19 +++--- test/functional/lcow_test.go | 8 +-- test/functional/main_test.go | 19 +++--- test/functional/uvm_mem_backingtype_test.go | 2 +- test/functional/uvm_memory_test.go | 2 +- test/functional/uvm_plannine_test.go | 4 +- test/functional/uvm_properties_test.go | 2 +- test/functional/uvm_scsi_test.go | 4 +- test/internal/cmd/io.go | 2 - test/internal/constants/images.go | 32 ++++++++++ test/internal/containerd/containerd.go | 2 +- test/internal/layers/layerfolders.go | 8 +++ 15 files changed, 103 insertions(+), 110 deletions(-) diff --git a/test/cri-containerd/main_test.go b/test/cri-containerd/main_test.go index c2823bc6e3..90fed1f476 100644 --- a/test/cri-containerd/main_test.go +++ b/test/cri-containerd/main_test.go @@ -25,6 +25,7 @@ import ( "google.golang.org/grpc" runtime "k8s.io/cri-api/pkg/apis/runtime/v1alpha2" + "github.com/Microsoft/hcsshim/test/internal/constants" _ "github.com/Microsoft/hcsshim/test/internal/manifest" ) @@ -73,12 +74,12 @@ const ( var ( imageWindowsNanoserver = getWindowsNanoserverImage(osversion.Build()) imageWindowsServercore = getWindowsServerCoreImage(osversion.Build()) - imageWindowsNanoserver17763 = getWindowsNanoserverImage(osversion.RS5) - imageWindowsNanoserver18362 = getWindowsNanoserverImage(osversion.V19H1) - imageWindowsNanoserver19041 = getWindowsNanoserverImage(osversion.V20H1) - imageWindowsServercore17763 = getWindowsServerCoreImage(osversion.RS5) - imageWindowsServercore18362 = getWindowsServerCoreImage(osversion.V19H1) - imageWindowsServercore19041 = getWindowsServerCoreImage(osversion.V20H1) + imageWindowsNanoserver17763 = constants.ImageWindowsNanoserver1809 + imageWindowsNanoserver18362 = constants.ImageWindowsNanoserver1903 + imageWindowsNanoserver19041 = constants.ImageWindowsNanoserver2004 + imageWindowsServercore17763 = constants.ImageWindowsServercore1809 + imageWindowsServercore18362 = constants.ImageWindowsServercore1903 + imageWindowsServercore19041 = constants.ImageWindowsServercore2004 ) // Flags @@ -162,55 +163,19 @@ func requireBinary(t *testing.T, binary string) string { } func getWindowsNanoserverImage(build uint16) string { - switch build { - case osversion.RS5: - return "mcr.microsoft.com/windows/nanoserver:1809" - case osversion.V19H1: - return "mcr.microsoft.com/windows/nanoserver:1903" - case osversion.V19H2: - return "mcr.microsoft.com/windows/nanoserver:1909" - case osversion.V20H1: - return "mcr.microsoft.com/windows/nanoserver:2004" - case osversion.V20H2: - return "mcr.microsoft.com/windows/nanoserver:2009" - case osversion.V21H2Server: - return "mcr.microsoft.com/windows/nanoserver:ltsc2022" - default: - // Due to some efforts in improving down-level compatibility for Windows containers (see - // https://techcommunity.microsoft.com/t5/containers/windows-server-2022-and-beyond-for-containers/ba-p/2712487) - // the ltsc2022 image should continue to work on builds ws2022 and onwards. With this in mind, - // if there's no mapping for the host build, just use the Windows Server 2022 image. - if build > osversion.V21H2Server { - return "mcr.microsoft.com/windows/nanoserver:ltsc2022" - } - panic("unsupported build") + tag, err := constants.ImageFromBuild(build) + if err != nil { + panic(err) } + return constants.NanoserverImage(tag) } func getWindowsServerCoreImage(build uint16) string { - switch build { - case osversion.RS5: - return "mcr.microsoft.com/windows/servercore:1809" - case osversion.V19H1: - return "mcr.microsoft.com/windows/servercore:1903" - case osversion.V19H2: - return "mcr.microsoft.com/windows/servercore:1909" - case osversion.V20H1: - return "mcr.microsoft.com/windows/servercore:2004" - case osversion.V20H2: - return "mcr.microsoft.com/windows/servercore:2009" - case osversion.V21H2Server: - return "mcr.microsoft.com/windows/servercore:ltsc2022" - default: - // Due to some efforts in improving down-level compatibility for Windows containers (see - // https://techcommunity.microsoft.com/t5/containers/windows-server-2022-and-beyond-for-containers/ba-p/2712487) - // the ltsc2022 image should continue to work on builds ws2022 and onwards. With this in mind, - // if there's no mapping for the host build, just use the Windows Server 2022 image. - if build > osversion.V21H2Server { - return "mcr.microsoft.com/windows/servercore:ltsc2022" - } - panic("unsupported build") + tag, err := constants.ImageFromBuild(build) + if err != nil { + panic(err) } + return constants.ServercoreImage(tag) } func createGRPCConn(ctx context.Context) (*grpc.ClientConn, error) { diff --git a/test/functional/lcow_bench_test.go b/test/functional/lcow_bench_test.go index 79950d6e8b..263fa2d870 100644 --- a/test/functional/lcow_bench_test.go +++ b/test/functional/lcow_bench_test.go @@ -22,7 +22,7 @@ func BenchmarkLCOW_UVM_Create(b *testing.B) { b.StopTimer() b.ResetTimer() for i := 0; i < b.N; i++ { - opts := defaultLCOWOptions(b, b.Name()) + opts := defaultLCOWOptions(b) b.StartTimer() vm := uvm.CreateLCOW(ctx, b, opts) @@ -43,7 +43,7 @@ func BenchmarkLCOW_UVM_Start(b *testing.B) { b.StopTimer() b.ResetTimer() for i := 0; i < b.N; i++ { - vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b, b.Name())) + vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b)) b.StartTimer() if err := vm.Start(ctx); err != nil { @@ -64,7 +64,7 @@ func BenchmarkLCOW_UVM_Kill(b *testing.B) { b.StopTimer() b.ResetTimer() for i := 0; i < b.N; i++ { - vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b, b.Name())) + vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b)) cleanup := uvm.Start(ctx, b, vm) b.StartTimer() @@ -87,7 +87,7 @@ func BenchmarkLCOW_UVM_Close(b *testing.B) { b.StopTimer() b.ResetTimer() for i := 0; i < b.N; i++ { - vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b, b.Name())) + vm := uvm.CreateLCOW(ctx, b, defaultLCOWOptions(b)) cleanup := uvm.Start(ctx, b, vm) b.StartTimer() diff --git a/test/functional/lcow_container_bench_test.go b/test/functional/lcow_container_bench_test.go index 335c3be161..7c6e3ec8ad 100644 --- a/test/functional/lcow_container_bench_test.go +++ b/test/functional/lcow_container_bench_test.go @@ -5,7 +5,6 @@ package functional import ( "context" - "strings" "testing" ctrdoci "github.com/containerd/containerd/oci" @@ -18,7 +17,6 @@ import ( "github.com/Microsoft/hcsshim/test/internal/cmd" "github.com/Microsoft/hcsshim/test/internal/constants" "github.com/Microsoft/hcsshim/test/internal/container" - "github.com/Microsoft/hcsshim/test/internal/containerd" "github.com/Microsoft/hcsshim/test/internal/layers" "github.com/Microsoft/hcsshim/test/internal/oci" "github.com/Microsoft/hcsshim/test/internal/require" @@ -30,17 +28,15 @@ func BenchmarkLCOW_Container(b *testing.B) { require.Build(b, osversion.RS5) ctx, _, client := newContainerdClient(context.Background(), b) - chainID := containerd.PullImage(ctx, b, client, constants.ImageLinuxAlpineLatest, constants.PlatformLinux) - ls := layers.FromChainID(ctx, b, client, chainID, constants.SnapshotterLinux) + ls := layers.FromImage(ctx, b, client, constants.ImageLinuxAlpineLatest, + constants.PlatformLinux, constants.SnapshotterLinux) // Create a new uvm per benchmark in case any left over state lingers b.Run("Create", func(b *testing.B) { - vmID := strings.ReplaceAll(b.Name(), "/", "") - opts := defaultLCOWOptions(b, vmID) - cache := layers.CacheFile(ctx, b, "") - vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, opts) + vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, defaultLCOWOptions(b)) uvm.SetSecurityPolicy(ctx, b, vm, "") + cache := layers.CacheFile(ctx, b, "") b.StopTimer() b.ResetTimer() @@ -86,11 +82,9 @@ func BenchmarkLCOW_Container(b *testing.B) { }) b.Run("Start", func(b *testing.B) { - vmID := strings.ReplaceAll(b.Name(), "/", "") - opts := defaultLCOWOptions(b, vmID) - cache := layers.CacheFile(ctx, b, "") - vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, opts) + vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, defaultLCOWOptions(b)) uvm.SetSecurityPolicy(ctx, b, vm, "") + cache := layers.CacheFile(ctx, b, "") b.StopTimer() b.ResetTimer() @@ -121,11 +115,9 @@ func BenchmarkLCOW_Container(b *testing.B) { }) b.Run("InitExec", func(b *testing.B) { - vmID := strings.ReplaceAll(b.Name(), "/", "") - opts := defaultLCOWOptions(b, vmID) - cache := layers.CacheFile(ctx, b, "") - vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, opts) + vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, defaultLCOWOptions(b)) uvm.SetSecurityPolicy(ctx, b, vm, "") + cache := layers.CacheFile(ctx, b, "") b.StopTimer() b.ResetTimer() @@ -157,11 +149,9 @@ func BenchmarkLCOW_Container(b *testing.B) { }) b.Run("InitExecKill", func(b *testing.B) { - vmID := strings.ReplaceAll(b.Name(), "/", "") - opts := defaultLCOWOptions(b, vmID) - cache := layers.CacheFile(ctx, b, "") - vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, opts) + vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, defaultLCOWOptions(b)) uvm.SetSecurityPolicy(ctx, b, vm, "") + cache := layers.CacheFile(ctx, b, "") b.StopTimer() b.ResetTimer() @@ -188,11 +178,9 @@ func BenchmarkLCOW_Container(b *testing.B) { }) b.Run("ContainerKill", func(b *testing.B) { - vmID := strings.ReplaceAll(b.Name(), "/", "") - opts := defaultLCOWOptions(b, vmID) - cache := layers.CacheFile(ctx, b, "") - vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, opts) + vm := uvm.CreateAndStartLCOWFromOpts(ctx, b, defaultLCOWOptions(b)) uvm.SetSecurityPolicy(ctx, b, vm, "") + cache := layers.CacheFile(ctx, b, "") b.StopTimer() b.ResetTimer() diff --git a/test/functional/lcow_container_test.go b/test/functional/lcow_container_test.go index 11bace59dd..c4b993b287 100644 --- a/test/functional/lcow_container_test.go +++ b/test/functional/lcow_container_test.go @@ -15,7 +15,6 @@ import ( "github.com/Microsoft/hcsshim/test/internal/cmd" "github.com/Microsoft/hcsshim/test/internal/constants" "github.com/Microsoft/hcsshim/test/internal/container" - "github.com/Microsoft/hcsshim/test/internal/containerd" "github.com/Microsoft/hcsshim/test/internal/layers" "github.com/Microsoft/hcsshim/test/internal/oci" "github.com/Microsoft/hcsshim/test/internal/require" @@ -27,10 +26,10 @@ func TestLCOW_ContainerLifecycle(t *testing.T) { require.Build(t, osversion.RS5) ctx, _, client := newContainerdClient(context.Background(), t) - chainID := containerd.PullImage(ctx, t, client, constants.ImageLinuxAlpineLatest, constants.PlatformLinux) - ls := layers.FromChainID(ctx, t, client, chainID, constants.SnapshotterLinux) + ls := layers.FromImage(ctx, t, client, constants.ImageLinuxAlpineLatest, + constants.PlatformLinux, constants.SnapshotterLinux) - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) vm := uvm.CreateAndStartLCOWFromOpts(ctx, t, opts) uvm.SetSecurityPolicy(ctx, t, vm, "") @@ -84,10 +83,10 @@ func TestLCOW_ContainerIO(t *testing.T) { require.Build(t, osversion.RS5) ctx, _, client := newContainerdClient(context.Background(), t) - chainID := containerd.PullImage(ctx, t, client, constants.ImageLinuxAlpineLatest, constants.PlatformLinux) - ls := layers.FromChainID(ctx, t, client, chainID, constants.SnapshotterLinux) + ls := layers.FromImage(ctx, t, client, constants.ImageLinuxAlpineLatest, + constants.PlatformLinux, constants.SnapshotterLinux) - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) cache := layers.CacheFile(ctx, t, "") vm := uvm.CreateAndStartLCOWFromOpts(ctx, t, opts) uvm.SetSecurityPolicy(ctx, t, vm, "") @@ -129,10 +128,10 @@ func TestLCOW_ContainerExec(t *testing.T) { require.Build(t, osversion.RS5) ctx, _, client := newContainerdClient(context.Background(), t) - chainID := containerd.PullImage(ctx, t, client, constants.ImageLinuxAlpineLatest, constants.PlatformLinux) - ls := layers.FromChainID(ctx, t, client, chainID, constants.SnapshotterLinux) + ls := layers.FromImage(ctx, t, client, constants.ImageLinuxAlpineLatest, + constants.PlatformLinux, constants.SnapshotterLinux) - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) vm := uvm.CreateAndStartLCOWFromOpts(ctx, t, opts) uvm.SetSecurityPolicy(ctx, t, vm, "") diff --git a/test/functional/lcow_test.go b/test/functional/lcow_test.go index af2ee72e98..fadc462e15 100644 --- a/test/functional/lcow_test.go +++ b/test/functional/lcow_test.go @@ -38,7 +38,7 @@ func TestLCOW_UVMCreateWait(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) t.Cleanup(cancel) - vm := testuvm.CreateLCOW(ctx, t, defaultLCOWOptions(t, t.Name())) + vm := testuvm.CreateLCOW(ctx, t, defaultLCOWOptions(t)) testuvm.Close(ctx, t, vm) } @@ -47,7 +47,7 @@ func TestLCOW_UVMCreateWait(t *testing.T) { func TestLCOW_UVMNoSCSINoVPMemInitrd(t *testing.T) { requireFeatures(t, featureLCOW) - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) opts.SCSIControllerCount = 0 opts.VPMemDeviceCount = 0 opts.PreferredRootFSType = uvm.PreferredRootFSTypeInitRd @@ -62,7 +62,7 @@ func TestLCOW_UVMNoSCSINoVPMemInitrd(t *testing.T) { func TestLCOW_UVMNoSCSISingleVPMemVHD(t *testing.T) { requireFeatures(t, featureLCOW) - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) opts.SCSIControllerCount = 0 opts.VPMemDeviceCount = 1 opts.PreferredRootFSType = uvm.PreferredRootFSTypeVHD @@ -139,7 +139,7 @@ func testLCOWTimeUVMStart(t *testing.T, kernelDirect bool, rfsType uvm.Preferred requireFeatures(t, featureLCOW) for i := 0; i < 3; i++ { - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) opts.KernelDirect = kernelDirect opts.VPMemDeviceCount = 32 opts.PreferredRootFSType = rfsType diff --git a/test/functional/main_test.go b/test/functional/main_test.go index 49261758af..b3ddf9af92 100644 --- a/test/functional/main_test.go +++ b/test/functional/main_test.go @@ -13,6 +13,7 @@ import ( "log" "os" "os/exec" + "regexp" "strconv" "testing" "time" @@ -97,10 +98,6 @@ func init() { pauseDurationOnCreateContainerFailure, "The number of minutes to wait after a container creation failure to try again "+ "[%HCSSHIM_FUNCTIONAL_TESTS_PAUSE_ON_CREATECONTAINER_FAIL_IN_MINUTES%]") - - // Try to stop any pre-existing compute processes - // cmd := exec.Command("powershell", `get-computeprocess | stop-computeprocess -force`) - // _ = cmd.Run() } func TestMain(m *testing.M) { @@ -158,15 +155,21 @@ func newContainerdClient(ctx context.Context, t testing.TB) (context.Context, co return getContainerdOptions().NewClient(ctx, t) } -func defaultLCOWOptions(_ testing.TB, name string) *uvm.OptionsLCOW { - opts := uvm.NewDefaultOptionsLCOW(name, "") +func defaultLCOWOptions(t testing.TB) *uvm.OptionsLCOW { + opts := uvm.NewDefaultOptionsLCOW(cleanName(t.Name()), "") opts.BootFilesPath = *flagLinuxBootFilesPath return opts } -func defaultWCOWOptions(_ testing.TB, name string) *uvm.OptionsWCOW { - opts := uvm.NewDefaultOptionsWCOW(name, "") +func defaultWCOWOptions(t testing.TB) *uvm.OptionsWCOW { + opts := uvm.NewDefaultOptionsWCOW(cleanName(t.Name()), "") return opts } + +var _nameRegex = regexp.MustCompile(`[\\\/\s]`) + +func cleanName(n string) string { + return _nameRegex.ReplaceAllString(n, "") +} diff --git a/test/functional/uvm_mem_backingtype_test.go b/test/functional/uvm_mem_backingtype_test.go index b11be95138..b2c83a8cc5 100644 --- a/test/functional/uvm_mem_backingtype_test.go +++ b/test/functional/uvm_mem_backingtype_test.go @@ -47,7 +47,7 @@ func runMemTests(t *testing.T, os string) { wopts.EnableDeferredCommit = bt.enableDeferredCommit runMemStartWCOWTest(t, wopts) } else { - lopts := defaultLCOWOptions(t, t.Name()) + lopts := defaultLCOWOptions(t) lopts.MemorySizeInMB = 512 lopts.AllowOvercommit = bt.allowOvercommit lopts.EnableDeferredCommit = bt.enableDeferredCommit diff --git a/test/functional/uvm_memory_test.go b/test/functional/uvm_memory_test.go index f67233ba3e..748ddde8f6 100644 --- a/test/functional/uvm_memory_test.go +++ b/test/functional/uvm_memory_test.go @@ -24,7 +24,7 @@ func TestUVMMemoryUpdateLCOW(t *testing.T) { ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second) defer cancel() - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) opts.MemorySizeInMB = 1024 * 2 u := tuvm.CreateAndStartLCOWFromOpts(ctx, t, opts) defer u.Close() diff --git a/test/functional/uvm_plannine_test.go b/test/functional/uvm_plannine_test.go index 5b12caa911..ae3aa5c392 100644 --- a/test/functional/uvm_plannine_test.go +++ b/test/functional/uvm_plannine_test.go @@ -29,7 +29,7 @@ func TestPlan9(t *testing.T) { requireFeatures(t, featureLCOW, featurePlan9) ctx := context.Background() - vm := testuvm.CreateAndStartLCOWFromOpts(ctx, t, defaultLCOWOptions(t, t.Name())) + vm := testuvm.CreateAndStartLCOWFromOpts(ctx, t, defaultLCOWOptions(t)) defer vm.Close() testuvm.SetSecurityPolicy(ctx, t, vm, "") @@ -59,7 +59,7 @@ func TestPlan9_Writable(t *testing.T) { requireFeatures(t, featureLCOW, featurePlan9) ctx := context.Background() - opts := defaultLCOWOptions(t, t.Name()) + opts := defaultLCOWOptions(t) opts.NoWritableFileShares = true vm := testuvm.CreateAndStartLCOWFromOpts(ctx, t, opts) defer vm.Close() diff --git a/test/functional/uvm_properties_test.go b/test/functional/uvm_properties_test.go index afb6579e93..5bf784ddc5 100644 --- a/test/functional/uvm_properties_test.go +++ b/test/functional/uvm_properties_test.go @@ -19,7 +19,7 @@ func TestPropertiesGuestConnection_LCOW(t *testing.T) { require.Build(t, osversion.RS5) requireFeatures(t, featureLCOW) - uvm := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t, t.Name())) + uvm := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t)) defer uvm.Close() p, gc := uvm.Capabilities() diff --git a/test/functional/uvm_scsi_test.go b/test/functional/uvm_scsi_test.go index 24fc5ff134..38c233219d 100644 --- a/test/functional/uvm_scsi_test.go +++ b/test/functional/uvm_scsi_test.go @@ -33,7 +33,7 @@ func TestSCSIAddRemoveLCOW(t *testing.T) { require.Build(t, osversion.RS5) requireFeatures(t, featureLCOW, featureSCSI) - u := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t, t.Name())) + u := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t)) defer u.Close() testSCSIAddRemoveMultiple(t, u, `/run/gcs/c/0/scsi`, "linux", []string{}) @@ -227,7 +227,7 @@ func TestParallelScsiOps(t *testing.T) { require.Build(t, osversion.RS5) requireFeatures(t, featureLCOW, featureSCSI) - u := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t, t.Name())) + u := tuvm.CreateAndStartLCOWFromOpts(context.Background(), t, defaultLCOWOptions(t)) defer u.Close() // Create a sandbox to use diff --git a/test/internal/cmd/io.go b/test/internal/cmd/io.go index 4d4955638d..9035d920df 100644 --- a/test/internal/cmd/io.go +++ b/test/internal/cmd/io.go @@ -10,8 +10,6 @@ import ( "github.com/Microsoft/hcsshim/internal/cmd" ) -// todo: add buffered input - type BufferedIO struct { in *bytes.Buffer out, err bytes.Buffer diff --git a/test/internal/constants/images.go b/test/internal/constants/images.go index 551b69a8da..18d056c926 100644 --- a/test/internal/constants/images.go +++ b/test/internal/constants/images.go @@ -3,7 +3,10 @@ package constants // not technically constants, but close enough ... import ( + "errors" "fmt" + + "github.com/Microsoft/hcsshim/osversion" ) const ( @@ -14,6 +17,8 @@ const ( ImageLinuxPause31 = "k8s.gcr.io/pause:3.1" ) +var ErrUnsupportedBuild = errors.New("unsupported build") + var ( ImageWindowsNanoserver1709 = NanoserverImage("1709") ImageWindowsNanoserver1803 = NanoserverImage("1803") @@ -52,3 +57,30 @@ func NanoserverImage(tag string) string { func ServercoreImage(tag string) string { return makeImageURL(McrWindowsImageRepo, "servercore", tag) } + +var _buildToTag = map[uint16]string{ + osversion.RS1: "1607", + osversion.RS2: "1703", + osversion.RS3: "1709", + osversion.RS4: "1803", + osversion.RS5: "1809", + osversion.V19H1: "1903", + osversion.V19H2: "1909", + osversion.V20H1: "2004", + osversion.V21H2Server: "ltsc2022", +} + +func ImageFromBuild(build uint16) (string, error) { + if tag, ok := _buildToTag[build]; ok { + return tag, nil + } + + // Due to some efforts in improving down-level compatibility for Windows containers (see + // https://techcommunity.microsoft.com/t5/containers/windows-server-2022-and-beyond-for-containers/ba-p/2712487) + // the ltsc2022 image should continue to work on builds ws2022 and onwards. With this in mind, + // if there's no mapping for the host build, just use the Windows Server 2022 image. + if build > osversion.V21H2Server { + return "ltsc2022", nil + } + return "", ErrUnsupportedBuild +} diff --git a/test/internal/containerd/containerd.go b/test/internal/containerd/containerd.go index a104e93df7..f29b0e2918 100644 --- a/test/internal/containerd/containerd.go +++ b/test/internal/containerd/containerd.go @@ -163,7 +163,7 @@ func CreateViewSnapshot(ctx context.Context, t testing.TB, client *containerd.Cl // copied from https://github.com/containerd/containerd/blob/main/cmd/ctr/commands/images/pull.go // PullImage pulls the image for the specified platform and returns the chain ID -func PullImage(ctx context.Context, t testing.TB, client *containerd.Client, ref string, plat string) string { +func PullImage(ctx context.Context, t testing.TB, client *containerd.Client, ref, plat string) string { if chainID, ok := images.Load(ref); ok { return chainID.(string) } diff --git a/test/internal/layers/layerfolders.go b/test/internal/layers/layerfolders.go index c117710922..347e22024b 100644 --- a/test/internal/layers/layerfolders.go +++ b/test/internal/layers/layerfolders.go @@ -24,6 +24,13 @@ func init() { imageLayers = make(map[string][]string) } +// FromImage returns thee layer paths of a given image, pulling it if necessary +func FromImage(ctx context.Context, t testing.TB, client *containerd.Client, ref, platform, snapshotter string) []string { + chainID := testctrd.PullImage(ctx, t, client, ref, platform) + return FromChainID(ctx, t, client, chainID, snapshotter) +} + +// FromChainID returns thee layer paths of a given image chain ID func FromChainID(ctx context.Context, t testing.TB, client *containerd.Client, chainID, snapshotter string) []string { ms := testctrd.CreateViewSnapshot(ctx, t, client, snapshotter, chainID, chainID+"view") if len(ms) != 1 { @@ -33,6 +40,7 @@ func FromChainID(ctx context.Context, t testing.TB, client *containerd.Client, c return FromMount(ctx, t, ms[0]) } +// FromMount returns the layer paths of a given mount func FromMount(_ context.Context, t testing.TB, m mount.Mount) (layers []string) { for _, option := range m.Options { if strings.HasPrefix(option, mount.ParentLayerPathsFlag) {