|
| 1 | +package io.github.hectorvent.floci.core.common.docker; |
| 2 | + |
| 3 | +import com.github.dockerjava.api.DockerClient; |
| 4 | +import com.github.dockerjava.api.command.InspectVolumeCmd; |
| 5 | +import com.github.dockerjava.api.command.InspectVolumeResponse; |
| 6 | +import com.github.dockerjava.api.exception.DockerException; |
| 7 | +import com.github.dockerjava.api.exception.NotFoundException; |
| 8 | +import io.github.hectorvent.floci.services.lambda.launcher.ImageCacheService; |
| 9 | +import org.junit.jupiter.api.BeforeEach; |
| 10 | +import org.junit.jupiter.api.Test; |
| 11 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 12 | +import org.mockito.Mock; |
| 13 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.*; |
| 16 | +import static org.mockito.Mockito.*; |
| 17 | + |
| 18 | +@ExtendWith(MockitoExtension.class) |
| 19 | +class ContainerLifecycleManagerVolumeTest { |
| 20 | + |
| 21 | + @Mock |
| 22 | + private DockerClient dockerClient; |
| 23 | + |
| 24 | + @Mock |
| 25 | + private ImageCacheService imageCacheService; |
| 26 | + |
| 27 | + @Mock |
| 28 | + private ContainerDetector containerDetector; |
| 29 | + |
| 30 | + @Mock |
| 31 | + private PortAllocator portAllocator; |
| 32 | + |
| 33 | + private ContainerLifecycleManager manager; |
| 34 | + |
| 35 | + @BeforeEach |
| 36 | + void setUp() { |
| 37 | + manager = new ContainerLifecycleManager(dockerClient, imageCacheService, containerDetector, portAllocator); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + void volumeExists_returnsTrue_whenVolumeExists() { |
| 42 | + InspectVolumeCmd cmd = mock(InspectVolumeCmd.class); |
| 43 | + when(dockerClient.inspectVolumeCmd("my-volume")).thenReturn(cmd); |
| 44 | + when(cmd.exec()).thenReturn(mock(InspectVolumeResponse.class)); |
| 45 | + |
| 46 | + assertTrue(manager.volumeExists("my-volume")); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void volumeExists_returnsFalse_whenVolumeNotFound() { |
| 51 | + InspectVolumeCmd cmd = mock(InspectVolumeCmd.class); |
| 52 | + when(dockerClient.inspectVolumeCmd("nonexistent")).thenReturn(cmd); |
| 53 | + when(cmd.exec()).thenThrow(new NotFoundException("No such volume")); |
| 54 | + |
| 55 | + assertFalse(manager.volumeExists("nonexistent")); |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + void volumeExists_returnsFalse_forNullName() { |
| 60 | + assertFalse(manager.volumeExists(null)); |
| 61 | + verifyNoInteractions(dockerClient); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + void volumeExists_returnsFalse_forBlankName() { |
| 66 | + assertFalse(manager.volumeExists(" ")); |
| 67 | + verifyNoInteractions(dockerClient); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void volumeExists_returnsFalse_forAbsolutePath() { |
| 72 | + assertFalse(manager.volumeExists("/var/lib/data")); |
| 73 | + verifyNoInteractions(dockerClient); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + void volumeExists_returnsFalse_forRelativePath() { |
| 78 | + assertFalse(manager.volumeExists("./data")); |
| 79 | + verifyNoInteractions(dockerClient); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + void volumeExists_returnsFalse_forWindowsAbsolutePathBackslash() { |
| 84 | + assertFalse(manager.volumeExists("C:\\Users\\data")); |
| 85 | + verifyNoInteractions(dockerClient); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + void volumeExists_returnsFalse_forWindowsAbsolutePathForwardSlash() { |
| 90 | + assertFalse(manager.volumeExists("D:/sources/data")); |
| 91 | + verifyNoInteractions(dockerClient); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + void volumeExists_returnsFalse_onDockerException() { |
| 96 | + InspectVolumeCmd cmd = mock(InspectVolumeCmd.class); |
| 97 | + when(dockerClient.inspectVolumeCmd("some-volume")).thenReturn(cmd); |
| 98 | + when(cmd.exec()).thenThrow(new DockerException("Connection refused", 500)); |
| 99 | + |
| 100 | + assertFalse(manager.volumeExists("some-volume")); |
| 101 | + } |
| 102 | +} |
| 103 | + |
0 commit comments