modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/bundle/BundleManagerBean.java | 10 modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java | 124 ++++++++++ modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/TestBundleServerPluginService.java | 12 modules/enterprise/server/plugins/ant-bundle/src/main/java/org/rhq/enterprise/server/plugins/ant/AntBundleServerPluginComponent.java | 19 - 4 files changed, 155 insertions(+), 10 deletions(-)
New commits: commit 87d870e8a980925c43fb6bf4124ae4ad4838fdf6 Author: John Mazzitelli mazz@redhat.com Date: Thu Apr 29 13:30:20 2010 -0400
unit test coding is done - need to make sure it actually works. this is a wip
diff --git a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java index 5c90ded..0889a80 100644 --- a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java +++ b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java @@ -24,8 +24,10 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.util.ArrayList; +import java.util.HashMap; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -279,58 +281,75 @@ public class BundleManagerBeanTest extends UpdateSubsytemTestBase { }
@Test(enabled = TESTS_ENABLED) - public void testCreateBundleFromDistribution() throws Exception { + public void testCreateBundleVersionFromDistributionFile() throws Exception {
File tmpDir = FileUtil.createTempDirectory("createBundleFromDistro", ".dir", null); - - String bundleFile1 = "subdir1/bundle-file-1.txt"; - String bundleFile2 = "subdir2/bundle-file-2.txt"; - String bundleFile3 = "bundle-file-3.txt"; - writeFile(new File(tmpDir, bundleFile1), "first bundle file found inside bundle distro"); - writeFile(new File(tmpDir, bundleFile2), "second bundle file found inside bundle distro"); - writeFile(new File(tmpDir, bundleFile3), "third bundle file found inside bundle distro"); - - String bundleName = "test-bundle-name"; - String bundleVersion = "1.2.3"; - String bundleDescription = "test bundle desc"; - DeploymentProperties bundleMetadata = new DeploymentProperties(0, bundleName, bundleVersion, bundleDescription); - - ConfigurationDefinition configDef = new ConfigurationDefinition("foo", null); - String propName = "prop1"; - String propDescription = "prop1desc"; - configDef.put(new PropertyDefinitionSimple(propName, propDescription, true, PropertySimpleType.INTEGER)); - - Set<String> bundleFileNames = new HashSet<String>(3); - bundleFileNames.add(bundleFile1); - bundleFileNames.add(bundleFile2); - bundleFileNames.add(bundleFile3); - - File bundleDistroFile = tmpDir; // not a real distro zip, but its just a simulation anyway - SLSB will pass this to our mock PC - String recipe = "mock recipe"; - BundleType bt1 = createBundleType("one"); - - ps.parseRecipe_returnValue = new RecipeParseResults(bundleMetadata, configDef, bundleFileNames); - ps.processBundleDistributionFile_returnValue = new BundleDistributionInfo(); - - BundleVersion bv; - bv = bundleManager.createBundleVersionViaUberBundleFileURL(overlord, bundleDistroFile.toURI().toURL()); - - assert bv.getId() > 0 : bv; - assert bv.getBundle().getName().equals(bundleName) : bv; - assert bv.getBundle().getDescription().equals(bundleDescription) : bv; - assert bv.getDescription().equals(bundleDescription) : "the bundle version desc should be the same as the bundle desc"; - assert bv.getVersion().equals(bundleVersion) : bv; - assert bv.getBundleFiles().size() == 3 : bv; - assert bv.getBundleFiles().contains(bundleFile1) : bv; - assert bv.getBundleFiles().contains(bundleFile2) : bv; - assert bv.getBundleFiles().contains(bundleFile3) : bv; - assert bv.getBundleDeployments().isEmpty() : bv; - assert bv.getConfigurationDefinition().getPropertyDefinitions().size() == 1; - assert bv.getConfigurationDefinition().get(propName) != null; - assert bv.getConfigurationDefinition().get(propName).getDescription().equals(propDescription); - assert bv.getConfigurationDefinition().get(propName).isRequired(); - assert bv.getConfigurationDefinition().getPropertyDefinitionSimple(propName).getType() == PropertySimpleType.INTEGER; - assert bv.getRecipe().equals(recipe); + try { + String bundleFile1 = "subdir1/bundle-file-1.txt"; + String bundleFile2 = "subdir2/bundle-file-2.txt"; + String bundleFile3 = "bundle-file-3.txt"; + writeFile(new File(tmpDir, bundleFile1), "first bundle file found inside bundle distro"); + writeFile(new File(tmpDir, bundleFile2), "second bundle file found inside bundle distro"); + writeFile(new File(tmpDir, bundleFile3), "third bundle file found inside bundle distro"); + + String bundleName = "test-bundle-name"; + String bundleVersion = "1.2.3"; + String bundleDescription = "test bundle desc"; + DeploymentProperties bundleMetadata = new DeploymentProperties(0, bundleName, bundleVersion, + bundleDescription); + + ConfigurationDefinition configDef = new ConfigurationDefinition("foo", null); + String propName = "prop1"; + String propDescription = "prop1desc"; + configDef.put(new PropertyDefinitionSimple(propName, propDescription, true, PropertySimpleType.INTEGER)); + + Map<String, File> bundleFiles = new HashMap<String, File>(3); + bundleFiles.put(bundleFile1, new File(tmpDir, bundleFile1)); + bundleFiles.put(bundleFile2, new File(tmpDir, bundleFile2)); + bundleFiles.put(bundleFile3, new File(tmpDir, bundleFile3)); + + File bundleDistroFile = tmpDir; // not a real distro zip, but its just a simulation anyway - SLSB will pass this to our mock PC + String recipe = "mock recipe"; + BundleType bt1 = createBundleType("one"); + + // prepare our mock bundle PC + ps.parseRecipe_returnValue = new RecipeParseResults(bundleMetadata, configDef, new HashSet<String>( + bundleFiles.keySet())); + ps.processBundleDistributionFile_returnValue = new BundleDistributionInfo(recipe, + ps.parseRecipe_returnValue, bundleFiles); + ps.processBundleDistributionFile_returnValue.setBundleTypeName(bt1.getName()); + + // now ask the SLSB to persist our bundle data given our mock distribution + BundleVersion bv1 = bundleManager.createBundleVersionViaURL(overlord, bundleDistroFile.toURI().toURL()); + + // to a db lookup to make sure our bundle version is queryable + BundleVersionCriteria criteria = new BundleVersionCriteria(); + criteria.addFilterId(bv1.getId()); + BundleVersion bv2 = bundleManager.findBundleVersionsByCriteria(overlord, criteria).get(0); + + // test that the PC's return value and our own DB lookup match the bundle version we expect to be in the DB + BundleVersion[] bvs = new BundleVersion[] { bv1, bv2 }; + for (BundleVersion bv : bvs) { + assert bv.getId() > 0 : bv; + assert bv.getBundle().getName().equals(bundleName) : bv; + assert bv.getBundle().getDescription().equals(bundleDescription) : bv; + assert bv.getDescription().equals(bundleDescription) : "the bundle version desc should be the same as the bundle desc"; + assert bv.getVersion().equals(bundleVersion) : bv; + assert bv.getBundleFiles().size() == 3 : bv; + assert bv.getBundleFiles().contains(bundleFile1) : bv; + assert bv.getBundleFiles().contains(bundleFile2) : bv; + assert bv.getBundleFiles().contains(bundleFile3) : bv; + assert bv.getBundleDeployments().isEmpty() : bv; + assert bv.getConfigurationDefinition().getPropertyDefinitions().size() == 1; + assert bv.getConfigurationDefinition().get(propName) != null; + assert bv.getConfigurationDefinition().get(propName).getDescription().equals(propDescription); + assert bv.getConfigurationDefinition().get(propName).isRequired(); + assert bv.getConfigurationDefinition().getPropertyDefinitionSimple(propName).getType() == PropertySimpleType.INTEGER; + assert bv.getRecipe().equals(recipe); + } + } finally { + FileUtil.purge(tmpDir, true); + } }
@Test(enabled = TESTS_ENABLED) diff --git a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/TestBundleServerPluginService.java b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/TestBundleServerPluginService.java index 871c812..42a88f9 100644 --- a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/TestBundleServerPluginService.java +++ b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/TestBundleServerPluginService.java @@ -62,6 +62,9 @@ public class TestBundleServerPluginService extends ServerPluginService implement public TestBundleServerPluginContainer bundlePC; public MasterServerPluginContainerConfiguration masterConfig;
+ public RecipeParseResults parseRecipe_returnValue = null; + public BundleDistributionInfo processBundleDistributionFile_returnValue; + public TestBundleServerPluginService() { // build the config at constructor time so tests have it even before the PC is initialized File dir = new File(System.getProperty("java.io.tmpdir"), "test-server-plugins"); @@ -200,6 +203,10 @@ public class TestBundleServerPluginService extends ServerPluginService implement
public RecipeParseResults parseRecipe(String recipe) throws Exception {
+ if (parseRecipe_returnValue != null) { + return parseRecipe_returnValue; + } + ConfigurationDefinition configDef; Set<String> bundleFileNames; DeploymentProperties metadata; @@ -218,7 +225,10 @@ public class TestBundleServerPluginService extends ServerPluginService implement }
public BundleDistributionInfo processBundleDistributionFile(File uberBundleFile) throws Exception { - // TODO: bundle implement me + if (processBundleDistributionFile_returnValue != null) { + return processBundleDistributionFile_returnValue; + } + throw new UnsupportedOperationException("this mock object cannot do this"); } }
commit 52b5b0f077e70a78e6793a322372fcb926700820 Author: John Mazzitelli mazz@redhat.com Date: Thu Apr 29 13:29:47 2010 -0400
couple fixes 1) don't close ZipInputStream, the walker util will handle that 2) make sure to create subdirectories when extracting visited zip entries 3) no need to put timestamp in temp directory - the utility will ensure uniqueness
diff --git a/modules/enterprise/server/plugins/ant-bundle/src/main/java/org/rhq/enterprise/server/plugins/ant/AntBundleServerPluginComponent.java b/modules/enterprise/server/plugins/ant-bundle/src/main/java/org/rhq/enterprise/server/plugins/ant/AntBundleServerPluginComponent.java index b68a20f..4c155bf 100644 --- a/modules/enterprise/server/plugins/ant-bundle/src/main/java/org/rhq/enterprise/server/plugins/ant/AntBundleServerPluginComponent.java +++ b/modules/enterprise/server/plugins/ant-bundle/src/main/java/org/rhq/enterprise/server/plugins/ant/AntBundleServerPluginComponent.java @@ -19,6 +19,7 @@ package org.rhq.enterprise.server.plugins.ant;
import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; @@ -163,15 +164,16 @@ public class AntBundleServerPluginComponent implements ServerPluginComponent, Bu }
public boolean visit(ZipEntry entry, ZipInputStream stream) throws Exception { - if (recipeName.equalsIgnoreCase(entry.getName())) { + if (this.recipeName.equalsIgnoreCase(entry.getName())) { // this should be safe downcast, recipes are not that big - byte[] recipeBytes = new byte[(int) entry.getSize()]; - recipeBytes = StreamUtil.slurp(stream); - this.recipe = new String(recipeBytes); + ByteArrayOutputStream out = new ByteArrayOutputStream((int) entry.getSize()); + StreamUtil.copy(stream, out, false); + this.recipe = new String(out.toByteArray()); + out = null; // no need for this anymore, help out GC try { - this.results = facet.parseRecipe(this.recipe); + this.results = this.facet.parseRecipe(this.recipe); } catch (Throwable t) { - results = null; + this.results = null; } } return true; @@ -194,14 +196,15 @@ public class AntBundleServerPluginComponent implements ServerPluginComponent, Bu public BundleFileVisitor(String bundleName, Set<String> bundleFileNames) throws IOException { this.bundleFileNames = bundleFileNames; this.bundleFiles = new HashMap<String, File>(bundleFileNames.size()); - tmpDir = FileUtil.createTempDirectory("bundle-" + bundleName, String.valueOf(System.currentTimeMillis()), - null); + this.tmpDir = FileUtil.createTempDirectory("bundle-" + bundleName, ".dir", null); }
public boolean visit(ZipEntry entry, ZipInputStream stream) throws Exception { if (bundleFileNames.contains(entry.getName())) {
File bundleFile = new File(tmpDir, entry.getName()); + bundleFile.getParentFile().mkdirs(); + FileOutputStream fos = null; try { fos = new FileOutputStream(bundleFile);
commit 5fbc5da51e31c1465bb8a844b1682ce1b452be51 Author: John Mazzitelli mazz@redhat.com Date: Thu Apr 29 13:27:41 2010 -0400
make sure streams are always closed, even in error conditions
diff --git a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/bundle/BundleManagerBean.java b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/bundle/BundleManagerBean.java index b2eaaf0..46ea98c 100644 --- a/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/bundle/BundleManagerBean.java +++ b/modules/enterprise/server/jar/src/main/java/org/rhq/enterprise/server/bundle/BundleManagerBean.java @@ -342,11 +342,13 @@ public class BundleManagerBean implements BundleManagerLocal, BundleManagerRemot BundleVersion bundleVersion = null;
try { - tempDistributionFile = File.createTempFile("bundle-" + distributionFileUrl.hashCode(), "zip"); + tempDistributionFile = File.createTempFile("bundle-" + distributionFileUrl.hashCode(), ".zip");
is = distributionFileUrl.openStream(); os = new FileOutputStream(tempDistributionFile); long len = StreamUtil.copy(is, os); + is = null; + os = null; log.debug("Copied [" + len + "] bytes from [" + distributionFileUrl + "] into [" + tempDistributionFile.getPath() + "]");
@@ -355,6 +357,12 @@ public class BundleManagerBean implements BundleManagerLocal, BundleManagerRemot if (null != tempDistributionFile) { tempDistributionFile.delete(); } + if (is != null) { + is.close(); + } + if (os != null) { + os.close(); + } }
return bundleVersion;
commit f886b37162a69e01d3a3b95f9a7ff5b9e7511e0b Author: John Mazzitelli mazz@redhat.com Date: Thu Apr 29 12:41:03 2010 -0400
start of test
diff --git a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java index 78559df..5c90ded 100644 --- a/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java +++ b/modules/enterprise/server/jar/src/test/java/org/rhq/enterprise/server/bundle/BundleManagerBeanTest.java @@ -19,9 +19,16 @@
package org.rhq.enterprise.server.bundle;
+import java.io.ByteArrayInputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; import java.util.Set; +import java.util.zip.ZipEntry; +import java.util.zip.ZipOutputStream;
import javax.persistence.EntityManager; import javax.persistence.Query; @@ -47,6 +54,9 @@ import org.rhq.core.domain.bundle.BundleVersion; import org.rhq.core.domain.bundle.composite.BundleWithLatestVersionComposite; import org.rhq.core.domain.configuration.Configuration; import org.rhq.core.domain.configuration.PropertySimple; +import org.rhq.core.domain.configuration.definition.ConfigurationDefinition; +import org.rhq.core.domain.configuration.definition.PropertyDefinitionSimple; +import org.rhq.core.domain.configuration.definition.PropertySimpleType; import org.rhq.core.domain.content.Architecture; import org.rhq.core.domain.content.Package; import org.rhq.core.domain.content.PackageType; @@ -64,6 +74,9 @@ import org.rhq.core.domain.resource.ResourceType; import org.rhq.core.domain.resource.group.ResourceGroup; import org.rhq.core.domain.util.PageList; import org.rhq.core.domain.util.PageOrdering; +import org.rhq.core.util.file.FileUtil; +import org.rhq.core.util.stream.StreamUtil; +import org.rhq.core.util.updater.DeploymentProperties; import org.rhq.enterprise.server.plugin.pc.MasterServerPluginContainer; import org.rhq.enterprise.server.resource.ResourceManagerLocal; import org.rhq.enterprise.server.resource.metadata.test.UpdateSubsytemTestBase; @@ -266,6 +279,61 @@ public class BundleManagerBeanTest extends UpdateSubsytemTestBase { }
@Test(enabled = TESTS_ENABLED) + public void testCreateBundleFromDistribution() throws Exception { + + File tmpDir = FileUtil.createTempDirectory("createBundleFromDistro", ".dir", null); + + String bundleFile1 = "subdir1/bundle-file-1.txt"; + String bundleFile2 = "subdir2/bundle-file-2.txt"; + String bundleFile3 = "bundle-file-3.txt"; + writeFile(new File(tmpDir, bundleFile1), "first bundle file found inside bundle distro"); + writeFile(new File(tmpDir, bundleFile2), "second bundle file found inside bundle distro"); + writeFile(new File(tmpDir, bundleFile3), "third bundle file found inside bundle distro"); + + String bundleName = "test-bundle-name"; + String bundleVersion = "1.2.3"; + String bundleDescription = "test bundle desc"; + DeploymentProperties bundleMetadata = new DeploymentProperties(0, bundleName, bundleVersion, bundleDescription); + + ConfigurationDefinition configDef = new ConfigurationDefinition("foo", null); + String propName = "prop1"; + String propDescription = "prop1desc"; + configDef.put(new PropertyDefinitionSimple(propName, propDescription, true, PropertySimpleType.INTEGER)); + + Set<String> bundleFileNames = new HashSet<String>(3); + bundleFileNames.add(bundleFile1); + bundleFileNames.add(bundleFile2); + bundleFileNames.add(bundleFile3); + + File bundleDistroFile = tmpDir; // not a real distro zip, but its just a simulation anyway - SLSB will pass this to our mock PC + String recipe = "mock recipe"; + BundleType bt1 = createBundleType("one"); + + ps.parseRecipe_returnValue = new RecipeParseResults(bundleMetadata, configDef, bundleFileNames); + ps.processBundleDistributionFile_returnValue = new BundleDistributionInfo(); + + BundleVersion bv; + bv = bundleManager.createBundleVersionViaUberBundleFileURL(overlord, bundleDistroFile.toURI().toURL()); + + assert bv.getId() > 0 : bv; + assert bv.getBundle().getName().equals(bundleName) : bv; + assert bv.getBundle().getDescription().equals(bundleDescription) : bv; + assert bv.getDescription().equals(bundleDescription) : "the bundle version desc should be the same as the bundle desc"; + assert bv.getVersion().equals(bundleVersion) : bv; + assert bv.getBundleFiles().size() == 3 : bv; + assert bv.getBundleFiles().contains(bundleFile1) : bv; + assert bv.getBundleFiles().contains(bundleFile2) : bv; + assert bv.getBundleFiles().contains(bundleFile3) : bv; + assert bv.getBundleDeployments().isEmpty() : bv; + assert bv.getConfigurationDefinition().getPropertyDefinitions().size() == 1; + assert bv.getConfigurationDefinition().get(propName) != null; + assert bv.getConfigurationDefinition().get(propName).getDescription().equals(propDescription); + assert bv.getConfigurationDefinition().get(propName).isRequired(); + assert bv.getConfigurationDefinition().getPropertyDefinitionSimple(propName).getType() == PropertySimpleType.INTEGER; + assert bv.getRecipe().equals(recipe); + } + + @Test(enabled = TESTS_ENABLED) public void testGetBundleTypes() throws Exception { BundleType bt1 = createBundleType("one"); BundleType bt2 = createBundleType("two"); @@ -889,4 +957,41 @@ public class BundleManagerBeanTest extends UpdateSubsytemTestBase {
return resourceGroup; } + + private String readFile(File file) throws Exception { + return new String(StreamUtil.slurp(new FileInputStream(file))); + } + + private void writeFile(File file, String content) throws Exception { + file.getParentFile().mkdirs(); + StreamUtil.copy(new ByteArrayInputStream(content.getBytes()), new FileOutputStream(file)); + } + + private File createZip(File destDir, String zipName, String[] entryNames, String[] contents) throws Exception { + FileOutputStream stream = null; + ZipOutputStream out = null; + + try { + destDir.mkdirs(); + File zipFile = new File(destDir, zipName); + stream = new FileOutputStream(zipFile); + out = new ZipOutputStream(stream); + + assert contents.length == entryNames.length; + for (int i = 0; i < contents.length; i++) { + ZipEntry zipAdd = new ZipEntry(entryNames[i]); + zipAdd.setTime(System.currentTimeMillis()); + out.putNextEntry(zipAdd); + out.write(contents[i].getBytes()); + } + return zipFile; + } finally { + if (out != null) { + out.close(); + } + if (stream != null) { + stream.close(); + } + } + } }
rhq-commits@lists.fedorahosted.org