/*
* Copyright © 2015 Cask Data, Inc.
*
* Licensed 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 co.cask.cdap.internal.app.services;
import co.cask.cdap.MissingMapReduceWorkflowApp;
import co.cask.cdap.common.ArtifactNotFoundException;
import co.cask.cdap.common.io.Locations;
import co.cask.cdap.internal.app.deploy.ProgramTerminator;
import co.cask.cdap.internal.app.runtime.artifact.ArtifactRepository;
import co.cask.cdap.internal.app.services.http.AppFabricTestBase;
import co.cask.cdap.internal.test.AppJarHelper;
import co.cask.cdap.proto.Id;
import com.google.common.io.Files;
import org.apache.twill.filesystem.Location;
import org.apache.twill.filesystem.LocationFactory;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;
import java.io.File;
/**
*/
public class ApplicationLifecycleServiceTest extends AppFabricTestBase {
private static ApplicationLifecycleService applicationLifecycleService;
private static LocationFactory locationFactory;
private static ArtifactRepository artifactRepository;
@BeforeClass
public static void setup() throws Exception {
applicationLifecycleService = getInjector().getInstance(ApplicationLifecycleService.class);
locationFactory = getInjector().getInstance(LocationFactory.class);
artifactRepository = getInjector().getInstance(ArtifactRepository.class);
}
// test that the call to deploy an artifact and application in a single step will delete the artifact
// if the application could not be created
@Test(expected = ArtifactNotFoundException.class)
public void testDeployArtifactAndApplicationCleansUpArtifactOnFailure() throws Exception {
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "missing-mr", "1.0.0-SNAPSHOT");
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, MissingMapReduceWorkflowApp.class);
File appJarFile = new File(tmpFolder.newFolder(),
String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Files.copy(Locations.newInputSupplier(appJar), appJarFile);
appJar.delete();
try {
applicationLifecycleService.deployAppAndArtifact(Id.Namespace.DEFAULT, "appName", artifactId, appJarFile, null,
new ProgramTerminator() {
@Override
public void stop(Id.Program programId) throws Exception {
// no-op
}
});
Assert.fail("expected application deployment to fail.");
} catch (Exception e) {
// expected
}
// the artifact should have been cleaned up, and this should throw a not found exception
artifactRepository.getArtifact(artifactId);
}
}