blob: bf814c26a035f22ccbc7bb9d1b31865a45dae1be [file] [log] [blame]
Romain Bertozzi88859c02016-12-05 16:34:55 -05001/*
2 * Copyright (C) 2016 Savoir-faire Linux Inc.
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
21import XCTest
Thibault Wittemberg67e34612017-07-04 15:36:03 -040022@testable import Ring
Romain Bertozzi88859c02016-12-05 16:34:55 -050023
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 */
31class 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
74 XCTAssertEqual(error as? StartDaemonError, StartDaemonError.DaemonAlreadyRunning)
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
84 XCTAssertEqual(error as? StopDaemonError, StopDaemonError.DaemonNotRunning)
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
95 XCTAssertEqual(error as? StartDaemonError, StartDaemonError.InitializationFailure)
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
106 XCTAssertEqual(error as? StartDaemonError, StartDaemonError.StartFailure)
107 }
108 }
109}