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 an integration test for canton-repl #12772

Merged
merged 14 commits into from
Feb 8, 2022
8 changes: 8 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -885,3 +885,11 @@ nixpkgs_package(
nix_file_deps = common_nix_file_deps,
repositories = dev_env_nix_repos,
)

nixpkgs_package(
name = "script_nix",
attribute_path = "script",
nix_file = "//nix:bazel.nix",
nix_file_deps = common_nix_file_deps,
repositories = dev_env_nix_repos,
) if not is_windows else None
2 changes: 1 addition & 1 deletion daml-assistant/integration-tests/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ da_haskell_test(
"//compiler/damlc/tests:generate-simple-dalf",
"@mvn_dev_env//:mvn",
"@tar_dev_env//:tar",
] + ([] if is_windows else ts_libraries),
] + ([] if is_windows else ts_libraries + ["@script_nix//:bin/script"]),
# I’m sure the mvn stuff will be flaky.
flaky = True,
hackage_deps = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import qualified Data.ByteString.Lazy.Char8 as LBS8
import qualified Data.Conduit.Tar.Extra as Tar.Conduit.Extra
import Data.List.Extra
import Data.String (fromString)
import Data.Maybe (maybeToList)
import Data.Maybe (maybeToList, isJust)
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Vector as Vector
Expand Down Expand Up @@ -49,13 +49,22 @@ main = do
mvnPath <- locateRunfiles "mvn_dev_env/bin"
tarPath <- locateRunfiles "tar_dev_env/bin"
yarnPath <- takeDirectory <$> locateRunfiles (mainWorkspace </> yarn)
-- NOTE(Sofia): We don't use `script` on Windows.
sofiafaro-da marked this conversation as resolved.
Show resolved Hide resolved
mbScriptPath <- if isWindows
then pure Nothing
else Just <$> locateRunfiles "script_nix/bin"
-- NOTE: `COMSPEC` env. variable on Windows points to cmd.exe, which is required to be present
-- on the PATH as mvn.cmd executes cmd.exe
mbComSpec <- getEnv "COMSPEC"
let mbCmdDir = takeDirectory <$> mbComSpec
limitJvmMemory defaultJvmMemoryLimits
withArgs args (withEnv
[ ("PATH", Just $ intercalate [searchPathSeparator] $ (tarPath : javaPath : mvnPath : yarnPath : oldPath) ++ maybeToList mbCmdDir)
[ ("PATH", Just $ intercalate [searchPathSeparator] $ concat
[ [tarPath, javaPath, mvnPath, yarnPath]
, maybeToList mbScriptPath
, oldPath
, maybeToList mbCmdDir
])
, ("TASTY_NUM_THREADS", Just "1")
] $ defaultMain (tests tmpDir))

Expand Down Expand Up @@ -766,35 +775,70 @@ codegenTests codegenDir = testGroup "daml codegen" (

cantonTests :: TestTree
cantonTests = testGroup "daml sandbox"
[ testCaseSteps "Can start Canton sandbox and run script" $ \step -> withTempDir $ \dir -> do
step "Creating project"
callCommandSilentIn dir $ unwords ["daml new", "skeleton", "--template=skeleton"]
step "Building project"
callCommandSilentIn (dir </> "skeleton") "daml build"
step "Finding free ports"
ledgerApiPort <- getFreePort
adminApiPort <- getFreePort
domainPublicApiPort <- getFreePort
domainAdminApiPort <- getFreePort
step "Staring Canton sandbox"
let portFile = dir </> "canton-portfile.json"
withDamlServiceIn (dir </> "skeleton") "sandbox"
[ "--port", show ledgerApiPort
, "--admin-api-port", show adminApiPort
, "--domain-public-port", show domainPublicApiPort
, "--domain-admin-port", show domainAdminApiPort
, "--canton-port-file", portFile
] $ \ ph -> do
-- wait for port file to be written
_ <- readPortFileWith decodeCantonSandboxPort ph maxRetries portFile
step "Uploading DAR"
callCommandSilentIn (dir </> "skeleton") $ unwords
["daml ledger upload-dar --host=localhost --port=" <> show ledgerApiPort, ".daml/dist/skeleton-0.0.1.dar"]
step "Running script"
callCommandSilentIn (dir </> "skeleton") $ unwords
[ "daml script"
, "--dar", ".daml/dist/skeleton-0.0.1.dar"
, "--script-name Main:setup"
, "--ledger-host=localhost", "--ledger-port=" <> show ledgerApiPort
]
]
[ testCaseSteps "Can start Canton sandbox and run script" $ \step -> withTempDir $ \dir -> do
step "Creating project"
callCommandSilentIn dir $ unwords ["daml new", "skeleton", "--template=skeleton"]
step "Building project"
callCommandSilentIn (dir </> "skeleton") "daml build"
step "Finding free ports"
ledgerApiPort <- getFreePort
adminApiPort <- getFreePort
domainPublicApiPort <- getFreePort
domainAdminApiPort <- getFreePort
step "Staring Canton sandbox"
let portFile = dir </> "canton-portfile.json"
withDamlServiceIn (dir </> "skeleton") "sandbox"
[ "--port", show ledgerApiPort
, "--admin-api-port", show adminApiPort
, "--domain-public-port", show domainPublicApiPort
, "--domain-admin-port", show domainAdminApiPort
, "--canton-port-file", portFile
] $ \ ph -> do
-- wait for port file to be written
_ <- readPortFileWith decodeCantonSandboxPort ph maxRetries portFile
step "Uploading DAR"
callCommandSilentIn (dir </> "skeleton") $ unwords
["daml ledger upload-dar --host=localhost --port=" <> show ledgerApiPort, ".daml/dist/skeleton-0.0.1.dar"]
step "Running script"
callCommandSilentIn (dir </> "skeleton") $ unwords
[ "daml script"
, "--dar", ".daml/dist/skeleton-0.0.1.dar"
, "--script-name Main:setup"
, "--ledger-host=localhost", "--ledger-port=" <> show ledgerApiPort
]
step "Start canton-repl"
env <- getEnvironment
let cmd = unwords
[ "daml canton-repl"
, "--port", show ledgerApiPort
, "--admin-api-port", show adminApiPort
, "--domain-public-port", show domainPublicApiPort
, "--domain-admin-port", show domainAdminApiPort
]
-- NOTE (Sofia): We need to use `script` on Mac and Linux because of this Ammonite issue:
-- https://github.com/com-lihaoyi/Ammonite/issues/276
-- Also, script for Mac and script for Linux have incompatible CLIs for unfathomable reasons.
-- Also, we need to set TERM to something, otherwise tput complains and crashes Ammonite.
wrappedCmd
| isWindows = cmd
| isMac = "script -q -- tty.txt " <> cmd
| otherwise = concat ["script -q -c '", cmd, "'"]
input =
[ "sandbox.health.running"
, "local.health.running"
, "exit" -- This "exit" is necessary on Linux, otherwise the REPL expects more input.
-- script on Linux doesn't transmit the EOF/^D to the REPL, unlike on Mac.
]
env' | isWindows || isJust (lookup "TERM" env) = Nothing
| otherwise = Just (("TERM", "xterm-16color") : env)
proc' = (shell wrappedCmd) { cwd = Just dir, env = env' }
output <- readCreateProcess proc' (unlines input)
let outputLines = lines output
-- NOTE (Sofia): We use `isInfixOf` extensively because
-- the REPL output is full of color codes.
Just res0 <- pure (find (isInfixOf "res0") outputLines)
assertBool "sandbox participant is not running" ("true" `isInfixOf` res0)
Just res1 <- pure (find (isInfixOf "res1") outputLines)
assertBool "local domain is not running" ("true" `isInfixOf` res1)

]
1 change: 1 addition & 0 deletions nix/bazel.nix
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ let shared = rec {
propagatedBuildInputs = attrs.propagatedBuildInputs ++ [sphinx-copybutton];
});

script = pkgs.unixtools.script;
sysctl = pkgs.unixtools.sysctl;

# Custom combination of latex packages for our latex needs
Expand Down