diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index bb952fb0d5f4227568659f20c6be7a09d17822bd..7b3f9dcbbf9d760f6d17eb23aa5edc53bfb8c7f0 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -6,6 +6,7 @@ before_script: - "mvn --version" - "python3 --version" - "df / -h" + - "docker logout" - "docker logout ghcr.io" - "docker logout registry.hub.docker.com" diff --git a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointIntegrationTest.java b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointIntegrationTest.java deleted file mode 100644 index 0854b2b98e1e30ac4827ed3783c71d22467df885..0000000000000000000000000000000000000000 --- a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointIntegrationTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package at.tuwien.endpoint; - -import at.tuwien.BaseUnitTest; -import at.tuwien.api.container.*; -import at.tuwien.config.DockerConfig; -import at.tuwien.config.ReadyConfig; -import at.tuwien.endpoints.ContainerEndpoint; -import at.tuwien.entities.container.Container; -import at.tuwien.exception.*; -import at.tuwien.repository.jpa.ContainerRepository; -import at.tuwien.repository.jpa.ImageRepository; -import at.tuwien.repository.jpa.RealmRepository; -import at.tuwien.repository.jpa.UserRepository; -import at.tuwien.service.impl.ContainerServiceImpl; -import lombok.extern.log4j.Log4j2; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.mock.mockito.MockBean; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.security.access.AccessDeniedException; -import org.springframework.security.test.context.support.WithAnonymousUser; -import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.test.context.junit.jupiter.SpringExtension; - -import java.security.Principal; -import java.util.List; -import java.util.Optional; - -import static org.junit.jupiter.api.Assertions.*; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.when; - -@Log4j2 -@ExtendWith(SpringExtension.class) -@SpringBootTest -public class ContainerEndpointIntegrationTest extends BaseUnitTest { - - @MockBean - private ReadyConfig readyConfig; - - @Autowired - private RealmRepository realmRepository; - - @Autowired - private ImageRepository imageRepository; - - @Autowired - private ContainerRepository containerRepository; - - @Autowired - private UserRepository userRepository; - - @Autowired - private ContainerEndpoint containerEndpoint; - - @BeforeEach - public void beforeEach() { - afterEach(); - /* networks */ - DockerConfig.createAllNetworks(); - /* metadata database */ - realmRepository.save(REALM_DBREPO); - imageRepository.save(IMAGE_1); - userRepository.save(USER_1); - userRepository.save(USER_2); - userRepository.save(USER_3); - } - - @AfterEach - public void afterEach() { - DockerConfig.removeAllContainers(); - DockerConfig.removeAllNetworks(); - } - - @Test - @WithAnonymousUser - public void findAll_anonymousNoLimit_succeeds() throws InterruptedException { - - /* mock */ - DockerConfig.createContainer(null, CONTAINER_1_SIMPLE, CONTAINER_1_ENV); - DockerConfig.startContainer(CONTAINER_1_SIMPLE); - containerRepository.save(CONTAINER_1_SIMPLE); - - /* test */ - final ResponseEntity<List<ContainerBriefDto>> response = containerEndpoint.findAll(null, null); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertNotNull(response.getBody()); - final List<ContainerBriefDto> body = response.getBody(); - assertEquals(1, body.size()); - final ContainerBriefDto container0 = body.get(0); - assertTrue(container0.getRunning()); - } - - @Test - @WithAnonymousUser - public void findById_anonymousNotRunning_succeeds() throws DockerClientException, ContainerNotFoundException { - - /* mock */ - DockerConfig.createContainer(null, CONTAINER_1_SIMPLE, CONTAINER_1_ENV); - containerRepository.save(CONTAINER_1_SIMPLE); - - /* test */ - final ResponseEntity<ContainerDto> response = containerEndpoint.findById(CONTAINER_1_ID); - assertEquals(HttpStatus.OK, response.getStatusCode()); - assertNotNull(response.getBody()); - final ContainerDto body = response.getBody(); - assertFalse(body.getRunning()); - assertEquals(ContainerStateDto.EXITED, body.getState()); - } - - @Test - @WithMockUser(username = USER_3_USERNAME, authorities = {"modify-container-state"}) - public void modify_foreign_fails() { - final ContainerChangeDto request = ContainerChangeDto.builder() - .action(ContainerActionTypeDto.STOP) - .build(); - - /* mock */ - containerRepository.save(CONTAINER_1_SIMPLE); - - /* test */ - assertThrows(NotAllowedException.class, () -> { - containerEndpoint.modify(CONTAINER_1_ID, request, USER_3_PRINCIPAL); - }); - } - -} diff --git a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointUnitTest.java b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointUnitTest.java index 921fc436a34e0b74e6199a76419471a6f7233d3f..b5b9a9deb7197d174b4ccc3c377143052dac1693 100644 --- a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointUnitTest.java +++ b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ContainerEndpointUnitTest.java @@ -2,6 +2,7 @@ package at.tuwien.endpoint; import at.tuwien.BaseUnitTest; import at.tuwien.api.container.*; +import at.tuwien.config.DockerConfig; import at.tuwien.config.ReadyConfig; import at.tuwien.endpoints.ContainerEndpoint; import at.tuwien.entities.container.Container; @@ -265,6 +266,28 @@ public class ContainerEndpointUnitTest extends BaseUnitTest { }); } + @Test + @WithAnonymousUser + public void findAll_anonymousNoLimit_succeeds() { + + /* test */ + findAll_generic(null, null); + } + + @Test + @WithMockUser(username = USER_3_USERNAME, authorities = {"modify-container-state"}) + public void modify_foreign_fails() { + + /* when */ + when(userRepository.findByUsername(USER_3_USERNAME)) + .thenReturn(Optional.of(USER_3)); + + /* test */ + assertThrows(NotAllowedException.class, () -> { + modify_generic(ContainerActionTypeDto.STOP, CONTAINER_1_ID, CONTAINER_1, USER_3_PRINCIPAL); + }); + } + /* ################################################################################################### */ /* ## GENERIC TEST CASES ## */ /* ################################################################################################### */ diff --git a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ImageEndpointUnitTest.java b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ImageEndpointUnitTest.java index 6c0bf962b36c494c0158413026e1563c30b83d50..77d238be78c50dd20ea144b4a2c90d1c2aa720d6 100644 --- a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ImageEndpointUnitTest.java +++ b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/endpoint/ImageEndpointUnitTest.java @@ -10,8 +10,8 @@ import at.tuwien.config.ReadyConfig; import at.tuwien.endpoints.ImageEndpoint; import at.tuwien.entities.container.image.ContainerImage; import at.tuwien.exception.*; -import at.tuwien.repository.jpa.ImageRepository; import at.tuwien.repository.jpa.UserRepository; +import at.tuwien.service.impl.ImageServiceImpl; import lombok.extern.log4j.Log4j2; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -41,7 +41,7 @@ public class ImageEndpointUnitTest extends BaseUnitTest { private ReadyConfig readyConfig; @MockBean - private ImageRepository imageRepository; + private ImageServiceImpl imageService; @MockBean private UserRepository userRepository; @@ -176,11 +176,12 @@ public class ImageEndpointUnitTest extends BaseUnitTest { } @Test - public void findById_anonymousNotFound_succeeds() { + public void findById_anonymousNotFound_succeeds() throws ImageNotFoundException { /* mock */ - when(imageRepository.findById(CONTAINER_1_ID)) - .thenReturn(Optional.empty()); + doThrow(ImageNotFoundException.class) + .when(imageService) + .find(CONTAINER_1_ID); /* test */ assertThrows(ImageNotFoundException.class, () -> { @@ -214,11 +215,12 @@ public class ImageEndpointUnitTest extends BaseUnitTest { @Test @WithMockUser(username = USER_2_USERNAME, authorities = {"delete-image"}) - public void delete_hasRole_succeeds() throws ImageNotFoundException { + public void delete_hasRole_succeeds() throws ImageNotFoundException, PersistenceException { /* mock */ - when(imageRepository.existsById(IMAGE_1_ID)) - .thenReturn(true); + doNothing() + .when(imageService) + .delete(IMAGE_1_ID); when(userRepository.findByUsername(USER_2_USERNAME)) .thenReturn(Optional.of(USER_2)); @@ -266,7 +268,7 @@ public class ImageEndpointUnitTest extends BaseUnitTest { @Test @WithMockUser(username = USER_2_USERNAME, authorities = {"modify-image"}) - public void modify_hasRole_succeeds() throws ImageNotFoundException { + public void modify_hasRole_succeeds() throws ImageNotFoundException, DockerClientException { final ImageChangeDto request = ImageChangeDto.builder() .registry(IMAGE_1_REGISTRY) .defaultPort(IMAGE_1_PORT) @@ -291,7 +293,7 @@ public class ImageEndpointUnitTest extends BaseUnitTest { public void findAll_generic(Principal principal) { /* mock */ - when(imageRepository.findAll()) + when(imageService.getAll()) .thenReturn(List.of(IMAGE_1)); /* test */ @@ -306,7 +308,7 @@ public class ImageEndpointUnitTest extends BaseUnitTest { ImageAlreadyExistsException, DockerClientException, ImageNotFoundException, ImageInvalidException { /* mock */ - when(imageRepository.save(any(ContainerImage.class))) + when(imageService.create(data, principal)) .thenReturn(IMAGE_1); /* test */ @@ -318,8 +320,8 @@ public class ImageEndpointUnitTest extends BaseUnitTest { public void findById_generic(Long imageId, ContainerImage image) throws ImageNotFoundException { /* mock */ - when(imageRepository.findById(imageId)) - .thenReturn(Optional.of(image)); + when(imageService.find(imageId)) + .thenReturn(image); /* test */ final ResponseEntity<ImageDto> response = imageEndpoint.findById(imageId); @@ -330,8 +332,8 @@ public class ImageEndpointUnitTest extends BaseUnitTest { public void delete_generic(Long imageId, ContainerImage image, Principal principal) throws ImageNotFoundException { /* mock */ - when(imageRepository.findById(imageId)) - .thenReturn(Optional.of(image)); + when(imageService.find(imageId)) + .thenReturn(image); /* test */ final ResponseEntity<?> response = imageEndpoint.delete(imageId, principal); @@ -340,12 +342,12 @@ public class ImageEndpointUnitTest extends BaseUnitTest { } public void modify_generic(Long imageId, ContainerImage image, ImageChangeDto data, Principal principal) - throws ImageNotFoundException { + throws ImageNotFoundException, DockerClientException { /* mock */ - when(imageRepository.findById(imageId)) - .thenReturn(Optional.of(image)); - when(imageRepository.save(any(ContainerImage.class))) + when(imageService.find(imageId)) + .thenReturn(image); + when(imageService.update(imageId, data)) .thenReturn(image); /* test */ diff --git a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/service/ImageServiceUnitTest.java b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/service/ImageServiceUnitTest.java index c75bfe2f6eda6923c4ef9819388e489d185e9f4a..820ca384b485640936e77087533cf4106b104261 100644 --- a/dbrepo-container-service/rest-service/src/test/java/at/tuwien/service/ImageServiceUnitTest.java +++ b/dbrepo-container-service/rest-service/src/test/java/at/tuwien/service/ImageServiceUnitTest.java @@ -18,6 +18,7 @@ import org.springframework.test.context.junit.jupiter.SpringExtension; import jakarta.persistence.EntityNotFoundException; import jakarta.validation.ConstraintViolationException; + import java.security.Principal; import java.util.List; import java.util.Optional; @@ -33,7 +34,7 @@ public class ImageServiceUnitTest extends BaseUnitTest { @MockBean private ReadyConfig readyConfig; - @Autowired + @MockBean private ImageServiceImpl imageService; @MockBean @@ -45,6 +46,8 @@ public class ImageServiceUnitTest extends BaseUnitTest { /* mock */ when(imageRepository.findAll()) .thenReturn(List.of(IMAGE_1)); + when(imageService.getAll()) + .thenCallRealMethod(); /* test */ final List<ContainerImage> response = imageService.getAll(); @@ -59,6 +62,8 @@ public class ImageServiceUnitTest extends BaseUnitTest { /* mock */ when(imageRepository.findById(IMAGE_1_ID)) .thenReturn(Optional.of(IMAGE_1)); + when(imageService.find(IMAGE_1_ID)) + .thenCallRealMethod(); /* test */ final ContainerImage response = imageService.find(IMAGE_1_ID); @@ -67,11 +72,13 @@ public class ImageServiceUnitTest extends BaseUnitTest { } @Test - public void getById_notFound_fails() { + public void getById_notFound_fails() throws ImageNotFoundException { /* mock */ when(imageRepository.findById(IMAGE_1_ID)) .thenReturn(Optional.empty()); + when(imageService.find(IMAGE_1_ID)) + .thenCallRealMethod(); /* test */ assertThrows(ImageNotFoundException.class, () -> { @@ -80,22 +87,24 @@ public class ImageServiceUnitTest extends BaseUnitTest { } @Test - public void create_duplicate_fails() { + public void create_duplicate_fails() throws UserNotFoundException, ImageAlreadyExistsException, + DockerClientException, ImageNotFoundException { final ImageCreateDto request = ImageCreateDto.builder() .repository(IMAGE_1_REPOSITORY) .tag(IMAGE_1_TAG) .defaultPort(IMAGE_1_PORT) .environment(IMAGE_1_ENV_DTO) .build(); - final Principal principal = new BasicUserPrincipal(USER_1_USERNAME); /* mock */ when(imageRepository.save(any(ContainerImage.class))) .thenThrow(ConstraintViolationException.class); + when(imageService.create(request, USER_1_PRINCIPAL)) + .thenCallRealMethod(); /* test */ assertThrows(ImageAlreadyExistsException.class, () -> { - imageService.create(request, principal); + imageService.create(request, USER_1_PRINCIPAL); }); } @@ -112,6 +121,11 @@ public class ImageServiceUnitTest extends BaseUnitTest { .thenReturn(Optional.of(IMAGE_1)); when(imageRepository.save(any())) .thenReturn(IMAGE_1); + doNothing() + .when(imageService) + .pull(IMAGE_1_REGISTRY, IMAGE_1_REPOSITORY, IMAGE_1_TAG); + when(imageService.update(IMAGE_1_ID, request)) + .thenCallRealMethod(); /* test */ final ContainerImage response = imageService.update(IMAGE_1_ID, request); @@ -132,6 +146,11 @@ public class ImageServiceUnitTest extends BaseUnitTest { .thenReturn(Optional.of(IMAGE_1)); when(imageRepository.save(any())) .thenReturn(IMAGE_1); + doNothing() + .when(imageService) + .pull(IMAGE_1_REGISTRY, IMAGE_1_REPOSITORY, IMAGE_1_TAG); + when(imageService.update(IMAGE_1_ID, request)) + .thenCallRealMethod(); /* test */ final ContainerImage response = imageService.update(IMAGE_1_ID, request); @@ -140,7 +159,7 @@ public class ImageServiceUnitTest extends BaseUnitTest { } @Test - public void update_notFound_fails() { + public void update_notFound_fails() throws ImageNotFoundException { final ImageChangeDto request = ImageChangeDto.builder() .environment(IMAGE_1_ENV_DTO) .defaultPort(IMAGE_1_PORT) @@ -149,6 +168,8 @@ public class ImageServiceUnitTest extends BaseUnitTest { /* mock */ when(imageRepository.findById(IMAGE_1_ID)) .thenReturn(Optional.empty()); + when(imageService.update(IMAGE_1_ID, request)) + .thenCallRealMethod(); /* test */ assertThrows(ImageNotFoundException.class, () -> { @@ -165,13 +186,16 @@ public class ImageServiceUnitTest extends BaseUnitTest { doNothing() .when(imageRepository) .deleteById(IMAGE_1_ID); + doCallRealMethod() + .when(imageService) + .delete(IMAGE_1_ID); /* test */ imageService.delete(IMAGE_1_ID); } @Test - public void delete_notFound_fails() { + public void delete_notFound_fails() throws ImageNotFoundException { /* mock */ when(imageRepository.existsById(IMAGE_1_ID)) @@ -179,6 +203,9 @@ public class ImageServiceUnitTest extends BaseUnitTest { doThrow(EntityNotFoundException.class) .when(imageRepository) .deleteById(IMAGE_1_ID); + doCallRealMethod() + .when(imageService) + .delete(IMAGE_1_ID); /* test */ assertThrows(ImageNotFoundException.class, () -> {