@@ -979,3 +979,45 @@ func TestMaskedAndReadonlyPaths(t *testing.T) {
979979 assert .Equal (t , test .expectedReadonly , spec .Linux .ReadonlyPaths )
980980 }
981981}
982+
983+ func TestHostname (t * testing.T ) {
984+ testID := "test-id"
985+ testSandboxID := "sandbox-id"
986+ testPid := uint32 (1234 )
987+ config , sandboxConfig , imageConfig , specCheck := getCreateContainerTestData ()
988+ c := newTestCRIService ()
989+ c .os .(* ostesting.FakeOS ).HostnameFn = func () (string , error ) {
990+ return "real-hostname" , nil
991+ }
992+ for desc , test := range map [string ]struct {
993+ hostname string
994+ networkNs runtime.NamespaceMode
995+ expectedEnv string
996+ }{
997+ "should add HOSTNAME=sandbox.Hostname for pod network namespace" : {
998+ hostname : "test-hostname" ,
999+ networkNs : runtime .NamespaceMode_POD ,
1000+ expectedEnv : "HOSTNAME=test-hostname" ,
1001+ },
1002+ "should add HOSTNAME=sandbox.Hostname for host network namespace" : {
1003+ hostname : "test-hostname" ,
1004+ networkNs : runtime .NamespaceMode_NODE ,
1005+ expectedEnv : "HOSTNAME=test-hostname" ,
1006+ },
1007+ "should add HOSTNAME=os.Hostname for host network namespace if sandbox.Hostname is not set" : {
1008+ hostname : "" ,
1009+ networkNs : runtime .NamespaceMode_NODE ,
1010+ expectedEnv : "HOSTNAME=real-hostname" ,
1011+ },
1012+ } {
1013+ t .Logf ("TestCase %q" , desc )
1014+ sandboxConfig .Hostname = test .hostname
1015+ sandboxConfig .Linux .SecurityContext = & runtime.LinuxSandboxSecurityContext {
1016+ NamespaceOptions : & runtime.NamespaceOption {Network : test .networkNs },
1017+ }
1018+ spec , err := c .generateContainerSpec (testID , testSandboxID , testPid , config , sandboxConfig , imageConfig , nil )
1019+ require .NoError (t , err )
1020+ specCheck (t , testID , testSandboxID , testPid , spec )
1021+ assert .Contains (t , spec .Process .Env , test .expectedEnv )
1022+ }
1023+ }
0 commit comments