@@ -34,6 +34,7 @@ func TestPodHostname(t *testing.T) {
3434 for name , test := range map [string ]struct {
3535 opts []PodSandboxOpts
3636 expectedHostname string
37+ expectErr bool
3738 }{
3839 "regular pod with custom hostname" : {
3940 opts : []PodSandboxOpts {
@@ -47,12 +48,12 @@ func TestPodHostname(t *testing.T) {
4748 },
4849 expectedHostname : hostname ,
4950 },
50- "host network pod with custom hostname" : {
51+ "host network pod with custom hostname should fail " : {
5152 opts : []PodSandboxOpts {
5253 WithHostNetwork ,
5354 WithPodHostname ("test-hostname" ),
5455 },
55- expectedHostname : "test-hostname" ,
56+ expectErr : true ,
5657 },
5758 } {
5859 t .Run (name , func (t * testing.T ) {
@@ -64,11 +65,21 @@ func TestPodHostname(t *testing.T) {
6465 t .Log ("Create a sandbox with hostname" )
6566 sbConfig := PodSandboxConfig ("sandbox" , "hostname" , opts ... )
6667 sb , err := runtimeService .RunPodSandbox (sbConfig )
67- require .NoError (t , err )
68- defer func () {
69- assert .NoError (t , runtimeService .StopPodSandbox (sb ))
70- assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
71- }()
68+ if err != nil {
69+ if ! test .expectErr {
70+ t .Fatalf ("Unexpected RunPodSandbox error: %v" , err )
71+ }
72+ return
73+ } else {
74+ // Make sure the sandbox is cleaned up.
75+ defer func () {
76+ assert .NoError (t , runtimeService .StopPodSandbox (sb ))
77+ assert .NoError (t , runtimeService .RemovePodSandbox (sb ))
78+ }()
79+ if test .expectErr {
80+ t .Fatalf ("Expected RunPodSandbox to return error" )
81+ }
82+ }
7283
7384 const (
7485 testImage = "busybox"
0 commit comments