-
Notifications
You must be signed in to change notification settings - Fork 912
/
CompareUpgradeTest.java
281 lines (219 loc) · 13.6 KB
/
CompareUpgradeTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/*
* 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
*
* http://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.activemq.artemis.tests.smoke.upgradeTest;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.lang.invoke.MethodHandles;
import java.nio.file.Files;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.stream.Stream;
import org.apache.activemq.artemis.cli.commands.Create;
import org.apache.activemq.artemis.cli.commands.Upgrade;
import org.junit.Assert;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/** This test will compare expected values at the upgraded servers. */
public class CompareUpgradeTest {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public static final String basedir = System.getProperty("basedir");
@Test
public void testCompareWindowsFreshInstance() throws Exception {
String windows = basedir + "/target/classes/servers/windowsUpgrade";
String windowsBin = windows + "/bin";
String windowsETC = basedir + "/target/classes/servers/windowsUpgradeETC";
String windowsExpected = basedir + "/target/classes/servers/windowsUpgradeExpected";
String windowsExpectedBin = windowsExpected + "/bin";
String windowsExpectedETC = basedir + "/target/classes/servers/windowsUpgradeETCExpected";
compareDirectories(windowsExpectedBin, windowsBin);
compareDirectories(windowsExpectedETC, windowsETC, "broker.xml", "artemis-users.properties", "management.xml");
String referenceBin = basedir + "/target/reference-for-backup-check/servers/windowsUpgrade/bin";
String referenceEtc = basedir + "/target/reference-for-backup-check/servers/windowsUpgradeETC";
verifyBackupFiles(windows + "/old-config-bkp.0/bin", referenceBin, Create.ARTEMIS_CMD, Create.ARTEMIS_SERVICE_EXE, Create.ARTEMIS_SERVICE_EXE_CONFIG, Create.ARTEMIS_SERVICE_XML);
verifyBackupFiles(windows + "/old-config-bkp.0/etc", referenceEtc, Create.ETC_ARTEMIS_PROFILE_CMD, Create.ETC_BOOTSTRAP_XML, Upgrade.OLD_LOGGING_PROPERTIES);
}
@Test
public void testCompareLinuxFreshInstance() throws Exception {
String linux = basedir + "/target/classes/servers/linuxUpgrade";
String linuxBin = linux + "/bin";
String linuxETC = basedir + "/target/classes/servers/linuxUpgradeETC";
String linuxExpected = basedir + "/target/classes/servers/linuxUpgradeExpected";
String linuxExpectedBin = linuxExpected + "/bin";
String linuxExpectedETC = basedir + "/target/classes/servers/linuxUpgradeETCExpected";
compareDirectories(linuxExpectedBin, linuxBin);
compareDirectories(linuxExpectedETC, linuxETC, "broker.xml", "artemis-users.properties", "management.xml");
String referenceBin = basedir + "/target/reference-for-backup-check/servers/linuxUpgrade/bin";
String referenceEtc = basedir + "/target/reference-for-backup-check/servers/linuxUpgradeETC";
verifyBackupFiles(linux + "/old-config-bkp.0/bin", referenceBin, Create.ARTEMIS, Create.ARTEMIS_SERVICE);
verifyBackupFiles(linux + "/old-config-bkp.0/etc", referenceEtc, Create.ETC_ARTEMIS_PROFILE, Create.ETC_BOOTSTRAP_XML, Upgrade.OLD_LOGGING_PROPERTIES);
}
private void verifyBackupFiles(String backupFolder, String referenceFolder, String... files) throws Exception {
assertTrue("Files to check must be specified", files.length > 0);
File bck = new File(backupFolder);
if (!(bck.exists() && bck.isDirectory())) {
Assert.fail("Backup folder does not exist at: " + bck.getAbsolutePath());
}
File[] backupFiles = bck.listFiles();
assertNotNull("Some backup files must exist", backupFiles);
int backupFilesCount = backupFiles.length;
assertTrue("Some backup files must exist", backupFilesCount > 0);
assertEquals("Different number of backup files found than specified for inspection, update test if backup procedure changed", files.length, backupFilesCount);
for (String f : files) {
File bf = new File(backupFolder, f);
if (!bf.exists()) {
Assert.fail("Expected backup file does not exist at: " + bf.getAbsolutePath());
}
File reference = new File(referenceFolder, bf.getName());
if (!reference.exists()) {
Assert.fail("Reference file does not exist at: " + reference.getAbsolutePath());
}
Assert.assertArrayEquals(bf.getName() + " backup contents do not match reference file", Files.readAllBytes(bf.toPath()), Files.readAllBytes(reference.toPath()));
}
}
private void compareDirectories(String expectedFolder, String upgradeFolder, String... ignoredFiles) throws Exception {
File expectedFolderFile = new File(expectedFolder);
File[] foundFiles = expectedFolderFile.listFiles(pathname -> {
for (String i :ignoredFiles) {
if (pathname.getName().contains(i)) {
return false;
}
}
return true;
});
File upgradeFolderFile = new File(upgradeFolder);
for (File f : foundFiles) {
File upgradeFile = new File(upgradeFolderFile, f.getName());
if (!upgradeFile.exists()) {
Assert.fail(upgradeFile.getAbsolutePath() + " not does exist");
}
if (f.getName().endsWith(".exe")) {
Assert.assertArrayEquals(f.getName() + " is different after upgrade", Files.readAllBytes(f.toPath()), Files.readAllBytes(upgradeFile.toPath()));
} else {
try (Stream<String> expectedStream = Files.lines(f.toPath()); Stream<String> upgradeStream = Files.lines(upgradeFile.toPath())) {
Iterator<String> expectedIterator = expectedStream.iterator();
Iterator<String> upgradeIterator = upgradeStream.iterator();
int line = 1;
while (expectedIterator.hasNext()) {
Assert.assertTrue(upgradeIterator.hasNext());
String expectedString = expectedIterator.next().replace("Expected", "").trim();
String upgradeString = upgradeIterator.next().trim();
if (f.getName().endsWith("jolokia-access.xml") && expectedString.startsWith("<!--") && upgradeString.startsWith("<!--")) {
// TODO: for now we will just ignore comments in jolokia-access.xml. We should do a proper parser of jologia-access.xml
logger.debug("Ignoring comments from jolokia-access.xml");
} else {
Assert.assertEquals("error on line " + line + " at " + upgradeFile, expectedString, upgradeString);
}
line++;
}
Assert.assertFalse(upgradeIterator.hasNext());
}
}
}
}
@Test
public void testWindows() throws Exception {
String windows = basedir + "/target/classes/servers/windowsUpgrade";
String windowsBin = windows + "/bin";
String windowsETC = basedir + "/target/classes/servers/windowsUpgradeETC";
checkExpectedValues(windowsBin + "/artemis.cmd", "set ARTEMIS_INSTANCE_ETC=", "\"" + windowsETC + "\"");
Map<String, String> result = checkExpectedValues(windowsBin + "/artemis-service.xml",
"<env name=\"ARTEMIS_HOME\" value=", null, // no expected value for this, we will check on the output
"<env name=\"ARTEMIS_INSTANCE\" value=", "\"" + windows + "\"/>",
"<env name=\"ARTEMIS_INSTANCE_ETC\" value=", "\"" + windowsETC + "\"/>",
"<env name=\"ARTEMIS_INSTANCE_URI\" value=", "\"file:" + windows + "/\"/>",
"<env name=\"ARTEMIS_DATA_DIR\" value=", "\"" + windows + "/data\"/>"
);
String home = result.get("<env name=\"ARTEMIS_HOME\" value=");
Assert.assertNotNull(home);
Assert.assertFalse("home value must be changed during upgrade", home.contains("must-change"));
result = checkExpectedValues(windowsETC + "/artemis.profile.cmd",
"set ARTEMIS_HOME=", null, // no expected value for this, we will check on the output
"set ARTEMIS_INSTANCE=", "\"" + windows + "\"",
"set ARTEMIS_DATA_DIR=","\"" + windows + "/data\"",
"set ARTEMIS_ETC_DIR=", "\"" + windowsETC + "\"",
"set ARTEMIS_OOME_DUMP=", "\"" + windows + "/log/oom_dump.hprof\"",
"set ARTEMIS_INSTANCE_URI=", "\"file:" + windows + "/\"",
"set ARTEMIS_INSTANCE_ETC_URI=", "\"file:" + windowsETC + "/\"");
home = result.get("set ARTEMIS_HOME=");
Assert.assertNotNull(home);
Assert.assertFalse("home value must be changed during upgrade", home.contains("must-change"));
checkExpectedValues(windowsETC + "/bootstrap.xml",
"<server configuration=", "\"file:" + windowsETC + "//broker.xml\"/>");
File oldLogging = new File(windowsETC + "/logging.properties");
File newLogging = new File(windowsETC + "/log4j2.properties");
Assert.assertFalse("Old logging must be removed by upgrade", oldLogging.exists());
Assert.assertTrue("New Logging must be installed by upgrade", newLogging.exists());
}
@Test
public void testLinux() throws Exception {
String instanceDir = basedir + "/target/classes/servers/linuxUpgrade";
String bin = instanceDir + "/bin";
String etc = basedir + "/target/classes/servers/linuxUpgradeETC";
checkExpectedValues(bin + "/artemis", "ARTEMIS_INSTANCE_ETC=", "'" + etc + "'");
Map<String, String> result = checkExpectedValues(etc + "/artemis.profile",
"ARTEMIS_HOME=", null, // no expected value, will check on result
"ARTEMIS_INSTANCE=", "'" + instanceDir + "'",
"ARTEMIS_DATA_DIR=", "'" + instanceDir + "/data'",
"ARTEMIS_ETC_DIR=", "'" + etc + "'",
"ARTEMIS_OOME_DUMP=", "'" + instanceDir + "/log/oom_dump.hprof'",
"ARTEMIS_INSTANCE_URI=", "'file:" + instanceDir + "/'",
"ARTEMIS_INSTANCE_ETC_URI=", "'file:" + etc + "/'");
String home = result.get("ARTEMIS_HOME=");
Assert.assertNotNull(home);
Assert.assertNotEquals("'must-change'", home);
File oldLogging = new File(etc + "/logging.properties");
File newLogging = new File(etc + "/log4j2.properties");
Assert.assertFalse("Old logging must be removed by upgrade", oldLogging.exists());
Assert.assertTrue("New Logging must be installed by upgrade", newLogging.exists());
}
private Map<String, String> checkExpectedValues(String fileName, String... expectedPairs) throws Exception {
Assert.assertTrue("You must pass a pair of expected values", expectedPairs.length > 0 && expectedPairs.length % 2 == 0);
HashMap<String, String> expectedValues = new HashMap<>();
HashMap<String, String> matchingValues = new HashMap<>();
for (int i = 0; i < expectedPairs.length; i += 2) {
Assert.assertFalse("Value duplicated on pairs ::" + expectedPairs[i], expectedValues.containsKey(expectedPairs[i]));
expectedValues.put(expectedPairs[i], expectedPairs[i + 1]);
}
File file = new File(fileName);
try (Stream<String> lines = Files.lines(file.toPath())) {
lines.forEach(line -> {
String trimmedLine = line.trim();
expectedValues.forEach((key, value) -> {
if (trimmedLine.startsWith(key)) {
String actualValue = trimmedLine.substring(key.length());
logger.debug("match = {}", line);
matchingValues.put(key, actualValue);
if (value == null) {
logger.debug("no expected value was defined for {}, we will just fill out the matchingValues for further evaluation", key);
} else {
if (logger.isDebugEnabled()) {
logger.debug("prefix={}, expecting={}, actualValue={}", key, value, actualValue);
}
Assert.assertEquals(key + " did not match", value, actualValue);
}
}
});
});
}
Assert.assertEquals("Some elements were not found in the output of " + fileName, matchingValues.size(), expectedValues.size());
return matchingValues;
}
}