|
| 1 | +package daemon_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os/user" |
| 6 | + "path/filepath" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/github/git-bundle-server/internal/daemon" |
| 10 | + "github.com/stretchr/testify/assert" |
| 11 | + "github.com/stretchr/testify/mock" |
| 12 | +) |
| 13 | + |
| 14 | +var systemdCreateTests = []struct { |
| 15 | + title string |
| 16 | + |
| 17 | + // Inputs |
| 18 | + config *daemon.DaemonConfig |
| 19 | + force boolArg |
| 20 | + |
| 21 | + // Mocked responses (ordered per list!) |
| 22 | + fileExists []pair[bool, error] |
| 23 | + writeFile []error |
| 24 | + systemctlDaemonReload []pair[int, error] |
| 25 | + |
| 26 | + // Expected values |
| 27 | + expectErr bool |
| 28 | +}{ |
| 29 | + { |
| 30 | + "Fresh service unit created if none exists, daemon reloaded", |
| 31 | + &daemon.DaemonConfig{ |
| 32 | + Label: "com.example.testdaemon", |
| 33 | + Program: "/usr/local/bin/test/git-bundle-web-server", |
| 34 | + }, |
| 35 | + Any, |
| 36 | + []pair[bool, error]{newPair[bool, error](false, nil)}, // file exists |
| 37 | + []error{nil}, // write file |
| 38 | + []pair[int, error]{newPair[int, error](0, nil)}, // systemctl daemon-reload |
| 39 | + false, |
| 40 | + }, |
| 41 | + { |
| 42 | + "Service unit exists, doesn't write file or reload", |
| 43 | + &daemon.DaemonConfig{ |
| 44 | + Label: "com.example.testdaemon", |
| 45 | + Program: "/usr/local/bin/test/git-bundle-web-server", |
| 46 | + }, |
| 47 | + False, |
| 48 | + []pair[bool, error]{newPair[bool, error](true, nil)}, // file exists |
| 49 | + []error{}, // write file |
| 50 | + []pair[int, error]{}, // systemctl daemon-reload |
| 51 | + false, |
| 52 | + }, |
| 53 | + { |
| 54 | + "'force' option overwrites service unit and reloads daemon", |
| 55 | + &daemon.DaemonConfig{ |
| 56 | + Label: "com.example.testdaemon", |
| 57 | + Program: "/usr/local/bin/test/git-bundle-web-server", |
| 58 | + }, |
| 59 | + True, |
| 60 | + []pair[bool, error]{newPair[bool, error](true, nil)}, // file exists |
| 61 | + []error{nil}, // write file |
| 62 | + []pair[int, error]{newPair[int, error](0, nil)}, // systemctl daemon-reload |
| 63 | + false, |
| 64 | + }, |
| 65 | +} |
| 66 | + |
| 67 | +func TestSystemd_Create(t *testing.T) { |
| 68 | + // Set up mocks |
| 69 | + testUser := &user.User{ |
| 70 | + Uid: "123", |
| 71 | + Username: "testuser", |
| 72 | + HomeDir: "/my/test/dir", |
| 73 | + } |
| 74 | + testUserProvider := &mockUserProvider{} |
| 75 | + testUserProvider.On("CurrentUser").Return(testUser, nil) |
| 76 | + |
| 77 | + testCommandExecutor := &mockCommandExecutor{} |
| 78 | + |
| 79 | + testFileSystem := &mockFileSystem{} |
| 80 | + |
| 81 | + systemd := daemon.NewSystemdProvider(testUserProvider, testCommandExecutor, testFileSystem) |
| 82 | + |
| 83 | + for _, tt := range systemdCreateTests { |
| 84 | + forceArg := tt.force.toBoolList() |
| 85 | + for _, force := range forceArg { |
| 86 | + t.Run(fmt.Sprintf("%s (force='%t')", tt.title, force), func(t *testing.T) { |
| 87 | + // Mock responses |
| 88 | + for _, retVal := range tt.fileExists { |
| 89 | + testFileSystem.On("FileExists", |
| 90 | + mock.AnythingOfType("string"), |
| 91 | + ).Return(retVal.first, retVal.second).Once() |
| 92 | + } |
| 93 | + for _, retVal := range tt.writeFile { |
| 94 | + testFileSystem.On("WriteFile", |
| 95 | + mock.AnythingOfType("string"), |
| 96 | + mock.Anything, |
| 97 | + ).Return(retVal).Once() |
| 98 | + } |
| 99 | + for _, retVal := range tt.systemctlDaemonReload { |
| 100 | + testCommandExecutor.On("Run", |
| 101 | + "systemctl", |
| 102 | + []string{"--user", "daemon-reload"}, |
| 103 | + ).Return(retVal.first, retVal.second).Once() |
| 104 | + } |
| 105 | + |
| 106 | + // Run "Create" |
| 107 | + err := systemd.Create(tt.config, force) |
| 108 | + |
| 109 | + // Assert on expected values |
| 110 | + if tt.expectErr { |
| 111 | + assert.NotNil(t, err) |
| 112 | + } else { |
| 113 | + assert.Nil(t, err) |
| 114 | + } |
| 115 | + mock.AssertExpectationsForObjects(t, testCommandExecutor, testFileSystem) |
| 116 | + |
| 117 | + // Reset mocks |
| 118 | + testCommandExecutor.Mock = mock.Mock{} |
| 119 | + testFileSystem.Mock = mock.Mock{} |
| 120 | + }) |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + // Verify content of created file |
| 125 | + t.Run("Created file content and path are correct", func(t *testing.T) { |
| 126 | + var actualFilename string |
| 127 | + var actualFileBytes []byte |
| 128 | + |
| 129 | + // Mock responses for successful fresh write |
| 130 | + testCommandExecutor.On("Run", |
| 131 | + "systemctl", |
| 132 | + []string{"--user", "daemon-reload"}, |
| 133 | + ).Return(0, nil).Once() |
| 134 | + testFileSystem.On("FileExists", |
| 135 | + mock.AnythingOfType("string"), |
| 136 | + ).Return(false, nil).Once() |
| 137 | + |
| 138 | + // Use mock to save off input args |
| 139 | + testFileSystem.On("WriteFile", |
| 140 | + mock.MatchedBy(func(filename string) bool { |
| 141 | + actualFilename = filename |
| 142 | + return true |
| 143 | + }), |
| 144 | + mock.MatchedBy(func(fileBytes any) bool { |
| 145 | + // Save off value and always match |
| 146 | + actualFileBytes = fileBytes.([]byte) |
| 147 | + return true |
| 148 | + }), |
| 149 | + ).Return(nil).Once() |
| 150 | + |
| 151 | + err := systemd.Create(&basicDaemonConfig, false) |
| 152 | + assert.Nil(t, err) |
| 153 | + mock.AssertExpectationsForObjects(t, testCommandExecutor, testFileSystem) |
| 154 | + |
| 155 | + // Check filename |
| 156 | + expectedFilename := filepath.Clean(fmt.Sprintf("/my/test/dir/.config/systemd/user/%s.service", basicDaemonConfig.Label)) |
| 157 | + assert.Equal(t, expectedFilename, actualFilename) |
| 158 | + |
| 159 | + // Check file contents |
| 160 | + fileContents := string(actualFileBytes) |
| 161 | + assert.Contains(t, fileContents, fmt.Sprintf("ExecStart=%s", basicDaemonConfig.Program)) |
| 162 | + assert.Contains(t, fileContents, fmt.Sprintf("Description=%s", basicDaemonConfig.Description)) |
| 163 | + }) |
| 164 | +} |
0 commit comments