sparsick

sparsick /testcontainers-git

This project contains a Testcontainers implementation for a plain git server based on the Docker image rockstorm/git-server

33
7
GitHub
Public

Repository Statistics

Key metrics and engagement data

33
Stars
7
Forks
9
Open Issues
0
Releases
1.21
Engagement Rate
Default branch: main

Timeline

Repository has been active for 2 years, 2 months

Repository Created

Last Commit
Recently active

README.md

testcontainers-git

codecov Java CI with Maven Maven Central

This project contains a Testcontainers implementation for a plain git server based on the Docker image rockstorm/git-server (Github Project).

It sets up the git server with a ready to use repository with the default name testRepo. The repository name can be overwritten. It exists two flavours for the git server (exposed by SSH or by HTTP) The port is set by testcontainers' mechanism.

Add me as Dependency

Maven:

xml
1<dependencyManagement>
2 <dependencies>
3 <dependency>
4 <groupId>io.github.sparsick.testcontainers.gitserver</groupId>
5 <artifactId>testcontainers-git-bom</artifactId>
6 <version>0.12.0</version>
7 <scope>test</scope>
8 </dependency>
9 </dependencies>
10</dependencyManagement>
11
12<dependencies>
13 <dependency>
14 <groupId>io.github.sparsick.testcontainers.gitserver</groupId>
15 <artifactId>testcontainers-gitserver</artifactId>
16 <scope>test</scope>
17 </dependency>
18</dependencies>

Gradle:

groovy
1dependencyManagement {
2 imports {
3 mavenBom("io.github.sparsick.testcontainers.gitserver:testcontainers-git-bom:0.12.0")
4 }
5}
6
7
8dependencies {
9 testImplementation 'io.github.sparsick.testcontainers.gitserver:testcontainers-gitserver'
10}

Getting started with a sample

The following samples show how to use the git server container in a JUnit 5 test. Currently, there exists two flavour:

  • git server via ssh (GitServerContainer)
  • git server via http (GitHttpServerContainer)

Git Server via SSH

The following sample shows how to use the git server container via SSH in a JUnit 5 test:

java
1import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
2import com.github.sparsick.testcontainers.gitserver.plain.GitServerContainer;
3import com.github.sparsick.testcontainers.gitserver.plain.SshHostKey;
4import com.github.sparsick.testcontainers.gitserver.plain.SshIdentity;
5
6@Testcontainers
7public class GitServerContainerUsedInJUnit5Test {
8
9 @Container
10 private GitServerContainer containerUnderTest =
11 new GitServerContainer(GitServerVersions.V2_43.getDockerImageName())
12 .withGitRepo("testRepo") // overwrite the default git repository name
13 .withGitPassword("12345") // overwrite the default git password
14 .withSshKeyAuth() // enabled public key authentication
15 .withCopyExistingGitRepoToContainer("src/test/resources/sampleRepo"); // path to an already existing Git repository
16
17 @Test
18 void checkInteractWithTheContainer() {
19 URI gitRepoURI = containerUnderTest.getGitRepoURIAsSSH();
20 String gitPassword = containerUnderTest.getGitPassword();
21
22 SshIdentity sshIdentity = containerUnderTest.getSshClientIdentity();
23 byte[] privateKey = sshIdentity.getPrivateKey();
24 byte[] publicKey = sshIdentity.getPublicKey();
25 byte[] passphrase = sshIdentity.getPassphrase();
26
27 SshHostKey hostKey = containerUnderTest.getHostKey();
28 String host = hostKey.getHostname();
29 byte[] key = hostKey.getKey();
30
31 // check interaction
32
33 }
34}

Git Server via HTTP

The following sample shows how to use the git server container via HTTP without Basic Authentication in a JUnit 5 test:

java
1import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
2import com.github.sparsick.testcontainers.gitserver.http.GitHttpServerContainer;
3
4@Testcontainers
5public class GitHttpServerContainerUsedInJUnit5Test {
6
7 @Container
8 private GitHttpServerContainer containerUnderTest =
9 new GitHttpServerContainer(GitServerVersions.V2_43.getDockerImageName());
10
11 @Test
12 void checkInteractWithTheContainer() {
13 URI gitRepoURI = containerUnderTest.getGitRepoURIAsHttp();
14
15 // check interaction
16 }
17}

HTTP with Basic Authentication

The next sample shows how to use the git server container via HTTP with Basic Authentication in a JUnit 5 test:

java
1import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
2import com.github.sparsick.testcontainers.gitserver.http.BasicAuthenticationCredentials;
3import com.github.sparsick.testcontainers.gitserver.http.GitHttpServerContainer;
4
5@Testcontainers
6public class GitHttpServerContainerUsedInJUnit5Test {
7
8 @Container
9 private GitHttpServerContainer containerUnderTest =
10 new GitHttpServerContainer(GitServerVersions.V2_43.getDockerImageName(), new BasicAuthenticationCredentials("testuser", "testPassword"));
11
12 @Test
13 void checkInteractWithTheContainer() {
14 URI gitRepoURI = containerUnderTest.getGitRepoURIAsHttp();
15
16 BasicAuthenticationCredentials basicAuthCredentials = containerUnderTest.getBasicAuthCredentials();
17 String username = basicAuthCredentials.getUsername();
18 String password = basicAuthCredentials.getPassword();
19
20 // check interaction
21 }
22}

Enabling HTTP Proxy

Since 0.9.0 it is possible to configure HTTP proxy, programmatically.

java
1import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
2import com.github.sparsick.testcontainers.gitserver.http.GitHttpServerContainer;
3
4@Testcontainers
5public class GitHttpServerContainerUsedInJUnit5Test {
6
7 @Container
8 private GitHttpServerContainer containerUnderTest =
9 new GitHttpServerContainer(GitServerVersions.V2_43.getDockerImageName())
10 .withHttpProxySetting(new HttpProxySetting("http://proxy.example.com", "https://proxy.example.com", ""));
11
12 @Test
13 void hasHttpProxySetting() {
14 assertThat(containerUnderTest.hasHttpProxy()).isTrue();
15 // check interaction
16 }
17}

Migration Guide

Migration from 0.4.x to 0.5.x

In 0.5.x the package structure has changed. The package com.github.sparsick.testcontainers.gitserver is split in com.github.sparsick.testcontainers.gitserver.plain and com.github.sparsick.testcontainers.gitserver.http. Making this migration easier, an OpenRewrite recipe io.github.sparsick.testcontainers.gitserver.rewrite.recipe.SplitPackage is provided.

shell
1mvn -U org.openrewrite.maven:rewrite-maven-plugin:run \
2 -Drewrite.recipeArtifactCoordinates=io.github.sparsick.testcontainers.gitserver:rewrite-testcontainers-gitserver:RELEASE \
3 -Drewrite.activeRecipes=io.github.sparsick.testcontainers.gitserver.rewrite.recipe.SplitPackage

License

MIT License