|
| 1 | +/* |
| 2 | + * Copyright 2020-2021 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.batch.core.test.repository; |
| 17 | + |
| 18 | +import java.time.Duration; |
| 19 | +import java.time.temporal.ChronoUnit; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.HashMap; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.Map; |
| 24 | +import java.util.Set; |
| 25 | + |
| 26 | +import org.testcontainers.containers.JdbcDatabaseContainer; |
| 27 | +import org.testcontainers.containers.wait.strategy.LogMessageWaitStrategy; |
| 28 | +import org.testcontainers.utility.DockerImageName; |
| 29 | +import org.testcontainers.utility.LicenseAcceptance; |
| 30 | + |
| 31 | +import com.github.dockerjava.api.model.Ulimit; |
| 32 | + |
| 33 | +/** |
| 34 | + * @author Jonathan Bregler |
| 35 | + */ |
| 36 | +public class HANAContainer<SELF extends HANAContainer<SELF>> extends JdbcDatabaseContainer<SELF> { |
| 37 | + |
| 38 | + private static final Integer PORT = 39041; |
| 39 | + |
| 40 | + private static final String SYSTEM_USER = "SYSTEM"; |
| 41 | + private static final String SYSTEM_USER_PASSWORD = "HXEHana1"; |
| 42 | + |
| 43 | + public HANAContainer(DockerImageName image) { |
| 44 | + |
| 45 | + super( image ); |
| 46 | + |
| 47 | + addExposedPorts( 39013, 39017, 39041, 39042, 39043, 39044, 39045, 1128, 1129, 59013, 59014 ); |
| 48 | + |
| 49 | + // create ulimits |
| 50 | + Ulimit[] ulimits = new Ulimit[]{ new Ulimit( "nofile", 1048576L, 1048576L ) }; |
| 51 | + |
| 52 | + // create sysctls Map. |
| 53 | + Map<String, String> sysctls = new HashMap<String, String>(); |
| 54 | + |
| 55 | + sysctls.put( "kernel.shmmax", "1073741824" ); |
| 56 | + sysctls.put( "net.ipv4.ip_local_port_range", "40000 60999" ); |
| 57 | + |
| 58 | + // Apply mounts, ulimits and sysctls. |
| 59 | + this.withCreateContainerCmdModifier( it -> it.getHostConfig().withUlimits( ulimits ).withSysctls( sysctls ) ); |
| 60 | + |
| 61 | + // Arguments for Image. |
| 62 | + this.withCommand( "--master-password " + SYSTEM_USER_PASSWORD + " --agree-to-sap-license" ); |
| 63 | + |
| 64 | + // Determine if container is ready. |
| 65 | + this.waitStrategy = new LogMessageWaitStrategy().withRegEx( ".*Startup finished!*\\s" ).withTimes( 1 ) |
| 66 | + .withStartupTimeout( Duration.of( 600, ChronoUnit.SECONDS ) ); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + protected void configure() { |
| 71 | + /* |
| 72 | + * Enforce that the license is accepted - do not remove. License available at: |
| 73 | + * https://www.sap.com/docs/download/cmp/2016/06/sap-hana-express-dev-agmt-and- exhibit.pdf |
| 74 | + */ |
| 75 | + |
| 76 | + // If license was not accepted programmatically, check if it was accepted via |
| 77 | + // resource file |
| 78 | + if ( !getEnvMap().containsKey( "AGREE_TO_SAP_LICENSE" ) ) { |
| 79 | + LicenseAcceptance.assertLicenseAccepted( this.getDockerImageName() ); |
| 80 | + acceptLicense(); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * Accepts the license for the SAP HANA Express container by setting the AGREE_TO_SAP_LICENSE=Y Calling this method |
| 86 | + * will automatically accept the license at: |
| 87 | + * https://www.sap.com/docs/download/cmp/2016/06/sap-hana-express-dev-agmt-and-exhibit.pdf |
| 88 | + * |
| 89 | + * @return The container itself with an environment variable accepting the SAP HANA Express license |
| 90 | + */ |
| 91 | + public SELF acceptLicense() { |
| 92 | + addEnv( "AGREE_TO_SAP_LICENSE", "Y" ); |
| 93 | + return self(); |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + protected Set<Integer> getLivenessCheckPorts() { |
| 98 | + return new HashSet<>( Arrays.asList( new Integer[]{ getMappedPort( PORT ) } ) ); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + protected void waitUntilContainerStarted() { |
| 103 | + getWaitStrategy().waitUntilReady( this ); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getDriverClassName() { |
| 108 | + return "com.sap.db.jdbc.Driver"; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public String getUsername() { |
| 113 | + return SYSTEM_USER; |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getPassword() { |
| 118 | + return SYSTEM_USER_PASSWORD; |
| 119 | + } |
| 120 | + |
| 121 | + @Override |
| 122 | + public String getTestQueryString() { |
| 123 | + return "SELECT 1 FROM SYS.DUMMY"; |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public String getJdbcUrl() { |
| 128 | + return "jdbc:sap://" + getContainerIpAddress() + ":" + getMappedPort( PORT ) + "/"; |
| 129 | + } |
| 130 | +} |
0 commit comments