diff options
author | joe <joe@jerkface.net> | 2017-09-15 06:22:10 -0400 |
---|---|---|
committer | joe <joe@jerkface.net> | 2017-09-15 06:22:10 -0400 |
commit | 12cbb3af2413dc28838ed271351dda16df8f7bdb (patch) | |
tree | 2db77a787e18a81a8369a8d73fee369d8826f064 /bittorrent/tests/Main.hs | |
parent | 362357c6d08cbd8dffa627a1e80199dcb9ba231f (diff) |
Separating dht-client library from bittorrent package.
Diffstat (limited to 'bittorrent/tests/Main.hs')
-rw-r--r-- | bittorrent/tests/Main.hs | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/bittorrent/tests/Main.hs b/bittorrent/tests/Main.hs new file mode 100644 index 00000000..5ed953da --- /dev/null +++ b/bittorrent/tests/Main.hs | |||
@@ -0,0 +1,97 @@ | |||
1 | {-# LANGUAGE RecordWildCards #-} | ||
2 | module Main where | ||
3 | import Control.Exception | ||
4 | import Control.Monad | ||
5 | import Data.Functor | ||
6 | import Data.Maybe | ||
7 | import System.Directory | ||
8 | import System.Exit | ||
9 | import System.Environment | ||
10 | import System.FilePath | ||
11 | import System.Process | ||
12 | import Text.Printf | ||
13 | import Test.Hspec | ||
14 | |||
15 | import Config | ||
16 | import qualified Spec as Generated | ||
17 | |||
18 | |||
19 | type Command = String | ||
20 | type Descr = (ClientName, ClientOpts -> FilePath -> Command) | ||
21 | |||
22 | torrents :: [FilePath] | ||
23 | torrents = | ||
24 | [ "dapper-dvd-amd64-iso.torrent" | ||
25 | , "pkg.torrent" | ||
26 | , "testfile.torrent" | ||
27 | ] | ||
28 | |||
29 | rtorrentSessionDir :: String | ||
30 | rtorrentSessionDir = "rtorrent-sessiondir" | ||
31 | |||
32 | sessionName :: String -- screen session name | ||
33 | sessionName = "bittorrent-testsuite" | ||
34 | |||
35 | tmpDir :: FilePath | ||
36 | tmpDir = "res" | ||
37 | |||
38 | clients :: [Descr] | ||
39 | clients = | ||
40 | [ ("rtorrent" | ||
41 | , \ ClientOpts {..} tfile -> printf | ||
42 | "rtorrent -p %i-%i -O dht=on -O dht_port=%i -O session=%s %s" | ||
43 | (fromEnum peerPort) (fromEnum peerPort) (fromEnum nodePort) | ||
44 | rtorrentSessionDir tfile | ||
45 | ) | ||
46 | ] | ||
47 | |||
48 | setupEnv :: EnvOpts -> IO (Maybe ()) | ||
49 | setupEnv EnvOpts {..} | ||
50 | | Just client <- testClient | ||
51 | , Just mkCmd <- lookup client clients = do | ||
52 | _ <- printf "Setting up %s\n" client | ||
53 | |||
54 | let torrentPath = "testfile.torrent" | ||
55 | let runner = printf "screen -dm -S %s %s" sessionName | ||
56 | (mkCmd remoteOpts torrentPath) | ||
57 | |||
58 | wd <- getCurrentDirectory | ||
59 | createDirectoryIfMissing True (wd </> tmpDir </> rtorrentSessionDir) | ||
60 | _ <- createProcess (shell runner) { cwd = Just (wd </> tmpDir) } | ||
61 | |||
62 | return (Just ()) | ||
63 | |||
64 | | Just client <- testClient = do | ||
65 | _ <- printf "Bad client `%s`, use one of %s\n" client (show (fst <$> clients)) | ||
66 | return Nothing | ||
67 | |||
68 | | otherwise = do | ||
69 | _ <- printf "Running without remote client\n" | ||
70 | return (Just ()) | ||
71 | |||
72 | terminateEnv :: IO () | ||
73 | terminateEnv = do | ||
74 | wd <- getCurrentDirectory | ||
75 | removeDirectoryRecursive (wd </> tmpDir </> rtorrentSessionDir) | ||
76 | _ <- printf "closing screen session: %s\n" sessionName | ||
77 | _ <- system (printf "screen -S %s -X quit" sessionName) | ||
78 | return () | ||
79 | |||
80 | runTestSuite :: [String] -> IO ExitCode | ||
81 | runTestSuite args = do | ||
82 | _ <- printf "running hspec test suite with args: %s\n" (show args) | ||
83 | catch (withArgs args (hspec Generated.spec) >> return ExitSuccess) return | ||
84 | |||
85 | withEnv :: EnvOpts -> IO a -> IO a | ||
86 | withEnv opts action = bracket (setupEnv opts) terminate (const action) | ||
87 | where | ||
88 | terminate running = do | ||
89 | when (isJust running) $ do | ||
90 | terminateEnv | ||
91 | |||
92 | main :: IO () | ||
93 | main = do | ||
94 | (envOpts, suiteArgs) <- getOpts | ||
95 | withEnv envOpts $ do | ||
96 | code <- runTestSuite suiteArgs | ||
97 | exitWith code | ||