-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasic.purs
More file actions
98 lines (79 loc) · 3.3 KB
/
Basic.purs
File metadata and controls
98 lines (79 loc) · 3.3 KB
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
90
91
92
93
94
95
96
97
module Test.Basic where
import Prelude
import Control.Monad.Error.Class (throwError)
import Data.Either (Either(..), isLeft, isRight)
import Data.String (trim)
import Effect.Aff (error)
import Effect.Class (liftEffect)
import Partial.Unsafe (unsafePartial)
import Test.Assertions (shouldInclude)
import Test.Partials (forceRight)
import Test.Spec (Spec, describe, it)
import Test.Spec.Assertions (shouldEqual, shouldNotEqual, shouldSatisfy)
import Test.Spec.Assertions.String (shouldStartWith)
import Test.Testcontainers (getFirstMappedPort, getHost, getId, getMappedPort, getName, setCommand, setName, setPrivilegedMode, setPullPolicy, setUser, setWorkingDirectory, withContainer)
import Test.Testcontainers.Types (PullPolicy(..))
import Test.Utils (launchCommand, mkAffContainer)
basicTest :: Spec Unit
basicTest = do
describe "Basic stuff" $ do
it "should launch a basic container" $ do
cnt <- mkAffContainer "alpine:latest" $
setCommand [ "sleep", "360" ]
<<< setPullPolicy AlwaysPull
<<< setName "sleeper"
<<< setPrivilegedMode
void $ withContainer cnt $ \c -> do
containerIdE <- liftEffect $ getId c
containerIdE `shouldSatisfy` isRight
let containerId = unsafePartial $ forceRight containerIdE
containerId `shouldNotEqual` ""
launchCommand c [ "echo", "hello", "world" ]
(\s -> s `shouldEqual` "hello world\n")
(\code -> code `shouldEqual` 0)
launchCommand c [ "whoami" ]
(\s -> s `shouldEqual` "root\n")
(\code -> code `shouldEqual` 0)
launchCommand c [ "ps", "x" ]
(\s -> s `shouldInclude` "sleep 360")
(\_ -> pure unit)
launchCommand c [ "ping", "-c", "1", "127.0.0.1" ]
(\s -> s `shouldInclude` "ping statistics")
(\code -> code `shouldEqual` 0)
launchCommand c [ "hostname" ]
(\s -> containerId `shouldStartWith` trim s)
(\_ -> pure unit)
eitherName <- liftEffect $ getName c
eitherName `shouldSatisfy` isRight
eitherHost <- liftEffect $ getHost c
eitherHost `shouldSatisfy` isRight
let name = unsafePartial $ forceRight eitherName
name `shouldEqual` "/sleeper"
let host = unsafePartial $ forceRight eitherHost
host `shouldEqual` "localhost"
-- Since there are no mapped ports, both these functions should fail
p <- getMappedPort 8080 c
p `shouldSatisfy` isLeft
p' <- getFirstMappedPort c
p' `shouldSatisfy` isLeft
it "should change the user" $ do
-- default user for redis container is "root"
redis <- mkAffContainer "redis:latest" $ setUser "redis"
res <- withContainer redis $ \c -> do
launchCommand c [ "whoami" ]
(\s -> s `shouldEqual` "redis\n")
(\_ -> pure unit)
case res of
Left e -> throwError $ error e
Right _ -> pure unit
it "should change the working directory" $ do
sleeper <- mkAffContainer "alpine:latest" $
setWorkingDirectory "/tmp"
<<< setCommand [ "sleep", "30" ]
res <- withContainer sleeper $ \c -> do
launchCommand c [ "pwd" ]
(\s -> s `shouldEqual` "/tmp\n")
(\_ -> pure unit)
case res of
Left e -> throwError $ error e
Right _ -> pure unit