@@ -23,49 +23,78 @@ import (
2323 "fmt"
2424 "os"
2525 "testing"
26+ "time"
2627
2728 "github.com/stretchr/testify/assert"
2829 "github.com/stretchr/testify/require"
30+ runtime "k8s.io/cri-api/pkg/apis/runtime/v1"
31+ v1 "k8s.io/cri-api/pkg/apis/runtime/v1"
2932)
3033
34+ type hpcAction func (* testing.T , string , * v1.ContainerConfig )
35+
3136var (
32- defaultCommand = WithCommand ("Powershell" , "/c" , "$env:CONTAINER_SANDBOX_MOUNT_POINT/pause.exe" )
33- localServiceUsername = WithWindowsUsername ("NT AUTHORITY\\ Local service" )
34- localSystemUsername = WithWindowsUsername ("NT AUTHORITY\\ System" )
35- hpcContainerOpt = WithWindowsHostProcessContainer ()
37+ defaultCommand = WithCommand ("Powershell" , "/c" , "$env:CONTAINER_SANDBOX_MOUNT_POINT/pause.exe" )
38+ localServiceUsername = WithWindowsUsername ("NT AUTHORITY\\ Local service" )
39+ localSystemUsername = WithWindowsUsername ("NT AUTHORITY\\ System" )
40+ hpcContainerOpt = WithWindowsHostProcessContainer ()
41+ defaultAction hpcAction = func (t * testing.T , containerID string , containerConfig * v1.ContainerConfig ) {}
3642)
3743
3844// Tests to verify the Windows HostProcess
3945func TestWindowsHostProcess (t * testing.T ) {
4046 EnsureImageExists (t , pauseImage )
4147
4248 t .Run ("run as Local Service" , func (t * testing.T ) {
43- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , defaultCommand )
49+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , defaultCommand )
4450 })
4551 t .Run ("run as Local System" , func (t * testing.T ) {
46- runHostProcess (t , false , pauseImage , hpcContainerOpt , localSystemUsername , defaultCommand )
52+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localSystemUsername , defaultCommand )
4753 })
4854 t .Run ("run as unacceptable user" , func (t * testing.T ) {
49- runHostProcess (t , true , pauseImage , hpcContainerOpt , WithWindowsUsername ("Guest" ), defaultCommand )
55+ runHostProcess (t , true , pauseImage , defaultAction , hpcContainerOpt , WithWindowsUsername ("Guest" ), defaultCommand )
5056 })
5157 t .Run ("run command on host" , func (t * testing.T ) {
5258 cmd := WithCommand ("Powershell" , "/c" , "Get-Command containerd.exe" )
53- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , cmd )
59+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , cmd )
5460 })
5561 t .Run ("run withHostNetwork" , func (t * testing.T ) {
5662 hostname , err := os .Hostname ()
5763 require .NoError (t , err )
5864 cmd := WithCommand ("Powershell" , "/c" , fmt .Sprintf ("if ($env:COMPUTERNAME -ne %s) { exit -1 }" , hostname ))
59- runHostProcess (t , false , pauseImage , hpcContainerOpt , localServiceUsername , cmd )
65+ runHostProcess (t , false , pauseImage , defaultAction , hpcContainerOpt , localServiceUsername , cmd )
6066 })
6167 t .Run ("run with a different os.version image" , func (t * testing.T ) {
6268 image := "docker.io/e2eteam/busybox:1.29-windows-amd64-1909"
6369 EnsureImageExists (t , image )
64- runHostProcess (t , false , image , hpcContainerOpt , localServiceUsername , defaultCommand )
70+ runHostProcess (t , false , image , defaultAction , hpcContainerOpt , localServiceUsername , defaultCommand )
71+ })
72+ t .Run ("run and check stats" , func (t * testing.T ) {
73+ var stats = func (t * testing.T , containerID string , containerConfig * v1.ContainerConfig ) {
74+ t .Logf ("Fetch stats for container" )
75+ var (
76+ s * runtime.ContainerStats
77+ err error
78+ )
79+ require .NoError (t , Eventually (func () (bool , error ) {
80+ s , err = runtimeService .ContainerStats (containerID )
81+ if err != nil {
82+ return false , err
83+ }
84+ if s .GetWritableLayer ().GetUsedBytes ().GetValue () != 0 {
85+ return true , nil
86+ }
87+ return false , nil
88+ }, time .Second , 30 * time .Second ))
89+
90+ t .Logf ("Verify stats received for container %q" , containerConfig )
91+ testStats (t , s , containerConfig )
92+ }
93+ runHostProcess (t , false , pauseImage , stats , hpcContainerOpt , localServiceUsername , defaultCommand , WithTestAnnotations (), WithTestLabels ())
6594 })
6695}
6796
68- func runHostProcess (t * testing.T , expectErr bool , image string , opts ... ContainerOpts ) {
97+ func runHostProcess (t * testing.T , expectErr bool , image string , action hpcAction , opts ... ContainerOpts ) {
6998 t .Logf ("Create a pod config and run sandbox container" )
7099 sb , sbConfig := PodSandboxConfigWithCleanup (t , "sandbox1" , "hostprocess" , WithWindowsHostProcessPod )
71100
@@ -90,4 +119,6 @@ func runHostProcess(t *testing.T, expectErr bool, image string, opts ...Containe
90119 defer func () {
91120 assert .NoError (t , runtimeService .StopContainer (cn , 10 ))
92121 }()
122+
123+ action (t , cn , containerConfig )
93124}
0 commit comments