Blame view

keycloak_test.groovy 2.49 KB
866567c3c   George Vlahavas   Add keycloak fail...
1
2
3
4
5
6
7
8
9
10
11
12
  /**
  * Jenkinsfile to deploy multiple Docker containers based on docker-compose into a DEV server and run any test.
  * This pipeline will run the Docker containers, execute the tests and then stop and remove the containers from the DEV  
  * server automatically.
  **/
  
  
  pipeline {
  
    agent any
  
      environment {
866567c3c   George Vlahavas   Add keycloak fail...
13
        
866567c3c   George Vlahavas   Add keycloak fail...
14
        APP_URL = "https://kea.curex-project.eu"
3d2811c1f   George Vlahavas   Remove deployment
15
16
17
        KEYCLOAK_URL = "https://keycloak-curex.gnubila.fr/auth/realms/Integration/protocol/openid-connect/token"
        USER = credentials('keycloak-test-user')
        PASSWORD = credentials('keycloak-test-password')
866567c3c   George Vlahavas   Add keycloak fail...
18

866567c3c   George Vlahavas   Add keycloak fail...
19
20
21
22
      }
  
    stages {
             
866567c3c   George Vlahavas   Add keycloak fail...
23
24
25
26
27
28
29
      stage('Run tests') {
        steps {
          script {
            echo '*************'
            echo '*** TESTS ***'
            echo '*************'
            /* Here do your tests */
866567c3c   George Vlahavas   Add keycloak fail...
30
31
32
33
34
35
36
            try {
              String testName = "KEA_keycloak_fail"
              String url = "$APP_URL/api/v1/od/status"
              String responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
              if ( responseCode != '401' ) {
                error("$testName: Returned status code = $responseCode when calling $url")
              }
9dd323112   George Vlahavas   Add succesfull ke...
37
38
  
              testName = "KEA_keycloak_success"
3d2811c1f   George Vlahavas   Remove deployment
39
              String token = sh(label: "KEA_keycloak_token", script: "curl -s -X POST $KEYCLOAK_URL -H 'Content-Type: application/x-www-form-urlencoded' -d 'username=$USER' -d 'password=$PASSWORD' -d 'grant_type=password'  -d 'client_id=KEA' | sed 's/.*access_token\":\"//g' | sed 's/\".*//g'", returnStdout: true)
9dd323112   George Vlahavas   Add succesfull ke...
40
41
42
43
              responseCode = sh(label: testName, script: "curl -m 10 -sLI -w '%{http_code}' -H 'Authorization: Bearer $token' -H 'Accept: application/json, text/plain, */*' $url -o /dev/null", returnStdout: true)
              if ( responseCode != '401' ) {
                error("$testName: Returned status code = $responseCode when calling $url")
              }
971ec8495   George Vlahavas   Add catch statement
44
45
46
            } catch (ignored) {
              currentBuild.result = 'FAILURE'
              echo "KEA Keycloak Tests failed"
866567c3c   George Vlahavas   Add keycloak fail...
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
            }
          }
        }
      }
  
      stage('Stop and remove the Docker containers in DEV server') {
        steps {
          script {
            docker.withServer("$DEPLOYMENT_HOST", "$DEPLOYMENT_HOST_CREDENTIALS") {
              docker.withRegistry("$PRIVATE_REGISTRY" , 'artifactory') {
  
                //echo 'Stop and remove the specified Docker containers from the DEV server'
                sh 'sh delete_hetzner.sh'
              }
            }
          }
        }
      }
    }
  }