|
| 1 | +package em.external.org.ind1; |
| 2 | + |
| 3 | +import org.evomaster.client.java.controller.ExternalSutController; |
| 4 | +import org.evomaster.client.java.controller.InstrumentedSutStarter; |
| 5 | +import org.evomaster.client.java.controller.api.dto.AuthenticationDto; |
| 6 | +import org.evomaster.client.java.controller.api.dto.SutInfoDto; |
| 7 | +import org.evomaster.client.java.controller.api.dto.database.schema.DatabaseType; |
| 8 | +import org.evomaster.client.java.controller.db.DbCleaner; |
| 9 | +import org.evomaster.client.java.controller.db.SqlScriptRunnerCached; |
| 10 | +import org.evomaster.client.java.controller.internal.db.DbSpecification; |
| 11 | +import org.evomaster.client.java.controller.problem.ProblemInfo; |
| 12 | +import org.evomaster.client.java.controller.problem.RestProblem; |
| 13 | +import org.testcontainers.containers.GenericContainer; |
| 14 | + |
| 15 | +import java.sql.Connection; |
| 16 | +import java.sql.DriverManager; |
| 17 | +import java.sql.SQLException; |
| 18 | +import java.util.Arrays; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +public class ExternalEvoMasterController extends ExternalSutController { |
| 23 | + |
| 24 | + /* |
| 25 | + All info that could possibly identify the case study is removed, and |
| 26 | + must be passed as either parameter input or environment variable |
| 27 | + */ |
| 28 | + private static final String SUT_LOCATION_IND1 = System.getenv("SUT_LOCATION_IND1"); |
| 29 | + private static final String SUT_PACKAGE_IND1 = System.getenv("SUT_PACKAGE_IND1"); |
| 30 | + |
| 31 | + public static void main(String[] args) { |
| 32 | + |
| 33 | + int controllerPort = 40100; |
| 34 | + if (args.length > 0) { |
| 35 | + controllerPort = Integer.parseInt(args[0]); |
| 36 | + } |
| 37 | + int sutPort = 12345; |
| 38 | + if (args.length > 1) { |
| 39 | + sutPort = Integer.parseInt(args[1]); |
| 40 | + } |
| 41 | + String jarLocation = SUT_LOCATION_IND1; |
| 42 | + if (args.length > 2) { |
| 43 | + jarLocation = args[2]; |
| 44 | + } |
| 45 | + |
| 46 | + int timeoutSeconds = 120; |
| 47 | + if(args.length > 3){ |
| 48 | + timeoutSeconds = Integer.parseInt(args[3]); |
| 49 | + } |
| 50 | + |
| 51 | + String packagesToInstrument = SUT_PACKAGE_IND1; |
| 52 | + if(args.length > 5){ |
| 53 | + packagesToInstrument = args[5]; |
| 54 | + } |
| 55 | + |
| 56 | + String command = "java"; |
| 57 | + if(args.length > 4){ |
| 58 | + command = args[4]; |
| 59 | + } |
| 60 | + |
| 61 | + |
| 62 | + ExternalEvoMasterController controller = |
| 63 | + new ExternalEvoMasterController(controllerPort, jarLocation, |
| 64 | + sutPort, timeoutSeconds, command, packagesToInstrument); |
| 65 | + InstrumentedSutStarter starter = new InstrumentedSutStarter(controller); |
| 66 | + |
| 67 | + starter.start(); |
| 68 | + } |
| 69 | + |
| 70 | + private final int timeoutSeconds; |
| 71 | + private final int sutPort; |
| 72 | + private String jarLocation; |
| 73 | + private final String packagesToInstrument; |
| 74 | + private Connection sqlConnection; |
| 75 | + private List<DbSpecification> dbSpecification; |
| 76 | + |
| 77 | + private static final GenericContainer postgres = new GenericContainer("postgres:9") |
| 78 | + .withExposedPorts(5432) |
| 79 | + .withEnv("POSTGRES_HOST_AUTH_METHOD","trust") |
| 80 | + .withTmpFs(Collections.singletonMap("/var/lib/postgresql/data", "rw")); |
| 81 | + |
| 82 | + public ExternalEvoMasterController(){ |
| 83 | + this(40100, SUT_LOCATION_IND1, 12345, 120, "java", SUT_PACKAGE_IND1); |
| 84 | + } |
| 85 | + |
| 86 | + public ExternalEvoMasterController(String jarLocation) { |
| 87 | + this(); |
| 88 | + this.jarLocation = jarLocation; |
| 89 | + } |
| 90 | + |
| 91 | + public ExternalEvoMasterController( |
| 92 | + int controllerPort, String jarLocation, int sutPort, int timeoutSeconds, String command, |
| 93 | + String packagesToInstrument) { |
| 94 | + |
| 95 | + if(jarLocation==null || jarLocation.isEmpty()){ |
| 96 | + throw new IllegalArgumentException("Missing jar location"); |
| 97 | + } |
| 98 | + if(packagesToInstrument==null || packagesToInstrument.isEmpty()){ |
| 99 | + throw new IllegalArgumentException("Missing packages to instrument"); |
| 100 | + } |
| 101 | + |
| 102 | + this.sutPort = sutPort; |
| 103 | + this.jarLocation = jarLocation; |
| 104 | + this.timeoutSeconds = timeoutSeconds; |
| 105 | + this.packagesToInstrument = packagesToInstrument; |
| 106 | + setControllerPort(controllerPort); |
| 107 | + setJavaCommand(command); |
| 108 | + } |
| 109 | + |
| 110 | + |
| 111 | + @Override |
| 112 | + public String[] getInputParameters() { |
| 113 | + return new String[]{ |
| 114 | + "--server.port=" + sutPort, |
| 115 | + "--spring.datasource.url="+dbUrl(), |
| 116 | + "--spring.datasource.username=postgres", |
| 117 | + "--spring.datasource.password", |
| 118 | + "--spring.cache.type=none", |
| 119 | + "--spring.jmx.enabled=false", |
| 120 | + "--spring.profiles.active=test", |
| 121 | + "--subscriptions.slack.notification_url=https://hooks.slack.com/services/fake/fake/fake", |
| 122 | + "--auth0.authorization.api_url=http://fakeaddressdoesnotexist.no/adf6e2f2b84784b57522e3b19dfc9201/api", |
| 123 | + "--auth0.subscriptions.client_id=fooSubscriptionsId", |
| 124 | + "--auth0.domain=foo-test.eu.auth0.com" |
| 125 | + }; |
| 126 | + } |
| 127 | + |
| 128 | + public String[] getJVMParameters() { |
| 129 | + |
| 130 | + return new String[]{ |
| 131 | + "-Xmx4G" |
| 132 | + }; |
| 133 | + } |
| 134 | + |
| 135 | + private String dbUrl( ) { |
| 136 | + |
| 137 | + String host = postgres.getContainerIpAddress(); |
| 138 | + int port = postgres.getMappedPort(5432); |
| 139 | + |
| 140 | + String url = "jdbc"; |
| 141 | + url += ":postgresql://"+host+":"+port+"/postgres?currentSchema=subscriptions"; |
| 142 | + |
| 143 | + return url; |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public String getBaseURL() { |
| 148 | + return "http://localhost:" + sutPort; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public String getPathToExecutableJar() { |
| 153 | + return jarLocation; |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public String getLogMessageOfInitializedServer() { |
| 158 | + return "Tomcat started on port"; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public long getMaxAwaitForInitializationInSeconds() { |
| 163 | + return timeoutSeconds; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public void preStart() { |
| 168 | + postgres.start(); |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public void postStart() { |
| 173 | + closeDataBaseConnection(); |
| 174 | + |
| 175 | + try { |
| 176 | + sqlConnection = DriverManager.getConnection(dbUrl(), "postgres", ""); |
| 177 | + dbSpecification = Arrays.asList(new DbSpecification(DatabaseType.POSTGRES,sqlConnection) |
| 178 | + .withSchemas("subscriptions").withDisabledSmartClean()); |
| 179 | + } catch (Exception e) { |
| 180 | + throw new RuntimeException(e); |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + @Override |
| 185 | + public void resetStateOfSUT() { |
| 186 | + DbCleaner.clearDatabase_Postgres(sqlConnection, |
| 187 | + "subscriptions", |
| 188 | + Arrays.asList("flyway_schema_history")); |
| 189 | + SqlScriptRunnerCached.runScriptFromResourceFile(sqlConnection,"/init_db.sql"); |
| 190 | + } |
| 191 | + |
| 192 | + @Override |
| 193 | + public void preStop() { |
| 194 | + closeDataBaseConnection(); |
| 195 | + } |
| 196 | + |
| 197 | + @Override |
| 198 | + public void postStop() { |
| 199 | + postgres.stop(); |
| 200 | + } |
| 201 | + |
| 202 | + private void closeDataBaseConnection() { |
| 203 | + if (sqlConnection != null) { |
| 204 | + try { |
| 205 | + sqlConnection.close(); |
| 206 | + } catch (SQLException e) { |
| 207 | + e.printStackTrace(); |
| 208 | + } |
| 209 | + sqlConnection = null; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + @Override |
| 214 | + public String getPackagePrefixesToCover() { |
| 215 | + return packagesToInstrument; |
| 216 | + } |
| 217 | + |
| 218 | + @Override |
| 219 | + public ProblemInfo getProblemInfo() { |
| 220 | + return new RestProblem( |
| 221 | + getBaseURL() + "/v3/api-docs", |
| 222 | + null |
| 223 | + ); |
| 224 | + } |
| 225 | + |
| 226 | + @Override |
| 227 | + public SutInfoDto.OutputFormat getPreferredOutputFormat() { |
| 228 | + return SutInfoDto.OutputFormat.JAVA_JUNIT_4; |
| 229 | + } |
| 230 | + |
| 231 | + |
| 232 | + @Override |
| 233 | + public List<AuthenticationDto> getInfoForAuthentication() { |
| 234 | + return null; |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public List<DbSpecification> getDbSpecifications() { |
| 239 | + return dbSpecification; |
| 240 | + } |
| 241 | +} |
0 commit comments