Ionic e2e CI tests in jenkins - jenkins

I want to achieve similar functionality to How to run ionic in the background to run e2e tests of my Ionic2 application in Jenkins. The e2e tests are created with protractor.
Option 1) running in the background did not work. As suggested I tried screen / tmux like:
stage 'e2e testing'
sh 'tmux new-session -d -s ionicServe'
sh 'tmux new-session -d -s e2e'
sh 'tmux send-keys -t ionicServe "ionic serve --nobrowser --nolivereload localhost" C-m'
sh 'tmux send-keys -t e2e "npm run e2ej" C-m'
sh 'tmux attach -t e2e'
which works great locally / in a docker container but does not work when Jenkins is executing the test cases.
Do you have any suggestions how to either get it to work in Jenkins with tmux or to have ionic serve the app without the CLI (plain gulp tasks)
Hint: a gulp serve:before does not seem to start the development server.

This (https://github.com/lathonez/clicker/blob/master/.travis.yml) is a fix. But does not seem to be as neatly integrated into Ionic CLI
cd www && python -m SimpleHTTPServer 8100 >> python_serve.log 2>&1 &

Related

How i can run sh script inside docker-compose file in background?

I have multiple scripts trying to run inside a docker-compose file, these scripts are initiating services that will produce ongoing logs, is there any way that I can run these commands explicitly in the background so docker-compose can execute all commands instead of stucking with the first one
command:
sh -c './root/clone_repo.sh &&
appium -p 4723 &&
./root/start_emu.sh'
You can run a command in a background process using a single ampersand & after the command. See this comment discussing how to run multiple commands in the background using subshells.
You can use something like this to run the first two commands in the background and continue onto the final command.
command:
sh -c '(./root/clone_repo.sh &) &&
(appium -p 4723 &) &&
./root/start_emu.sh'

Why e2e database tests failing within CI but not locally?

I've got pipelines for dev, staging and production.
The staging pipeline is where I've got the issue. The pipeline builds just fine on dev (on and off the CI runner) but staging code builds only locally and on live server but will fail
in the CI runner. I indicated suspecting code with <--.
I've checked whether the database container is running at the time of testing and it is up and running. Logs show nothing unusual.
Cypress tests fail on tests where interaction with the database is being tested:
test-ci.sh:
#!/bin/bash
env=$1
fails=""
inspect() {
if [ $1 -ne 0 ]; then
fails="${fails} $2"
fi
}
# run server-side tests
dev() {
docker-compose up -d --build
docker-compose exec -T users python manage.py recreate_db
docker-compose exec -T users python manage.py test
inspect $? users
docker-compose exec -T client npm test -- --coverage --watchAll --watchAll=false
inspect $? client
docker-compose down
}
# run e2e tests
e2e() {
if [ "${env}" = "staging" ]; then
docker-compose -f docker-compose-stage.yml up -d --build
docker-compose -f docker-compose-stage.yml exec -T users python manage.py recreate_db # <--
docker run -e REACT_APP_USERS_SERVICE_URL=$REACT_APP_USERS_SERVICE_URL -v $PWD:/e2e -w /e2e -e CYPRESS_VIDEO=$CYPRESS_VIDEO --network flaskondocker_default cypress/included:6.0.0 --config baseUrl=http://nginx
inspect $? e2e
docker-compose -f docker-compose-stage.yml down
else
docker-compose -f docker-compose-prod.yml up -d --build
docker-compose -f docker-compose-prod.yml exec -T users python manage.py recreate_db
docker run -e REACT_APP_USERS_SERVICE_URL=$REACT_APP_USERS_SERVICE_URL -v $PWD:/e2e -w /e2e -e CYPRESS_VIDEO=$CYPRESS_VIDEO --network flaskondocker_default cypress/included:6.0.0 --config baseUrl=http://nginx
inspect $? e2e
docker-compose -f docker-compose-prod.yml down
fi
}
# run specific tests
if [ "${env}" = "staging" ]; then
echo "****************************************"
echo "Running e2e tests ..."
echo "****************************************"
e2e
elif [ "${env}" = "production" ]; then
echo "****************************************"
echo "Running e2e tests ..."
echo "****************************************"
e2e
else
echo "****************************************"
echo "Running client and server-side tests ..."
echo "****************************************"
dev
fi
if [ -n "${fails}" ]; then
echo "Test failed: ${fails}"
exit 1
else
echo "Tests passed!"
exit 0
fi
The tests are behaving like docker-compose -f docker-compose-stage.yml exec -T users python manage.py recreate_db failed or hasn't been executed but logs show no errors.
gitlab-ci.yml file:
image: docker:stable
services:
- docker:19.03.12-dind
variables:
COMMIT: ${CI_COMMIT_SHORT_SHA}
MAIN_REPO: https://gitlab.com/coding_hedgehog/flaskondocker.git
USERS: training-users
USERS_REPO: ${MAIN_REPO}#${CI_COMMIT_BRANCH}:services/users
USERS_DB: training-users-db
USERS_DB_REPO: ${MAIN_REPO}#${CI_COMMIT_BRANCH}:services/users-db
CLIENT: training-client
CLIENT_REPO: ${MAIN_REPO}#${CI_COMMIT_BRANCH}:services/client
SWAGGER: training-swagger
SWAGGER_REPO: ${MAIN_REPO}#${CI_COMMIT_BRANCH}:services/swagger
stages:
- build
- push
before_script:
- export REACT_APP_USERS_SERVICE_URL=http://127.0.0.1
- export CYPRESS_VIDEO=false
- export SECRET_KEY=pythonrocks
- export AWS_ACCOUNT_ID=nada
- export AWS_ACCESS_KEY_ID=nada
- export AWS_SECRET_ACCESS_KEY=nada
- apk add --no-cache py-pip python2-dev python3-dev libffi-dev openssl-dev gcc libc-dev make npm
- pip install docker-compose
- npm install
compile:
stage: build
script:
- docker pull cypress/included:6.0.0
- sh test-ci.sh $CI_COMMIT_BRANCH
deployment:
stage: push
script:
- sh ./docker-push.sh
when: on_success
Let me just emphasize that the tests are passing locally on my computer as well as on live server. The database-related e2e tests fail when ran headlessly within CI.
What debugging steps I can take knowing that no containers are crashing, logs show no errors, same code builds locally and runs OK live but fails in the CI ?
We have had some issues where database checks worked locally, but not in headless CI. We found out that it was because of datetime fields. The markup response in CI was different than locally. Thus, all assertions that checked dates failed. We fixed this by writing MySQL queries that format the datetime result. Then adjust the assertions in Cypress accordingly. Maybe your problem has to do with this issue.
SELECT DATE_FORMAT(columnname, "%d-%c-%Y") as columnname FROM table
So for further debugging, do you have any simple tests that run correctly in CI? Or does nothing work?

Run commands inside Docker container without mounting project directory

My Jenkins pipeline uses the docker-workflow plugin. It builds a Docker image and tags it app. The build step fetches some dependencies and bakes them into the image along with my app.
I want to run two commands inside a container based on that image. The command should be executed in the built environment, with access to the dependencies. I tried using Image.inside, but it seems to fail because inside mounts the project directory over the working directory (?) and so the dependencies aren't available.
docker.image("app").inside {
sh './run prepare-tests'
sh './run tests'
}
I tried using docker.script.withDockerContainer too, but the commands don't seem to run inside the container. The same seems to be true for Image.withRun. At least with that I could specify a command, but it seems that I'd have to run specify both commands in one statement. Also it seems that withRun doesn't fail the build if the command doesn't exit cleanly.
docker
.image("app")
.withRun('', 'bash -c "./app prepare-tests && ./app tests"') { container ->
sh "exit \$(docker wait ${container.id})"
}
Is there a way to use Image.inside without mounting the project directory? Or is there are more elegant way of doing this?
docker DSL, like docker.image().inside() {} etc will mount jenkins job workspace dir to container and make it as the WORKDIR which will overwrite the WORKDIR in Dockerfile.
You can verify that from jenkins console output .
1) CD workdir fristly
docker.image("app").inside {
sh '''
cd <WORKDIR of image specifyed in Dockerfile>
./run prepare-tests
./run tests
'''
}
2) Run container in sh , rather than via docker DSL
sh '''
docker run -i app bash -c "./run prepare-tests && ./run tests"
'''

Batch file is not getting completely executed.

We are trying to make a Jenkins job where we will deploy our Angular app to Tomcat's webapps folder.
Our Jenkins is running on CentOS and dev server is on Windows.
We executing the following shell script in Jenkins build step.
sshpass -p "password" ssh -o StrictHostKeyChecking=no username#host "D:\Ratikanta\temp.bat"
Our batch file content is following.
d: && cd Ratikanta && svn checkout http://192.168.1.5/svn/KSP/trunk/kspweb/ && cd kspweb && npm i && npm run build && move ksp D:\Ratikanta\Tomcat7\webapps
These are working fine when running the shell script(sshpass -p ...) from CentOS terminal but, when running from Jenkins job it is executing up to npm i. Other commands like npm run build && move ksp D:\Ratikanta\Tomcat7\webapps are not getting executed.
Please help. Did we miss something? Please suggest if there are any other way to achieve our goal.

How do I get ionic serve to work with Jenkins (trying to run end to end tests with protractor)

I've got a project written with Ionic and I'm trying to run the end to end tests (written with Protractor) on Jenkins. This is the script I'm using to run the tests:
#make sure ionic serve isn't running
kill -9 $(lsof -n -ti4TCP:8100)
./node_modules/protractor/bin/webdriver-manager update --ignore_ssl
echo starting ionic serve...
screen -d -m -L ionic serve --nolivereload --nobrowser --address localhost
echo waiting for ionic to start...
while ! curl http://localhost:8100 &>/dev/null; do :; done
echo ionic serve started
#run end to end tests
./node_modules/.bin/protractor test/e2e/protractor.config.js
echo stoping ionic...
kill -9 $(lsof -n -ti4TCP:8100)
echo stopped.
echo done
If I run this script on the command line it works fine. But if run through Jenkins, it gets to "waiting for ionic to start" and never finishes. I've seen this question, but running "ionic serve" with an ampersand doesn't work for me. It runs and immediately quits. Using screen is the only way I've been able to get it to work.
How can I get "ionic serve" to run in the background with Jenkins?

Resources