A peer-to-peer stack for code collaboration
Proper containerization for e2e tests
Julian Arnief4f38a1fce2
diff --git a/README.md b/README.mdindex d938e5c..3af2453 100644--- a/README.md+++ b/README.md@@ -82,14 +82,19 @@ into version control.The end-to-end test suite is run with- RAD_IPFS_API_URL=http://localhost:19301 stack test :e2e+ RAD_IPFS_API_URL=http://localhost:19301 \+ RAD_BIN="$(stack path --docker --local-install-root)/bin" stack test :e2e-It requires you to start up an IPFS test network and the Radicle daemon.+It requires you to first start up an IPFS test network and the Radicle daemon.docker-compose -f test/docker-compose.yaml up -d ipfs-test-networkRAD_IPFS_API_URL=http://localhost:19301 stack exec -- \rad-daemon-radicle --machine-config /tmp/radicle-machines.json+And to build the project with stack's docker support:++ stack build --docker+If you use `docker-compose up` for the first time you will also need toinitialize the IPFS test network withdiff --git a/cloudbuild.yaml b/cloudbuild.yamlindex 1997a8c..ab46cf7 100644--- a/cloudbuild.yaml+++ b/cloudbuild.yaml@@ -187,22 +187,24 @@ steps:set -euxo pipefailexport COMPOSE_FILE=test/docker-compose.yaml+ export RAD_BIN="$(stack path --local-install-root)/bin"+ ls "$(stack path --local-install-root)/bin"docker-compose up -dsleep 1 # Wait for containers to be started# Connect services to 'cloudbuild' network so they can be# accessed by other stepsdocker network connect cloudbuild test_ipfs-test-network_1 --alias ipfs-test-network+ docker network connect cloudbuild test_radicle-daemon1_1 --alias radicle-daemon1+ docker network connect cloudbuild test_radicle-daemon2_1 --alias radicle-daemon2docker exec test_ipfs-test-network_1 /app/wait-until-ready-+ sleep 10# Add the empty entry to the IPFS test network. Radicle requires itecho '{"radicle": true}' | docker exec -i test_ipfs-test-network_1 ipfs dag put --pinexport RAD_IPFS_API_URL=http://ipfs-test-network:5001export RADPATH="$(pwd)/rad"-- stack exec -- rad-daemon-radicle &-+ export IS_CI_NET="yes"stack test :e2e- id: "Package"diff --git a/nix/shell.nix b/nix/shell.nixindex f8e201d..b648b21 100644--- a/nix/shell.nix+++ b/nix/shell.nix@@ -28,6 +28,7 @@ stdenv.mkDerivation {export STACK_IN_NIX_EXTRA_ARGS="--system-ghc --nix-packages zlib"export IS_NIX_SHELL="true"export LOCALE_ARCHIVE="${pkgs.glibcLocales}/lib/locale/locale-archive";+ export RAD_BIN="$(stack path --docker --local-install-root)/bin"eval $(grep export ${ghc}/bin/ghc)alias check="pushd $PWD && ./scripts/check-fmt.sh && hlint . && popd"alias mkdocs="pushd $PWD/docs && make html && popd"diff --git a/package.yaml b/package.yamlindex 00539fa..935725a 100644--- a/package.yaml+++ b/package.yaml@@ -119,6 +119,7 @@ tests:dependencies:- process- directory+ - exceptions- filepath# Requires the 'git-remote-ipfs' executable- git-remote-ipfsdiff --git a/test/docker-compose.yaml b/test/docker-compose.yamlindex ed0aa1f..c855e48 100644--- a/test/docker-compose.yaml+++ b/test/docker-compose.yaml@@ -4,7 +4,35 @@ version: '3'services:ipfs-test-network:image: gcr.io/opensourcecoin/ipfs-test-network:010c506- ports:- - "19301:5001"command:- --enable-pubsub-experiment+ ports:+ - "19301:5001"+ radicle-daemon1:+ image: haskell:8.6.3+ ports:+ - "19302:8909"+ volumes:+ - ${RAD_BIN}:/usr/local/bin/+ environment:+ RAD_IPFS_API_URL: http://ipfs-test-network:5001+ command:+ - bash+ - -c+ - 'sleep 10 && rad-daemon-radicle --debug'+ depends_on:+ - ipfs-test-network+ radicle-daemon2:+ image: haskell:8.6.3+ ports:+ - "19303:8909"+ volumes:+ - ${RAD_BIN}:/usr/local/bin/+ environment:+ RAD_IPFS_API_URL: http://ipfs-test-network:5002+ command:+ - bash+ - -c+ - 'sleep 10 && rad-daemon-radicle --debug'+ depends_on:+ - ipfs-test-networkdiff --git a/test/e2e/CounterAppTest.hs b/test/e2e/CounterAppTest.hsindex c17498f..2a4798a 100644--- a/test/e2e/CounterAppTest.hs+++ b/test/e2e/CounterAppTest.hs@@ -11,8 +11,7 @@ import System.FilePathimport Test.E2ESupporttest_counter_app :: TestTree-test_counter_app = testCaseSteps "counter app" $ \step -> do- step "Create machine"+test_counter_app = testCaseSteps "counter app" $ \step -> using RadDaemon1 $ domachineId <- runTestCommand "rad-machines" ["create"]step "Initialize machine"@@ -24,7 +23,7 @@ test_counter_app = testCaseSteps "counter app" $ \step -> doforM_ [(1::Int)..3] $ \i -> dostep $ "Increment to " <> show i- valueInc <- runCounter [machineId, "increment"]+ valueInc <- using RadDaemon2 $ runCounter [machineId, "increment"]assertEqual "(increment) on counter chain" (show i) valueIncvalueGet <- runCounter [machineId, "get-value"]diff --git a/test/e2e/PatchAppTest.hs b/test/e2e/PatchAppTest.hsindex 052fb83..f8ac6e1 100644--- a/test/e2e/PatchAppTest.hs+++ b/test/e2e/PatchAppTest.hs@@ -15,25 +15,27 @@ import Test.E2ESupportinitProjectWithPatch :: TestM TextinitProjectWithPatch = doprepareRadicle- _ <- runTestCommand' "rad-project" ["init"] ["project-name", "project desc", "1"]+ _ <- using RadDaemon1 $ runTestCommand' "rad-project" ["init"] ["project-name", "project desc", "1"]_ <- runTestCommand "git" ["checkout", "-b", "f/test"]_ <- runTestCommand "touch" ["test"]_ <- runTestCommand "git" ["add", "test"]_ <- runTestCommand "git" ["commit", "--message", "first commit"]- runTestCommand "rad-patch" ["propose", "HEAD"]+ using RadDaemon2 $ runTestCommand "rad-patch" ["propose", "HEAD"]test_patch_propose :: TestTreetest_patch_propose = testCaseSteps "patch propose" $ \step -> dostep "init project with patch"initProjectWithPatch+ step "verify patch list"commitSha <- runTestCommand "git" ["rev-parse", "--short", "HEAD"]- listOutput <- runTestCommand "rad-patch" ["list"]+ listOutput <- using RadDaemon1 $ runTestCommand "rad-patch" ["list"]assertContains listOutput "state #"assertContains listOutput "pending 0"- showOutput <- runTestCommand "rad-patch" ["show", "0"]+ step "verify patch show"+ showOutput <- using RadDaemon1 $ runTestCommand "rad-patch" ["show", "0"]assertContains showOutput "pending 0"assertContains showOutput $ "From " <> commitShaassertContains showOutput "Comments\n---"@@ -45,7 +47,7 @@ test_patch_checkout = testCaseSteps "patch checkout" $ \step -> dostep "rad patch checkout"_ <- runTestCommand "git" ["checkout", "master"]- _ <- runTestCommand "rad-patch" ["checkout", "0"]+ _ <- using RadDaemon1 $ runTestCommand "rad-patch" ["checkout", "0"]branchOutput <- runTestCommand "git" ["branch"]assertContains branchOutput "* patch/0"@@ -63,9 +65,9 @@ test_patch_comment = testCaseSteps "patch comment" $ \step -> doinitProjectWithPatchstep "rad patch comment"- _ <- runTestCommand "rad-patch" ["comment", "0", "Test commenting"]+ _ <- using RadDaemon1 $ runTestCommand "rad-patch" ["comment", "0", "Test commenting"]- showOutput <- runTestCommand "rad-patch" ["show", "0"]+ showOutput <- using RadDaemon1 $ runTestCommand "rad-patch" ["show", "0"]assertContains showOutput "Test commenting"test_patch_retract :: TestTree@@ -74,9 +76,9 @@ test_patch_retract = testCaseSteps "patch retract" $ \step -> doinitProjectWithPatchstep "rad patch retract"- _ <- runTestCommand "rad-patch" ["retract", "0"]+ _ <- using RadDaemon1 $ runTestCommand "rad-patch" ["retract", "0"]- showAcceptedOutput <- runTestCommand "rad-patch" ["show", "0"]+ showAcceptedOutput <- using RadDaemon1 $ runTestCommand "rad-patch" ["show", "0"]assertContains showAcceptedOutput "retracted 0"test_patch_accept :: TestTree@@ -85,14 +87,15 @@ test_patch_accept = testCaseSteps "patch accept" $ \step -> doinitProjectWithPatchstep "rad patch accept"- _ <- runTestCommand "git" ["checkout", "master"]- _ <- runTestCommand "rad-patch" ["accept", "0"]+ using RadDaemon1 $ do+ _ <- runTestCommand "git" ["checkout", "master"]+ _ <- runTestCommand "rad-patch" ["accept", "0"]- showAcceptedOutput <- runTestCommand "rad-patch" ["show", "0"]- assertContains showAcceptedOutput "accepted 0"+ showAcceptedOutput <- runTestCommand "rad-patch" ["show", "0"]+ assertContains showAcceptedOutput "accepted 0"- logOutput <- runTestCommand "git" ["log", "master"]- assertContains logOutput "first commit"+ logOutput <- runTestCommand "git" ["log", "master"]+ assertContains logOutput "first commit"test_patch_unauthorized :: TestTreetest_patch_unauthorized = testCaseSteps "patch unauthorized actions" $ \step -> do@@ -101,13 +104,14 @@ test_patch_unauthorized = testCaseSteps "patch unauthorized actions" $ \step ->_ <- runTestCommand "git" ["checkout", "master"]step "rad patch reject fails (contributer)"- _ <- runTestCommand "rad-key" ["create", "--force"]- rejectOutput <- runTestCommandForError "rad-patch" ["reject", "0"]- assertContains rejectOutput "Are you authorized to do so?"+ using RadDaemon1 $ do+ _ <- runTestCommand "rad-key" ["create", "--force"]+ rejectOutput <- runTestCommandForError "rad-patch" ["reject", "0"]+ assertContains rejectOutput "Are you authorized to do so?"- step "rad patch accept fails (contributer)"- acceptOutput <- runTestCommandForError "rad-patch" ["accept", "0"]- assertContains acceptOutput "Are you authorized to do so?"+ step "rad patch accept fails (contributer)"+ acceptOutput <- runTestCommandForError "rad-patch" ["accept", "0"]+ assertContains acceptOutput "Are you authorized to do so?"- showOutput <- runTestCommand "rad-patch" ["show", "0"]- assertContains showOutput "pending 0"+ showOutput <- runTestCommand "rad-patch" ["show", "0"]+ assertContains showOutput "pending 0"diff --git a/test/e2e/ProjectAppTest.hs b/test/e2e/ProjectAppTest.hsindex 1449c99..6369b3b 100644--- a/test/e2e/ProjectAppTest.hs+++ b/test/e2e/ProjectAppTest.hs@@ -11,31 +11,32 @@ import UnliftIO.Directorytest_project_show_id :: TestTreetest_project_show_id = testCase "project show-id" $ do- prepareRadicle-- _ <- runTestCommand' "rad-project" ["init"] ["project-name", "project desc", "1"]- projectId <- runTestCommand "git" ["config", "--get" ,"radicle.project-id"]- showIdOut <- runTestCommand' "rad-project" ["show-id"] []- assertContains showIdOut projectId+ _ <- prepareRadicle+ using RadDaemon1 $ do+ _ <- runTestCommand' "rad-project" ["init"] ["project-name", "project desc", "1"]+ projectId <- runTestCommand "git" ["config", "--get" ,"radicle.project-id"]+ showIdOut <- runTestCommand' "rad-project" ["show-id"] []+ assertContains showIdOut projectIdtest_project_checkout :: TestTreetest_project_checkout = testCase "project checkout" $ doprepareRadiclecreateDirectory "origin"- projectId <- withCurrentDirectory "origin" $ do+ projectId <- withCurrentDirectory "origin" $ using RadDaemon1 $ do_ <- runTestCommand' "rad-project" ["init"] ["project-name", "project desc", "1"]runTestCommand "git" ["config", "--get" ,"radicle.project-id"]- runTestCommand "rad-project" ["checkout", projectId]- withCurrentDirectory "project-name" $ do- showIdOut <- runTestCommand' "rad-project" ["show-id"] []- assertContains showIdOut projectId+ using RadDaemon2 $ do+ _ <- runTestCommand "rad-project" ["checkout", projectId]+ withCurrentDirectory "project-name" $ do+ showIdOut <- runTestCommand' "rad-project" ["show-id"] []+ assertContains showIdOut projectIdtest_project_init_own_remote :: TestTreetest_project_init_own_remote = testCase "project init own remote" $ doprepareRadicle- _ <- runTestCommand' "rad-project" ["init"]+ _ <- using RadDaemon2 $ runTestCommand' "rad-project" ["init"][ "project-name", "project desc", "2"diff --git a/test/e2e/Test/E2ESupport.hs b/test/e2e/Test/E2ESupport.hsindex e76f696..ebef03c 100644--- a/test/e2e/Test/E2ESupport.hs+++ b/test/e2e/Test/E2ESupport.hs@@ -6,6 +6,8 @@module Test.E2ESupport( TestM, projectDir+ , using+ , RadDaemon(..), testCaseSteps, testCase@@ -24,7 +26,8 @@ module Test.E2ESupportimport Protolude hiding (bracket)-import Control.Exception.Safe+import Control.Exception.Safe ()+import Control.Monad.Catch (bracket)import qualified Data.Text as Timport System.Directoryimport System.Environment@@ -41,6 +44,9 @@ data TestEnv = TestEnv, projectDir :: FilePath}+data RadDaemon = RadDaemon1 | RadDaemon2+ deriving (Eq, Show, Read)+-- | Prepares 'TestEnv' by creating a temporary directory that serves as the-- home directory, then runs 'TestM' with the environment.--@@ -49,7 +55,8 @@ data TestEnv = TestEnvrunTestM :: TestM a -> IO arunTestM testM = doprojectDir <- getCurrentDirectory- withSystemTempDirectory "radicle-test" $ \dir ->+ setRadPath (projectDir </> "rad")+ withSystemTempDirectory "radicle-test1" $ \dir ->let env = TestEnv{ homeDir = dir, projectDir = projectDir@@ -72,6 +79,28 @@ runTestM testM = dosetSearchPath :: [FilePath] -> IO ()setSearchPath paths = setEnv "PATH" $ intercalate [searchPathSeparator] paths+ setRadPath :: FilePath -> IO ()+ setRadPath path = setEnv "RADPATH" path++-- | Run a command against a particular radicle daemon. Behaves like `local`,+-- so nesting a `using` will override for the computation.+--+-- Example:+-- > using RadDaemon1 $ runTestCommand ...+using :: RadDaemon -> TestM a -> TestM a+using rd act = do+ isCINet <- liftIO (lookupEnv "IS_CI_NET") >>= \x -> pure $ case x of+ Nothing -> False+ Just _ -> True+ bracket (liftIO $ lookupEnv apiPath >>= \old -> setEnv apiPath (envFor isCINet rd) >> pure old)+ (\old -> liftIO $ case old of+ Nothing -> unsetEnv apiPath+ Just o' -> setEnv apiPath o')+ (const act)+ where+ apiPath = "RAD_DAEMON_API_URL"+ envFor isCI RadDaemon1 = if isCI then "http://radicle-daemon1:8909" else "http://localhost:19302"+ envFor isCI RadDaemon2 = if isCI then "http://radicle-daemon2:8909" else "http://localhost:19303"-- * Lift test definitions and assertions to 'TestM'@@ -113,7 +142,7 @@ assertAbsence str substr =-- | Runs @rad key create@ and sets the Git user name and email.prepareRadicle :: TestM TextprepareRadicle = do- _ <- runTestCommand "rad-key" ["create"]+ _ <- using RadDaemon1 $ runTestCommand "rad-key" ["create"]_ <- runTestCommand "git" ["config", "--global", "user.name", "Alice"]runTestCommand "git" ["config", "--global", "user.email", "[email protected]"]@@ -176,8 +205,14 @@ executeCommand bin args inputLines = dolet argsString = map toS argsTestEnv {..} <- asksearchPath <- liftIO $ getEnv "PATH"- radIpfsApiUrl <- liftIO $ getEnv "RAD_IPFS_API_URL"- let env = [("PATH", searchPath), ("HOME", homeDir), ("RADPATH", projectDir </> "rad"), ("RAD_IPFS_API_URL", radIpfsApiUrl)]+ radDaemon <- liftIO $ fromMaybe "" <$> lookupEnv "RAD_DAEMON_API_URL"+ apiDaemon <- liftIO $ fromMaybe "" <$> lookupEnv "RAD_IPFS_API_URL"+ let env = [ ("PATH", searchPath)+ , ("HOME", homeDir)+ , ("RAD_DAEMON_API_URL", radDaemon)+ , ("RAD_IPFS_API_URL", apiDaemon)+ , ("RADPATH", projectDir </> "rad")+ ]let procSpec = (proc bin argsString) { env = Just env }let input = T.unpack $ T.intercalate "\n" inputLines(code, stout, sterr) <- liftIO $ readCreateProcessWithExitCode procSpec input