Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code coverage it #183

Merged
merged 22 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
937aad3
Update version for testcontainers and use of docker compose
thelarsjohansson Nov 6, 2023
13c7dc8
Repair integration tests for Olog API
thelarsjohansson Nov 7, 2023
f937523
Adjust copyright notice
thelarsjohansson Nov 7, 2023
215a72a
Cleanup for Sonar rules
thelarsjohansson Nov 7, 2023
0a32acb
Collect strings and urls
thelarsjohansson Nov 7, 2023
2e89870
Refactor integration tests
thelarsjohansson Nov 7, 2023
7ec583b
Refactor integration tests for tags
thelarsjohansson Nov 8, 2023
cd13bbd
Refactor integration tests for properties
thelarsjohansson Nov 8, 2023
2868d72
Refactor integration tests for logbooks
thelarsjohansson Nov 8, 2023
ec808a6
Refactor integration tests for logs
thelarsjohansson Nov 8, 2023
5e7e749
Add documentation on how to run integration tests with Docker
thelarsjohansson Nov 9, 2023
7a7843b
Add documentation on tutorial for integration tests with Docker
thelarsjohansson Nov 9, 2023
c9fccd6
Add code coverage for integration tests with Docker
thelarsjohansson Nov 10, 2023
489a153
Cleanup for Sonar rules
thelarsjohansson Nov 13, 2023
b28dd30
Cleanup for Sonar rules
thelarsjohansson Nov 13, 2023
493cbaf
Cleanup for Sonar rules
thelarsjohansson Nov 13, 2023
3f345ce
Cleanup for Sonar rules
thelarsjohansson Nov 13, 2023
1db48a8
Cleanup for Sonar rules
thelarsjohansson Nov 13, 2023
8d2dff4
Collect log texts and use formatted strings in logging
thelarsjohansson Nov 14, 2023
cb01e92
Add integration tests for logs
thelarsjohansson Nov 22, 2023
a349ab1
Update version for testcontainers
thelarsjohansson Nov 22, 2023
7c3c097
Handle extraction of Jacoco report by test utilities
thelarsjohansson Dec 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions Dockerfile.integrationtest
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# ------------------------------------------------------------------------------
# Copyright (C) 2023 European Spallation Source ERIC.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# ------------------------------------------------------------------------------

FROM openjdk:11

# apt clean is run automatically in debian-based images.
RUN apt update && apt install -y wait-for-it
# Run commands as user 'olog'
RUN useradd -ms /bin/bash olog

# deployment unit
COPY /target/service-olog*.jar /olog-target/service-olog*.jar

# code coverage
COPY target/jacoco/jacocoagent.jar /olog-target/jacocoagent.jar

RUN chown olog:olog /olog-target
USER olog
EXPOSE 8080
EXPOSE 8181

CMD ["java", "-jar", "/olog-target/service-olog*.jar", "--spring.config.name=application"]
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ backends.
* Build the image: `docker-compose build`
* Run the containers: `docker-compose up`

### Integration tests with Docker containers

Purpose is to have integration tests for Olog API with Docker.

See `src/test/java` and package
* `org.phoebus.olog.docker`

Integration tests start docker containers for Olog, Elasticsearch and MongoDB, and run http requests (GET) and curl commands (POST, PUT, DELETE) towards the application to test behavior (read, list, query, create, update, remove) and replies are received and checked if content is as expected.

There are tests for properties, tags, logbooks and logs separately and in combination.

Integration tests can be run in IDE and via Maven.

```
mvn failsafe:integration-test -DskipITs=false -Pintegrationtest-docker
```

See
* [How to run Integration test with Docker](src/test/resources/INTEGRATIONTEST_DOCKER_RUN.md)
* [Tutorial for Integration test with Docker](src/test/resources/INTEGRATIONTEST_DOCKER_TUTORIAL.md)

### Release Olog Server binaries to maven central

The Phoebus Olog service uses the maven release plugin to prepare the publish the olog server binaries to maven central
Expand Down
63 changes: 63 additions & 0 deletions docker-compose-integrationtest.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Use the default network olog_default.
version: "3.7"
services:
olog:
build:
context: .
dockerfile: Dockerfile.integrationtest
ports:
- "8080:8080"
- "8181:8181"
depends_on:
- mongo
- elastic
# Ensure that the elastic server is up before starting olog.
environment:
ldap.enabled: "false"
embedded_ldap.enabled: "false"
demo_auth.enabled: "true"
skipITCoverage: "true"
command: >
/bin/bash -c "
while ! curl -s -f elastic:9200/_cluster/health?wait_for_status=yellow;
do
echo Waiting for Elasticsearch;
sleep 1;
done;
if [ ${skipITCoverage} == false ]; then
export JAVA_TOOL_OPTIONS=-javaagent:/olog-target/jacocoagent.jar=destfile=/olog-target/jacoco.exec,output=file,append=false
fi
java -jar /olog-target/service-olog*.jar --spring.config.name=application-docker
"

mongo:
image: mongo
ports:
- 27017:27017
volumes:
- olog-mongodata:/etc/mongo

elastic:
image: elasticsearch:8.2.3
environment:
cluster.name: elasticsearch
bootstrap.memory_lock: "true"
discovery.type: single-node
ES_JAVA_OPTS: "-Xms512m -Xmx512m"
xpack.security.enabled: "false"
xpack.security.http.ssl.enabled: "false"
ports:
- 9200:9200
volumes:
- olog-esdata:/usr/share/elasticsearch/data
ulimits:
memlock:
soft: -1
hard: -1

# Configure persistent storage for mongo and elastic.
volumes:
olog-esdata:
driver: local
olog-mongodata:
driver: local
108 changes: 107 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@
<elasticsearch.version>8.2.0</elasticsearch.version>
<lombok.version>1.18.16</lombok.version>
<junit.version>5.8.2</junit.version>
<skipITs>true</skipITs>
<skipITCoverage>true</skipITCoverage>
<jacoco.skip>true</jacoco.skip>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -195,6 +198,12 @@
<version>2.2.220</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<version>0.8.10</version>
<scope>runtime</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
Expand All @@ -207,7 +216,7 @@
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>junit-jupiter</artifactId>
<version>1.17.5</version>
<version>1.19.3</version>
<scope>test</scope>
</dependency>

Expand Down Expand Up @@ -245,6 +254,18 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M7</version>
</plugin>
<!-- use self-contained integration tests - org.phoebus.olog.docker -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<skipITs>${skipITs}</skipITs>
<includes>
<include>**/*IT.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
Expand All @@ -255,6 +276,72 @@
<encoding>${project.build.sourceEncoding}</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.jacoco</groupId>
<artifactId>org.jacoco.agent</artifactId>
<classifier>runtime</classifier>
<outputDirectory>${project.build.directory}/jacoco</outputDirectory>
<destFileName>jacocoagent.jar</destFileName>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.10</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>merge</id>
<phase>verify</phase>
<goals>
<goal>merge</goal>
</goals>
<configuration>
<fileSets>
<fileSet>
<directory>${project.build.directory}</directory>
<includes>
<include>jacoco*.exec</include>
</includes>
</fileSet>
</fileSets>
<destFile>${project.build.directory}/site/jacoco/jacoco.exec</destFile>
</configuration>
</execution>
<execution>
<id>report</id>
<!-- <phase>prepare-package</phase> -->
<phase>verify</phase>
<goals>
<goal>report</goal>
</goals>
<configuration>
<dataFile>${project.build.directory}/site/jacoco/jacoco.exec</dataFile>
<outputDirectory>${project.build.directory}/site/jacoco</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
Expand Down Expand Up @@ -373,6 +460,25 @@
</plugins>
</build>
</profile>
<!-- profile for integrationtest with docker -->
<profile>
<id>integrationtest-docker</id>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-failsafe-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<skipITs>${skipITs}</skipITs>
<includes>
<include>**/docker/*IT.java</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</profile>

<!-- A profile to build deployable jar -->
<profile>
Expand Down
19 changes: 2 additions & 17 deletions src/main/java/org/phoebus/olog/Application.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,16 @@
package org.phoebus.olog;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.AbstractHttp11Protocol;
import org.phoebus.olog.notification.LogEntryNotifier;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.servlet.server.ServletWebServerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.core.task.TaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.mvc.method.annotation.PrincipalMethodArgumentResolver;

import javax.annotation.PostConstruct;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -57,7 +42,7 @@ public class Application {
private long propertyProvidersTimeout;

public static void main(String[] args) {
logger.info("Starting Olog Service");
logger.log(Level.INFO, TextUtil.OLOG_STARTING);
configureTruststore();
ConfigurableApplicationContext olog = SpringApplication.run(Application.class, args);
}
Expand All @@ -76,7 +61,7 @@ private static void configureTruststore() {
tempFile.deleteOnExit();
System.setProperty("javax.net.ssl.trustStore", tempFile.getAbsolutePath());
} catch (IOException e) {
logger.log(Level.SEVERE, "failed to configure olog truststore", e);
logger.log(Level.SEVERE, TextUtil.OLOG_FAILED_CONFIGURE_TRUSTSTORE, e);
}
}
if (System.getProperty("javax.net.ssl.trustStorePassword") == null) {
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/phoebus/olog/AttachmentRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.text.MessageFormat;
import java.util.Optional;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -63,7 +64,7 @@ public <S extends Attachment> S save(S entity) {
return entity;
} catch (IOException e) {
Logger.getLogger(AttachmentRepository.class.getName())
.log(Level.WARNING, String.format("Unable to persist attachment %s", entity.getFilename()), e);
.log(Level.WARNING, MessageFormat.format(TextUtil.ATTACHMENT_NOT_PERSISTED, entity.getFilename()), e);
}
return null;
}
Expand Down
Loading