Add existing autotests to existing GitLab CI/CD
Tags: gitlab, maven
Published: 2022-03-17 06:22:21

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:

```
scale-replicas:
  <<: *dp-auth-docker
  stage: scale-replicas
  script:
    - 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
  only:
    - merge_requests
```

- $CI_COMMIT_REF_NAME is a predefined GitLab variable ([documentation](https://docs.gitlab.com/ee/ci/variables/predefined_variables.html))

- 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](https://docs.docker.com/engine/reference/run/#clean-up---rm))

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

```
sleep:
  stage: sleep
  script:
    - sleep 120
  only:
    - merge_requests
```

- GitLab measures time in seconds. 2 minutes delay is enough to scale a replica in the system.

Next step is running tests. 

```
run-tests:
  <<: *dp-auth-docker
  stage: run-tests
  script:
    - docker run --rm $CORPORATIVE_DOCKER_IMAGE_DIND dockerd & >/dev/null
    - |
      while [docker run --rm $CORPORATIVE_DOCKER_IMAGE_DIND ! -S /var/run/docker.sock ];
      do
          echo "waiting";
          sleep 1;
      done
    - pathKube="./.kube"
    - mkdir $pathKube
    - echo -e
    - |
      config=$(docker run \
        --rm -t \
        -p 7000:7000 \
        $CORPORATIVE_DOCKER_IMAGE_K8S \
        cat /root/.kube/config)
    - file="$pathKube/config"
    - echo "$config">$file
    - echo -e
    - |
      if test -f "$file"; then
          echo "New config was saved to: $file"
      else
          echo"New config was not saved"
          exit -1
      fi
    - 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
  cache:
    paths:
      - .m2/repository
  only:
    - merge_requests
```
 

- dockerd runs daemon in docker ([documentaion](https://docs.docker.com/engine/reference/commandline/dockerd/))

- 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