brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [3/7] brooklyn-server git commit: Add support for `force-remove-bundles: *`
Date Fri, 27 Oct 2017 10:57:05 GMT
Add support for `force-remove-bundles: *`

Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo
Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/54ecbf96
Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/54ecbf96
Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/54ecbf96

Branch: refs/heads/master
Commit: 54ecbf9605154e6f134a73578b6300bcba614a65
Parents: 22fda0d
Author: Aled Sage <aled.sage@gmail.com>
Authored: Wed Oct 25 08:32:50 2017 +0100
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Wed Oct 25 08:37:58 2017 +0100

----------------------------------------------------------------------
 .../core/typereg/BundleUpgradeParser.java       | 30 ++++++++++--
 .../core/typereg/BundleUpgradeParserTest.java   | 51 ++++++++++++++++----
 .../util/text/BrooklynVersionSyntax.java        | 14 ++++++
 .../util/text/BrooklynVersionSyntaxTest.java    | 23 +++++++++
 4 files changed, 104 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54ecbf96/core/src/main/java/org/apache/brooklyn/core/typereg/BundleUpgradeParser.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/typereg/BundleUpgradeParser.java
b/core/src/main/java/org/apache/brooklyn/core/typereg/BundleUpgradeParser.java
index 96cd2b6..9766514 100644
--- a/core/src/main/java/org/apache/brooklyn/core/typereg/BundleUpgradeParser.java
+++ b/core/src/main/java/org/apache/brooklyn/core/typereg/BundleUpgradeParser.java
@@ -213,18 +213,33 @@ public class BundleUpgradeParser {
     }
 
     public static CatalogUpgrades parseBundleManifestForCatalogUpgrades(Bundle bundle) {
-        // TODO Add support for "*" for "force-remove-bundles", to indicate all lower-versioned

-        // bundles with the same symbolic name as this bundle. Also add support for the other

-        // options described in the proposal:
+        // TODO Add support for the other options described in the proposal:
         //   https://docs.google.com/document/d/1Lm47Kx-cXPLe8BO34-qrL3ZMPosuUHJILYVQUswEH6Y/edit#
         //   section "Bundle Upgrade Metadata"
         
         Dictionary<String, String> headers = bundle.getHeaders();
         return CatalogUpgrades.builder()
                 .removedLegacyItems(parseVersionRangedNameList(headers.get(MANIFEST_HEADER_FORCE_REMOVE_LEGACY_ITEMS),
false))
-                .removedBundles(parseVersionRangedNameList(headers.get(MANIFEST_HEADER_FORCE_REMOVE_BUNDLES),
false))
+                .removedBundles(parseForceRemoveBundlesHeader(bundle, headers.get(MANIFEST_HEADER_FORCE_REMOVE_BUNDLES)))
                 .build();
     }
+
+    @VisibleForTesting
+    static List<VersionRangedName> parseForceRemoveBundlesHeader(Bundle context, String
input) {
+        if (input == null) return ImmutableList.of();
+        if (stripQuotes(input).equals("*")) {
+            String bundleVersion = context.getVersion().toString();
+            String maxVersion;
+            if (BrooklynVersionSyntax.isSnapshot(bundleVersion)) {
+                maxVersion = BrooklynVersionSyntax.stripSnapshot(bundleVersion);
+            } else {
+                maxVersion = bundleVersion;
+            }
+            return ImmutableList.of(new VersionRangedName(context.getSymbolicName(), "[0,"+maxVersion+")"));
+        } else {
+            return parseVersionRangedNameList(input, false);
+        }
+    }
     
     @VisibleForTesting
     static List<VersionRangedName> parseVersionRangedNameList(String input, boolean
singleVersionIsOsgiRange) {
@@ -242,4 +257,11 @@ public class BundleUpgradeParser {
         }
         return versionedItems;
     }
+    
+    private static String stripQuotes(String input) {
+        String quoteChars = QuotedStringTokenizer.DEFAULT_QUOTE_CHARS;
+        boolean quoted = (input.length() >= 2) && quoteChars.contains(input.substring(0,
1))
+                && quoteChars.contains(input.substring(input.length() - 1));
+        return (quoted ? input.substring(1, input.length() - 1) : input);
+    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54ecbf96/core/src/test/java/org/apache/brooklyn/core/typereg/BundleUpgradeParserTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/org/apache/brooklyn/core/typereg/BundleUpgradeParserTest.java
b/core/src/test/java/org/apache/brooklyn/core/typereg/BundleUpgradeParserTest.java
index 143649a..7573bee 100644
--- a/core/src/test/java/org/apache/brooklyn/core/typereg/BundleUpgradeParserTest.java
+++ b/core/src/test/java/org/apache/brooklyn/core/typereg/BundleUpgradeParserTest.java
@@ -28,7 +28,6 @@ import java.util.List;
 import java.util.Map;
 
 import org.apache.brooklyn.api.catalog.CatalogItem;
-import org.apache.brooklyn.core.typereg.BundleUpgradeParser;
 import org.apache.brooklyn.core.typereg.BundleUpgradeParser.CatalogUpgrades;
 import org.apache.brooklyn.core.typereg.BundleUpgradeParser.VersionRangedName;
 import org.apache.brooklyn.test.Asserts;
@@ -46,6 +45,7 @@ import com.google.common.collect.ImmutableMap;
 public class BundleUpgradeParserTest {
 
     private VersionRange from0lessThan1 = new VersionRange('[', Version.valueOf("0"), Version.valueOf("1.0.0"),
')');
+    private VersionRange from0lessThan1_2_3 = new VersionRange('[', Version.valueOf("0"),
Version.valueOf("1.2.3"), ')');
     private VersionRange exactly0dot1 = new VersionRange('[', Version.valueOf("0.1.0"), Version.valueOf("0.1.0"),
']');
     private VersionRangedName fooFrom0lessThan1 = new VersionRangedName("foo", from0lessThan1);
     private VersionRangedName barFrom0lessThan1 = new VersionRangedName("bar", from0lessThan1);
@@ -82,28 +82,49 @@ public class BundleUpgradeParserTest {
     @Test
     public void testParseSingleQuotedVal() throws Exception {
         String input = "\"foo:[0,1.0.0)\"";
-        assertParsed(input, ImmutableList.of(fooFrom0lessThan1));
+        assertParseList(input, ImmutableList.of(fooFrom0lessThan1));
     }
     
     @Test
     public void testParseSingleQuotedValWithNestedQuotes() throws Exception {
         String input = "\"foo:[0,\"1.0.0\")\"";
-        assertParsed(input, ImmutableList.of(fooFrom0lessThan1));
+        assertParseList(input, ImmutableList.of(fooFrom0lessThan1));
     }
     
     @Test
     public void testParseMultipleVals() throws Exception {
         String input = "\"foo:[0,1.0.0)\",\"bar:[0,1.0.0)\"";
-        assertParsed(input, ImmutableList.of(fooFrom0lessThan1, barFrom0lessThan1));
+        assertParseList(input, ImmutableList.of(fooFrom0lessThan1, barFrom0lessThan1));
     }
 
     @Test
     public void testParseValWithExactVersion() throws Exception {
         String input = "\"foo:0.1.0\"";
-        assertParsed(input, ImmutableList.of(new VersionRangedName("foo", exactly0dot1)));
+        assertParseList(input, ImmutableList.of(new VersionRangedName("foo", exactly0dot1)));
     }
 
     @Test
+    public void testParseForceRemoveBundlesHeader() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(bundle.getSymbolicName()).thenReturn("foo.bar");
+        Mockito.when(bundle.getVersion()).thenReturn(Version.valueOf("1.2.3"));
+        
+        assertParseForceRemoveBundlesHeader(bundle, "\"foo:0.1.0\"", ImmutableList.of(new
VersionRangedName("foo", exactly0dot1)));
+        assertParseForceRemoveBundlesHeader(bundle, "\"*\"", ImmutableList.of(new VersionRangedName("foo.bar",
from0lessThan1_2_3)));
+        assertParseForceRemoveBundlesHeader(bundle, "*", ImmutableList.of(new VersionRangedName("foo.bar",
from0lessThan1_2_3)));
+    }
+    
+    @Test
+    public void testParseForceRemoveBundlesHeaderWithSnapshot() throws Exception {
+        Bundle bundle = Mockito.mock(Bundle.class);
+        Mockito.when(bundle.getSymbolicName()).thenReturn("foo.bar");
+        Mockito.when(bundle.getVersion()).thenReturn(Version.valueOf("1.2.3.SNAPSHOT"));
+        
+        assertParseForceRemoveBundlesHeader(bundle, "\"*\"", ImmutableList.of(new VersionRangedName("foo.bar",
from0lessThan1_2_3)));
+        assertParseForceRemoveBundlesHeader(bundle, "*", ImmutableList.of(new VersionRangedName("foo.bar",
from0lessThan1_2_3)));
+    }
+    
+    @Test
     public void testParseBundleEmptyManifest() throws Exception {
         Bundle bundle = newMockBundle(ImmutableMap.of());
         
@@ -147,15 +168,25 @@ public class BundleUpgradeParserTest {
         return result;
     }
     
-    private void assertParsed(String input, List<VersionRangedName> expected) throws
Exception {
+    private void assertParseList(String input, List<VersionRangedName> expected) throws
Exception {
         List<VersionRangedName> actual = BundleUpgradeParser.parseVersionRangedNameList(input,
false);
-        assertEquals(actual.size(), expected.size(), "actual="+actual); 
+        assertListsEqual(actual, expected);
+    }
+    
+    private void assertParseForceRemoveBundlesHeader(Bundle bundle, String input, List<VersionRangedName>
expected) throws Exception {
+        List<VersionRangedName> actual = BundleUpgradeParser.parseForceRemoveBundlesHeader(bundle,
input);
+        assertListsEqual(actual, expected);
+    }
+
+    private void assertListsEqual(List<VersionRangedName> actual, List<VersionRangedName>
expected) throws Exception {
+        String errMsg = "actual="+actual;
+        assertEquals(actual.size(), expected.size(), errMsg); 
         for (int i = 0; i < actual.size(); i++) {
-            assertEquals(actual.get(i).getSymbolicName(), expected.get(i).getSymbolicName());
-            assertEquals(actual.get(i).getOsgiVersionRange(), expected.get(i).getOsgiVersionRange());
+            assertEquals(actual.get(i).getSymbolicName(), expected.get(i).getSymbolicName(),
errMsg);
+            assertEquals(actual.get(i).getOsgiVersionRange(), expected.get(i).getOsgiVersionRange(),
errMsg);
         }
     }
-    
+
     private void assertVersionRangedNameFails(String input, String expectedFailure, String...
optionalOtherExpectedFailures) {
         try {
             VersionRangedName.fromString(input, false);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54ecbf96/utils/common/src/main/java/org/apache/brooklyn/util/text/BrooklynVersionSyntax.java
----------------------------------------------------------------------
diff --git a/utils/common/src/main/java/org/apache/brooklyn/util/text/BrooklynVersionSyntax.java
b/utils/common/src/main/java/org/apache/brooklyn/util/text/BrooklynVersionSyntax.java
index 89262d5..6f23489 100644
--- a/utils/common/src/main/java/org/apache/brooklyn/util/text/BrooklynVersionSyntax.java
+++ b/utils/common/src/main/java/org/apache/brooklyn/util/text/BrooklynVersionSyntax.java
@@ -225,4 +225,18 @@ public class BrooklynVersionSyntax {
         return version.toUpperCase().contains(SNAPSHOT);
     }
 
+    /**
+     * Returns the version without "SNAPSHOT" (normally this will return the next expected
release version).
+     * For example, "1.0.0.SNAPSHOT" or "1.0.0-SNAPSHOT" becomes "1.0.0".
+     */
+    public static String stripSnapshot(String input) {
+        if (input==null) return input;
+        int stripIndex = input.toUpperCase().indexOf(SNAPSHOT);
+        if (stripIndex <= 0) return input;
+        char charBeforeSnapshot = input.charAt(stripIndex - 1);
+        if (charBeforeSnapshot == '.' || charBeforeSnapshot == '_' || charBeforeSnapshot
== '-') {
+            stripIndex--;
+        }
+        return (stripIndex <= 0) ? input : input.substring(0, stripIndex);
+    }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/54ecbf96/utils/common/src/test/java/org/apache/brooklyn/util/text/BrooklynVersionSyntaxTest.java
----------------------------------------------------------------------
diff --git a/utils/common/src/test/java/org/apache/brooklyn/util/text/BrooklynVersionSyntaxTest.java
b/utils/common/src/test/java/org/apache/brooklyn/util/text/BrooklynVersionSyntaxTest.java
index 1bd8e3a..4fa02d6 100644
--- a/utils/common/src/test/java/org/apache/brooklyn/util/text/BrooklynVersionSyntaxTest.java
+++ b/utils/common/src/test/java/org/apache/brooklyn/util/text/BrooklynVersionSyntaxTest.java
@@ -107,6 +107,29 @@ public class BrooklynVersionSyntaxTest {
         assertOsgiVersionRange("(1.0.0-SNAPSHOT,2.0.0-SNAPSHOT]", "(1.0.0.SNAPSHOT,2.0.0.SNAPSHOT]");
     }
     
+    public void testIsSnapshot() {
+        Assert.assertTrue(BrooklynVersionSyntax.isSnapshot("1.0.0.SNAPSHOT"));
+        Assert.assertTrue(BrooklynVersionSyntax.isSnapshot("1.0.0.20171025_SNAPSHOT"));
+        Assert.assertTrue(BrooklynVersionSyntax.isSnapshot("1.0.0-SNAPSHOT"));
+        Assert.assertFalse(BrooklynVersionSyntax.isSnapshot("1.0.0"));
+    }
+
+    public void testStripSnapshot() {
+        assertStripSnapshot("1.0.0.SNAPSHOT", "1.0.0");
+        assertStripSnapshot("1.0.0-SNAPSHOT", "1.0.0");
+        assertStripSnapshot("1-SNAPSHOT", "1");
+        assertStripSnapshot("1.0.0.20171025_SNAPSHOT", "1.0.0.20171025");
+        assertStripSnapshot("1.0.0", "1.0.0");
+        
+        // for weird things, don't make them even weirder!
+        assertStripSnapshot("SNAPSHOT", "SNAPSHOT");
+        assertStripSnapshot("_SNAPSHOT", "_SNAPSHOT");
+    }
+
+    private void assertStripSnapshot(String input, String expected) {
+        Assert.assertEquals(BrooklynVersionSyntax.stripSnapshot(input), expected, "conversion
to strip snapshot");
+    }
+    
     private void assertOsgiVersionRange(String input, String osgi) {
         Assert.assertEquals(BrooklynVersionSyntax.toValidOsgiVersionRange(input), osgi, "conversion
to valid osgi range");
     }


Mime
View raw message