diff --git a/patching/src/test/java/org/jboss/as/patching/cli/CLIPatchInfoUtil.java b/patching/src/test/java/org/jboss/as/patching/cli/CLIPatchInfoUtil.java deleted file mode 100644 index 78a588de6bd..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/CLIPatchInfoUtil.java +++ /dev/null @@ -1,141 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.junit.Assert; - -/** - * - * @author Alexey Loubyansky - */ -public class CLIPatchInfoUtil { - - private static final String PATCH_ID = "Patch ID"; - private static final String TYPE = "Type"; - private static final String IDENTITY_NAME = "Identity name"; - private static final String IDENTITY_VERSION = "Identity version"; - private static final String DESCR = "Description"; - - private static final String CP = "cumulative"; - private static final String ONE_OFF = "one-off"; - - public static void assertPatchInfo(byte[] info, String patchId, String link, boolean oneOff, String targetName, String targetVersion, - String description) { - try (final ByteArrayInputStream bis = new ByteArrayInputStream(info); - final InputStreamReader reader = new InputStreamReader(bis, StandardCharsets.UTF_8); - final BufferedReader buf = new BufferedReader(reader)){ - assertPatchInfo(buf, patchId, link, oneOff, targetName, targetVersion, description); - } catch (IOException e) { - // - } - } - - public static void assertPatchInfo(BufferedReader buf, String patchId, String link, boolean oneOff, String targetName, String targetVersion, - String description) throws IOException { - - final Map actual = parseTable(buf); - - final Map expected = new HashMap(); - expected.put(PATCH_ID, patchId); - expected.put(TYPE, oneOff ? ONE_OFF : CP); - expected.put(IDENTITY_NAME, targetName); - expected.put(IDENTITY_VERSION, targetVersion); - expected.put(DESCR, description); - expected.put("Link", link); - - Assert.assertEquals(expected, actual); - } - - public static void assertPatchInfo(byte[] info, String patchId, String link, boolean oneOff, String targetName, String targetVersion, - String description, List> elements) { - - try (final ByteArrayInputStream bis = new ByteArrayInputStream(info); - final InputStreamReader reader = new InputStreamReader(bis, StandardCharsets.UTF_8); - final BufferedReader buf = new BufferedReader(reader)){ - assertPatchInfo(buf, patchId, link, oneOff, targetName, targetVersion, description, elements); - if (buf.ready()) { - final StringBuilder str = new StringBuilder(); - String line; - while ((line = buf.readLine()) != null) { - str.append(line).append("\n"); - } - Assert.fail("The output contained more info: " + str.toString()); - } - } catch (IOException e) { - throw new IllegalStateException("Failed to read the input", e); - } - } - - public static void assertPatchInfo(BufferedReader buf, String patchId, String link, boolean oneOff, String targetName, String targetVersion, - String description, List> elements) throws IOException { - - final Map expected = new HashMap(); - expected.put(PATCH_ID, patchId); - expected.put(TYPE, oneOff ? ONE_OFF : CP); - expected.put(IDENTITY_NAME, targetName); - expected.put(IDENTITY_VERSION, targetVersion); - expected.put(DESCR, description); - expected.put("Link", link); - - Map actual = parseTable(buf); - Assert.assertEquals(expected, actual); - - if (buf.ready()) { - String readLine = buf.readLine(); - if (!"ELEMENTS".equals(readLine)) { - Assert.fail("Expected 'ELEMENTS' but was '" + readLine + "'"); - } - if (!buf.ready()) { - Assert.fail("Expected an empty line"); - } - readLine = buf.readLine(); - if (readLine == null || !readLine.isEmpty()) { - Assert.fail("Expected an empty line but received '" + readLine + "'"); - } - } - - for (Map e : elements) { - if (!buf.ready()) { - Assert.fail("No more output"); - } - actual = parseTable(buf); - Assert.assertEquals(e, actual); - } - } - - public static Map parseTable(byte[] table) throws IOException { - try (final ByteArrayInputStream bis = new ByteArrayInputStream(table); - final InputStreamReader reader = new InputStreamReader(bis, StandardCharsets.UTF_8); - final BufferedReader buf = new BufferedReader(reader)) { - return parseTable(buf); - } - } - - public static Map parseTable(final BufferedReader buf) throws IOException { - final Map actual = new HashMap(); - String line = null; - while ((line = buf.readLine()) != null && !line.isEmpty()) { - final int colon = line.indexOf(':'); - if (colon < 0) { - Assert.fail("Failed to locate ':' in '" + line + "'"); - } - if (colon == line.length() - 1) { - Assert.fail("The line appears to end on ':'"); - } - actual.put(line.substring(0, colon), line.substring(colon + 1).trim()); - } - return actual; - } -} diff --git a/patching/src/test/java/org/jboss/as/patching/cli/ContentConflictsUnitTestCase.java b/patching/src/test/java/org/jboss/as/patching/cli/ContentConflictsUnitTestCase.java deleted file mode 100644 index 9c2b195344a..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/ContentConflictsUnitTestCase.java +++ /dev/null @@ -1,240 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import static org.jboss.as.patching.Constants.BASE; -import static org.jboss.as.patching.Constants.LAYERS; -import static org.jboss.as.patching.Constants.SYSTEM; -import static org.jboss.as.patching.IoUtils.mkdir; -import static org.jboss.as.patching.IoUtils.newFile; -import static org.jboss.as.patching.runner.TestUtils.createInstalledImage; -import static org.jboss.as.patching.runner.TestUtils.createModule0; -import static org.jboss.as.patching.runner.TestUtils.createPatchXMLFile; -import static org.jboss.as.patching.runner.TestUtils.createZippedPatchFile; -import static org.jboss.as.patching.runner.TestUtils.dump; -import static org.jboss.as.patching.runner.TestUtils.randomString; -import static org.jboss.as.patching.runner.TestUtils.touch; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.fail; - -import java.io.File; - -import org.jboss.as.cli.CommandContext; -import org.jboss.as.cli.CommandContextFactory; -import org.jboss.as.cli.CommandLineException; -import org.jboss.as.patching.logging.PatchLogger; -import org.jboss.as.patching.metadata.ContentModification; -import org.jboss.as.patching.metadata.Patch; -import org.jboss.as.patching.metadata.PatchBuilder; -import org.jboss.as.patching.runner.AbstractTaskTestCase; -import org.jboss.as.patching.runner.ContentModificationUtils; -import org.junit.Test; - - -/** - * @author Alexey Loubyansky - * - */ -public class ContentConflictsUnitTestCase extends AbstractTaskTestCase { - - /** - * Tests content conflicts reporting in the CLI during patch application. - * - * The test creates 2 misc files, 2 modules and 2 bundles and a patch - * which updates all of them. Before the patch is applied, one file, - * one module and one bundle are modified on the disk. The patch is - * expected to fail and the failure description should contain the - * info about the conflicting content items. - * - * @throws Exception - */ - @Test - public void testApply() throws Exception { - - final File binDir = createInstalledImage(env, "consoleSlot", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a module for the conflict - File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); - String moduleConflictName = "module-conflict"; - File moduleConflictDir = createModule0(baseModuleDir, moduleConflictName); - // create the patch with the updated module - ContentModification moduleConflictModified = ContentModificationUtils.modifyModule(patchDir, patchID, moduleConflictDir, "new resource in the module"); - - // create a module to be updated w/o a conflict - String moduleNoConflictName = "module-no-conflict"; - File moduleNoConflictDir = createModule0(baseModuleDir, moduleNoConflictName); - // create the patch with the updated module - ContentModification moduleNoConflictModified = ContentModificationUtils.modifyModule(patchDir, patchID, moduleNoConflictDir, "new resource in the module"); - - // create a file for the conflict - String fileConflictName = "file-conflict.txt"; - File conflictFile = touch(binDir, fileConflictName); - dump(conflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", conflictFile, "bin", fileConflictName); - - // create a file for the conflict - String fileNoConflictName = "file-no-conflict.txt"; - File noConflictFile = touch(binDir, fileNoConflictName); - dump(noConflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileNoConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", noConflictFile, "bin", fileNoConflictName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(randomString()) - .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) - .getParent() - .addContentModification(fileConflictModified) - .addContentModification(fileNoConflictModified) - .oneOffPatchElement(patchID, "base", false) - .addContentModification(moduleConflictModified) - .addContentModification(moduleNoConflictModified) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - // create a conflict for the file - dump(conflictFile, "conflicting change"); - // create a conflict for the module - createModule0(baseModuleDir, moduleConflictName, "oops"); - - // apply the patch using the cli - CommandContext ctx = CommandContextFactory.getInstance().newCommandContext(); - try { - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - fail("Conflicts expected."); - } catch(CommandLineException e) { - //e.printStackTrace(); - final int relativeIndex = env.getInstalledImage().getJbossHome().getAbsolutePath().length() + 1; - assertConflicts(e, moduleConflictName + ":main", conflictFile.getAbsolutePath().substring(relativeIndex)); - } finally { - ctx.terminateSession(); - } - } - - /** - * Tests content conflicts reporting in the CLI during patch rollback. - * - * The test creates 2 misc files, 2 modules and 2 bundles and a patch - * which updates all of them. The patch is applied. Then one file, - * one module and one bundle are modified on the disk. The patch is - * rolled back then which is expected to fail and the failure description - * should contain the info about the conflicting content items. - * - * @throws Exception - */ - @Test - public void testRollback() throws Exception { - - final File binDir = createInstalledImage(env, "consoleSlot", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a module for the conflict - File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); - String moduleConflictName = "module-conflict"; - File moduleConflictDir = createModule0(baseModuleDir, moduleConflictName); - // create the patch with the updated module - ContentModification moduleConflictModified = ContentModificationUtils.modifyModule(patchDir, patchElementId, moduleConflictDir, "new resource in the module"); - - // create a module to be updated w/o a conflict - String moduleNoConflictName = "module-no-conflict"; - File moduleNoConflictDir = createModule0(baseModuleDir, moduleNoConflictName); - // create the patch with the updated module - ContentModification moduleNoConflictModified = ContentModificationUtils.modifyModule(patchDir, patchElementId, moduleNoConflictDir, "new resource in the module"); - - // create a file for the conflict - String fileConflictName = "file-conflict.txt"; - File conflictFile = touch(binDir, fileConflictName); - dump(conflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", conflictFile, "bin", fileConflictName); - - // create a file for the conflict - String fileNoConflictName = "file-no-conflict.txt"; - File noConflictFile = touch(binDir, fileNoConflictName); - dump(noConflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileNoConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", noConflictFile, "bin", fileNoConflictName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(randomString()) - .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) - .getParent() - .addContentModification(fileConflictModified) - .addContentModification(fileNoConflictModified) - .oneOffPatchElement(patchElementId, "base", false) - .addContentModification(moduleConflictModified) - .addContentModification(moduleNoConflictModified) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - // apply the patch using the cli - CommandContext ctx = CommandContextFactory.getInstance().newCommandContext(); - try { - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(CommandLineException e) { - ctx.terminateSession(); - fail("Failed to apply the patch: " + e); - } - - // create a conflict for the file - dump(conflictFile, "conflicting change"); - // create a conflict for the module - createModule0(baseModuleDir, moduleConflictName, "oops"); - - try { - ctx.handle("patch rollback --patch-id=" + patchID + " --distribution=" + env.getInstalledImage().getJbossHome() + " --reset-configuration=false"); - fail("Conflicts expected"); - } catch(CommandLineException e) { - final int relativeIndex = env.getInstalledImage().getJbossHome().getAbsolutePath().length() + 1; - // TODO modules and bundles are not checked at the moment - assertConflicts(e, moduleConflictName + ":main", conflictFile.getAbsolutePath().substring(relativeIndex)); - //assertConflicts(e, conflictFile.getAbsolutePath().substring(relativeIndex)); - } finally { - ctx.terminateSession(); - } - - } - - protected void assertConflicts(CommandLineException e, String... conflicts) { - final StringBuilder buf = new StringBuilder(); - buf.append(PatchLogger.ROOT_LOGGER.detectedConflicts()).append(": "); - int i = 0; - while(i < conflicts.length) { - buf.append(conflicts[i++].replace('\\', '/')); // fix paths on windows - if(i < conflicts.length) { - buf.append(", "); - } - } - String msg = e.getMessage().substring(e.getMessage().indexOf(PatchLogger.ROOT_LOGGER.detectedConflicts())); - assertEquals(msg.split(System.getProperty("line.separator"))[0], buf.toString()); - } -} diff --git a/patching/src/test/java/org/jboss/as/patching/cli/LocalPatchInfoPatchIdUnitTestCase.java b/patching/src/test/java/org/jboss/as/patching/cli/LocalPatchInfoPatchIdUnitTestCase.java deleted file mode 100644 index 19ea91415f0..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/LocalPatchInfoPatchIdUnitTestCase.java +++ /dev/null @@ -1,176 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import static org.jboss.as.patching.Constants.BASE; -import static org.jboss.as.patching.Constants.LAYERS; -import static org.jboss.as.patching.Constants.SYSTEM; -import static org.jboss.as.patching.IoUtils.mkdir; -import static org.jboss.as.patching.IoUtils.newFile; -import static org.jboss.as.patching.runner.TestUtils.createInstalledImage; -import static org.jboss.as.patching.runner.TestUtils.createModule0; -import static org.jboss.as.patching.runner.TestUtils.createPatchXMLFile; -import static org.jboss.as.patching.runner.TestUtils.createZippedPatchFile; -import static org.jboss.as.patching.runner.TestUtils.dump; -import static org.jboss.as.patching.runner.TestUtils.randomString; -import static org.jboss.as.patching.runner.TestUtils.touch; - -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -import org.jboss.as.cli.CommandContext; -import org.jboss.as.cli.CommandContextFactory; -import org.jboss.as.patching.metadata.ContentModification; -import org.jboss.as.patching.metadata.Patch; -import org.jboss.as.patching.metadata.PatchBuilder; -import org.jboss.as.patching.runner.AbstractTaskTestCase; -import org.jboss.as.patching.runner.ContentModificationUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - - -/** - * - * @author Alexey Loubyansky - */ -public class LocalPatchInfoPatchIdUnitTestCase extends AbstractTaskTestCase { - - private ByteArrayOutputStream bytesOs; - private CommandContext ctx; - - @Before - public void before() throws Exception { - bytesOs = new ByteArrayOutputStream(); - ctx = CommandContextFactory.getInstance().newCommandContext(null, null, null, System.in, bytesOs); - } - - @After - public void after() throws Exception { - if(ctx != null) { - ctx.terminateSession(); - } - if(bytesOs != null) { - bytesOs = null; - } - } - - @Test - public void testMain() throws Exception { - final File binDir = createInstalledImage(env, "patch-info-test", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a module to be updated w/o a conflict - File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); - String moduleName = "module-test"; - File moduleDir = createModule0(baseModuleDir, moduleName); - // create the patch with the updated module - ContentModification moduleModified = ContentModificationUtils.modifyModule(patchDir, patchElementId, moduleDir, "new resource in the module"); - - // create a file - String fileName = "file-test.txt"; - File miscFile = touch(binDir, fileName); - dump(miscFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", miscFile, "bin", fileName); - - // create a bundle to be updated w/o a conflict - File baseBundleDir = newFile(env.getInstalledImage().getBundlesDir(), SYSTEM, LAYERS, BASE); - String bundleName = "bundle-test"; - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - final String patchIDDescr = "this is one-off patch 1"; - final String oneOffElementDescr = "one off patch for the base"; - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(patchIDDescr) - .setLink("http://test.one") - .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) - .getParent() - .addContentModification(fileModified) - .oneOffPatchElement(patchElementId, "base", false) - .setDescription(oneOffElementDescr) - .addContentModification(moduleModified) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - - // next patch - final String patchID2 = randomString(); - final String patchElementId2 = randomString(); - - final File patchedModule = newFile(baseModuleDir, ".overlays", patchElementId, moduleName); - final File patchedBundle = newFile(baseBundleDir, ".overlays", patchElementId, bundleName); - - ContentModification fileModified2 = ContentModificationUtils.modifyMisc(patchDir, patchID2, "another file update", miscFile, "bin", fileName); - ContentModification moduleModified2 = ContentModificationUtils.modifyModule(patchDir, patchElementId2, patchedModule, "another module update"); - - final String patchID2Descr = "This is cumulative patch 2"; - final String cpElementDescr = "upgrade of the base layer"; - Patch patch2 = PatchBuilder.create() - .setPatchId(patchID2) - .setDescription(patchID2Descr) - .setLink("http://test.two") - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion(), productConfig.getProductName() + "CP2") - .getParent() - .addContentModification(fileModified2) - .upgradeElement(patchElementId2, "base", false) - .setDescription(cpElementDescr) - .addContentModification(moduleModified2) - .getParent() - .build(); - - createPatchXMLFile(patchDir, patch2, false); - final File zippedPatch2 = createZippedPatchFile(patchDir, patch2.getPatchId()); - ctx.handle("patch apply " + zippedPatch2.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - bytesOs.reset(); - ctx.handle("patch info --patch-id=" + patchID + " --distribution=" + env.getInstalledImage().getJbossHome()); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID, "http://test.one", true, - productConfig.getProductName(), productConfig.getProductVersion(), patchIDDescr); - - Map element = new HashMap(); - element.put("Patch ID", patchElementId); - element.put("Name", "base"); - element.put("Type", "layer"); - element.put("Description", oneOffElementDescr); - bytesOs.reset(); - ctx.handle("patch info --patch-id=" + patchID + " --verbose" + " --distribution=" + env.getInstalledImage().getJbossHome()); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID, "http://test.one", true, - productConfig.getProductName(), productConfig.getProductVersion(), patchIDDescr, Collections.singletonList(element)); - - bytesOs.reset(); - ctx.handle("patch info " + patchID2 + " --distribution=" + env.getInstalledImage().getJbossHome()); // w/o argument name --patch-id - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID2, "http://test.two", false, - productConfig.getProductName(), productConfig.getProductVersion(), patchID2Descr); - - element.put("Patch ID", patchElementId2); - element.put("Name", "base"); - element.put("Type", "layer"); - element.put("Description", cpElementDescr); - bytesOs.reset(); - ctx.handle("patch info " + patchID2 + " --verbose" + " --distribution=" + env.getInstalledImage().getJbossHome()); // w/o argument name --patch-id - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID2, "http://test.two", false, - productConfig.getProductName(), productConfig.getProductVersion(), patchID2Descr, Collections.singletonList(element)); - } -} diff --git a/patching/src/test/java/org/jboss/as/patching/cli/PatchInspectUnitTestCase.java b/patching/src/test/java/org/jboss/as/patching/cli/PatchInspectUnitTestCase.java deleted file mode 100644 index e5b55531c99..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/PatchInspectUnitTestCase.java +++ /dev/null @@ -1,231 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import static org.jboss.as.patching.Constants.BASE; -import static org.jboss.as.patching.Constants.LAYERS; -import static org.jboss.as.patching.Constants.SYSTEM; -import static org.jboss.as.patching.IoUtils.mkdir; -import static org.jboss.as.patching.IoUtils.newFile; -import static org.jboss.as.patching.runner.TestUtils.createInstalledImage; -import static org.jboss.as.patching.runner.TestUtils.createModule0; -import static org.jboss.as.patching.runner.TestUtils.createPatchXMLFile; -import static org.jboss.as.patching.runner.TestUtils.createZippedPatchFile; -import static org.jboss.as.patching.runner.TestUtils.dump; -import static org.jboss.as.patching.runner.TestUtils.randomString; -import static org.jboss.as.patching.runner.TestUtils.touch; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.io.BufferedReader; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.InputStreamReader; -import java.nio.charset.StandardCharsets; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; - -import org.jboss.as.cli.CommandContext; -import org.jboss.as.cli.CommandContextFactory; -import org.jboss.as.patching.IoUtils; -import org.jboss.as.patching.metadata.BundledPatch; -import org.jboss.as.patching.metadata.ContentModification; -import org.jboss.as.patching.metadata.Patch; -import org.jboss.as.patching.metadata.PatchBuilder; -import org.jboss.as.patching.runner.AbstractTaskTestCase; -import org.jboss.as.patching.runner.ContentModificationUtils; -import org.jboss.as.patching.runner.TestUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - - -/** - * - * @author Alexey Loubyansky - */ -public class PatchInspectUnitTestCase extends AbstractTaskTestCase { - - private ByteArrayOutputStream bytesOs; - private CommandContext ctx; - - @Before - public void before() throws Exception { - bytesOs = new ByteArrayOutputStream(); - ctx = CommandContextFactory.getInstance().newCommandContext(null, null, null, System.in, bytesOs); - } - - @After - public void after() throws Exception { - if(ctx != null) { - ctx.terminateSession(); - } - if(bytesOs != null) { - bytesOs = null; - } - } - - @Test - public void testMain() throws Exception { - final File binDir = createInstalledImage(env, "patch-inspect-test", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a module to be updated w/o a conflict - File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); - String moduleName = "module-test"; - File moduleDir = createModule0(baseModuleDir, moduleName); - // create the patch with the updated module - ContentModification moduleModified = ContentModificationUtils.modifyModule(patchDir, patchElementId, moduleDir, "new resource in the module"); - - // create a file for the conflict - String fileName = "file-test.txt"; - File miscFile = touch(binDir, fileName); - dump(miscFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", miscFile, "bin", fileName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - final String patchIDDescr = "this is one-off patch 1"; - final String oneOffElementDescr = "one-off element"; - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(patchIDDescr) - .setLink("http://test.one") - .oneOffPatchIdentity(productConfig.getProductName(), productConfig.getProductVersion()) - .getParent() - .addContentModification(fileModified) - .oneOffPatchElement(patchElementId, "base", false) - .setDescription(oneOffElementDescr) - .addContentModification(moduleModified) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - final File zippedOneOff = createZippedPatchFile(patchDir, patch.getPatchId()); - - // next patch - final String patchID2 = randomString(); - final String patchElementId2 = randomString(); - - ContentModification fileModified2 = fileModified; - ContentModification moduleModified2 = moduleModified; - - final String patchID2Descr = "This is cumulative patch 2"; - final String cpElementDescr = "CP element"; - Patch patch2 = PatchBuilder.create() - .setPatchId(patchID2) - .setDescription(patchID2Descr) - .setLink("http://test.two") - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion(), productConfig.getProductName() + "CP2") - .getParent() - .addContentModification(fileModified2) - .upgradeElement(patchElementId2, "base", false) - .setDescription(cpElementDescr) - .addContentModification(moduleModified2) - .getParent() - .build(); - - createPatchXMLFile(patchDir, patch2, false); - final File zippedCP = createZippedPatchFile(patchDir, patch2.getPatchId()); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - bytesOs.reset(); - ctx.handle("patch inspect " + zippedOneOff.getAbsolutePath()); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID, "http://test.one", true, - productConfig.getProductName(), productConfig.getProductVersion(), patchIDDescr); - - final Map oneOffElements = new HashMap(); - oneOffElements.put("Patch ID", patchElementId); - oneOffElements.put("Name", "base"); - oneOffElements.put("Type", "layer"); - oneOffElements.put("Description", oneOffElementDescr); - bytesOs.reset(); - ctx.handle("patch inspect " + zippedOneOff.getAbsolutePath() + " --verbose"); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID, "http://test.one", true, - productConfig.getProductName(), productConfig.getProductVersion(), patchIDDescr, Collections.singletonList(oneOffElements)); - - bytesOs.reset(); - ctx.handle("patch inspect " + zippedCP); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID2, "http://test.two", false, - productConfig.getProductName(), productConfig.getProductVersion(), patchID2Descr); - - final Map cpElements = new HashMap(); - cpElements.put("Patch ID", patchElementId2); - cpElements.put("Name", "base"); - cpElements.put("Type", "layer"); - cpElements.put("Description", cpElementDescr); - bytesOs.reset(); - ctx.handle("patch inspect " + zippedCP + " --verbose"); - CLIPatchInfoUtil.assertPatchInfo(bytesOs.toByteArray(), patchID2, "http://test.two", false, - productConfig.getProductName(), productConfig.getProductVersion(), patchID2Descr, Collections.singletonList(cpElements)); - - // Bundle - final File patchBundleDir = mkdir(tempDir, "bundle"); - IoUtils.copy(zippedOneOff, new File(patchBundleDir, zippedOneOff.getName())); - IoUtils.copy(zippedCP, new File(patchBundleDir, zippedCP.getName())); - final List bundledPatches = new ArrayList(); - bundledPatches.add(new BundledPatch.BundledPatchEntry(patchID, zippedOneOff.getName())); - bundledPatches.add(new BundledPatch.BundledPatchEntry(patchID2, zippedCP.getName())); - TestUtils.createPatchBundleXMLFile(patchBundleDir, bundledPatches); - final File zippedBundle = createZippedPatchFile(patchBundleDir, "patch-bundle.zip"); - - bytesOs.reset(); - ctx.handle("patch inspect " + zippedBundle); - - ByteArrayInputStream bis = new ByteArrayInputStream(bytesOs.toByteArray()); - BufferedReader reader = new BufferedReader(new InputStreamReader(bis, StandardCharsets.UTF_8)); - try { - assertTrue(reader.ready()); - CLIPatchInfoUtil.assertPatchInfo(reader, patchID, "http://test.one", true, productConfig.getProductName(), - productConfig.getProductVersion(), patchIDDescr); - assertTrue(reader.ready()); - CLIPatchInfoUtil.assertPatchInfo(reader, patchID2, "http://test.two", false, productConfig.getProductName(), - productConfig.getProductVersion(), patchID2Descr); - assertFalse(reader.ready()); - } finally { - IoUtils.safeClose(bis); - } - - bytesOs.reset(); - ctx.handle("patch inspect " + zippedBundle + " --verbose"); - - bis = new ByteArrayInputStream(bytesOs.toByteArray()); - reader = new BufferedReader(new InputStreamReader(bis, StandardCharsets.UTF_8)); - try { - assertTrue(reader.ready()); - assertEquals("CONTENT OF " + zippedOneOff.getName() + ':', reader.readLine()); - assertTrue(reader.ready()); - assertEquals("", reader.readLine()); - assertTrue(reader.ready()); - CLIPatchInfoUtil.assertPatchInfo(reader, patchID, "http://test.one", true, - productConfig.getProductName(), productConfig.getProductVersion(), patchIDDescr, Collections.singletonList(oneOffElements)); - assertTrue(reader.ready()); - assertEquals("CONTENT OF " + zippedCP.getName() + ':', reader.readLine()); - assertTrue(reader.ready()); - assertEquals("", reader.readLine()); - assertTrue(reader.ready()); - CLIPatchInfoUtil.assertPatchInfo(reader, patchID2, "http://test.two", false, - productConfig.getProductName(), productConfig.getProductVersion(), patchID2Descr, Collections.singletonList(cpElements)); - assertFalse(reader.ready()); - } finally { - IoUtils.safeClose(bis); - } - } -} diff --git a/patching/src/test/java/org/jboss/as/patching/cli/ResetConfigurationUnitTestCase.java b/patching/src/test/java/org/jboss/as/patching/cli/ResetConfigurationUnitTestCase.java deleted file mode 100644 index ac741bcb07f..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/ResetConfigurationUnitTestCase.java +++ /dev/null @@ -1,265 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotEquals; -import static org.jboss.as.patching.HashUtils.bytesToHexString; -import static org.jboss.as.patching.HashUtils.hashFile; -import static org.jboss.as.patching.IoUtils.mkdir; -import static org.jboss.as.patching.runner.PatchingAssert.assertDirDoesNotExist; -import static org.jboss.as.patching.runner.PatchingAssert.assertFileContent; -import static org.jboss.as.patching.runner.PatchingAssert.assertFileExists; -import static org.jboss.as.patching.runner.TestUtils.createInstalledImage; -import static org.jboss.as.patching.runner.TestUtils.createPatchXMLFile; -import static org.jboss.as.patching.runner.TestUtils.createZippedPatchFile; -import static org.jboss.as.patching.runner.TestUtils.dump; -import static org.jboss.as.patching.runner.TestUtils.randomString; -import static org.jboss.as.patching.runner.TestUtils.touch; - -import java.io.File; -import java.io.IOException; - -import org.jboss.as.cli.CommandContext; -import org.jboss.as.cli.CommandContextFactory; -import org.jboss.as.patching.metadata.ContentModification; -import org.jboss.as.patching.metadata.Patch; -import org.jboss.as.patching.metadata.PatchBuilder; -import org.jboss.as.patching.runner.AbstractTaskTestCase; -import org.jboss.as.patching.runner.ContentModificationUtils; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; - - -/** - * @author Alexey Loubyansky - * - */ -public class ResetConfigurationUnitTestCase extends AbstractTaskTestCase { - - private byte[] originalAppClientHash; - private byte[] originalStandaloneHash; - private byte[] originalDomainHash; - private File appClientXmlFile; - private File standaloneXmlFile; - private File domainXmlFile; - - @Before - public void setUp() throws Exception { - // with some files in the configuration directories - appClientXmlFile = touch(env.getInstalledImage().getAppClientDir(), "configuration", "appclient.xml"); - dump(appClientXmlFile, ""); - originalAppClientHash = hashFile(appClientXmlFile); - standaloneXmlFile = touch(env.getInstalledImage().getStandaloneDir(), "configuration", "standalone.xml"); - dump(standaloneXmlFile, ""); - originalStandaloneHash = hashFile(standaloneXmlFile); - domainXmlFile = touch(env.getInstalledImage().getDomainDir(), "configuration", "domain.xml"); - dump(domainXmlFile, ""); - originalDomainHash = hashFile(domainXmlFile); - } - - @After - public void tearDown() { - super.tearDown(); - originalAppClientHash = null; - originalStandaloneHash = null; - originalDomainHash = null; - standaloneXmlFile = null; - appClientXmlFile = null; - domainXmlFile = null; - } - - /** - * Applies a patch, modifies standalone, appclient and domain xml config and rolls back the patch - * with --reset-configuration=false - * The expected result is: - * - the modified config files remain as-is; - * - in each configuration dir a new restored-configuration dir is created with the original, - * unmodified configuration files. - * - * - * @throws Exception - */ - @Test - public void testResetConfigurationFalse() throws Exception { - - final File binDir = createInstalledImage(env, "consoleSlot", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a file for the conflict - String fileNoConflictName = "file-no-conflict.txt"; - File noConflictFile = touch(binDir, fileNoConflictName); - dump(noConflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileNoConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", noConflictFile, "bin", fileNoConflictName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(randomString()) - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion(), productConfig.getProductVersion() + "CP1") - .getParent() - .addContentModification(fileNoConflictModified) - .upgradeElement(patchElementId, "base", false) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - // apply the patch using the cli - CommandContext ctx = CommandContextFactory.getInstance().newCommandContext(); - try { - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } - - // check the config files have been backed up - File backupAppclientXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "appclient", "appclient.xml"); - assertFileContent(originalAppClientHash, backupAppclientXmlFile); - File backupStandaloneXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "standalone", "standalone.xml"); - assertFileContent(originalStandaloneHash, backupStandaloneXmlFile); - File backupDomainXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "domain", "domain.xml"); - assertFileContent(originalDomainHash, backupDomainXmlFile); - - // let's change the standalone.xml file - dump(standaloneXmlFile, ""); - byte[] updatedStandaloneXmlHash = hashFile(standaloneXmlFile); - - dump(appClientXmlFile, ""); - byte[] updatedAppClientXmlHash = hashFile(appClientXmlFile); - - dump(domainXmlFile, ""); - byte[] updatedDomainXmlHash = hashFile(domainXmlFile); - - try { - ctx.handle("patch rollback --reset-configuration=false --distribution=" + env.getInstalledImage().getJbossHome()); - } finally { - ctx.terminateSession(); - } - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - assertRestoredConfig(env.getInstalledImage().getStandaloneDir(), "standalone.xml", updatedStandaloneXmlHash, originalStandaloneHash); - assertRestoredConfig(env.getInstalledImage().getAppClientDir(), "appclient.xml", updatedAppClientXmlHash, originalAppClientHash); - assertRestoredConfig(env.getInstalledImage().getDomainDir(), "domain.xml", updatedDomainXmlHash, originalDomainHash); - } - - protected void assertRestoredConfig(File baseDir, String xmlName, byte[] updatedHash, byte[] originalHash) - throws IOException { - File rolledBackXmlFile = assertFileExists(baseDir, "configuration", xmlName); - assertEquals(bytesToHexString(updatedHash), bytesToHexString(hashFile(rolledBackXmlFile))); - File restoredXmlFile = assertFileExists(baseDir, "configuration", "restored-configuration", xmlName); - assertEquals(bytesToHexString(originalHash), bytesToHexString(hashFile(restoredXmlFile))); - } - - /** - * Applies a patch, modifies standalone, appclient and domain xml config and rolls back the patch - * with --reset-configuration=true - * The expected result is: - * - the modified config files are replaced with the original configuration files; - * - the restored-configuration dir is not created in any of the configuration dirs - * . - * @throws Exception - */ - @Test - public void testResetConfigurationTrue() throws Exception { - - final File binDir = createInstalledImage(env, "consoleSlot", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a file for the conflict - String fileNoConflictName = "file-no-conflict.txt"; - File noConflictFile = touch(binDir, fileNoConflictName); - dump(noConflictFile, "original script to run standalone AS7"); - // patch the file - ContentModification fileNoConflictModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", noConflictFile, "bin", fileNoConflictName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(randomString()) - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion(), productConfig.getProductVersion() + "CP1") - .getParent() - .addContentModification(fileNoConflictModified) - .upgradeElement(patchElementId, "base", false) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - // apply the patch using the cli - CommandContext ctx = CommandContextFactory.getInstance().newCommandContext(); - try { - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } - - // check the config files have been backed up - File backupAppclientXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "appclient", "appclient.xml"); - assertFileContent(originalAppClientHash, backupAppclientXmlFile); - File backupStandaloneXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "standalone", "standalone.xml"); - assertFileContent(originalStandaloneHash, backupStandaloneXmlFile); - File backupDomainXmlFile = assertFileExists(env.getInstalledImage().getPatchHistoryDir(patch.getPatchId()), "configuration", "domain", "domain.xml"); - assertFileContent(originalDomainHash, backupDomainXmlFile); - - // let's change the standalone.xml file - dump(standaloneXmlFile, ""); - byte[] updatedStandaloneXmlFile = hashFile(standaloneXmlFile); - assertNotEquals(bytesToHexString(originalStandaloneHash), bytesToHexString(updatedStandaloneXmlFile)); - - dump(appClientXmlFile, ""); - byte[] updatedAppClientXmlHash = hashFile(appClientXmlFile); - assertNotEquals(bytesToHexString(originalAppClientHash), bytesToHexString(updatedAppClientXmlHash)); - - dump(domainXmlFile, ""); - byte[] updatedDomainXmlHash = hashFile(domainXmlFile); - assertNotEquals(bytesToHexString(originalDomainHash), bytesToHexString(updatedDomainXmlHash)); - - try { - ctx.handle("patch rollback --reset-configuration=true --distribution=" + env.getInstalledImage().getJbossHome()); - } finally { - ctx.terminateSession(); - } - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - File rolledBackStandaloneXmlFile = assertFileExists(env.getInstalledImage().getStandaloneDir(), "configuration", "standalone.xml"); - assertEquals(bytesToHexString(originalStandaloneHash), bytesToHexString(hashFile(rolledBackStandaloneXmlFile))); - - File rolledBackAppClientXmlFile = assertFileExists(env.getInstalledImage().getAppClientDir(), "configuration", "appclient.xml"); - assertEquals(bytesToHexString(originalAppClientHash), bytesToHexString(hashFile(rolledBackAppClientXmlFile))); - - File rolledBackDomainXmlFile = assertFileExists(env.getInstalledImage().getDomainDir(), "configuration", "domain.xml"); - assertEquals(bytesToHexString(originalDomainHash), bytesToHexString(hashFile(rolledBackDomainXmlFile))); - - assertDirDoesNotExist(env.getInstalledImage().getStandaloneDir(), "configuration", "restored-configuration"); - assertDirDoesNotExist(env.getInstalledImage().getAppClientDir(), "configuration", "restored-configuration"); - assertDirDoesNotExist(env.getInstalledImage().getDomainDir(), "configuration", "restored-configuration"); - } -} diff --git a/patching/src/test/java/org/jboss/as/patching/cli/RollbackLastUnitTestCase.java b/patching/src/test/java/org/jboss/as/patching/cli/RollbackLastUnitTestCase.java deleted file mode 100644 index 93bc072f8f2..00000000000 --- a/patching/src/test/java/org/jboss/as/patching/cli/RollbackLastUnitTestCase.java +++ /dev/null @@ -1,242 +0,0 @@ -/* - * Copyright The WildFly Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package org.jboss.as.patching.cli; - -import static org.jboss.as.patching.Constants.BASE; -import static org.jboss.as.patching.Constants.LAYERS; -import static org.jboss.as.patching.Constants.SYSTEM; -import static org.jboss.as.patching.IoUtils.mkdir; -import static org.jboss.as.patching.IoUtils.newFile; -import static org.jboss.as.patching.runner.TestUtils.createInstalledImage; -import static org.jboss.as.patching.runner.TestUtils.createModule0; -import static org.jboss.as.patching.runner.TestUtils.createPatchXMLFile; -import static org.jboss.as.patching.runner.TestUtils.createZippedPatchFile; -import static org.jboss.as.patching.runner.TestUtils.dump; -import static org.jboss.as.patching.runner.TestUtils.randomString; -import static org.jboss.as.patching.runner.TestUtils.touch; -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.fail; - -import java.io.File; -import java.io.FileFilter; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -import org.jboss.as.cli.CommandContext; -import org.jboss.as.cli.CommandContextFactory; -import org.jboss.as.cli.impl.CommandContextConfiguration; -import org.jboss.as.patching.HashUtils; -import org.jboss.as.patching.metadata.ContentModification; -import org.jboss.as.patching.metadata.Patch; -import org.jboss.as.patching.metadata.PatchBuilder; -import org.jboss.as.patching.runner.AbstractTaskTestCase; -import org.jboss.as.patching.runner.ContentModificationUtils; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import org.junit.Test; - - -/** - * @author Alexey Loubyansky - * - */ -public class RollbackLastUnitTestCase extends AbstractTaskTestCase { - - @Test - public void testMain() throws Exception { - - final File binDir = createInstalledImage(env, "consoleSlot", productConfig.getProductName(), productConfig.getProductVersion()); - - // build a one-off patch for the base installation - // with 1 updated file - String patchID = randomString(); - String patchElementId = randomString(); - File patchDir = mkdir(tempDir, patchID); - - // create a module to be updated w/o a conflict - File baseModuleDir = newFile(env.getInstalledImage().getModulesDir(), SYSTEM, LAYERS, BASE); - String moduleName = "module-test"; - File moduleDir = createModule0(baseModuleDir, moduleName); - // create the patch with the updated module - ContentModification moduleModified = ContentModificationUtils.modifyModule(patchDir, patchElementId, moduleDir, "new resource in the module"); - - // create a file for the conflict - String fileName = "file-test.txt"; - File miscFile = touch(binDir, fileName); - dump(miscFile, "original script to run standalone AS7"); - byte[] originalFileHash = HashUtils.hashFile(miscFile); - // patch the file - ContentModification fileModified = ContentModificationUtils.modifyMisc(patchDir, patchID, "updated script", miscFile, "bin", fileName); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - - Patch patch = PatchBuilder.create() - .setPatchId(patchID) - .setDescription(randomString()) - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion(), productConfig.getProductVersion() + "CP1") - .getParent() - .addContentModification(fileModified) - .upgradeElement(patchElementId, "base", false) - .addContentModification(moduleModified) - .getParent() - .build(); - - // create the patch - createPatchXMLFile(patchDir, patch, false); - - File zippedPatch = createZippedPatchFile(patchDir, patch.getPatchId()); - - // no patches applied - assertPatchElements(baseModuleDir, null); - - // apply the patch using the cli - CommandContextConfiguration.Builder config = new CommandContextConfiguration.Builder(); - config.setInitConsole(true); - CommandContext ctx = CommandContextFactory.getInstance().newCommandContext(config.build()); - try { - ctx.handle("patch apply " + zippedPatch.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } - - // first patch applied - assertPatchElements(baseModuleDir, new String[]{patchElementId}); - - byte[] patch1FileHash = HashUtils.hashFile(miscFile); - assertNotEqual(originalFileHash, patch1FileHash); - - // next patch - final String patchID2 = randomString(); - final String patchElementId2 = randomString(); - - final File patchedModule = newFile(baseModuleDir, ".overlays", patchElementId, moduleName); - - ContentModification fileModified2 = ContentModificationUtils.modifyMisc(patchDir, patchID2, "another file update", miscFile, "bin", fileName); - ContentModification moduleModified2 = ContentModificationUtils.modifyModule(patchDir, patchElementId2, patchedModule, "another module update"); - - Patch patch2 = PatchBuilder.create() - .setPatchId(patchID2) - .setDescription(randomString()) - .upgradeIdentity(productConfig.getProductName(), productConfig.getProductVersion() + "CP1", productConfig.getProductVersion() + "CP2") - .getParent() - .addContentModification(fileModified2) - .upgradeElement(patchElementId2, "base", false) - .addContentModification(moduleModified2) - .getParent() - .build(); - - createPatchXMLFile(patchDir, patch2, false); - File zippedPatch2 = createZippedPatchFile(patchDir, patch2.getPatchId()); - - try { - ctx.handle("patch apply " + zippedPatch2.getAbsolutePath() + " --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } - - // both patches applied - assertPatchElements(baseModuleDir, new String[]{patchElementId, patchElementId2}); - - byte[] patch2FileHash = HashUtils.hashFile(miscFile); - assertNotEqual(patch1FileHash, patch2FileHash); - assertNotEqual(originalFileHash, patch2FileHash); - - //TestUtils.tree(env.getInstalledImage().getJbossHome()); - // rollback patch-id auto-completion. - { - String cmd = "patch rollback " + " --distribution=" + env.getInstalledImage().getJbossHome() + " "; - List candidates = new ArrayList<>(); - ctx.getDefaultCommandCompleter().complete(ctx, cmd, - cmd.length(), candidates); - assertTrue(candidates.toString(), candidates.size() == 2); - assertTrue(candidates.toString(), candidates.contains(patchID)); - assertTrue(candidates.toString(), candidates.contains(patchID2)); - } - - // info patch-id auto-completion. - { - String cmd = "patch info " + " --distribution=" + env.getInstalledImage().getJbossHome() + " "; - List candidates = new ArrayList<>(); - ctx.getDefaultCommandCompleter().complete(ctx, cmd, - cmd.length(), candidates); - assertTrue(candidates.toString(), candidates.size() == 2); - assertTrue(candidates.toString(), candidates.contains(patchID)); - assertTrue(candidates.toString(), candidates.contains(patchID2)); - } - - try { - ctx.handle("patch rollback --reset-configuration=false --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } - - // only the first patch is present - assertPatchElements(baseModuleDir, new String[]{patchElementId}); - - byte[] curFileHash = HashUtils.hashFile(miscFile); - assertNotEqual(curFileHash, patch2FileHash); - assertArrayEquals(curFileHash, patch1FileHash); - assertNotEqual(curFileHash, originalFileHash); - - try { - ctx.handle("patch rollback --reset-configuration=false --distribution=" + env.getInstalledImage().getJbossHome()); - } catch(Exception e) { - ctx.terminateSession(); - throw e; - } finally { - ctx.terminateSession(); - } - - // no patches present - assertPatchElements(baseModuleDir, null); - - curFileHash = HashUtils.hashFile(miscFile); - assertNotEqual(curFileHash, patch2FileHash); - assertNotEqual(curFileHash, patch1FileHash); - assertArrayEquals(curFileHash, originalFileHash); - } - - private static void assertPatchElements(File baseModuleDir, String[] patchElements) { - - File modulesPatchesDir = new File(baseModuleDir, ".overlays"); - if(!modulesPatchesDir.exists()) { - assertNull(patchElements); - return; - } - assertTrue(modulesPatchesDir.exists()); - final List patchDirs = Arrays.asList(modulesPatchesDir.listFiles(new FileFilter(){ - @Override - public boolean accept(File pathname) { - return pathname.isDirectory(); - }})); - if(patchElements == null) { - assertTrue(patchDirs.isEmpty()); - } else { - final List ids = Arrays.asList(patchElements); - assertEquals(patchDirs.size(), patchElements.length); - for (File f : patchDirs) { - assertTrue(ids.contains(f.getName())); - } - } - } - - private static void assertNotEqual(byte[] a1, byte[] a2) { - if(a1.length != a2.length) { - return; - } - for(int i = 0; i < a1.length; ++i) { - if(a1[i] != a2[i]) { - return; - } - } - fail("arrays are equal"); - } -}