@@ -990,6 +990,31 @@ TEST_F(ExecInNetnsTest, Basic) {
990990 EXPECT_EQ (getCurrentNetns (), " /proc/thread-self/ns/net" );
991991}
992992
993+ TEST_F (ExecInNetnsTest, RestoresOriginalNetnsWhenCallbackThrows) {
994+ // Make the tests use mock syscalls.
995+ testing::StrictMock<Api::MockLinuxOsSysCalls> linux_os_syscalls;
996+ testing::StrictMock<Api::MockOsSysCalls> os_syscalls;
997+ TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls (&os_syscalls);
998+ TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls (&linux_os_syscalls);
999+
1000+ testing::InSequence s;
1001+ EXPECT_CALL (os_syscalls, open (testing::StrEq (" /proc/thread-self/ns/net" ), O_RDONLY ))
1002+ .WillOnce (Return (Api::SysCallIntResult{1337 , 0 }));
1003+ EXPECT_CALL (os_syscalls, open (testing::StrEq (" ns1" ), O_RDONLY ))
1004+ .WillOnce (Return (Api::SysCallIntResult{1338 , 0 }));
1005+ EXPECT_CALL (linux_os_syscalls, setns (1338 , CLONE_NEWNET ))
1006+ .WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1007+ // The original netns is restored before its fd is closed.
1008+ EXPECT_CALL (linux_os_syscalls, setns (1337 , CLONE_NEWNET ))
1009+ .WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1010+ EXPECT_CALL (os_syscalls, close (1338 )).WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1011+ EXPECT_CALL (os_syscalls, close (1337 )).WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1012+
1013+ EXPECT_THROW_WITH_MESSAGE (std::ignore = Utility::execInNetworkNamespace (
1014+ []() -> int { throw EnvoyException (" callback failed" ); }, " ns1" ),
1015+ EnvoyException, " callback failed" );
1016+ }
1017+
9931018TEST_F (ExecInNetnsTest, OpenFail) {
9941019 // Make the tests use mock syscalls.
9951020 testing::StrictMock<Api::MockLinuxOsSysCalls> linux_os_syscalls;
@@ -1008,6 +1033,28 @@ TEST_F(ExecInNetnsTest, OpenFail) {
10081033 EXPECT_THAT (result, HasStatusMessage (testing::StartsWith (" failed to open netns file" )));
10091034}
10101035
1036+ TEST_F (ExecInNetnsTest, SetnsFail) {
1037+ // Make the tests use mock syscalls.
1038+ testing::StrictMock<Api::MockLinuxOsSysCalls> linux_os_syscalls;
1039+ testing::StrictMock<Api::MockOsSysCalls> os_syscalls;
1040+ TestThreadsafeSingletonInjector<Api::OsSysCallsImpl> os_calls (&os_syscalls);
1041+ TestThreadsafeSingletonInjector<Api::LinuxOsSysCallsImpl> linux_os_calls (&linux_os_syscalls);
1042+
1043+ testing::InSequence s;
1044+ EXPECT_CALL (os_syscalls, open (testing::StrEq (" /proc/thread-self/ns/net" ), O_RDONLY ))
1045+ .WillOnce (Return (Api::SysCallIntResult{1337 , 0 }));
1046+ EXPECT_CALL (os_syscalls, open (testing::StrEq (" ns1" ), O_RDONLY ))
1047+ .WillOnce (Return (Api::SysCallIntResult{1338 , 0 }));
1048+ // The thread never left its netns, so there is nothing to restore.
1049+ EXPECT_CALL (linux_os_syscalls, setns (1338 , CLONE_NEWNET ))
1050+ .WillOnce (Return (Api::SysCallIntResult{-1 , EPERM }));
1051+ EXPECT_CALL (os_syscalls, close (1338 )).WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1052+ EXPECT_CALL (os_syscalls, close (1337 )).WillOnce (Return (Api::SysCallIntResult{0 , 0 }));
1053+
1054+ auto result = Utility::execInNetworkNamespace ([]() -> int { return 0 ; }, " ns1" );
1055+ EXPECT_THAT (result, HasStatusMessage (testing::StartsWith (" failed to set netns to ns1" )));
1056+ }
1057+
10111058TEST_F (ExecInNetnsTest, FailtoReturnToOriginalNetns) {
10121059 EXPECT_DEATH (
10131060 {
0 commit comments