|
62 | 62 | import java.nio.file.Paths;
|
63 | 63 | import java.util.concurrent.ExecutionException;
|
64 | 64 | import java.util.stream.Stream;
|
| 65 | +import org.testcontainers.containers.BindMode; |
| 66 | +import org.testcontainers.containers.GenericContainer; |
| 67 | +import org.testcontainers.containers.output.Slf4jLogConsumer; |
| 68 | +import org.testcontainers.containers.wait.strategy.Wait; |
| 69 | +import org.testcontainers.images.builder.ImageFromDockerfile; |
| 70 | +import org.testcontainers.jib.JibImage; |
| 71 | +import org.testcontainers.junit.jupiter.Container; |
| 72 | +import org.testcontainers.junit.jupiter.Testcontainers; |
65 | 73 |
|
66 | 74 | import static java.util.Map.entry;
|
67 | 75 | import static org.assertj.core.api.Assertions.assertThat;
|
68 | 76 | import static org.assertj.core.api.Assertions.fail;
|
69 | 77 |
|
| 78 | +@Testcontainers(disabledWithoutDocker = true) |
70 | 79 | @DisableOnJenkinsFlag
|
71 |
| -@Disabled("This test regularly fails the build, it needs to be generally fixed.") |
72 | 80 | public class OpcuaPlcDriverTest {
|
73 | 81 |
|
74 | 82 | private static final Logger LOGGER = LoggerFactory.getLogger(OpcuaPlcDriverTest.class);
|
75 | 83 |
|
| 84 | + @Container |
| 85 | + public static final GenericContainer milo = new MiloTestContainer() |
| 86 | + //.withCreateContainerCmdModifier(cmd -> cmd.withHostName("test-opcua-server")) |
| 87 | + .withReuse(true) |
| 88 | + .withLogConsumer(new Slf4jLogConsumer(LOGGER)) |
| 89 | + .withFileSystemBind("target/tmp/server/security", "/tmp/server/security", BindMode.READ_WRITE); |
| 90 | + |
76 | 91 | // Read only variables of milo example server of version 3.6
|
77 | 92 | private static final String BOOL_IDENTIFIER_READ_WRITE = "ns=2;s=HelloWorld/ScalarTypes/Boolean";
|
78 | 93 | private static final String BYTE_IDENTIFIER_READ_WRITE = "ns=2;s=HelloWorld/ScalarTypes/Byte";
|
@@ -123,50 +138,55 @@ public class OpcuaPlcDriverTest {
|
123 | 138 | //Restricted
|
124 | 139 | public static final String STRING_IDENTIFIER_ONLY_ADMIN_READ_WRITE = "ns=2;s=HelloWorld/OnlyAdminCanRead/String";
|
125 | 140 |
|
126 |
| - // Address of local milo server |
127 |
| - private final String miloLocalAddress = "127.0.0.1:12686/milo"; |
| 141 | + // Address of local milo server, since it comes from test container its hostname and port is not static |
| 142 | + private final String miloLocalAddress = "%s:%d/milo"; |
128 | 143 | //Tcp pattern of OPC UA
|
129 | 144 | private final String opcPattern = "opcua:tcp://";
|
130 | 145 |
|
131 | 146 | private final String paramSectionDivider = "?";
|
132 | 147 | private final String paramDivider = "&";
|
133 | 148 |
|
134 |
| - private final String tcpConnectionAddress = opcPattern + miloLocalAddress; |
135 |
| - |
136 |
| - private final List<String> connectionStringValidSet = List.of(tcpConnectionAddress); |
137 |
| - private final List<String> connectionStringCorruptedSet = List.of(); |
138 |
| - |
139 | 149 | private final String discoveryValidParamTrue = "discovery=true";
|
140 | 150 | private final String discoveryValidParamFalse = "discovery=false";
|
141 | 151 | private final String discoveryCorruptedParamWrongValueNum = "discovery=1";
|
142 | 152 | private final String discoveryCorruptedParamWrongName = "diskovery=false";
|
143 | 153 |
|
| 154 | + private String tcpConnectionAddress; |
| 155 | + private List<String> connectionStringValidSet; |
| 156 | + |
144 | 157 | final List<String> discoveryParamValidSet = List.of(discoveryValidParamTrue, discoveryValidParamFalse);
|
145 | 158 | List<String> discoveryParamCorruptedSet = List.of(discoveryCorruptedParamWrongValueNum, discoveryCorruptedParamWrongName);
|
146 | 159 |
|
147 | 160 | private static TestMiloServer exampleServer;
|
148 | 161 |
|
| 162 | + @BeforeEach |
| 163 | + public void startUp() { |
| 164 | + //System.out.println(milo.getMappedPort(12686)); |
| 165 | + tcpConnectionAddress = String.format(opcPattern + miloLocalAddress, milo.getHost(), milo.getMappedPort(12686)); |
| 166 | + connectionStringValidSet = List.of(tcpConnectionAddress); |
| 167 | + } |
| 168 | + |
149 | 169 | @BeforeAll
|
150 | 170 | public static void setup() throws Exception {
|
151 | 171 | // When switching JDK versions from a newer to an older version,
|
152 | 172 | // this can cause the server to not start correctly.
|
153 | 173 | // Deleting the directory makes sure the key-store is initialized correctly.
|
154 |
| - Path securityBaseDir = Paths.get(System.getProperty("java.io.tmpdir"), "server", "security"); |
155 |
| - try { |
156 |
| - Files.delete(securityBaseDir); |
157 |
| - } catch (Exception e) { |
158 |
| - // Ignore this ... |
159 |
| - } |
160 |
| - |
161 |
| - exampleServer = new TestMiloServer(); |
162 |
| - exampleServer.startup().get(); |
| 174 | +// Path securityBaseDir = Paths.get(System.getProperty("java.io.tmpdir"), "server", "security"); |
| 175 | +// try { |
| 176 | +// Files.delete(securityBaseDir); |
| 177 | +// } catch (Exception e) { |
| 178 | +// // Ignore this ... |
| 179 | +// } |
| 180 | +// |
| 181 | +// exampleServer = new TestMiloServer(); |
| 182 | +// exampleServer.startup().get(); |
163 | 183 | }
|
164 | 184 |
|
165 | 185 | @AfterAll
|
166 | 186 | public static void tearDown() throws Exception {
|
167 |
| - if (exampleServer != null) { |
168 |
| - exampleServer.shutdown().get(); |
169 |
| - } |
| 187 | +// if (exampleServer != null) { |
| 188 | +// exampleServer.shutdown().get(); |
| 189 | +// } |
170 | 190 | }
|
171 | 191 |
|
172 | 192 | @Nested
|
@@ -400,7 +420,7 @@ public void writeVariables(SecurityPolicy policy, MessageSecurity messageSecurit
|
400 | 420 |
|
401 | 421 | PlcWriteRequest.Builder builder = opcuaConnection.writeRequestBuilder();
|
402 | 422 | tags.forEach((tagName, tagEntry) -> {
|
403 |
| - System.out.println("Write tag " + tagName); |
| 423 | + System.out.println("Write tag " + tagName + " " + tagEntry); |
404 | 424 | try {
|
405 | 425 | Object value = tagEntry.getValue();
|
406 | 426 | if (value.getClass().isArray()) {
|
@@ -526,7 +546,8 @@ private String getConnectionString(SecurityPolicy policy, MessageSecurity messag
|
526 | 546 | case Basic256Sha256:
|
527 | 547 | case Aes128_Sha256_RsaOaep:
|
528 | 548 | case Aes256_Sha256_RsaPss:
|
529 |
| - Path keyStoreFile = Paths.get(System.getProperty("java.io.tmpdir"), "server", "security", "example-server.pfx"); |
| 549 | + // this file and its contents should be populated by milo container |
| 550 | + Path keyStoreFile = Paths.get("target/tmp/server/security/example-server.pfx"); |
530 | 551 | String connectionParams = Stream.of(
|
531 | 552 | entry("key-store-file", keyStoreFile.toAbsolutePath().toString().replace("\\", "/")), // handle windows paths
|
532 | 553 | entry("key-store-password", "password"),
|
|
0 commit comments