Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for healthcheck argument start-interval #98

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/Language/Docker/Parser/Healthcheck.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ data CheckFlag
= FlagInterval Duration
| FlagTimeout Duration
| FlagStartPeriod Duration
| FlagStartInterval Duration
| FlagRetries Retries
| CFlagInvalid (Text, Text)

Expand All @@ -42,20 +43,23 @@ parseHealthcheck = do
let intervals = [x | FlagInterval x <- flags]
let timeouts = [x | FlagTimeout x <- flags]
let startPeriods = [x | FlagStartPeriod x <- flags]
let startIntervals = [x | FlagStartInterval x <- flags]
let retriesD = [x | FlagRetries x <- flags]
let invalid = [x | CFlagInvalid x <- flags]
-- Let's do some validation on the flags
case (invalid, intervals, timeouts, startPeriods, retriesD) of
((k, v) : _, _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _, _) -> customError $ DuplicateFlagError "--interval"
(_, _, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--timeout"
(_, _, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--start-period"
(_, _, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--retries"
case (invalid, intervals, timeouts, startPeriods, startIntervals, retriesD) of
((k, v) : _, _, _, _, _, _) -> unexpectedFlag k v
(_, _ : _ : _, _, _, _, _) -> customError $ DuplicateFlagError "--interval"
(_, _, _ : _ : _, _, _, _) -> customError $ DuplicateFlagError "--timeout"
(_, _, _, _ : _ : _, _, _) -> customError $ DuplicateFlagError "--start-period"
(_, _, _, _, _ : _ : _, _) -> customError $ DuplicateFlagError "--start-interval"
(_, _, _, _, _, _ : _ : _) -> customError $ DuplicateFlagError "--retries"
Comment on lines +50 to +56

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mmh Haskell case blocks scale as O(n^2)?! Is there an alternative?

_ -> do
Cmd checkCommand <- parseCmd
let interval = listToMaybe intervals
let timeout = listToMaybe timeouts
let startPeriod = listToMaybe startPeriods
let startInterval = listToMaybe startIntervals
let retries = listToMaybe retriesD
return $ Check CheckArgs {..}

Expand All @@ -64,6 +68,7 @@ checkFlag =
(FlagInterval <$> durationFlag "--interval=" <?> "--interval")
<|> (FlagTimeout <$> durationFlag "--timeout=" <?> "--timeout")
<|> (FlagStartPeriod <$> durationFlag "--start-period=" <?> "--start-period")
<|> (FlagStartInterval <$> durationFlag "--start-interval=" <?> "--start-interval")
<|> (FlagRetries <$> retriesFlag <?> "--retries")
<|> (CFlagInvalid <$> anyFlag <?> "no flags")

Expand Down
1 change: 1 addition & 0 deletions src/Language/Docker/PrettyPrint.hs
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,7 @@ prettyPrintInstruction i =
prettyPrintDuration "--interval=" interval
prettyPrintDuration "--timeout=" timeout
prettyPrintDuration "--start-period=" startPeriod
prettyPrintDuration "--start-interval" startInterval
prettyPrintRetries retries
"CMD"
prettyPrintArguments checkCommand
Expand Down
1 change: 1 addition & 0 deletions src/Language/Docker/Syntax.hs
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ data CheckArgs args
interval :: !(Maybe Duration),
timeout :: !(Maybe Duration),
startPeriod :: !(Maybe Duration),
startInterval :: !(Maybe Duration),
retries :: !(Maybe Retries)
}
deriving (Show, Eq, Ord, Functor)
Expand Down
22 changes: 16 additions & 6 deletions test/Language/Docker/ParseHealthcheckSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,55 @@ spec = do
"HEALTHCHECK --interval=5m \\\nCMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" (Just 300) Nothing Nothing Nothing
CheckArgs "curl -f http://localhost/" (Just 300) Nothing Nothing Nothing Nothing
]
it "parse healthcheck with retries" $
assertAst
"HEALTHCHECK --retries=10 CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" Nothing Nothing Nothing (Just $ Retries 10)
CheckArgs "curl -f http://localhost/" Nothing Nothing Nothing Nothing (Just $ Retries 10)
]
it "parse healthcheck with timeout" $
assertAst
"HEALTHCHECK --timeout=10s CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" Nothing (Just 10) Nothing Nothing
CheckArgs "curl -f http://localhost/" Nothing (Just 10) Nothing Nothing Nothing
]
it "parse healthcheck with start-period" $
assertAst
"HEALTHCHECK --start-period=2m CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" Nothing Nothing (Just 120) Nothing
CheckArgs "curl -f http://localhost/" Nothing Nothing (Just 120) Nothing Nothing
]
it "parse healthcheck with start-interval" $
assertAst
"HEALTHCHECK --start-interval=4m CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" Nothing Nothing Nothing (Just 240) Nothing
]
it "parse healthcheck with all flags" $
assertAst
"HEALTHCHECK --start-period=2s --timeout=1m --retries=3 --interval=5s CMD curl -f http://localhost/"
"HEALTHCHECK --start-period=2s --start-interval=10s --timeout=1m --retries=3 --interval=5s CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs
"curl -f http://localhost/"
(Just 5)
(Just 60)
(Just 2)
(Just 10)
(Just $ Retries 3)
]
it "parse healthcheck with no flags" $
assertAst
"HEALTHCHECK CMD curl -f http://localhost/"
[ Healthcheck $
Check $
CheckArgs "curl -f http://localhost/" Nothing Nothing Nothing Nothing
CheckArgs "curl -f http://localhost/" Nothing Nothing Nothing Nothing Nothing
]

it "fractional arguments to flags" $
Expand All @@ -66,6 +74,7 @@ spec = do
" --interval=0.5s \\",
" --timeout=0.1s \\",
" --start-period=0.2s \\",
" --start-interval=0.5s \\",
" CMD curl -f http://localhost"
]
in assertAst
Expand All @@ -77,5 +86,6 @@ spec = do
( Just 0.5 )
( Just 0.10000000149 )
( Just 0.20000000298 )
( Just 0.5 )
Nothing
]