|
27 | 27 | end |
28 | 28 | end |
29 | 29 |
|
30 | | - it 'enables linger' do |
31 | | - resource = Puppet::Type.type(:loginctl_user).new(common_params) |
32 | | - expect(provider_class).to receive(:loginctl).with('enable-linger', 'foo') |
33 | | - resource.provider.linger = :enabled |
| 30 | + context 'when enabling linger' do |
| 31 | + let(:resource) { Puppet::Type.type(:loginctl_user).new(common_params) } |
| 32 | + let(:provider) { resource.provider } |
| 33 | + |
| 34 | + it 'enables linger and waits for systemd --user to be ready' do |
| 35 | + expect(provider).to receive(:loginctl).with('enable-linger', 'foo') |
| 36 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 37 | + and_return("RuntimePath=/run/user/1000\n") |
| 38 | + expect(File).to receive(:exist?).with('/run/user/1000/systemd/private').and_return(true) |
| 39 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'State'). |
| 40 | + and_return("State=active\n") |
| 41 | + expect(Puppet).to receive(:debug).with('systemd --user for foo is ready') |
| 42 | + |
| 43 | + provider.linger = :enabled |
| 44 | + end |
| 45 | + |
| 46 | + it 'waits and retries if systemd --user is not immediately ready' do |
| 47 | + expect(provider).to receive(:loginctl).with('enable-linger', 'foo') |
| 48 | + |
| 49 | + # First attempt: RuntimePath not ready |
| 50 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 51 | + and_return("RuntimePath=\n") |
| 52 | + |
| 53 | + # Second attempt: Socket doesn't exist yet |
| 54 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 55 | + and_return("RuntimePath=/run/user/1000\n") |
| 56 | + expect(File).to receive(:exist?).with('/run/user/1000/systemd/private').and_return(false) |
| 57 | + |
| 58 | + # Third attempt: State not active yet |
| 59 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 60 | + and_return("RuntimePath=/run/user/1000\n") |
| 61 | + expect(File).to receive(:exist?).with('/run/user/1000/systemd/private').and_return(true) |
| 62 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'State'). |
| 63 | + and_return("State=opening\n") |
| 64 | + |
| 65 | + # Fourth attempt: Success with lingering state |
| 66 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 67 | + and_return("RuntimePath=/run/user/1000\n") |
| 68 | + expect(File).to receive(:exist?).with('/run/user/1000/systemd/private').and_return(true) |
| 69 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'State'). |
| 70 | + and_return("State=lingering\n") |
| 71 | + expect(Puppet).to receive(:debug).with('systemd --user for foo is ready') |
| 72 | + |
| 73 | + allow(provider).to receive(:sleep) |
| 74 | + |
| 75 | + provider.linger = :enabled |
| 76 | + end |
| 77 | + |
| 78 | + it 'handles loginctl failures gracefully while waiting' do |
| 79 | + expect(provider).to receive(:loginctl).with('enable-linger', 'foo') |
| 80 | + |
| 81 | + # First attempt: loginctl fails |
| 82 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 83 | + and_raise(Puppet::ExecutionFailure, 'Failed to get user properties') |
| 84 | + expect(Puppet).to receive(:debug).with(%r{Waiting for systemd --user for foo}) |
| 85 | + |
| 86 | + # Second attempt: Success |
| 87 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 88 | + and_return("RuntimePath=/run/user/1000\n") |
| 89 | + expect(File).to receive(:exist?).with('/run/user/1000/systemd/private').and_return(true) |
| 90 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'State'). |
| 91 | + and_return("State=active\n") |
| 92 | + expect(Puppet).to receive(:debug).with('systemd --user for foo is ready') |
| 93 | + |
| 94 | + allow(provider).to receive(:sleep) |
| 95 | + |
| 96 | + provider.linger = :enabled |
| 97 | + end |
| 98 | + |
| 99 | + it 'logs a warning and continues if timeout is exceeded' do |
| 100 | + expect(provider).to receive(:loginctl).with('enable-linger', 'foo') |
| 101 | + |
| 102 | + # Mock Time to simulate timeout |
| 103 | + start_time = Time.now |
| 104 | + allow(Time).to receive(:now).and_return(start_time, start_time + 11) |
| 105 | + |
| 106 | + expect(provider).to receive(:loginctl).with('show-user', 'foo', '-p', 'RuntimePath'). |
| 107 | + and_return("RuntimePath=\n") |
| 108 | + |
| 109 | + expect(Puppet).to receive(:warning).with(%r{Timeout waiting for systemd --user instance for user foo to become ready, continuing anyway}) |
| 110 | + |
| 111 | + provider.linger = :enabled |
| 112 | + end |
34 | 113 | end |
35 | 114 |
|
36 | | - it 'disables linger' do |
| 115 | + it 'disables linger without waiting' do |
37 | 116 | resource = Puppet::Type.type(:loginctl_user).new(common_params) |
38 | 117 | expect(provider_class).to receive(:loginctl).with('disable-linger', 'foo') |
39 | 118 | resource.provider.linger = :disabled |
|
0 commit comments