@@ -17,6 +17,7 @@ limitations under the License.
1717package server
1818
1919import (
20+ "os"
2021 "path/filepath"
2122 "reflect"
2223 "strings"
@@ -29,6 +30,7 @@ import (
2930 imagespec "github.com/opencontainers/image-spec/specs-go/v1"
3031 runtimespec "github.com/opencontainers/runtime-spec/specs-go"
3132 "github.com/opencontainers/runtime-tools/generate"
33+ "github.com/pkg/errors"
3234 "github.com/stretchr/testify/assert"
3335 "github.com/stretchr/testify/require"
3436 runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
@@ -541,6 +543,7 @@ func TestGenerateVolumeMounts(t *testing.T) {
541543func TestGenerateContainerMounts (t * testing.T ) {
542544 const testSandboxID = "test-id"
543545 for desc , test := range map [string ]struct {
546+ statFn func (string ) (os.FileInfo , error )
544547 criMounts []* runtime.Mount
545548 securityContext * runtime.LinuxContainerSecurityContext
546549 expectedMounts []* runtime.Mount
@@ -646,6 +649,30 @@ func TestGenerateContainerMounts(t *testing.T) {
646649 securityContext : & runtime.LinuxContainerSecurityContext {},
647650 expectedMounts : nil ,
648651 },
652+ "should skip hostname mount if the old sandbox doesn't have hostname file" : {
653+ statFn : func (path string ) (os.FileInfo , error ) {
654+ assert .Equal (t , filepath .Join (testRootDir , sandboxesDir , testSandboxID , "hostname" ), path )
655+ return nil , errors .New ("random error" )
656+ },
657+ securityContext : & runtime.LinuxContainerSecurityContext {},
658+ expectedMounts : []* runtime.Mount {
659+ {
660+ ContainerPath : "/etc/hosts" ,
661+ HostPath : filepath .Join (testRootDir , sandboxesDir , testSandboxID , "hosts" ),
662+ Readonly : false ,
663+ },
664+ {
665+ ContainerPath : resolvConfPath ,
666+ HostPath : filepath .Join (testRootDir , sandboxesDir , testSandboxID , "resolv.conf" ),
667+ Readonly : false ,
668+ },
669+ {
670+ ContainerPath : "/dev/shm" ,
671+ HostPath : filepath .Join (testStateDir , sandboxesDir , testSandboxID , "shm" ),
672+ Readonly : false ,
673+ },
674+ },
675+ },
649676 } {
650677 config := & runtime.ContainerConfig {
651678 Metadata : & runtime.ContainerMetadata {
@@ -658,6 +685,7 @@ func TestGenerateContainerMounts(t *testing.T) {
658685 },
659686 }
660687 c := newTestCRIService ()
688+ c .os .(* ostesting.FakeOS ).StatFn = test .statFn
661689 mounts := c .generateContainerMounts (testSandboxID , config )
662690 assert .Equal (t , test .expectedMounts , mounts , desc )
663691 }
0 commit comments