github.com/containerd/nerdctl@v1.7.7/pkg/testutil/testutil_windows.go (about) 1 /* 2 Copyright The containerd Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package testutil 18 19 import ( 20 "os" 21 "strconv" 22 "strings" 23 "sync" 24 25 "github.com/Microsoft/hcsshim" 26 "github.com/containerd/nerdctl/pkg/inspecttypes/dockercompat" 27 "golang.org/x/sys/windows/svc/mgr" 28 ) 29 30 const ( 31 WindowsNano = "gcr.io/k8s-staging-e2e-test-images/busybox:1.29-2" 32 33 // CommonImage. 34 // 35 // More work needs to be done to support windows containers in test framework 36 // for the tests that are run now this image (used in k8s upstream testing) meets the needs 37 // use gcr.io/k8s-staging-e2e-test-images/busybox:1.29-2-windows-amd64-ltsc2022 locally on windows 11 38 // https://github.com/microsoft/Windows-Containers/issues/179 39 CommonImage = WindowsNano 40 41 // NOTE(aznashwan): the upstream e2e Nginx test image is actually based on BusyBox. 42 NginxAlpineImage = "registry.k8s.io/e2e-test-images/nginx:1.14-2" 43 NginxAlpineIndexHTMLSnippet = "<title>Welcome to nginx!</title>" 44 45 // This error string is expected when attempting to connect to a TCP socket 46 // for a service which actively refuses the connection. 47 // (e.g. attempting to connect using http to an https endpoint). 48 // It should be "connection refused" as per the TCP RFC, but it is the 49 // below string constant on Windows. 50 // https://www.rfc-editor.org/rfc/rfc793 51 ExpectedConnectionRefusedError = "No connection could be made because the target machine actively refused it." 52 ) 53 54 var ( 55 hypervContainer bool 56 hypervSupported bool 57 hypervSupportedOnce sync.Once 58 ) 59 60 // HyperVSupported is a test helper to check if hyperv is enabled on 61 // the host. This can be used to skip tests that require virtualization. 62 func HyperVSupported() bool { 63 if s := os.Getenv("NO_HYPERV"); s != "" { 64 if b, err := strconv.ParseBool(s); err == nil && b { 65 return false 66 } 67 } 68 hypervSupportedOnce.Do(func() { 69 // Hyper-V Virtual Machine Management service name 70 const hypervServiceName = "vmms" 71 72 m, err := mgr.Connect() 73 if err != nil { 74 return 75 } 76 defer m.Disconnect() 77 78 s, err := m.OpenService(hypervServiceName) 79 // hyperv service was present 80 if err == nil { 81 hypervSupported = true 82 s.Close() 83 } 84 }) 85 return hypervSupported 86 } 87 88 // HyperVContainer is a test helper to check if the container is a 89 // hyperv type container, lists only running containers 90 func HyperVContainer(inspect dockercompat.Container) (bool, error) { 91 query := hcsshim.ComputeSystemQuery{} 92 containersList, err := hcsshim.GetContainers(query) 93 if err != nil { 94 hypervContainer = false 95 return hypervContainer, err 96 } 97 98 for _, container := range containersList { 99 // have to use IDs, not all containers have name set 100 if strings.Contains(container.ID, inspect.ID) { 101 if container.SystemType == "VirtualMachine" { 102 hypervContainer = true 103 } 104 } 105 } 106 107 return hypervContainer, nil 108 }