@@ -23,17 +23,23 @@ import (
2323 "fmt"
2424 "os"
2525 "testing"
26+ "time"
2627
2728 "github.com/containerd/containerd/integration/images"
2829 "github.com/stretchr/testify/assert"
2930 "github.com/stretchr/testify/require"
31+ runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
32+ v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
3033)
3134
35+ type hpcAction func (* testing.T , string , * v1.ContainerConfig )
36+
3237var (
33- defaultCommand = WithCommand ("Powershell" , "/c" , "$env:CONTAINER_SANDBOX_MOUNT_POINT/pause.exe" )
34- localServiceUsername = WithWindowsUsername ("NT AUTHORITY\\ Local service" )
35- localSystemUsername = WithWindowsUsername ("NT AUTHORITY\\ System" )
36- hpcContainerOpt = WithWindowsHostProcessContainer ()
38+ defaultCommand = WithCommand ("Powershell" , "/c" , "$env:CONTAINER_SANDBOX_MOUNT_POINT/pause.exe" )
39+ localServiceUsername = WithWindowsUsername ("NT AUTHORITY\\ Local service" )
40+ localSystemUsername = WithWindowsUsername ("NT AUTHORITY\\ System" )
41+ hpcContainerOpt = WithWindowsHostProcessContainer ()
42+ defaultAction hpcAction = func (t * testing.T , containerID string , containerConfig * v1.ContainerConfig ) {}
3743)
3844
3945// Tests to verify the Windows HostProcess
@@ -42,32 +48,55 @@ func TestWindowsHostProcess(t *testing.T) {
4248 EnsureImageExists (t , pauseImage )
4349
4450 t .Run ("run as Local Service" , func (t * testing.T ) {
45- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , defaultCommand )
51+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , defaultCommand )
4652 })
4753 t .Run ("run as Local System" , func (t * testing.T ) {
48- runHostProcess (t , false , pauseImage , hpcContainerOpt , localSystemUsername , defaultCommand )
54+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localSystemUsername , defaultCommand )
4955 })
5056 t .Run ("run as unacceptable user" , func (t * testing.T ) {
51- runHostProcess (t , true , pauseImage , hpcContainerOpt , WithWindowsUsername ("Guest" ), defaultCommand )
57+ runHostProcess (t , true , pauseImage , defaultAction , hpcContainerOpt , WithWindowsUsername ("Guest" ), defaultCommand )
5258 })
5359 t .Run ("run command on host" , func (t * testing.T ) {
5460 cmd := WithCommand ("Powershell" , "/c" , "Get-Command containerd.exe" )
55- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , cmd )
61+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , cmd )
5662 })
5763 t .Run ("run withHostNetwork" , func (t * testing.T ) {
5864 hostname , err := os .Hostname ()
5965 require .NoError (t , err )
6066 cmd := WithCommand ("Powershell" , "/c" , fmt .Sprintf ("if ($env:COMPUTERNAME -ne %s) { exit -1 }" , hostname ))
61- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , cmd )
67+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , cmd )
6268 })
6369 t .Run ("run with a different os.version image" , func (t * testing.T ) {
6470 image := "docker.io/e2eteam/busybox:1.29-windows-amd64-1909"
6571 EnsureImageExists (t , image )
66- runHostProcess (t , false , image , hpcContainerOpt , localServiceUsername , defaultCommand )
72+ runHostProcess (t , false , image , defaultAction , hpcContainerOpt , localServiceUsername , defaultCommand )
73+ })
74+ t .Run ("run and check stats" , func (t * testing.T ) {
75+ var stats = func (t * testing.T , containerID string , containerConfig * v1.ContainerConfig ) {
76+ t .Logf ("Fetch stats for container" )
77+ var (
78+ s * runtime.ContainerStats
79+ err error
80+ )
81+ require .NoError (t , Eventually (func () (bool , error ) {
82+ s , err = runtimeService .ContainerStats (containerID )
83+ if err != nil {
84+ return false , err
85+ }
86+ if s .GetWritableLayer ().GetUsedBytes ().GetValue () != 0 {
87+ return true , nil
88+ }
89+ return false , nil
90+ }, time .Second , 30 * time .Second ))
91+
92+ t .Logf ("Verify stats received for container %q" , containerConfig )
93+ testStats (t , s , containerConfig )
94+ }
95+ runHostProcess (t , false , pauseImage , stats , hpcContainerOpt , localServiceUsername , defaultCommand , WithTestAnnotations (), WithTestLabels ())
6796 })
6897}
6998
70- func runHostProcess (t * testing.T , expectErr bool , image string , opts ... ContainerOpts ) {
99+ func runHostProcess (t * testing.T , expectErr bool , image string , action hpcAction , opts ... ContainerOpts ) {
71100 t .Logf ("Create a pod config and run sandbox container" )
72101 sb , sbConfig := PodSandboxConfigWithCleanup (t , "sandbox1" , "hostprocess" , WithWindowsHostProcessPod )
73102
@@ -92,4 +121,6 @@ func runHostProcess(t *testing.T, expectErr bool, image string, opts ...Containe
92121 defer func () {
93122 assert .NoError (t , runtimeService .StopContainer (cn , 10 ))
94123 }()
124+
125+ action (t , cn , containerConfig )
95126}
0 commit comments