image: docker:stable

services:
    - docker:stable-dind

workflow:
  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_COMMIT_BRANCH

build-and-test:
    script:
        - >
          if [ -n "$CI_MERGE_REQUEST_ID" ]; then
          MR_ARGS="--build-arg REPO_URL=$CI_MERGE_REQUEST_SOURCE_PROJECT_URL" ;
          MR_ARGS="$MR_ARGS --build-arg REPO_BRANCH=$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME" ;
          export MR_ARGS ;
          fi
        - mkdir Dockercontext
        - >
          docker build -t retroshare:testing $MR_ARGS
          --file $CI_PROJECT_DIR/build_scripts/GitlabCI/gitlabCI.Dockerfile
          Dockercontext
        - >
          docker run --name retroshare --detach --tty retroshare:testing
          retroshare-service --jsonApiPort 9092
        - apk add jq
        - >
          docker exec retroshare
          curl --verbose http://127.0.0.1:9092/rsJsonApi/version | jq
        - >
          docker exec retroshare
          curl --verbose http://127.0.0.1:9092/rsLoginHelper/getLocations  | jq
        - >
          docker exec retroshare
          curl --verbose --data
          '{ "location":{ "mLocationName":"Test 1", "mPgpName":"Test2" },
          "password":"Test 3", "caller_data":"Test 5" }'
          http://127.0.0.1:9092/rsLoginHelper/createLocation  | jq
        - >
          docker exec retroshare
          curl --verbose http://127.0.0.1:9092/rsLoginHelper/getLocations | jq
        - >
          docker exec retroshare
          curl --verbose http://127.0.0.1:9092/rsLoginHelper/isLoggedIn | jq
        - >
          docker exec retroshare
          curl --verbose http://127.0.0.1:9092/rsMsgs/getChatLobbyList | jq
        - docker container stop retroshare
