Skip to content

sparsick/testcontainers-git

Repository files navigation

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. As default this git repository would be exposed as a SSH URI. The port is set by testcontainers' mechanism. The access is via a password (Default: 12345, can also be overwritten) or SSH public key that is given by the container instance.

Add me as Dependency

Maven:

 <dependency>
    <groupId>io.github.sparsick.testcontainers.gitserver</groupId>
    <artifactId>testcontainers-gitserver</artifactId>
    <version>0.5.0</version>
    <scope>test</scope>
</dependency>

Gradle:

dependencies {
    testImplementation 'io.github.sparsick.testcontainers.gitserver:testcontainers-gitserver:0.5.0'
}

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:

import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
import com.github.sparsick.testcontainers.gitserver.plain.GitServerContainer;
import com.github.sparsick.testcontainers.gitserver.plain.SshHostKey;
import com.github.sparsick.testcontainers.gitserver.plain.SshIdentity;

@Testcontainers
public class GitServerContainerUsedInJUnit5Test {

    @Container
    private GitServerContainer containerUnderTest = 
            new GitServerContainer(GitServerVersions.V2_43.getDockerImageName())
                    .withGitRepo("testRepo") // overwrite the default git repository name
                    .withGitPassword("12345") // overwrite the default git password
                    .withSshKeyAuth() // enabled public key authentication
                    .withCopyExistingGitRepoToContainer("src/test/resources/sampleRepo"); // path to an already existing Git repository

    @Test
    void checkInteractWithTheContainer() {
        URI gitRepoURI = containerUnderTest.getGitRepoURIAsSSH(); 
        String gitPassword = containerUnderTest.getGitPassword();

        SshIdentity sshIdentity = containerUnderTest.getSshClientIdentity();
        byte[] privateKey = sshIdentity.getPrivateKey();
        byte[] publicKey = sshIdentity.getPublicKey();
        byte[] passphrase = sshIdentity.getPassphrase();
        
        SshHostKey hostKey = containerUnderTest.getHostKey();
        String host = hostKey.getHostname();
        byte[] key = hostKey.getKey();
        
        // check interaction

    }
}

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:

import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
import com.github.sparsick.testcontainers.gitserver.http.GitHttpServerContainer;

@Testcontainers
public class GitHttpServerContainerUsedInJUnit5Test {

    @Container
    private GitHttpServerContainer containerUnderTest =
            new GitHttpServerContainer(GitServerVersions.V2_43.getDockerImageName());

    @Test
    void checkInteractWithTheContainer() {
        URI gitRepoURI = containerUnderTest.getGitRepoURIAsHttp();

        // check interaction
    }
}

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

import com.github.sparsick.testcontainers.gitserver.GitServerVersions;
import com.github.sparsick.testcontainers.gitserver.http.BasicAuthenticationCredentials;
import com.github.sparsick.testcontainers.gitserver.http.GitHttpServerContainer;

@Testcontainers
public class GitHttpServerContainerUsedInJUnit5Test {

    @Container
    private GitHttpServerContainer containerUnderTest =
            new GitHttpServerContainer(GitServerVersions.V2_43.getDockerImageName(), new BasicAuthenticationCredentials("testuser", "testPassword"));

    @Test
    void checkInteractWithTheContainer() {
        URI gitRepoURI = containerUnderTest.getGitRepoURIAsHttp();

        BasicAuthenticationCredentials basicAuthCredentials = containerUnderTest.getBasicAuthCredentials();
        String username = basicAuthCredentials.getUsername();
        String password = basicAuthCredentials.getPassword();

        // check interaction
    }
}

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.

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

License

MIT License

About

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

Topics

Resources

License

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •