github.com/Axway/agent-sdk@v1.1.101/pkg/cmd/service/daemon/daemon_test.go (about) 1 package daemon 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 "testing" 8 9 "github.com/stretchr/testify/assert" 10 ) 11 12 const tempfile = "/tmp/tempfile" 13 14 type mockOSFS struct { 15 statErrorOrder []bool 16 statCalls int 17 removeCalled bool 18 } 19 20 func (m *mockOSFS) Open(name string) (*os.File, error) { return nil, nil } 21 func (m *mockOSFS) Readlink(name string) (string, error) { return "", nil } 22 23 func (m *mockOSFS) Create(name string) (*os.File, error) { 24 return os.Create(tempfile) 25 } 26 27 func (m *mockOSFS) Remove(name string) error { 28 m.removeCalled = true 29 return nil 30 } 31 32 func (m *mockOSFS) Stat(name string) (os.FileInfo, error) { 33 if len(m.statErrorOrder) <= m.statCalls { 34 return nil, nil 35 } 36 if m.statErrorOrder[m.statCalls] { 37 m.statCalls++ 38 return nil, fmt.Errorf("error in stat") 39 } 40 m.statCalls++ 41 return nil, nil 42 } 43 44 type fakeCommand struct { 45 calls int 46 outputs []string 47 cmds []string 48 } 49 50 var fakeOutput fakeCommand 51 52 func fakeExecCommand(name string, arg ...string) ([]byte, error) { 53 output := []byte(fakeOutput.outputs[fakeOutput.calls]) 54 fakeOutput.calls++ 55 fakeOutput.cmds = append(fakeOutput.cmds, fmt.Sprint(name, " ", strings.Join(arg, " "))) 56 return output, nil 57 } 58 59 func TestNew(t *testing.T) { 60 mOSFS := mockOSFS{} 61 fs = &mOSFS 62 63 daemon, err := New("daemon", "desc", "network") 64 assert.Nil(t, err, "Error was not nil") 65 assert.NotNil(t, daemon, "The daemon object was not returned") 66 assert.IsType(t, &systemDRecord{}, daemon, "The returned type was incorrect") 67 } 68 69 func TestNewDaemon(t *testing.T) { 70 mOSFS := mockOSFS{} 71 fs = &mOSFS 72 73 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 74 assert.Nil(t, err, "Error was not nil") 75 assert.NotNil(t, daemon, "The daemon object was not returned") 76 assert.IsType(t, &systemDRecord{}, daemon, "The returned type was incorrect") 77 78 mOSFS = mockOSFS{ 79 statErrorOrder: []bool{true}, 80 } 81 fs = &mOSFS 82 83 daemon, err = newDaemon("daemon", "desc", []string{"network"}) 84 assert.NotNil(t, err, "An error was expected") 85 assert.Nil(t, daemon, "The daemon interface was not expected") 86 } 87 88 func TestInstall(t *testing.T) { 89 execCmd = fakeExecCommand 90 fakeOutput = fakeCommand{ 91 calls: 0, 92 outputs: []string{"1000"}, 93 } 94 mOSFS := mockOSFS{} 95 fs = &mOSFS 96 97 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 98 assert.Nil(t, err, "Error was not nil") 99 assert.NotNil(t, daemon, "The daemon object was not returned") 100 101 output, err := daemon.Install() 102 assert.NotNil(t, output, "Expected an output to be returned") 103 assert.NotNil(t, err, "expected an error since we were not root") 104 105 mOSFS = mockOSFS{ 106 statErrorOrder: []bool{true}, 107 } 108 fs = &mOSFS 109 execCmd = fakeExecCommand 110 fakeOutput = fakeCommand{ 111 calls: 0, 112 outputs: []string{"0", ""}, 113 } 114 115 output, err = daemon.Install() 116 assert.NotNil(t, output, "Expected an output to be returned") 117 assert.Nil(t, err, "no error expected") 118 assert.Len(t, fakeOutput.cmds, 2) 119 assert.Equal(t, "systemctl daemon-reload", fakeOutput.cmds[1]) 120 } 121 122 func TestUpdate(t *testing.T) { 123 execCmd = fakeExecCommand 124 fakeOutput = fakeCommand{ 125 calls: 0, 126 outputs: []string{"1000"}, 127 } 128 mOSFS := mockOSFS{} 129 fs = &mOSFS 130 131 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 132 assert.Nil(t, err, "Error was not nil") 133 assert.NotNil(t, daemon, "The daemon object was not returned") 134 135 output, err := daemon.Update() 136 assert.NotNil(t, output, "Expected an output to be returned") 137 assert.NotNil(t, err, "expected an error since we were not root") 138 139 mOSFS = mockOSFS{ 140 statErrorOrder: []bool{false, true}, 141 } 142 fs = &mOSFS 143 execCmd = fakeExecCommand 144 fakeOutput = fakeCommand{ 145 calls: 0, 146 outputs: []string{"0", "", "", "0", ""}, 147 } 148 149 output, err = daemon.Update() 150 assert.NotNil(t, output, "Expected an output to be returned") 151 assert.Nil(t, err, "no error expected") 152 assert.Len(t, fakeOutput.cmds, 5) 153 assert.Equal(t, "systemctl status daemon.service", fakeOutput.cmds[1]) 154 assert.Equal(t, "systemctl disable daemon.service", fakeOutput.cmds[2]) 155 assert.True(t, mOSFS.removeCalled, "Exppected a call to remove the service definition file") 156 assert.Equal(t, "systemctl daemon-reload", fakeOutput.cmds[4]) 157 } 158 159 func TestRemove(t *testing.T) { 160 execCmd = fakeExecCommand 161 fakeOutput = fakeCommand{ 162 calls: 0, 163 outputs: []string{"1000"}, 164 } 165 mOSFS := mockOSFS{} 166 fs = &mOSFS 167 168 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 169 assert.Nil(t, err, "Error was not nil") 170 assert.NotNil(t, daemon, "The daemon object was not returned") 171 172 output, err := daemon.Remove() 173 assert.NotNil(t, output, "Expected an output to be returned") 174 assert.NotNil(t, err, "expected an error since we were not root") 175 176 execCmd = fakeExecCommand 177 fakeOutput = fakeCommand{ 178 calls: 0, 179 outputs: []string{"0", "", ""}, 180 } 181 182 output, err = daemon.Remove() 183 assert.NotNil(t, output, "Expected an output to be returned") 184 assert.Nil(t, err, "no error expected") 185 assert.Len(t, fakeOutput.cmds, 3) 186 assert.Equal(t, "systemctl status daemon.service", fakeOutput.cmds[1]) 187 assert.Equal(t, "systemctl disable daemon.service", fakeOutput.cmds[2]) 188 assert.True(t, mOSFS.removeCalled, "Exppected a call to remove the service definition file") 189 } 190 191 func TestStart(t *testing.T) { 192 execCmd = fakeExecCommand 193 fakeOutput = fakeCommand{ 194 calls: 0, 195 outputs: []string{"1000"}, 196 } 197 mOSFS := mockOSFS{} 198 fs = &mOSFS 199 200 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 201 assert.Nil(t, err, "Error was not nil") 202 assert.NotNil(t, daemon, "The daemon object was not returned") 203 204 output, err := daemon.Start() 205 assert.NotNil(t, output, "Expected an output to be returned") 206 assert.NotNil(t, err, "expected an error since we were not root") 207 208 execCmd = fakeExecCommand 209 fakeOutput = fakeCommand{ 210 calls: 0, 211 outputs: []string{"0", "Active: active"}, 212 } 213 214 output, err = daemon.Start() 215 assert.NotNil(t, output, "Expected an output to be returned") 216 assert.NotNil(t, err, "expected an error since the service would be 'running'") 217 218 execCmd = fakeExecCommand 219 fakeOutput = fakeCommand{ 220 calls: 0, 221 outputs: []string{"0", "Active: stopped", ""}, 222 } 223 224 output, err = daemon.Start() 225 assert.NotNil(t, output, "Expected an output to be returned") 226 assert.Nil(t, err, "no error expected") 227 assert.Len(t, fakeOutput.cmds, 3) 228 assert.Equal(t, "systemctl start daemon.service", fakeOutput.cmds[2]) 229 } 230 231 func TestStop(t *testing.T) { 232 execCmd = fakeExecCommand 233 fakeOutput = fakeCommand{ 234 calls: 0, 235 outputs: []string{"1000"}, 236 } 237 mOSFS := mockOSFS{} 238 fs = &mOSFS 239 240 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 241 assert.Nil(t, err, "Error was not nil") 242 assert.NotNil(t, daemon, "The daemon object was not returned") 243 244 output, err := daemon.Stop() 245 assert.NotNil(t, output, "Expected an output to be returned") 246 assert.NotNil(t, err, "expected an error since we were not root") 247 248 execCmd = fakeExecCommand 249 fakeOutput = fakeCommand{ 250 calls: 0, 251 outputs: []string{"0", "Active: stopped"}, 252 } 253 254 output, err = daemon.Stop() 255 assert.NotNil(t, output, "Expected an output to be returned") 256 assert.NotNil(t, err, "expected an error since the service would be 'stopped'") 257 258 execCmd = fakeExecCommand 259 fakeOutput = fakeCommand{ 260 calls: 0, 261 outputs: []string{"0", "Active: active", ""}, 262 } 263 264 output, err = daemon.Stop() 265 assert.NotNil(t, output, "Expected an output to be returned") 266 assert.Nil(t, err, "no error expected") 267 assert.Len(t, fakeOutput.cmds, 3) 268 assert.Equal(t, "systemctl stop daemon.service", fakeOutput.cmds[2]) 269 } 270 271 func TestLogs(t *testing.T) { 272 execCmd = fakeExecCommand 273 fakeOutput = fakeCommand{ 274 calls: 0, 275 outputs: []string{"1000"}, 276 } 277 mOSFS := mockOSFS{} 278 fs = &mOSFS 279 280 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 281 assert.Nil(t, err, "Error was not nil") 282 assert.NotNil(t, daemon, "The daemon object was not returned") 283 284 output, err := daemon.Status() 285 assert.NotNil(t, output, "Expected an output to be returned") 286 assert.NotNil(t, err, "expected an error since we were not root") 287 288 execCmd = fakeExecCommand 289 fakeOutput = fakeCommand{ 290 calls: 0, 291 outputs: []string{"0", "Active: active"}, 292 } 293 294 output, err = daemon.Logs() 295 assert.NotNil(t, output, "Expected an output to be returned") 296 assert.Nil(t, err, "don't expect error for status command") 297 assert.Len(t, fakeOutput.cmds, 1) 298 assert.Equal(t, "journalctl --no-pager -b -u daemon.service", fakeOutput.cmds[0]) 299 } 300 301 func TestStatus(t *testing.T) { 302 execCmd = fakeExecCommand 303 fakeOutput = fakeCommand{ 304 calls: 0, 305 outputs: []string{"1000"}, 306 } 307 mOSFS := mockOSFS{} 308 fs = &mOSFS 309 310 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 311 assert.Nil(t, err, "Error was not nil") 312 assert.NotNil(t, daemon, "The daemon object was not returned") 313 314 output, err := daemon.Status() 315 assert.NotNil(t, output, "Expected an output to be returned") 316 assert.NotNil(t, err, "expected an error since we were not root") 317 318 execCmd = fakeExecCommand 319 fakeOutput = fakeCommand{ 320 calls: 0, 321 outputs: []string{"0", "Active: active"}, 322 } 323 324 output, err = daemon.Status() 325 assert.NotNil(t, output, "Expected an output to be returned") 326 assert.Nil(t, err, "don't expect error for status command") 327 assert.Len(t, fakeOutput.cmds, 2) 328 assert.Equal(t, "systemctl status daemon.service", fakeOutput.cmds[1]) 329 } 330 331 func TestEnabled(t *testing.T) { 332 execCmd = fakeExecCommand 333 fakeOutput = fakeCommand{ 334 calls: 0, 335 outputs: []string{"1000"}, 336 } 337 mOSFS := mockOSFS{} 338 fs = &mOSFS 339 340 daemon, err := newDaemon("daemon", "desc", []string{"network"}) 341 assert.Nil(t, err, "Error was not nil") 342 assert.NotNil(t, daemon, "The daemon object was not returned") 343 344 output, err := daemon.Enable() 345 assert.NotNil(t, output, "Expected an output to be returned") 346 assert.NotNil(t, err, "expected an error since we were not root") 347 348 execCmd = fakeExecCommand 349 fakeOutput = fakeCommand{ 350 calls: 0, 351 outputs: []string{"0", ""}, 352 } 353 354 output, err = daemon.Enable() 355 assert.NotNil(t, output, "Expected an output to be returned") 356 assert.Nil(t, err, "no error expected") 357 assert.Len(t, fakeOutput.cmds, 2) 358 assert.Equal(t, "systemctl enable daemon.service", fakeOutput.cmds[1]) 359 }