DEV Community

Galina Melnik
Galina Melnik

Posted on


Add existing autotests to existing GitLab CI/CD

Hello. Our command has java-autotests in TeamCity for the C# project. It's our heritage, because our testers have java-background and they prefer to use java for their purposes.
The company standart was changed some time ago. We begin to use GitLab CI/CD instead of TeamCity. I have a task to transfer java-tests from TeamCity to GitLab.
The main idea of maven autotests was to deploy c# project into kubernetes and after that tried to call java-autotests using maven outside of deployed project.
I've added stages after deploy to existed .gitlab-ci.yaml. I don't describe build and deploy stages.

I should mention that "<<: *dp-auth-docker" is a common template that we use to auth in corporative instance of docker.

My collegue found that k8s sometimes scaled down replicas to count equaled 0. That is why the first added stage is called scale-replicas:

  <<: *dp-auth-docker
  stage: scale-replicas
    - theBranch=$(echo $CI_COMMIT_REF_NAME | tr '[:upper:]' '[:lower:]')
    - |
      echo 'Check replicas. Will be set value 1.'
      docker run --rm $CORPORATIVE_DOCKER_IMAGE_K8S kubectl scale --replicas=1 deployments/$theBranch
    - merge_requests
Enter fullscreen mode Exit fullscreen mode
  • $CI_COMMIT_REF_NAME is a predefined GitLab variable (documentation)

  • theBranch is a local stage variable, which contains a name of a branch. We always use number of a task as the branch name. I should convert the branch name to lower case because k8s deployment names are case sensitive.

  • --rm flag define clean up container (documentation)

Time to scale a replica could be significant. That is why it's needed to delay between scale the replica and tests.

  stage: sleep
    - sleep 120
    - merge_requests
Enter fullscreen mode Exit fullscreen mode
  • GitLab measures time in seconds. 2 minutes delay is enough to scale a replica in the system.

Next step is running tests.

  <<: *dp-auth-docker
  stage: run-tests
    - docker run --rm $CORPORATIVE_DOCKER_IMAGE_DIND dockerd & >/dev/null
    - |
      while [docker run --rm $CORPORATIVE_DOCKER_IMAGE_DIND ! -S /var/run/docker.sock ];
          echo "waiting";
          sleep 1;
    - pathKube="./.kube"
    - mkdir $pathKube
    - echo -e
    - |
      config=$(docker run \
        --rm -t \
        -p 7000:7000 \
        cat /root/.kube/config)
    - file="$pathKube/config"
    - echo "$config">$file
    - echo -e
    - |
      if test -f "$file"; then
          echo "New config was saved to: $file"
          echo"New config was not saved"
          exit -1
    - docker run --rm --volume $(pwd):/tests $CORPORATIVE_DOCKER_IMAGE_NEPTUNE mvn antrun:run -P !QA,featureBranch -f /tests/pom.xml -Dkubectl.config=/tests/.kube/config -Dfeature.branch.number=$CI_COMMIT_REF_NAME clean test -Dsuite=$SUITE_NAME
      - .m2/repository
    - merge_requests
Enter fullscreen mode Exit fullscreen mode
  • dockerd runs daemon in docker (documentaion)

  • while an unix socket creates and openes, script waits.

  • copy /.kube/config from k8s image to local. I need it to run tests from k8s and to forward ports for MongoDB.

  • maven use cache in directory .m2/repository

Top comments (0)

An Animated Guide to Node.js Event Loop

Node.js doesn’t stop from running other operations because of Libuv, a C++ library responsible for the event loop and asynchronously handling tasks such as network requests, DNS resolution, file system operations, data encryption, etc.

What happens under the hood when Node.js works on tasks such as database queries? We will explore it by following this piece of code step by step.