Skip to content
Software Engineering Stories
Go back

Server Testing CI

Pre-requisite, setup, and tests

Jenkins Build Page showcasing quest completion

You’ve been given the quest to automate the process of maintaining a web server’s configurations.

Redirecting www to non-www pages, http to https pages, and a simple health check.

Completing this quest, rewards you with the automated testing process notifying you when the web server configurations misbehave.

You decide to complete this quest by setting up a Jenkins Pipeline. It will run the server tests, and notify you via emails.

Pre-requisites

Nginx, Jenkins, Docker, Jenkins SSH Credentials, Jenkins Secret text for webhook

Setup Pipeline

You login to your Jenkins server. Create a new item, name it, and select Pipeline type.

[ Jenkins -> New Item -> Pipeline & Name -> OK ]

You now find yourself on the configuration page.

Server Tests

Codeception is one of the most popular PHP testing Framework. It provides the ability to test web server configurations.

Since you want to test three configurations, you create three codecept test suites.

For example one of these tests verifies http://jenkins.rdok.dev redirects to https://jenkins.rdok.dev/

public function nonWwwHttpRedirectsToNonWwwHttps(Rdok_devTester $I)
{
    $I->wantToTest('non-www http redirects to non-www https.');
    $I->sendGET('http://jenkins.rdok.dev');
    $I->seeResponseCodeIsRedirection();
    $I->seeHttpHeader('Location', 'https://jenkins.rdok.dev/');
}

Then you create the Jenkinsfile that will execute these three testing suites.

Jenkinsfile

pipeline {
    environment {
        AUTHOR_EMAIL = """${sh(
            returnStdout: true,
            script: 'git show -s --format="%ae" HEAD | sed "s/^ *//;s/ *$//"'
        )}"""
    }
    agent {
        docker {
            image 'codeception/codeception:2.5.2'
            args '''
                --volume="$PWD:/app"
                --workdir=/app
                --entrypoint=''
            '''
        }
    }
    stages {
        stage('code-quests.rdok.dev') {
            steps {
                sh 'codecept run codequests_rdok_dev --no-colors'
            }
        }
        stage('rdok.dev') {
            steps {
                sh 'codecept run rdok_dev --no-colors'
            }
        }
        stage('jenkins.rdok.dev') {
            steps {
                sh 'codecept run jenkins_rdok_dev --no-colors'
            }
        }
    }
    post {
        failure {
            mail (
                to: "${AUTHOR_EMAIL}",
                subject: "${BUILD_TAG} - Failure",
                body: "Job Url: ${env.JOB_URL}"
            )
        }
        success {
            mail (
                to: "${AUTHOR_EMAIL}",
                subject: "${BUILD_TAG} - Stable",
                body: "Job Url: ${env.JOB_URL}"
            )
        }
    }
}

Finally, you instruct Jenkinsfile to notify the author of the latest commit with the status of the tests. Initially you planned to use a local mail server, however with the Gmail being quite picky, and blocking the mails, you decided to use Mailgun smtp instead.

Summary

In this quest, you managed to setup a Jenkins Pipeline that:


Share this post:

Previous Post
Setup Jenkins Pipeline
Next Post
How to find the least privileges for AWS IAM policies