Skip to content

Commit

Permalink
tests: Be more resilient against parallel runs
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Siewert <[email protected]>
  • Loading branch information
sinuscosinustan committed May 21, 2022
1 parent 006bb01 commit 457ce8b
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 31 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
---
name: Build
on: [ push, workflow_dispatch ]
on:
push:
branches:
- main
pull_request: {}
workflow_dispatch: {}

jobs:
build:
strategy:
matrix:
version: [11, 17]
runs-on: ubuntu-latest
concurrency: ci-${{ github.ref }}
steps:
- uses: actions/checkout@v3

Expand Down
10 changes: 8 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,17 @@ jobs:
passphrase: ${{ secrets.OSSRH_GPG_SECRET_PASS }}

- name: Deploy package
run: mvn --batch-mode -Dmaven.test.skip=true -Dgpg.passphrase=${{ secrets.OSSRH_GPG_SECRET_PASS }} clean deploy
run: mvn --batch-mode -Dmaven.test.skip=true -Dgpg.passphrase=${{ secrets.OSSRH_GPG_SECRET_PASS }} clean package deploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}

- name: Release to GitHub
uses: softprops/action-gh-release@v1
with:
files: |
target/hetznercloud-api*.jar
pages:
runs-on: ubuntu-latest
steps:
Expand All @@ -47,7 +53,7 @@ jobs:
distribution: 'adopt'

- name: Generate Javadocs
run: mvn javadoc:javadoc
run: mvn --batch-mode -Dgpg.skip=true javadoc:javadoc

- name: Deploy to gh-pages
uses: JamesIves/[email protected]
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
---
name: Test
on: [ push, workflow_dispatch ]
on:
workflow_dispatch: {}

jobs:
test:
strategy:
matrix:
version: [11, 17]
runs-on: ubuntu-latest
concurrency: ci-${{ github.ref }}
steps:
- uses: actions/checkout@v3

Expand Down
36 changes: 20 additions & 16 deletions src/test/java/me/tomsdevsn/hetznercloud/HetznerCloudAPITest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,31 @@
import org.springframework.test.context.junit.jupiter.SpringExtension;
import org.springframework.web.client.HttpClientErrorException;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.*;

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE, classes = App.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
public class HetznerCloudAPITest {

private static final Logger logger = LoggerFactory.getLogger(HetznerCloudAPITest.class);
private static final Logger LOGGER = LoggerFactory.getLogger(HetznerCloudAPITest.class);
private final List<CleanupObject> cleanupObjects = new ArrayList<>();

private final String testIdentifier = new Random().ints(48, 122)
.filter(i -> (i < 57 || i > 65) && (i < 90 || i > 97))
.mapToObj(i -> (char) i)
.limit(8)
.collect(StringBuilder::new, StringBuilder::append, StringBuilder::append)
.toString();

@Autowired
private HetznerCloudAPI hetznerCloudAPI;

@AfterAll
public void cleanUp() {
cleanupObjects.forEach(cleanupObject -> {
Object response = cleanup(cleanupObject);
logger.info("Cleanup " + cleanupObject + " response: " + response);
LOGGER.info("Cleanup " + cleanupObject + " response: " + response);
});
}

Expand All @@ -64,7 +69,6 @@ private Object cleanup(CleanupObject cleanupObject) {

@Test
void getServers() {
hetznerCloudAPI.getServers();
}

@Test
Expand Down Expand Up @@ -196,9 +200,9 @@ void getISOs() {

@Test
void getISOById() {
ISOSResponse isos = hetznerCloudAPI.getISOS();
Long firstIso = isos.getIsos().stream().findFirst().get().getId();
ISOResponse iso = hetznerCloudAPI.getISOById(firstIso);
ISOResponse iso = hetznerCloudAPI.getISOById(
hetznerCloudAPI.getISOS().getIsos().stream().findFirst().get().getId()
);

Assertions.assertNotNull(iso);
Assertions.assertNotNull(iso.getIso());
Expand Down Expand Up @@ -505,7 +509,7 @@ void createLoadBalancer() {
LoadBalancerResponse loadBalancer = null;
NetworkResponse networkResponse = null;
try {
String networkName = "testNetwork" + System.currentTimeMillis();
String networkName = testIdentifier + "-createLoadBalancer";
NetworkRequest.NetworkRequestBuilder networkRequest = NetworkRequest.builder()
.ipRange("10.0.0.0/16")
.subnets(Collections.singletonList(getDefaultSubnet()))
Expand All @@ -516,15 +520,15 @@ void createLoadBalancer() {

LoadBalancerType loadBalancerType = hetznerCloudAPI.getAllLoadBalancerTypes().getLoadBalancerTypes().get(0);
Assertions.assertNotNull(loadBalancerType);
String name = "test" + System.currentTimeMillis();
String loadBalancerName = testIdentifier + "-createLoadBalancer";
LoadBalancerRequest loadBalancerRequest = LoadBalancerRequest.builder()
.networkZone("eu-central")
.network(networkResponse.getNetwork().getId())
.loadBalancerType(loadBalancerType.getId() + "")
.loadBalancerType(loadBalancerType.getId().toString())
.publicInterface(true)
.services(Collections.singletonList(getHttpService()))
.targets(Collections.singletonList(getLabelSelector(name)))
.name(name)
.targets(Collections.singletonList(getLabelSelector(loadBalancerName)))
.name(loadBalancerName)
.build();

loadBalancer = hetznerCloudAPI.createLoadBalancer(loadBalancerRequest);
Expand Down Expand Up @@ -655,7 +659,7 @@ void changeProtectionOfLoadBalancer() {
void createPlacementGroup() {
PlacementGroupResponse placementGroup = null;
try {
String name = "test-" + System.currentTimeMillis();
String name = testIdentifier + "-createPlacementGroup";
PlacementGroupRequest placementGroupRequest = PlacementGroupRequest.builder()
.name(name)
.type(PlacementGroupType.spread)
Expand Down
12 changes: 3 additions & 9 deletions src/test/java/me/tomsdevsn/hetznercloud/init/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,24 @@
import org.springframework.context.annotation.Bean;

import java.util.Arrays;
import java.util.stream.Stream;

@SpringBootApplication(scanBasePackages = {"me.tomsdevsn.hetznercloud"})

public class App {

public static void main(String[] args) {
SpringApplication app = new SpringApplication(App.class);
app.addListeners(new ApplicationPidFileWriter());
ConfigurableApplicationContext context=app.run(args);

ConfigurableApplicationContext context = app.run(args);
}

@Bean
public CommandLineRunner commandLineRunner(ApplicationContext ctx) {
return args -> {

System.out.println("Let's inspect the beans provided by Spring Boot:");

String[] beanNames = ctx.getBeanDefinitionNames();
Arrays.sort(beanNames);
for (String beanName : beanNames) {
System.out.println(beanName);
}

Stream.of(beanNames).forEach(System.out::println);
};
}
}

0 comments on commit 457ce8b

Please sign in to comment.