13
13
import org .junit .Test ;
14
14
import org .mockito .Mockito ;
15
15
import org .testcontainers .DockerClientFactory ;
16
+ import org .testcontainers .DockerRegistryContainer ;
16
17
import org .testcontainers .containers .ContainerState ;
17
18
import org .testcontainers .containers .DockerComposeContainer ;
18
19
import org .testcontainers .containers .GenericContainer ;
19
- import org .testcontainers .containers .output .FrameConsumerResultCallback ;
20
- import org .testcontainers .containers .output .OutputFrame ;
21
- import org .testcontainers .containers .output .WaitingConsumer ;
22
20
import org .testcontainers .images .builder .ImageFromDockerfile ;
23
21
24
22
import java .io .IOException ;
25
23
import java .nio .file .Files ;
26
24
import java .nio .file .Path ;
27
25
import java .nio .file .Paths ;
28
26
import java .util .concurrent .TimeUnit ;
29
- import java .util .concurrent .atomic .AtomicInteger ;
30
- import java .util .regex .Matcher ;
31
- import java .util .regex .Pattern ;
32
27
33
28
import static org .mockito .ArgumentMatchers .any ;
34
29
import static org .mockito .ArgumentMatchers .eq ;
@@ -51,18 +46,14 @@ public class AuthenticatedImagePullTest {
51
46
* Containerised docker image registry, with simple hardcoded credentials
52
47
*/
53
48
@ ClassRule
54
- public static GenericContainer <?> authenticatedRegistry = new GenericContainer <> (new ImageFromDockerfile ()
49
+ public static DockerRegistryContainer authenticatedRegistry = new DockerRegistryContainer (new ImageFromDockerfile ()
55
50
.withDockerfileFromBuilder (builder -> {
56
51
builder .from (DOCKER_REGISTRY_IMAGE .asCanonicalNameString ())
57
52
.run ("htpasswd -Bbn testuser notasecret > /htpasswd" )
58
53
.env ("REGISTRY_AUTH" , "htpasswd" )
59
54
.env ("REGISTRY_AUTH_HTPASSWD_PATH" , "/htpasswd" )
60
55
.env ("REGISTRY_AUTH_HTPASSWD_REALM" , "Test" );
61
- }))
62
- .withEnv ("REGISTRY_HTTP_ADDR" , "127.0.0.1:0" )
63
- .withCreateContainerCmdModifier (cmd -> {
64
- cmd .getHostConfig ().withNetworkMode ("host" );
65
- });
56
+ }));
66
57
67
58
private static RegistryAuthLocator originalAuthLocatorSingleton ;
68
59
private static DockerClient client ;
@@ -75,30 +66,7 @@ public static void setUp() throws Exception {
75
66
originalAuthLocatorSingleton = RegistryAuthLocator .instance ();
76
67
client = DockerClientFactory .instance ().client ();
77
68
78
- AtomicInteger port = new AtomicInteger (-1 );
79
- try (FrameConsumerResultCallback resultCallback = new FrameConsumerResultCallback ()) {
80
- WaitingConsumer waitingConsumer = new WaitingConsumer ();
81
- resultCallback .addConsumer (OutputFrame .OutputType .STDERR , waitingConsumer );
82
-
83
- client .logContainerCmd (authenticatedRegistry .getContainerId ())
84
- .withStdErr (true )
85
- .withFollowStream (true )
86
- .exec (resultCallback );
87
-
88
- Pattern pattern = Pattern .compile (".*listening on .*:(\\ d+).*" , Pattern .DOTALL | Pattern .CASE_INSENSITIVE | Pattern .MULTILINE );
89
- waitingConsumer .waitUntil (it -> {
90
- String s = it .getUtf8String ();
91
- Matcher matcher = pattern .matcher (s );
92
- if (matcher .matches ()) {
93
- port .set (Integer .parseInt (matcher .group (1 )));
94
- return true ;
95
- } else {
96
- return false ;
97
- }
98
- }, 10 , TimeUnit .SECONDS );
99
- }
100
-
101
- String testRegistryAddress = authenticatedRegistry .getHost () + ":" + port .get ();
69
+ String testRegistryAddress = authenticatedRegistry .getEndpoint ();
102
70
testImageName = testRegistryAddress + "/alpine" ;
103
71
104
72
final DockerImageName expectedName = DockerImageName .parse (testImageName );
0 commit comments