@@ -17,6 +17,7 @@ limitations under the License.
17
17
package server
18
18
19
19
import (
20
+ "os"
20
21
"path/filepath"
21
22
"reflect"
22
23
"strings"
@@ -29,6 +30,7 @@ import (
29
30
imagespec "github.com/opencontainers/image-spec/specs-go/v1"
30
31
runtimespec "github.com/opencontainers/runtime-spec/specs-go"
31
32
"github.com/opencontainers/runtime-tools/generate"
33
+ "github.com/pkg/errors"
32
34
"github.com/stretchr/testify/assert"
33
35
"github.com/stretchr/testify/require"
34
36
runtime "k8s.io/kubernetes/pkg/kubelet/apis/cri/runtime/v1alpha2"
@@ -542,6 +544,7 @@ func TestGenerateVolumeMounts(t *testing.T) {
542
544
func TestGenerateContainerMounts (t * testing.T ) {
543
545
const testSandboxID = "test-id"
544
546
for desc , test := range map [string ]struct {
547
+ statFn func (string ) (os.FileInfo , error )
545
548
criMounts []* runtime.Mount
546
549
securityContext * runtime.LinuxContainerSecurityContext
547
550
expectedMounts []* runtime.Mount
@@ -647,6 +650,30 @@ func TestGenerateContainerMounts(t *testing.T) {
647
650
securityContext : & runtime.LinuxContainerSecurityContext {},
648
651
expectedMounts : nil ,
649
652
},
653
+ "should skip hostname mount if the old sandbox doesn't have hostname file" : {
654
+ statFn : func (path string ) (os.FileInfo , error ) {
655
+ assert .Equal (t , filepath .Join (testRootDir , sandboxesDir , testSandboxID , "hostname" ), path )
656
+ return nil , errors .New ("random error" )
657
+ },
658
+ securityContext : & runtime.LinuxContainerSecurityContext {},
659
+ expectedMounts : []* runtime.Mount {
660
+ {
661
+ ContainerPath : "/etc/hosts" ,
662
+ HostPath : filepath .Join (testRootDir , sandboxesDir , testSandboxID , "hosts" ),
663
+ Readonly : false ,
664
+ },
665
+ {
666
+ ContainerPath : resolvConfPath ,
667
+ HostPath : filepath .Join (testRootDir , sandboxesDir , testSandboxID , "resolv.conf" ),
668
+ Readonly : false ,
669
+ },
670
+ {
671
+ ContainerPath : "/dev/shm" ,
672
+ HostPath : filepath .Join (testStateDir , sandboxesDir , testSandboxID , "shm" ),
673
+ Readonly : false ,
674
+ },
675
+ },
676
+ },
650
677
} {
651
678
config := & runtime.ContainerConfig {
652
679
Metadata : & runtime.ContainerMetadata {
@@ -659,6 +686,7 @@ func TestGenerateContainerMounts(t *testing.T) {
659
686
},
660
687
}
661
688
c := newTestCRIService ()
689
+ c .os .(* ostesting.FakeOS ).StatFn = test .statFn
662
690
mounts := c .generateContainerMounts (testSandboxID , config )
663
691
assert .Equal (t , test .expectedMounts , mounts , desc )
664
692
}
0 commit comments