Skip to content

Commit 3dbce0c

Browse files
committed
Merged master into development branch
2 parents 72928ba + 5a778fc commit 3dbce0c

File tree

27 files changed

+1697
-124
lines changed

27 files changed

+1697
-124
lines changed

.gitignore

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ release.properties
2626
dependency-reduced-pom.xml
2727
buildNumber.properties
2828
.mvn/timing.properties
29-
30-
# IntelliJ
3129
.idea/
30+
gogs-webhook.iml
31+
32+
/src/test/docker/jenkins-dockerfile/gogs-webhook.hpi

Jenkinsfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
buildPlugin()

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@ Gogs-Webhook Plugin
22
===================
33

44
This plugin integrates [Gogs](https://gogs.io/) to Jenkins.<br>
5-
Download the plugin [here](https://github.com/sanderv32/gogs-webhook-plugin/raw/master/bin/gogs-webhook.hpi).<br>
65

76
In Gogs configure your webhook like this:<br>
87
http(s)://<< jenkins-server >>/gogs-webhook/?job=<< jobname >>
98

109
Example how your the webhook in Gogs should look like:
1110
![Example webhook](https://github.com/jenkinsci/gogs-webhook-plugin/raw/master/bin/gogs-webhook-screenshot.png)
1211

12+
### About integration tests
13+
14+
This project has some integration tests available. For more details see the [dedicated readme](about_integration_tests.md).

about_integration_tests.md

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
# About GOGS webhooks integration tests
2+
3+
## Introduction
4+
5+
End-to-end Webhook integration tests are available during the Maven `verify` phase.
6+
7+
For a detailed explanation of how it works refer to the [Integration Test Internals](#integration-test-internals) section.
8+
9+
The `verify` phase will start a local docker environment composed of a Jenkins and a Gogs server during the Maven `pre-integration-test` phase.
10+
The plugin under development as well as all necessary plugins are pre-loaded in the docker container. No security is set on the Jenkins server. The Gogs server
11+
is configured with a user called "butler" and password "butler". The credentials are pre-loaded
12+
in the Jenkins server.
13+
14+
The tests, running with Failsafe, are located in the `Webhook_IT.java` class.
15+
16+
A single smoke test is currently available.
17+
18+
The test environment is torn down during the Maven`post-integration-test` phase. Note that all traces of the docker environment are removed,
19+
in particular the data volumes used by both servers.
20+
21+
As the Integration tests are still experimental, they are grouped under a dedicated profile called `withIntegrationTest`.
22+
To compile the project with the integration tests execute `mvn clean install -P withIntegrationTest`.
23+
24+
To setup an environment in order to debug the integration tests, just use `mvn clean pre-integration-test -P withIntegrationTest`.
25+
The test can be run with with maven with `mvn failsafe:integration-test -P withIntegrationTest`. To tear down the docker environment execute
26+
`src/test/scripts/stop-test-environment.sh` from the project's root.
27+
28+
To connect to the servers when the environment is up, connect to `http://localhost:8080` for the Jenkins server (no password) and to
29+
`http://localhost:3000` for the Gogs server. The user is "butler" with password "butler".
30+
31+
### Warning
32+
33+
- does not check for port collision (ports used by an other application)
34+
- is not jenkins ready (see above)
35+
- Smoke test should be refactored to allow a re-use of test setup
36+
37+
### Pre-requisite
38+
39+
- Docker must be pre-installed on the machine running the tests.
40+
- The test machine must have Internet access (to download the Docker images and Jenkins dependencies).
41+
- A windows dev environment has not been tested (and will probably not work as it misses bash support).
42+
- Others I might not be aware of (sorry about that)...
43+
44+
(#integration-test-internals)
45+
## Integration test internals
46+
47+
The Integration test use a Docker environment that is started with a bash script. This bash script is integrated in Maven via the exec plugin.
48+
The reason for that choice is discussed in [following article](https://www.the-captains-shack.com/post/MavenAndDocker/).
49+
50+
### Environment setup
51+
52+
The environment needed to perform the Integration Tests is described and managed by a Docker-Compose file. It is located in the `src/test/docker` directory.
53+
In that same directory are the Dockerfiles that describe the two needed entities: the Jenkins server and the Gogs server.
54+
55+
Note that either for Jenkins and Gogs, the test is based on the LATEST version (as well as for the Jenkins plugins).
56+
57+
#### The Jenkins Docker Image
58+
59+
The Jenkins container image is built from the `jenkinsci/jenkins:latest` image. The necessary dependencies for the Gogs Webhook plugin under test are installed using the built-in `install-plugins.sh` script (git, workflow-aggregator, pipeline-model-extensions).
60+
The previously built plugin hpi is copied in the container so that it will automatically be loaded at startup.
61+
The password credential for Gogs is loaded in the Jenkins container with the `setup-gogs-user-credentials.groovy.override` groovy script (executed at startup).
62+
It is hardcoded in the script to user "butler" with password "butler".
63+
64+
The jenkins container is started via the Docker-compose file. With environment variables, Jenkins is configured to listen to a paricular port and not to stat the iinstall wizard.
65+
With the Docker port command, the Jenkins port is published so that it is available on machine level (for debugging for exemple).
66+
The current version of the Compose file will lead to port collision if several webhook integration tests are fired at the same time.
67+
68+
Note that the container is given a fixed name that can also lead to collisions.
69+
70+
For speed, the JENKINS_HOME is stored in an unamed data volume (that is destroyed at tear down).
71+
72+
#### The Gogs Docker Image
73+
74+
The Gogs container image is built on `gogs/gogs:0.11.4`. Note that this version is pinned as it is the Webhook (and underlaying Jenkins that is under test).
75+
76+
Several configuration scripts are loaded in the image for execution during build time.
77+
78+
* `app.ini` the static settings of the Gogs server
79+
* `setup-gogs.sh` the dynamic values or operations (ex setting up users or preloading a repository). It uses the REST API to perform these actions.
80+
* `repos-to-mirror` the path to the repository to preload in the Gogs container.
81+
82+
The setup script is than executed (after changing the base URL in the app.ini)
83+
84+
#### Starting the images with Docker-compose
85+
86+
The docker-compose and its parameters are controlled by the scripts located in `src/test/scripts`.
87+
88+
To start the environment, the script `start-test-environmnent.sh` first calls the docker-compose "build" command so that the images are recreated. The "--no-cache" parameter is to avoid the reuse of cached artifacts.
89+
The "--pull" parameter is to always attempt to pull a newer version of the image.
90+
91+
It then starts the environment with the classical "up -d" parameters (start as daemon) but also specifying the "--force-recreate" switch to avoid the reuse of an already existing container.
92+
93+
These precautions are taken to be sure that we always start with a clean and equivalent environment.
94+
95+
The environment is torn down with the `stop-test-environment.sh` script. It first stops all containers ("stop") and than removes the dead containers and associated data volumes.
96+
97+
Note: this automatic cleanup will destroy all logfiles available in the containers.
98+
99+
### Running the integration tests interactively
100+
101+
This is a use case where you want to understand why a test is failing and want to use your debugger on the Integration test.
102+
103+
As the Integration tests rely on a properly compiled plugin release candidate, the easiest is to run the Maven lifecycle up to the environment setup.
104+
This will take care of compiling it, running the unit test, copy the resulting compiled plugin (hpi) to the appropriate directory so that it gets properly included in the new Jenkins test image.
105+
To achieve this just use `mvn clean pre-integration-test -P withIntegrationTest`.
106+
The test can be run with the maven command `mvn failsafe:integration-test -P withIntegrationTest` or with your IDE.
107+
108+
To tear down the docker environment execute `src/test/scripts/stop-test-environment.sh` from the project's root.
109+
110+
To connect to the servers when the environment is up, connect to `http://localhost:8080` for the Jenkins server (no password) and to
111+
`http://localhost:3000` for the Gogs server. The user is "butler" with password "butler".
112+
113+
---
114+
115+
### Test workflow
116+
117+
This section describes how the smoke-test (happy case) is constructed. From this structure, other test cases can be built.
118+
119+
120+
### Step 1: Setting up the Gogs server
121+
122+
The first part is dealing with the setup of the Gogs server. For that, it uses a "handler" class that hides the complexity of the various REST commands.
123+
This class is instantiated with the Gogs URL, the User and Password to use to issue the command.
124+
The first command used is the waitForServer that will try for a minute, every 5 seconds, to establish a contact with the server. If that period expires without a contact, the test fails.
125+
126+
The "createEmptyRepo" method is then used to create a blank repository on the Gogs server.
127+
128+
We then create and configure a local test git repository under "`target/test-repos`" by using the java git API. The files for that repository are copied there during
129+
the Integration Test setup from the "`src/test/test-repos`" directory. This configuration encompasses the setup of the Origin remote server.
130+
131+
Note that the URL is relative to the machine launching the Integration tests. Thus localhost in most cases.
132+
133+
The files are added to the local git repository and then pushed to the Gogs server.
134+
Using this technique will help to setup a repository with branches to automate testing of complex multi-branch setups.
135+
136+
137+
### Step 2: Setting up the Jenkins server
138+
139+
After establishing the contact with the Jenkins server and waiting for it to be fully operational, we setup a test job.
140+
The Job definition XML is stored in the `src/test/resources/Jenkins-config` directory. Note that this is a very simple pipeline job defined in a Jenkinsfile.
141+
142+
We then check whether everything is properly working.
143+
Using the Jenkins java API, we get the next build number, launch the build and check that the resulting build number is the one we expected.
144+
145+
The build process created a marker file with the git reference of what it built. This marker file is archived during the build and
146+
retrieved to compare the local git commit reference with the one that has been built.
147+
148+
If these two validations succeed, this means that Gogs and Jenkins are ready to perform the actual hook test.
149+
150+
151+
### Step 3: Setting up and using the webhook
152+
153+
We then setup the hook in gogs by using the definitions stored in `src/test/resources/`. Note that the URL for Jenkins is Docker-compose defined one (`http://jenkins:8080/job/test%20project/build?delay=0`).
154+
155+
We are now ready to try if a new commit pushed to the Gogs repository will trigger a build in the correct job. For that, we store the next expected build number.
156+
157+
A file in the local git repository is modified, committed and pushed to the Gogs repository. The git commit ID is stored validate later that the correct commit was built.
158+
159+
We then wait for a preset time for the build to complete (the "last build number" should match the "next build number" before the commit to Gogs).
160+
161+
If completed successfully, we assert with the archived maker file that the git commit ids match as we did earlier.
162+
163+
This demonstrates that a push to the Gogs repository (master branch) correctly triggered a build of the expected commit.

0 commit comments

Comments
 (0)