@@ -2088,6 +2088,58 @@ func (s *SystemdTestSuite) TestRemoveMountUnit(c *C) {
20882088 })
20892089}
20902090
2091+ func (s * SystemdTestSuite ) TestRemoveMountUnitWhenIsMountedButDeletedUnitFile (c * C ) {
2092+ // When the unit file is missing but the directory is still mounted
2093+ // (e.g. a user manually deleted the unit file), RemoveMountUnitFile
2094+ // must still unmount the directory and return nil error
2095+ rootDir := dirs .GlobalRootDir
2096+ mountDir := rootDir + "/snap/foo/42"
2097+
2098+ restore := MockOsutilIsMounted (func (path string ) (bool , error ) {
2099+ c .Check (path , Equals , mountDir )
2100+ return true , nil
2101+ })
2102+ defer restore ()
2103+
2104+ mockUmountCmd := testutil .MockCommand (c , "umount" , "" )
2105+ defer mockUmountCmd .Restore ()
2106+
2107+ // No unit file on disk
2108+ err := NewUnderRoot (rootDir , SystemMode , nil ).RemoveMountUnitFile (mountDir )
2109+ c .Assert (err , IsNil )
2110+
2111+ // umount was called
2112+ c .Check (mockUmountCmd .Calls (), HasLen , 1 )
2113+ c .Check (mockUmountCmd .Calls ()[0 ], DeepEquals , []string {"umount" , "-d" , "-l" , mountDir })
2114+ // no systemctl calls: no unit file to disable or daemon-reload for
2115+ c .Check (s .argses , HasLen , 0 )
2116+ }
2117+
2118+ func (s * SystemdTestSuite ) TestRemoveMountUnitWhenIsNotMountedAndDeletedUnitFile (c * C ) {
2119+ // When the unit file is missing and the directory is not mounted
2120+ // RemoveMountUnitFile must return nil error without calling umount
2121+ // or systemctl
2122+ rootDir := dirs .GlobalRootDir
2123+ mountDir := rootDir + "/snap/foo/42"
2124+
2125+ restore := MockOsutilIsMounted (func (path string ) (bool , error ) {
2126+ c .Check (path , Equals , mountDir )
2127+ return false , nil
2128+ })
2129+ defer restore ()
2130+
2131+ mockUmountCmd := testutil .MockCommand (c , "umount" , "" )
2132+ defer mockUmountCmd .Restore ()
2133+
2134+ // No unit file on disk
2135+ err := NewUnderRoot (rootDir , SystemMode , nil ).RemoveMountUnitFile (mountDir )
2136+ c .Assert (err , IsNil )
2137+
2138+ // nothing to unmount or disable
2139+ c .Check (mockUmountCmd .Calls (), HasLen , 0 )
2140+ c .Check (s .argses , HasLen , 0 )
2141+ }
2142+
20912143func (s * SystemdTestSuite ) TestDaemonReloadMutex (c * C ) {
20922144 s .testDaemonOpWithMutex (c , Systemd .DaemonReload )
20932145}
@@ -2684,6 +2736,56 @@ func (s *SystemdTestSuite) TestPreseedModeRemoveMountUnitUnmounted(c *C) {
26842736 c .Check (mockUmountCmd .Calls (), HasLen , 0 )
26852737}
26862738
2739+ func (s * SystemdTestSuite ) TestPreseedModeRemoveMountUnitMountedButNoUnitFile (c * C ) {
2740+ // When the unit file is missing but the directory is still mounted,
2741+ // emulation-mode RemoveMountUnitFile must still unmount and return nil
2742+ // error.
2743+ mountDir := dirs .GlobalRootDir + "/snap/foo/42"
2744+
2745+ restore := MockOsutilIsMounted (func (path string ) (bool , error ) {
2746+ c .Check (path , Equals , mountDir )
2747+ return true , nil
2748+ })
2749+ defer restore ()
2750+
2751+ mockUmountCmd := testutil .MockCommand (c , "umount" , "" )
2752+ defer mockUmountCmd .Restore ()
2753+
2754+ // No unit file on disk
2755+ sysd := NewEmulationMode (dirs .GlobalRootDir )
2756+ c .Assert (sysd .RemoveMountUnitFile (mountDir ), IsNil )
2757+
2758+ // umount was called
2759+ c .Check (mockUmountCmd .Calls (), HasLen , 1 )
2760+ c .Check (mockUmountCmd .Calls ()[0 ], DeepEquals , []string {"umount" , "-d" , "-l" , mountDir })
2761+ // no systemctl calls: no unit file to disable
2762+ c .Check (s .argses , HasLen , 0 )
2763+ }
2764+
2765+ func (s * SystemdTestSuite ) TestPreseedModeRemoveMountUnitUnmountedAndNoUnitFile (c * C ) {
2766+ // When the unit file is missing and the directory is not mounted
2767+ // RemoveMountUnitFile must return nil error without calling umount
2768+ // or systemctl
2769+ mountDir := dirs .GlobalRootDir + "/snap/foo/42"
2770+
2771+ restore := MockOsutilIsMounted (func (path string ) (bool , error ) {
2772+ c .Check (path , Equals , mountDir )
2773+ return false , nil
2774+ })
2775+ defer restore ()
2776+
2777+ mockUmountCmd := testutil .MockCommand (c , "umount" , "" )
2778+ defer mockUmountCmd .Restore ()
2779+
2780+ // No unit file on disk
2781+ sysd := NewEmulationMode (dirs .GlobalRootDir )
2782+ c .Assert (sysd .RemoveMountUnitFile (mountDir ), IsNil )
2783+
2784+ // nothing to unmount or disable
2785+ c .Check (mockUmountCmd .Calls (), HasLen , 0 )
2786+ c .Check (s .argses , HasLen , 0 )
2787+ }
2788+
26872789func (s * SystemdTestSuite ) TestPreseedModeBindmountNotSupported (c * C ) {
26882790 sysd := NewEmulationMode (dirs .GlobalRootDir )
26892791
0 commit comments