Simplify integration test, avoid TestHttpServer and just have one test file.
https://github.com/apache/incubator-brooklyn/pull/1030#discussion_r44860563 https://github.com/apache/incubator-brooklyn/pull/1030#discussion_r44860570 Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/f3a6395e Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/f3a6395e Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/f3a6395e Branch: refs/heads/master Commit: f3a6395e0028d26af473428f9d248a7d917ee7d1 Parents: 4467878 Author: Geoff Macartney <[email protected]> Authored: Tue Nov 17 13:19:43 2015 +0000 Committer: Geoff Macartney <[email protected]> Committed: Tue Nov 17 14:10:59 2015 +0000 ---------------------------------------------------------------------- .../SimpleCommandImplIntegrationTest.java | 112 ------------- .../framework/SimpleCommandIntegrationTest.java | 166 +++++++++++++++++++ .../SimpleCommandScriptIntegrationTest.java | 165 ------------------ 3 files changed, 166 insertions(+), 277 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f3a6395e/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandImplIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandImplIntegrationTest.java b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandImplIntegrationTest.java deleted file mode 100644 index dea50e9..0000000 --- a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandImplIntegrationTest.java +++ /dev/null @@ -1,112 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.test.framework; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; -import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; -import org.apache.brooklyn.core.test.entity.TestApplication; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; -import org.testng.annotations.Test; - -import java.util.UUID; - -import static org.apache.brooklyn.test.framework.BaseTest.TARGET_ENTITY; -import static org.apache.brooklyn.test.framework.SimpleCommand.COMMAND; -import static org.apache.brooklyn.test.framework.SimpleCommandTest.*; -import static org.assertj.core.api.Assertions.assertThat; - -public class SimpleCommandImplIntegrationTest { - private static final Logger LOG = LoggerFactory.getLogger(SimpleCommandImplIntegrationTest.class); - - private static final String UP = "up"; - private TestApplication app; - private ManagementContext managementContext; - private LocalhostMachineProvisioningLocation localhost; - private String testId; - - - @BeforeMethod - public void setUp() { - - testId = UUID.randomUUID().toString(); - - app = TestApplication.Factory.newManagedInstanceForTests(); - managementContext = app.getManagementContext(); - - localhost = managementContext.getLocationManager() - .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) - .configure("name", testId)); - } - - @AfterMethod(alwaysRun = true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); - } - - @Test(groups = "Integration") - public void shouldInvokeCommand() { - TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - - SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) - .configure(TARGET_ENTITY, testEntity) - .configure(COMMAND, "uptime") - .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) - .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, UP))); - - app.start(ImmutableList.of(localhost)); - - assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() - .withFailMessage("Service should be up"); - assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) - .withFailMessage("Service should be marked running"); - - } - - @Test(groups = "Integration") - public void shouldNotBeUpIfAssertionFails() { - TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - - SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) - .configure(TARGET_ENTITY, testEntity) - .configure(COMMAND, "uptime") - .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 1))); - - try { - app.start(ImmutableList.of(localhost)); - } catch (Exception e) { - assertThat(e.getCause().getMessage().contains("exit code equals 1")); - } - - assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.ON_FIRE) - .withFailMessage("Service should be marked on fire"); - - } - -} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f3a6395e/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandIntegrationTest.java b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandIntegrationTest.java new file mode 100644 index 0000000..0b7b121 --- /dev/null +++ b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandIntegrationTest.java @@ -0,0 +1,166 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.brooklyn.test.framework; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.location.LocationSpec; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.test.BrooklynAppUnitTestSupport; +import org.apache.brooklyn.core.test.entity.TestEntity; +import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.testng.annotations.Test; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.Random; +import java.util.UUID; + +import static org.apache.brooklyn.test.framework.BaseTest.TARGET_ENTITY; +import static org.apache.brooklyn.test.framework.SimpleCommand.COMMAND; +import static org.apache.brooklyn.test.framework.SimpleCommandTest.*; +import static org.assertj.core.api.Assertions.assertThat; + +public class SimpleCommandIntegrationTest extends BrooklynAppUnitTestSupport { + private static final Logger LOG = LoggerFactory.getLogger(SimpleCommandIntegrationTest.class); + + private static final String UP = "up"; + private LocalhostMachineProvisioningLocation localhost; + private String testId; + + + protected void setUpApp() { + super.setUpApp(); + testId = UUID.randomUUID().toString(); + + localhost = app.getManagementContext().getLocationManager() + .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) + .configure("name", testId)); + } + + @Test(groups = "Integration") + public void shouldInvokeCommand() { + TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + + SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(COMMAND, "uptime") + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) + .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, UP))); + + app.start(ImmutableList.of(localhost)); + + assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() + .withFailMessage("Service should be up"); + assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) + .withFailMessage("Service should be marked running"); + + } + + @Test(groups = "Integration") + public void shouldNotBeUpIfAssertionFails() { + TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + + SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(COMMAND, "uptime") + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 1))); + + try { + app.start(ImmutableList.of(localhost)); + } catch (Exception e) { + assertThat(e.getCause().getMessage().contains("exit code equals 1")); + } + + assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.ON_FIRE) + .withFailMessage("Service should be marked on fire"); + + } + + @Test(groups = "Integration") + public void shouldInvokeScript() { + TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + + String text = "hello world"; + String testUrl = createTempScript("script.sh", "echo " + text); + + SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(DOWNLOAD_URL, testUrl) + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) + .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, text))); + + app.start(ImmutableList.of(localhost)); + + assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() + .withFailMessage("Service should be up"); + assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) + .withFailMessage("Service should be marked running"); + } + + @Test + public void shouldExecuteInTheRightPlace() throws Exception { + TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); + + String remoteTmp = randomName(); + SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(COMMAND, "mkdir " + remoteTmp) + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0))); + + String pwdUrl = createTempScript("pwd.sh", "pwd"); + + SimpleCommandTest pwd = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) + .configure(TARGET_ENTITY, testEntity) + .configure(DOWNLOAD_URL, pwdUrl) + .configure(RUN_DIR, remoteTmp) + .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) + .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, remoteTmp))); + + app.start(ImmutableList.of(localhost)); + + assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() + .withFailMessage("Service should be up"); + assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) + .withFailMessage("Service should be marked running"); + } + + private String createTempScript(String filename, String contents) { + try { + Path tempDirectory = Files.createTempDirectory(randomName()); + tempDirectory.toFile().deleteOnExit(); + Path tempFile = Files.createFile(tempDirectory.resolve(filename)); + Files.write(tempFile, contents.getBytes()); + return "file:" + tempFile.toString(); + } catch (IOException e) { + throw Exceptions.propagate(e); + } + } + + private String randomName() { + return Integer.valueOf(new Random(System.currentTimeMillis()).nextInt(100000)).toString(); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/f3a6395e/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java ---------------------------------------------------------------------- diff --git a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java b/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java deleted file mode 100644 index 3b6b875..0000000 --- a/usage/test-framework/src/test/java/org/apache/brooklyn/test/framework/SimpleCommandScriptIntegrationTest.java +++ /dev/null @@ -1,165 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -package org.apache.brooklyn.test.framework; - -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import org.apache.brooklyn.api.entity.EntitySpec; -import org.apache.brooklyn.api.location.LocationSpec; -import org.apache.brooklyn.api.mgmt.ManagementContext; -import org.apache.brooklyn.core.entity.Entities; -import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; -import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; -import org.apache.brooklyn.core.test.entity.TestApplication; -import org.apache.brooklyn.core.test.entity.TestEntity; -import org.apache.brooklyn.location.localhost.LocalhostMachineProvisioningLocation; -import org.apache.brooklyn.test.http.TestHttpRequestHandler; -import org.apache.brooklyn.test.http.TestHttpServer; -import org.apache.brooklyn.util.http.HttpAsserts; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.testng.annotations.*; - -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.Random; -import java.util.UUID; - -import static org.apache.brooklyn.test.framework.BaseTest.TARGET_ENTITY; -import static org.apache.brooklyn.test.framework.SimpleCommand.COMMAND; -import static org.apache.brooklyn.test.framework.SimpleCommandTest.*; -import static org.assertj.core.api.Assertions.assertThat; - -public class SimpleCommandScriptIntegrationTest { - private static final Logger LOG = LoggerFactory.getLogger(SimpleCommandScriptIntegrationTest.class); - - private static final String UP = "up"; - private static final String SCRIPT_NAME = "script.sh"; - private static final String TEXT = "hello world"; - private TestApplication app; - private ManagementContext managementContext; - private LocalhostMachineProvisioningLocation localhost; - private TestHttpServer server; - private String testId; - - - @BeforeClass - public void setUpTests() { - server = initializeServer(); - } - - @AfterClass - public void tearDownTests() { - if (null != server) { - server.stop(); - } - } - - @BeforeMethod - public void setUp() { - - testId = UUID.randomUUID().toString(); - - app = TestApplication.Factory.newManagedInstanceForTests(); - managementContext = app.getManagementContext(); - - localhost = managementContext.getLocationManager() - .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class) - .configure("name", testId)); - } - - @AfterMethod(alwaysRun = true) - public void tearDown() throws Exception { - if (app != null) Entities.destroyAll(app.getManagementContext()); - } - - - private TestHttpServer initializeServerUnstarted() { - return new TestHttpServer() - .handler("/" + SCRIPT_NAME, - new TestHttpRequestHandler().response("#!/bin/sh\necho " + TEXT + "\n")); - } - private TestHttpServer initializeServer() { - return initializeServerUnstarted().start(); - } - - - - @Test(groups = "Integration") - public void shouldInvokeScript() { - TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - - String testUrl = server.getUrl() + "/" + SCRIPT_NAME; - HttpAsserts.assertContentContainsText(testUrl, TEXT); - - SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) - .configure(TARGET_ENTITY, testEntity) - .configure(DOWNLOAD_URL, testUrl) - .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) - .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, TEXT))); - - app.start(ImmutableList.of(localhost)); - - assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() - .withFailMessage("Service should be up"); - assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) - .withFailMessage("Service should be marked running"); - } - - @Test - public void shouldExecuteInTheRightPlace() throws Exception { - TestEntity testEntity = app.createAndManageChild(EntitySpec.create(TestEntity.class)); - - String testUrl = server.getUrl() + "/" + SCRIPT_NAME; - HttpAsserts.assertContentContainsText(testUrl, TEXT); - - String remoteTmp = randomName(); - SimpleCommandTest uptime = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) - .configure(TARGET_ENTITY, testEntity) - .configure(COMMAND, "mkdir " + remoteTmp) - .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0))); - - Path localTmpPath = Paths.get("/tmp/").resolve(randomName()); - Path pwdSh = localTmpPath.resolve("pwd.sh"); - Files.createDirectory(localTmpPath); - Files.createFile(pwdSh); - Files.write(pwdSh, "pwd".getBytes()); - String pwdUrl = "file:" + pwdSh.toString(); - - SimpleCommandTest pwd = app.createAndManageChild(EntitySpec.create(SimpleCommandTest.class) - .configure(TARGET_ENTITY, testEntity) - .configure(DOWNLOAD_URL, pwdUrl) - .configure(RUN_DIR, remoteTmp) - .configure(ASSERT_STATUS, ImmutableMap.of(EQUALS, 0)) - .configure(ASSERT_OUT, ImmutableMap.of(CONTAINS, remoteTmp))); - - app.start(ImmutableList.of(localhost)); - - assertThat(uptime.sensors().get(SERVICE_UP)).isTrue() - .withFailMessage("Service should be up"); - assertThat(ServiceStateLogic.getExpectedState(uptime)).isEqualTo(Lifecycle.RUNNING) - .withFailMessage("Service should be marked running"); - } - - private String randomName() { - return Integer.valueOf(new Random(System.currentTimeMillis()).nextInt(100000)).toString(); - } - -}
