Support specifying sandbox/navigator/json-api/script options in daml.yaml (#4877)

changelog_begin

- [DAML Assistant] You can now specify options for
  Sandbox/Navigator/the HTTP JSON API in ``daml.yaml`` via
  ``sandbox-options``/``navigator-options``/``json-api-options``. These
  options will be picked up by running ``daml start``. This is
  particularly useful for specifying ``--wall-clock-time`` and for
  specifying a fixed ledger ID during development.

changelog_end

fixes #2993
This commit is contained in:
Moritz Kiefer 2020-03-06 14:52:45 +01:00 committed by GitHub
parent c7eb3f6b6c
commit d8afe7a4cc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 74 additions and 0 deletions

View File

@ -752,6 +752,15 @@ newtype NavigatorOptions = NavigatorOptions [String]
newtype JsonApiOptions = JsonApiOptions [String]
newtype ScriptOptions = ScriptOptions [String]
withOptsFromProjectConfig :: T.Text -> [String] -> ProjectConfig -> IO [String]
withOptsFromProjectConfig fieldName cliOpts projectConfig = do
optsYaml :: [String] <-
fmap (fromMaybe []) $
requiredE ("Failed to parse " <> fieldName) $
queryProjectConfig [fieldName] projectConfig
pure (optsYaml ++ cliOpts)
runStart
:: Maybe SandboxPort
-> StartNavigator
@ -785,6 +794,10 @@ runStart
mbInitScript :: Maybe String <-
requiredE "Failed to parse init-script" $
queryProjectConfig ["init-script"] projectConfig
sandboxOpts <- withOptsFromProjectConfig "sandbox-options" sandboxOpts projectConfig
navigatorOpts <- withOptsFromProjectConfig "navigator-options" navigatorOpts projectConfig
jsonApiOpts <- withOptsFromProjectConfig "json-api-options" jsonApiOpts projectConfig
scriptOpts <- withOptsFromProjectConfig "script-options" scriptOpts projectConfig
doBuild
let scenarioArgs = maybe [] (\scenario -> ["--scenario", scenario]) mbScenario
withSandbox sandboxPort (darPath : scenarioArgs ++ sandboxOpts) $ \sandboxPh -> do

View File

@ -255,6 +255,62 @@ packagingTests = testGroup "packaging"
_ -> assertFailure "Expected JSON object in response body"
-- waitForProcess' will block on Windows so we explicitly kill the process.
terminateProcess startPh
, testCase "sandbox-options is picked up" $ withTempDir $ \tmpDir -> do
let projDir = tmpDir </> "sandbox-options"
createDirectoryIfMissing True (projDir </> "daml")
writeFileUTF8 (projDir </> "daml.yaml") $ unlines
[ "sdk-version: " <> sdkVersion
, "name: sandbox-options"
, "version: \"1.0\""
, "source: daml"
, "sandbox-options:"
, " - --wall-clock-time"
, " - --ledgerid=MyLedger"
, "dependencies:"
, " - daml-prim"
, " - daml-stdlib"
]
writeFileUTF8 (projDir </> "daml/Main.daml") $ unlines
[ "daml 1.2"
, "module Main where"
, "template T with p : Party where signatory p"
]
sandboxPort :: Int <- fromIntegral <$> getFreePort
jsonApiPort :: Int <- fromIntegral <$> getFreePort
let startProc = shell $ unwords
[ "daml"
, "start"
, "--start-navigator=no"
, "--sandbox-port=" <> show sandboxPort
, "--json-api-port=" <> show jsonApiPort
]
withCurrentDirectory projDir $
withCreateProcess startProc $ \_ _ _ startPh ->
race_ (waitForProcess' startProc startPh) $ do
let token = JWT.encodeSigned (JWT.HMACSecret "secret") mempty mempty
{ JWT.unregisteredClaims = JWT.ClaimsMap $
Map.fromList [("https://daml.com/ledger-api", Aeson.Object $ HashMap.fromList [("actAs", Aeson.toJSON ["Alice" :: T.Text]), ("ledgerId", "MyLedger"), ("applicationId", "foobar")])]
}
let headers =
[ ("Authorization", "Bearer " <> T.encodeUtf8 token)
] :: RequestHeaders
waitForHttpServer (threadDelay 100000) ("http://localhost:" <> show jsonApiPort <> "/v1/query") headers
manager <- newManager defaultManagerSettings
initialRequest <- parseRequest $ "http://localhost:" <> show jsonApiPort <> "/v1/create"
let createRequest = initialRequest
{ method = "POST"
, requestHeaders = headers
, requestBody = RequestBodyLBS $ Aeson.encode $ Aeson.object
["templateId" Aeson..= Aeson.String "Main:T"
,"payload" Aeson..= [Aeson.String "Alice"]
]
}
createResponse <- httpLbs createRequest manager
-- If the ledger id or wall clock time is not picked up this would fail.
statusCode (responseStatus createResponse) @?= 200
-- waitForProcess' will block on Windows so we explicitly kill the process.
terminateProcess startPh
]
quickstartTests :: FilePath -> FilePath -> TestTree

View File

@ -133,6 +133,11 @@ Here is what each field means:
errors, there should be no reason to modify this.
- ``build-options``: a list of tokens that will be appended to some invocations of ``damlc`` (currently `build` and `ide`). Note that there is no further shell parsing applied.
- ``sandbox-options``: a list of options that will be passed to Sandbox in ``daml start``.
- ``navigator-options``: a list of options that will be passed to Navigator in ``daml start``.
- ``json-api-options``: a list of options that will be passed to the HTTP JSON API in ``daml start``.
- ``script-options``: a list of options that will be passed to the DAML script
runner when running the ``init-script`` as part of ``daml start``.
.. TODO (@robin-da) document the dependency syntax