Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 1 | /* |
Sébastien Blin | 77c737a | 2019-01-02 17:34:46 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016-2019 Savoir-faire Linux Inc. |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 3 | * |
| 4 | * Author: Romain Bertozzi <romain.bertozzi@savoirfairelinux.com> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation; either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program; if not, write to the Free Software |
| 18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | */ |
| 20 | |
| 21 | import XCTest |
Thibault Wittemberg | 67e3461 | 2017-07-04 15:36:03 -0400 | [diff] [blame] | 22 | @testable import Ring |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 23 | |
| 24 | /** |
| 25 | A test class designed to validate that the daemon service runs as expected. |
| 26 | It will test for example that: |
| 27 | - the daemon correctly starts |
| 28 | - correctly stops |
| 29 | - fails to achieve one or the other as we expect it to do. |
| 30 | */ |
| 31 | class DaemonServiceTests: XCTestCase { |
| 32 | /** |
| 33 | Tests that the Ring Daemon Service starts the daemon correctly |
| 34 | |
| 35 | - Returns: the DaemonService used to do the test |
| 36 | */ |
| 37 | func testStart() -> DaemonService { |
| 38 | var hasStartError = false |
| 39 | let daemonService = DaemonService.init(dRingAdaptor: DRingAdapter()) |
| 40 | do { |
| 41 | try daemonService.startDaemon() |
| 42 | } catch { |
| 43 | hasStartError = true |
| 44 | } |
| 45 | XCTAssertFalse(hasStartError) |
| 46 | XCTAssertTrue(daemonService.daemonStarted) |
| 47 | return daemonService |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | Tests that the Ring Daemon Service stops the daemon correctly |
| 52 | |
| 53 | - Returns: the DaemonService used to do the test |
| 54 | */ |
| 55 | func testStop() -> DaemonService { |
| 56 | var hasStopError = false |
| 57 | let daemonService = testStart() |
| 58 | do { |
| 59 | try daemonService.stopDaemon() |
| 60 | } catch { |
| 61 | hasStopError = true |
| 62 | } |
| 63 | XCTAssertFalse(hasStopError) |
| 64 | XCTAssertFalse(daemonService.daemonStarted) |
| 65 | return daemonService |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | Tests that the Ring Daemon Service does not start again if it is already started. |
| 70 | */ |
| 71 | func testAlreadyRunningException() { |
| 72 | let daemonService = testStart() |
| 73 | XCTAssertThrowsError(try daemonService.startDaemon()) { (error) in |
Thibault Wittemberg | 14b092a | 2017-07-04 18:13:26 -0400 | [diff] [blame] | 74 | XCTAssertEqual(error as? StartDaemonError, StartDaemonError.daemonAlreadyRunning) |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | Tests that the Ring Daemon Service does not stop if it is not currently running. |
| 80 | */ |
| 81 | func testDaemonNotRunningException() { |
| 82 | let daemonService = DaemonService.init(dRingAdaptor: DRingAdapter()) |
| 83 | XCTAssertThrowsError(try daemonService.stopDaemon()) { (error) in |
Thibault Wittemberg | 14b092a | 2017-07-04 18:13:26 -0400 | [diff] [blame] | 84 | XCTAssertEqual(error as? StopDaemonError, StopDaemonError.daemonNotRunning) |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | |
| 88 | /** |
| 89 | Tests that the Ring Daemon Service fails to initialize. |
| 90 | This test use a dedicated DRingAdaptor fixture. |
| 91 | */ |
| 92 | func testDaemonFailToInit() { |
| 93 | let daemonService = DaemonService.init(dRingAdaptor: FixtureFailInitDRingAdapter()) |
| 94 | XCTAssertThrowsError(try daemonService.startDaemon()) { (error) in |
Thibault Wittemberg | 14b092a | 2017-07-04 18:13:26 -0400 | [diff] [blame] | 95 | XCTAssertEqual(error as? StartDaemonError, StartDaemonError.initializationFailure) |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | Tests that the Ring Daemon Service fails to start. |
| 101 | This test use a dedicated DRingAdaptor fixture. |
| 102 | */ |
| 103 | func testDaemonFailToStart() { |
| 104 | let daemonService = DaemonService.init(dRingAdaptor: FixtureFailStartDRingAdapter()) |
| 105 | XCTAssertThrowsError(try daemonService.startDaemon()) { (error) in |
Thibault Wittemberg | 14b092a | 2017-07-04 18:13:26 -0400 | [diff] [blame] | 106 | XCTAssertEqual(error as? StartDaemonError, StartDaemonError.startFailure) |
Romain Bertozzi | 88859c0 | 2016-12-05 16:34:55 -0500 | [diff] [blame] | 107 | } |
| 108 | } |
| 109 | } |