Skip to content

Commit

Permalink
chore(plc4j): Added a new Unit-Test annotation to let a test only get…
Browse files Browse the repository at this point in the history
… executed if the build is not run on a Parallels VM.
  • Loading branch information
chrisdutz committed Jul 13, 2022
1 parent 67c97cd commit bd0b41b
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.plc4x.java.api.messages.PlcSubscriptionResponse;
import org.apache.plc4x.java.api.types.PlcResponseCode;
import org.apache.plc4x.java.opcua.OpcuaPlcDriverTest;
import org.apache.plc4x.test.DisableOnParallelsVmFlag;
import org.eclipse.milo.examples.server.ExampleServer;
import org.junit.jupiter.api.*;
import org.slf4j.Logger;
Expand All @@ -35,22 +36,9 @@

import static org.junit.jupiter.api.Assumptions.assumeTrue;

@DisableOnParallelsVmFlag
public class OpcuaSubscriptionHandleTest {

@BeforeAll
static void setUp() {
assumeTrue(() -> {
String OS = System.getProperty("os.name").toLowerCase();
if (OS.contains("nix")
|| OS.contains("nux")
|| OS.contains("aix")) {
return false;
}

return true;
}, "somehow opcua doesn't run properly on linux");
}

private static final Logger LOGGER = LoggerFactory.getLogger(OpcuaPlcDriverTest.class);

private static ExampleServer exampleServer;
Expand Down
4 changes: 4 additions & 0 deletions plc4j/utils/test-utils/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-artifact</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.test;

import org.junit.jupiter.api.extension.ExtendWith;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Some tests seem to only fail or block on VMs run by Parallels.
* Instead of trying to fix this problem, we'll try this for the
* time till someone finds the problem.
*/
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@ExtendWith(DisableOnParallelsVmFlagCondition.class)
public @interface DisableOnParallelsVmFlag {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.plc4x.test;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.SystemUtils;
import org.junit.jupiter.api.extension.ConditionEvaluationResult;
import org.junit.jupiter.api.extension.ExecutionCondition;
import org.junit.jupiter.api.extension.ExtensionContext;

import java.io.*;
import java.nio.charset.StandardCharsets;

public class DisableOnParallelsVmFlagCondition implements ExecutionCondition {

private static final String PARALLELS_STRING = "Parallels Virtual Platform";
private static final boolean isParallels;
static {
boolean localIsParallels = false;
if(SystemUtils.IS_OS_WINDOWS) {
// TODO: If on Windows: Run "systeminfo /fo CSV /nh" command and check if the output contains "Parallels Virtual Platform"
try {
var processBuilder = new ProcessBuilder();
processBuilder.command("systeminfo", "/fo", "CSV", "/nh");
var process = processBuilder.start();
try (var reader = new BufferedReader(
new InputStreamReader(process.getInputStream()))) {
String line;
while ((line = reader.readLine()) != null) {
if(line.contains(PARALLELS_STRING)) {
localIsParallels = true;
break;
}
}
}
} catch (Exception err) {
// Ignore this...
}
} else if (SystemUtils.IS_OS_LINUX) {
// If on Linux: check /sys/devices/virtual/dmi/id/product_name contains "Parallels Virtual Platform"
File productNameFile = new File("/sys/devices/virtual/dmi/id/product_name");
try(InputStream is = new FileInputStream(productNameFile)) {
String content = IOUtils.toString(is, StandardCharsets.UTF_8);
localIsParallels = content.contains(PARALLELS_STRING);
} catch (IOException e) {
// Ignore this...
}
}
isParallels = localIsParallels;
}

@Override
public ConditionEvaluationResult evaluateExecutionCondition(ExtensionContext extensionContext) {
if(isParallels) {
return ConditionEvaluationResult.disabled("Parallels detected");
}
return ConditionEvaluationResult.enabled("Parallels not detected");
}

}

0 comments on commit bd0b41b

Please sign in to comment.