Skip to content

Commit ae40eeb

Browse files
committed
added monitoring exporter test and utility to build and start it part 5
1 parent c0225a5 commit ae40eeb

File tree

3 files changed

+62
-95
lines changed

3 files changed

+62
-95
lines changed

integration-tests/src/test/java/oracle/kubernetes/operator/ITMonitoringExporter.java

Lines changed: 17 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,8 @@
1515
import com.gargoylesoftware.htmlunit.html.HtmlPage;
1616
import com.gargoylesoftware.htmlunit.html.HtmlRadioButtonInput;
1717
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
18-
import java.io.BufferedReader;
19-
import java.io.InputStream;
20-
import java.io.InputStreamReader;
2118
import java.net.Authenticator;
2219
import java.net.PasswordAuthentication;
23-
import java.net.URL;
2420
import java.util.List;
2521
import java.util.Map;
2622
import javax.xml.bind.DatatypeConverter;
@@ -45,26 +41,18 @@ public class ITMonitoringExporter extends BaseTest {
4541
private static Operator operator = null;
4642
private static Domain domain = null;
4743
private static String myhost = "";
48-
private static String metricsUrl = "";
4944
private static String exporterUrl = "";
50-
private static String serverName = "managed-server";
5145
private static String configPath = "";
52-
private static String appName = TESTWSAPP;
53-
private static String appNameUC = "TestWSApp";
54-
private static String testWSAppTotalServletInvokesSearchKey1 =
55-
"weblogic_servlet_invocation_total_count{app=\"testwsapp\",name=\"managed-server1_/TestWSApp\",servletName=\"TestWSAppServlethttp\"}";
56-
private static String testWSAppTotalServletInvokesSearchKey2 =
57-
"weblogic_servlet_invocation_total_count{app=\"testwsapp\",name=\"managed-server2_/TestWSApp\",servletName=\"TestWSAppServlethttp\"}";
58-
private static String loadBalancer = "TRAEFIK";
46+
private static String metricsUrl = "";
47+
// "heap_free_current{name="managed-server1"}[15s]" search for results for last 15secs
5948
private static String prometheusSearchKey1 =
60-
"heap_free_current%7Bname%3D%22managed-server1%22%7D";
49+
"heap_free_current%7Bname%3D%22managed-server1%22%7D%5B15s%5D";
6150
private static String prometheusSearchKey2 =
62-
"heap_free_current%7Bname%3D%22managed-server2%22%7D";
51+
"heap_free_current%7Bname%3D%22managed-server2%22%7D%5B15s%5D";
6352
private static String testwsappPrometheusSearchKey =
64-
"weblogic_servlet_invocation_total_count%7Bapp%3D%22testwsapp%22%7D";
53+
"weblogic_servlet_invocation_total_count%7Bapp%3D%22testwsapp%22%7D%5B15s%5D";
6554
String oprelease = "op" + number;
6655
private int waitTime = 5;
67-
private int maxIterations = 30;
6856

6957
/**
7058
* This method gets called only once before any of the test methods are executed. It does the
@@ -91,7 +79,7 @@ public static void staticPrepare() throws Exception {
9179
myhost = domain.getHostNameForCurl();
9280
exporterUrl = "http://" + myhost + ":" + domain.getLoadBalancerWebPort() + "/wls-exporter/";
9381
metricsUrl = exporterUrl + "metrics";
94-
configPath = BaseTest.getProjectRoot() + "/integration-tests/src/test/resources/exporter/";
82+
configPath = BaseTest.getProjectRoot() + "/integration-tests/src/test/resources/exporter";
9583
upgradeTraefikHostName();
9684
deployRunMonitoringExporter(domain, operator);
9785
buildDeployWebServiceApp(domain, TESTWSAPP, TESTWSSERVICE);
@@ -158,47 +146,9 @@ private static void startExporterPrometheusGrafana(Domain domain, Operator opera
158146
throws Exception {
159147
logger.info("deploy exporter, prometheus, grafana ");
160148
TestUtils.deployMonitoringExporterPrometethusGrafana(
161-
TestUtils.monitoringDir + "/apps/monitoringexporter/wls-exporter.war", domain, operator);
162-
}
163-
164-
private static Object getMetricsValue(BufferedReader contents, String metricKey)
165-
throws Exception {
166-
boolean found = false;
167-
String line;
168-
String result = null;
169-
while ((line = contents.readLine()) != null) {
170-
if (line.contains(metricKey)) {
171-
found = true;
172-
result = line.substring(line.lastIndexOf(" ") + 1);
173-
}
174-
}
175-
return result;
176-
}
177-
178-
private static boolean containsWordsIndexOf(String inputString, String[] words) {
179-
boolean found = true;
180-
for (String word : words) {
181-
logger.info(" Checking inputString" + inputString + " word " + word);
182-
if (inputString.indexOf(word) == -1) {
183-
found = false;
184-
break;
185-
}
186-
}
187-
return found;
188-
}
189-
190-
private static Object getMetricsValue(BufferedReader contents, String... metricExp)
191-
throws Exception {
192-
boolean found = false;
193-
String line;
194-
String result = null;
195-
while ((line = contents.readLine()) != null) {
196-
if (containsWordsIndexOf(line, metricExp)) {
197-
logger.info("found metric value for " + line + " :");
198-
result = line.substring(line.lastIndexOf(" ") + 1);
199-
}
200-
}
201-
return result;
149+
TestUtils.monitoringExporterDir + "/apps/monitoringexporter/wls-exporter.war",
150+
domain,
151+
operator);
202152
}
203153

204154
private static void setCredentials(WebClient webClient) {
@@ -280,28 +230,23 @@ public void test01_CheckMetricsViaPrometheus() throws Exception {
280230
*
281231
* @throws Exception
282232
*/
283-
// commenting out due bug OWLS-74163
284-
// @Test
233+
@Test
285234
public void test02_ReplaceConfiguration() throws Exception {
286235
Assume.assumeFalse(QUICKTEST);
287236
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
288237
logTestBegin(testMethodName);
289238
boolean testCompletedSuccessfully = false;
290-
291239
HtmlPage page = submitConfigureForm(exporterUrl, "replace", configPath + "/rest_jvm.yml");
292240

293-
// check for updated metrics
294-
String searchKey1 = "heap_free_current{name=\"managed-server1\"}";
295-
String searchKey2 = "heap_free_current{name=\"managed-server2\"}";
296241
boolean isFoundNewKey1 = false;
297242
boolean isFoundNewKey2 = false;
298243
boolean isFoundOldKey1 = true;
299244
boolean isFoundOldKey2 = true;
300245

301-
isFoundNewKey1 = checkMetrics(searchKey1);
302-
isFoundNewKey2 = checkMetrics(searchKey2);
303-
isFoundOldKey1 = checkMetrics(testWSAppTotalServletInvokesSearchKey1);
304-
isFoundOldKey2 = checkMetrics(testWSAppTotalServletInvokesSearchKey2);
246+
isFoundNewKey1 = checkMetricsViaPrometheus(prometheusSearchKey1, "managed-server1");
247+
isFoundNewKey2 = checkMetricsViaPrometheus(prometheusSearchKey2, "managed-server2");
248+
isFoundOldKey1 = checkMetricsViaPrometheus(testwsappPrometheusSearchKey, "managed-server1");
249+
isFoundOldKey2 = checkMetricsViaPrometheus(testwsappPrometheusSearchKey, "managed-server2");
305250
String foundResults =
306251
" server1: ( newMetrics:"
307252
+ isFoundNewKey1
@@ -659,10 +604,10 @@ public void test14_AppendMetricsNameSnakeCaseTrueToSnakeCaseFalseConfiguration()
659604
String testMethodName = new Object() {}.getClass().getEnclosingMethod().getName();
660605
logTestBegin(testMethodName);
661606
boolean testCompletedSuccessfully = false;
607+
662608
HtmlPage page =
663609
submitConfigureForm(exporterUrl, "append", configPath + "/rest_snakecasetrue.yml");
664610
assertNotNull(page);
665-
assertTrue(page.asText().contains("metricsNameSnakeCase"));
666611
String searchKey = "weblogic_servlet_executionTimeAverage%7Bapp%3D%22testwsapp%22%7D";
667612
assertFalse(checkMetricsViaPrometheus(searchKey, "testwsapp"));
668613
testCompletedSuccessfully = true;
@@ -798,15 +743,6 @@ private void changeConfigNegativeAuth(
798743
}
799744
}
800745

801-
private Object getMetricsFromPage(String testUrl, String searchKey) throws Exception {
802-
Authenticator.setDefault(new MyTestAuthenticator());
803-
InputStream stream = new URL(testUrl).openStream();
804-
BufferedReader contents = new BufferedReader(new InputStreamReader(stream));
805-
assertNotNull(contents);
806-
807-
return getMetricsValue(contents, searchKey);
808-
}
809-
810746
private HtmlPage submitConfigureForm(
811747
String exporterUrl, String effect, String configFile, String username, String password)
812748
throws Exception {
@@ -858,7 +794,7 @@ private HtmlPage submitConfigureForm(
858794
assertNotNull(page2);
859795
return page2;
860796
}
861-
797+
/*
862798
private boolean checkMetrics(String searchKey) throws Exception {
863799
boolean result = false;
864800
int i = 0;
@@ -878,6 +814,7 @@ private boolean checkMetrics(String searchKey) throws Exception {
878814
}
879815
return result;
880816
}
817+
*/
881818
}
882819

883820
class MyTestAuthenticator extends Authenticator {

integration-tests/src/test/java/oracle/kubernetes/operator/utils/TestUtils.java

Lines changed: 43 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,8 @@ public class TestUtils {
4242
private static final Logger logger = Logger.getLogger("OperatorIT", "OperatorIT");
4343

4444
private static K8sTestUtils k8sTestUtils = new K8sTestUtils();
45-
public static String monitoringDir = "";
45+
public static String monitoringExporterDir = "";
46+
public static String resourceExporterDir = "";
4647

4748
/**
4849
* @param cmd - kubectl get pod <podname> -n namespace
@@ -940,7 +941,7 @@ public static void createDirUnderDomainPV(String dirPath) throws Exception {
940941
* @throws IOException when copying files from source location to staging area fails
941942
*/
942943
private static void createCrossNSRBACFile(String domainNS, String operatorNS) throws IOException {
943-
String samplesDir = monitoringDir + "/src/samples/kubernetes/";
944+
String samplesDir = monitoringExporterDir + "/src/samples/kubernetes/";
944945
Path src = Paths.get(samplesDir + "/crossnsrbac.yaml");
945946
Path dst = Paths.get(samplesDir + "/crossnsrbac_" + domainNS + "_" + operatorNS + ".yaml");
946947
if (!dst.toFile().exists()) {
@@ -954,23 +955,47 @@ private static void createCrossNSRBACFile(String domainNS, String operatorNS) th
954955
}
955956
}
956957

958+
/**
959+
* A utility method to add desired domain namespace to coordinator yaml template file replacing
960+
* the DOMAIN_NS,
961+
*
962+
* @throws IOException when copying files from source location to staging area fails
963+
*/
964+
private static void createCoordinatorFile(String domainNS) throws IOException {
965+
// String coordinatorDir = BaseTest.getProjectRoot() +
966+
// "/integration-tests/src/test/resources/exporter/";
967+
Path src = Paths.get(resourceExporterDir + "/coordinator.yml");
968+
Path dst = Paths.get(resourceExporterDir + "/coordinator_" + domainNS + ".yaml");
969+
if (!dst.toFile().exists()) {
970+
logger.log(Level.INFO, "Copying {0}", src.toString());
971+
Charset charset = StandardCharsets.UTF_8;
972+
String content = new String(Files.readAllBytes(src), charset);
973+
content = content.replaceAll("default", domainNS);
974+
logger.log(Level.INFO, "to {0}", dst.toString());
975+
Files.write(dst, content.getBytes(charset));
976+
}
977+
}
978+
957979
/**
958980
* Remove monitoring exporter directory if exists and clone latest from github for monitoring
959981
* exporter code
960982
*
961983
* @throws Exception if could not run the command successfully to clone from github
962984
*/
963985
public static void gitCloneBuildMonitoringExporter() throws Exception {
964-
monitoringDir = BaseTest.getResultDir() + "/monitoring";
965-
String monitoringExporterSrcDir = monitoringDir + "/src";
986+
monitoringExporterDir = BaseTest.getResultDir() + "/monitoring";
987+
resourceExporterDir =
988+
BaseTest.getProjectRoot() + "/integration-tests/src/test/resources/exporter";
989+
String monitoringExporterSrcDir = monitoringExporterDir + "/src";
966990
String resourceDir =
967991
BaseTest.getProjectRoot() + "/integration-tests/src/test/resources/exporter";
968-
String monitoringExporterWar = monitoringDir + "/apps/monitoringexporter/wls-exporter.war";
992+
String monitoringExporterWar =
993+
monitoringExporterDir + "/apps/monitoringexporter/wls-exporter.war";
969994
if (new File(monitoringExporterWar).exists()) {
970995
logger.info(" Weblogic Server Monitoring Exporter application is ready to use");
971996
} else {
972-
if (!new File(monitoringDir).exists()) {
973-
Files.createDirectories(Paths.get(monitoringDir));
997+
if (!new File(monitoringExporterDir).exists()) {
998+
Files.createDirectories(Paths.get(monitoringExporterDir));
974999
}
9751000
if (!monitoringExporterSrcDir.isEmpty()) {
9761001
StringBuffer removeAndClone = new StringBuffer();
@@ -1012,15 +1037,10 @@ public static void gitCloneBuildMonitoringExporter() throws Exception {
10121037
.append(" docker build -t config_coordinator . ");
10131038
executeCmd(buildCoordinatorImage.toString());
10141039

1015-
StringBuffer deployCoordinatorImage = new StringBuffer();
1016-
1017-
deployCoordinatorImage.append(" kubectl apply -f ").append(resourceDir + "/coordinator.yml ");
1018-
executeCmd(deployCoordinatorImage.toString());
1019-
10201040
buildExporterWAR = new StringBuffer();
10211041
buildExporterWAR
1022-
.append(" mkdir " + monitoringDir + "/apps")
1023-
.append(" && mkdir " + monitoringDir + "/apps/monitoringexporter")
1042+
.append(" mkdir " + monitoringExporterDir + "/apps")
1043+
.append(" && mkdir " + monitoringExporterDir + "/apps/monitoringexporter")
10241044
.append(" && ")
10251045
.append(" cp " + monitoringExporterSrcDir)
10261046
.append("/webapp/target/wls-exporter.war ")
@@ -1032,7 +1052,7 @@ public static void gitCloneBuildMonitoringExporter() throws Exception {
10321052
public static void deployMonitoringExporterPrometethusGrafana(
10331053
String exporterAppPath, Domain domain, Operator operator) throws Exception {
10341054

1035-
String samplesDir = monitoringDir + "/src/samples/kubernetes/";
1055+
String samplesDir = monitoringExporterDir + "/src/samples/kubernetes/";
10361056

10371057
String crdCmd = " kubectl apply -f " + samplesDir + "monitoring-namespace.yaml";
10381058
ExecResult result = ExecCommand.exec(crdCmd);
@@ -1048,6 +1068,14 @@ public static void deployMonitoringExporterPrometethusGrafana(
10481068
result = ExecCommand.exec(crdCmd);
10491069
logger.info("command result " + result.stdout().trim());
10501070

1071+
// create and start coordinator
1072+
createCoordinatorFile(domainNS);
1073+
StringBuffer deployCoordinatorImage = new StringBuffer();
1074+
deployCoordinatorImage
1075+
.append(" kubectl create -f ")
1076+
.append(resourceExporterDir + "/coordinator_" + domainNS + ".yaml ");
1077+
executeCmd(deployCoordinatorImage.toString());
1078+
10511079
crdCmd = " kubectl apply -f " + samplesDir + "grafana-deployment.yaml";
10521080
result = ExecCommand.exec(crdCmd);
10531081
logger.info("command result " + result.stdout().trim());

integration-tests/src/test/resources/exporter/coordinator.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ apiVersion: extensions/v1beta1
22
kind: Deployment
33
metadata:
44
name: coordinator
5+
namespace: default
56
labels:
67
app: coordinator
78
spec:
@@ -27,6 +28,7 @@ metadata:
2728
labels:
2829
app: coordinator
2930
name: coordinator
31+
namespace: default
3032
spec:
3133
type: NodePort
3234
ports:

0 commit comments

Comments
 (0)