Skip to content

Commit 0f649fe

Browse files
committed
Closes #556
Speed up tests Starting an embedded OSGi container is slow - over half a second per `setUp()` call (and thus per test method) that uses it. It will likely be considerably slower on the jenkins build machine etc. This PR separates out those classes that include an embedded OSGi container into those tests that actually need it and those that don't. It primarily moves code around (e.g. separating out the osgi-based tests from `CatalogYamlLocationTest` into a new `CatalogOsgiYamlLocationTest`). It also cherry-pick @ahgittin's commit that was included in #480 (which marks some more slow tests as "integration"). If you're wondering why just moving the code has resulted in a bunch of extra lines (`+1,956 −1,038` according to the "Files changed" summary), there are two primary reasons: first there are 6 new classes which have the apache header and a bunch of imports; second the `CatalogOsgiYamlEntityTest` repeats some tests that are done in `CatalogOsgiEntityTest`. The reason is that there are subtleties in how bundles are used to load catalog items (especially when composing multiple items, and sub-typing). It's therefore worth having slow versions of some of these tests that use actual OSGi bundles.
2 parents 6571bab + 29191b7 commit 0f649fe

16 files changed

Lines changed: 1956 additions & 1038 deletions

camp/camp-brooklyn/src/test/java/org/apache/brooklyn/camp/brooklyn/AbstractYamlTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.brooklyn.api.entity.EntitySpec;
2929
import org.apache.brooklyn.api.mgmt.ManagementContext;
3030
import org.apache.brooklyn.api.mgmt.Task;
31+
import org.apache.brooklyn.api.typereg.RegisteredType;
3132
import org.apache.brooklyn.camp.brooklyn.spi.creation.CampTypePlanTransformer;
3233
import org.apache.brooklyn.core.catalog.internal.CatalogUtils;
3334
import org.apache.brooklyn.core.entity.Entities;
@@ -38,6 +39,7 @@
3839
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
3940
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests.Builder;
4041
import org.apache.brooklyn.core.typereg.RegisteredTypeLoadingContexts;
42+
import org.apache.brooklyn.core.typereg.RegisteredTypePredicates;
4143
import org.apache.brooklyn.util.collections.MutableMap;
4244
import org.apache.brooklyn.util.core.ResourceUtils;
4345
import org.apache.brooklyn.util.net.Urls;
@@ -48,6 +50,8 @@
4850
import org.testng.annotations.BeforeMethod;
4951

5052
import com.google.common.base.Joiner;
53+
import com.google.common.base.Predicate;
54+
import com.google.common.collect.Iterables;
5155

5256
public abstract class AbstractYamlTest {
5357

@@ -228,6 +232,22 @@ protected String ver(String id) {
228232
return CatalogUtils.getVersionedId(id, TEST_VERSION);
229233
}
230234

235+
protected String ver(String id, String version) {
236+
return CatalogUtils.getVersionedId(id, version);
237+
}
238+
239+
protected int countCatalogLocations() {
240+
return countCatalogItemsMatching(RegisteredTypePredicates.IS_LOCATION);
241+
}
242+
243+
protected int countCatalogPolicies() {
244+
return countCatalogItemsMatching(RegisteredTypePredicates.IS_POLICY);
245+
}
246+
247+
protected int countCatalogItemsMatching(Predicate<? super RegisteredType> filter) {
248+
return Iterables.size(mgmt().getTypeRegistry().getMatching(filter));
249+
}
250+
231251
public void forceCatalogUpdate() {
232252
forceUpdate = true;
233253
}
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.brooklyn.camp.brooklyn;
20+
21+
import java.util.Collection;
22+
23+
import org.apache.brooklyn.api.catalog.BrooklynCatalog;
24+
import org.apache.brooklyn.api.entity.Entity;
25+
import org.apache.brooklyn.core.mgmt.osgi.OsgiStandaloneTest;
26+
import org.apache.brooklyn.entity.stock.BasicEntity;
27+
import org.apache.brooklyn.test.support.TestResourceUnavailableException;
28+
import org.apache.brooklyn.util.osgi.OsgiTestResources;
29+
import org.testng.Assert;
30+
import org.testng.annotations.Test;
31+
32+
import com.google.common.collect.Iterables;
33+
34+
public class ReferencedOsgiYamlTest extends AbstractYamlTest {
35+
36+
@Override
37+
protected boolean disableOsgi() {
38+
return false;
39+
}
40+
41+
@Test
42+
public void testCatalogReferencingYamlUrlFromOsgiBundle() throws Exception {
43+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
44+
45+
addCatalogItems(
46+
"brooklyn.catalog:",
47+
" id: yaml.reference",
48+
" version: " + TEST_VERSION,
49+
" itemType: entity",
50+
" libraries:",
51+
" - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
52+
" item:",
53+
" type: classpath://yaml-ref-osgi-entity.yaml");
54+
55+
String entityName = "YAML -> catalog item -> yaml url (osgi)";
56+
Entity app = createAndStartApplication(
57+
"services:",
58+
"- name: " + entityName,
59+
" type: " + ver("yaml.reference"));
60+
61+
checkChildEntitySpec(app, entityName);
62+
}
63+
64+
/**
65+
* Tests that a YAML referenced by URL from a catalog item
66+
* will have access to the catalog item's bundles.
67+
*/
68+
@Test
69+
public void testCatalogLeaksBundlesToReferencedYaml() throws Exception {
70+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
71+
72+
String parentCatalogId = "my.catalog.app.id.url.parent";
73+
addCatalogItems(
74+
"brooklyn.catalog:",
75+
" id: " + parentCatalogId,
76+
" version: " + TEST_VERSION,
77+
" itemType: entity",
78+
" libraries:",
79+
" - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
80+
" item:",
81+
" type: classpath://yaml-ref-bundle-without-libraries.yaml");
82+
83+
Entity app = createAndStartApplication(
84+
"services:",
85+
"- type: " + ver(parentCatalogId));
86+
87+
Collection<Entity> children = app.getChildren();
88+
Assert.assertEquals(children.size(), 1);
89+
Entity child = Iterables.getOnlyElement(children);
90+
Assert.assertEquals(child.getEntityType().getName(), "org.apache.brooklyn.test.osgi.entities.SimpleEntity");
91+
92+
deleteCatalogEntity(parentCatalogId);
93+
}
94+
95+
@Test
96+
public void testCatalogReference() throws Exception {
97+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
98+
99+
addCatalogItems(
100+
"brooklyn.catalog:",
101+
" brooklyn.libraries:",
102+
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
103+
" items:",
104+
" - classpath://yaml-ref-parent-catalog.bom");
105+
106+
assertCatalogReference();
107+
}
108+
109+
@Test
110+
public void testCatalogReferenceByExplicitUrl() throws Exception {
111+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
112+
113+
addCatalogItems(
114+
"brooklyn.catalog:",
115+
" brooklyn.libraries:",
116+
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
117+
" items:",
118+
" - include: classpath://yaml-ref-parent-catalog.bom");
119+
120+
assertCatalogReference();
121+
}
122+
123+
@Test
124+
public void testCatalogReferenceByMultipleUrls() throws Exception {
125+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
126+
127+
addCatalogItems(
128+
"brooklyn.catalog:",
129+
" items:",
130+
" - include: classpath://yaml-ref-simple.bom",
131+
" - include: classpath://yaml-ref-more.bom"
132+
);
133+
134+
assertCatalogReference();
135+
}
136+
137+
@Test
138+
public void testCatalogReferenceByMultipleUrlsSimplerSyntax() throws Exception {
139+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
140+
141+
addCatalogItems(
142+
"brooklyn.catalog:",
143+
" items:",
144+
" - classpath://yaml-ref-simple.bom",
145+
" - classpath://yaml-ref-more.bom"
146+
);
147+
148+
assertCatalogReference();
149+
}
150+
151+
152+
@Test
153+
public void testCatalogReferenceSeesPreviousItems() throws Exception {
154+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
155+
156+
addCatalogItems(
157+
"brooklyn.catalog:",
158+
" brooklyn.libraries:",
159+
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
160+
" items:",
161+
" - id: yaml.nested.catalog.simple",
162+
" itemType: entity",
163+
" item:",
164+
" type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY,
165+
" - include: classpath://yaml-ref-back-catalog.bom");
166+
167+
String entityNameSimple = "YAML -> catalog -> catalog (osgi)";
168+
Entity app = createAndStartApplication(
169+
"services:",
170+
"- name: " + entityNameSimple,
171+
" type: back-reference");
172+
173+
Collection<Entity> children = app.getChildren();
174+
Assert.assertEquals(children.size(), 1);
175+
Entity childSimple = Iterables.getOnlyElement(children);
176+
Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
177+
Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);
178+
}
179+
180+
@Test
181+
public void testCatalogReferenceMixesMetaAndUrl() throws Exception {
182+
TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH);
183+
184+
addCatalogItems(
185+
"brooklyn.catalog:",
186+
" brooklyn.libraries:",
187+
" - " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL,
188+
" items:",
189+
" - include: classpath://yaml-ref-parent-catalog.bom",
190+
" items:",
191+
" - id: yaml.nested.catalog.nested",
192+
" itemType: entity",
193+
" item:",
194+
" type: " + OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);
195+
196+
BrooklynCatalog catalog = mgmt().getCatalog();
197+
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.nested", BrooklynCatalog.DEFAULT_VERSION));
198+
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.simple", BrooklynCatalog.DEFAULT_VERSION));
199+
Assert.assertNotNull(catalog.getCatalogItem("yaml.nested.catalog.more", BrooklynCatalog.DEFAULT_VERSION));
200+
}
201+
202+
protected void assertCatalogReference() throws Exception {
203+
String entityNameSimple = "YAML -> catalog -> catalog simple (osgi)";
204+
String entityNameMore = "YAML -> catalog -> catalog more (osgi)";
205+
Entity app = createAndStartApplication(
206+
"services:",
207+
"- name: " + entityNameSimple,
208+
" type: yaml.nested.catalog.simple",
209+
"- name: " + entityNameMore,
210+
" type: yaml.nested.catalog.more");
211+
212+
Collection<Entity> children = app.getChildren();
213+
Assert.assertEquals(children.size(), 2);
214+
Entity childSimple = Iterables.get(children, 0);
215+
Assert.assertEquals(childSimple.getDisplayName(), entityNameSimple);
216+
Assert.assertEquals(childSimple.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_ENTITY);
217+
218+
Entity childMore = Iterables.get(children, 1);
219+
Assert.assertEquals(childMore.getDisplayName(), entityNameMore);
220+
Assert.assertEquals(childMore.getEntityType().getName(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY);
221+
}
222+
223+
private void checkChildEntitySpec(Entity app, String entityName) {
224+
Collection<Entity> children = app.getChildren();
225+
Assert.assertEquals(children.size(), 1);
226+
Entity child = Iterables.getOnlyElement(children);
227+
Assert.assertEquals(child.getDisplayName(), entityName);
228+
Assert.assertEquals(child.getEntityType().getName(), BasicEntity.class.getName());
229+
}
230+
}

0 commit comments

Comments
 (0)