-
Notifications
You must be signed in to change notification settings - Fork 0
/
Network.purs
89 lines (72 loc) · 3.19 KB
/
Network.purs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
module Test.Network where
import Prelude
import Data.Either (Either(..), isRight)
import Partial.Unsafe (unsafePartial)
import Test.Assertions (shouldInclude)
import Test.Partials (forceRight)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (fail, shouldEqual, shouldSatisfy)
import Test.Testcontainers (setCommand, setExtraHosts, setNetwork, setNetworkAliases, withContainer)
import Test.Testcontainers.Network (getId, getName, mkNetwork, startNetwork)
import Test.Utils (launchCommand, mkAffContainer)
networkTest :: Spec Unit
networkTest = do
describe "Network creation" $ do
it "should create a network" $ do
startedNetwork <- startNetwork mkNetwork
case startedNetwork of
Left err -> fail err
Right _ -> pure unit
it "should retrieve network information" $ do
otherNetwork <- startNetwork mkNetwork
otherNetwork `shouldSatisfy` isRight
networkNameE <- getName (unsafePartial $ forceRight otherNetwork)
networkNameE `shouldSatisfy` isRight
networkIdE <- getId (unsafePartial $ forceRight otherNetwork)
networkIdE `shouldSatisfy` isRight
let realName = unsafePartial $ forceRight networkNameE
let realId = unsafePartial $ forceRight networkIdE
realName `shouldSatisfy` ((/=) "")
realId `shouldSatisfy` ((/=) "")
describe "Network utilisation" $ do
it "should define extra hosts" $ do
alpine <- mkAffContainer "alpine:latest" $
setCommand [ "sleep", "infinity" ]
<<< setExtraHosts
[ { host: "foo", ipAddress: "10.250.0.42" }
, { host: "bar", ipAddress: "10.250.0.43" }
]
res <- withContainer alpine $ \c -> do
launchCommand c [ "getent", "hosts", "foo" ]
(\s -> s `shouldEqual` "10.250.0.42 foo foo\n")
(\exitCode -> exitCode `shouldEqual` 0)
launchCommand c [ "getent", "hosts", "bar" ]
(\s -> s `shouldEqual` "10.250.0.43 bar bar\n")
(\exitCode -> exitCode `shouldEqual` 0)
case res of
Left err -> fail err
Right _ -> pure unit
it "should use a network" $ do
commonNetwork <- startNetwork mkNetwork
case commonNetwork of
Left e -> fail e
Right network -> do
firstAlpine <- mkAffContainer "alpine:latest" $
setCommand [ "sleep", "infinity" ]
<<< setNetwork network
<<< setNetworkAliases [ "firstAlpine" ]
secondAlpine <- mkAffContainer "alpine:latest" $
setCommand [ "sleep", "infinity" ]
<<< setNetwork network
<<< setNetworkAliases [ "secondAlpine" ]
res <- withContainer firstAlpine $ \c ->
withContainer secondAlpine $ \c' -> do
launchCommand c [ "getent", "hosts", "secondAlpine" ]
(\s -> s `shouldInclude` "secondAlpine secondAlpine\n")
(\exitCode -> exitCode `shouldEqual` 0)
launchCommand c' [ "getent", "hosts", "firstAlpine" ]
(\s -> s `shouldInclude` "firstAlpine firstAlpine\n")
(\exitCode -> exitCode `shouldEqual` 0)
case res of
Left e -> fail e
Right _ -> pure unit