Commit 385960c6 authored by Joerg Domaschka's avatar Joerg Domaschka

more testability to shells.

parent 44ceeaeb
/*
* Copyright (c) 2014-2015 University of Ulm
*
* See the NOTICE file distributed with this work for additional information
* regarding copyright ownership. 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 de.uniulm.omi.cloudiator.lance.lca.containers.plain;
import de.uniulm.omi.cloudiator.lance.container.spec.os.OperatingSystem;
import de.uniulm.omi.cloudiator.lance.lca.containers.plain.shell.PlainShell;
import de.uniulm.omi.cloudiator.lance.lca.containers.plain.shell.PlainShellImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.atomic.AtomicReference;
/**
* Created by Daniel Seybold on 12.08.2015.
*/
final class PlainShellFactoryImpl implements PlainShellFactory {
private static final Logger LOGGER = LoggerFactory.getLogger(PlainShell.class);
private final AtomicReference<PlainShellWrapper> reference = new AtomicReference<>();
@Override
public PlainShellWrapper createShell() {
PlainShellWrapper wrapper = reference.get();
if(wrapper == null)
throw new IllegalStateException("plain shell not set");
return wrapper;
}
@Override
public PlainShell createAndinstallPlainShell(OperatingSystem os) {
PlainShell plainShell = new PlainShellImpl(os);
final PlainShellWrapper wrapper = new PlainShellWrapper(plainShell);
PlainShellWrapper old = reference.getAndSet(wrapper);
if(old != null) {
LOGGER.error("ERROR: overriding plain shell with new one. this should never happen.");
}
return plainShell;
}
@Override
public void closeShell() {
PlainShellWrapper old = reference.getAndSet(null);
if(old == null) {
LOGGER.error("ERROR: no plain shell set that can be closed.");
} else {
old.plainShell.close();
}
}
}
package de.uniulm.omi.cloudiator.lance.lca.containers.plain;
import java.util.Map;
import org.junit.Test;
import de.uniulm.omi.cloudiator.lance.container.spec.os.OperatingSystem;
import de.uniulm.omi.cloudiator.lance.container.standard.ErrorAwareContainer;
import de.uniulm.omi.cloudiator.lance.lca.GlobalRegistryAccessor;
import de.uniulm.omi.cloudiator.lance.lca.container.ComponentInstanceId;
import de.uniulm.omi.cloudiator.lance.lca.container.ContainerException;
import de.uniulm.omi.cloudiator.lance.lca.container.port.NetworkHandler;
import de.uniulm.omi.cloudiator.lance.lca.containers.dummy.DummyInterceptor;
import de.uniulm.omi.cloudiator.lance.lca.registry.RegistrationException;
import de.uniulm.omi.cloudiator.lance.lifecycle.ExecutionContext;
import de.uniulm.omi.cloudiator.lance.lifecycle.LifecycleController;
import de.uniulm.omi.cloudiator.lance.lifecycles.CoreElements;
import de.uniulm.omi.cloudiator.lance.lifecycles.LifecycleStoreCreator;
public class PlainContainerTest {
public volatile DummyInterceptor interceptor;
public volatile PlainContainerLogic containerLogic;
private volatile CoreElements core;
public volatile LifecycleStoreCreator creator;
private volatile ExecutionContext ctx;
private volatile ErrorAwareContainer<PlainContainerLogic> controller;
private static final int DEFAULT_PROPERTIES = 5;
private static final String INITIAL_LOCAL_ADDRESS = "<unknown>";
private volatile Map<ComponentInstanceId, Map<String, String>> dumb;
private void init(boolean creatRegistry) {
dumb = null;
core = new CoreElements(creatRegistry);
ctx = new ExecutionContext(OperatingSystem.WINDOWS_7, null);
creator = new LifecycleStoreCreator();
creator.addDefaultStartDetector();
}
/* copied from PlainContainerManager createNewContainer*/
private void createNewContainer() throws ContainerException {
PlainShellFactory plainShellFactory = new TestPlainShellFactory();
GlobalRegistryAccessor accessor =
new GlobalRegistryAccessor(core.ctx, core.comp, CoreElements.componentInstanceId);
NetworkHandler networkHandler = core.networkHandler;
containerLogic =
new PlainContainerLogic(CoreElements.componentInstanceId, core.comp, core.ctx, ctx.getOperatingSystem(), networkHandler,
plainShellFactory, CoreElements.context);
ExecutionContext executionContext = new ExecutionContext(ctx.getOperatingSystem(), plainShellFactory);
LifecycleController lifecycleController =
new LifecycleController(core.comp.getLifecycleStore(), containerLogic, accessor,
executionContext, CoreElements.context);
try {
accessor.init(CoreElements.componentInstanceId);
} catch (RegistrationException re) {
throw new ContainerException("cannot start container, because registry not available",
re);
}
controller =
new ErrorAwareContainer<PlainContainerLogic>(CoreElements.componentInstanceId, containerLogic, networkHandler,
lifecycleController, accessor);
controller.create();
}
@Test
public void testInit() throws ContainerException{
init(true);
createNewContainer();
}
}
package de.uniulm.omi.cloudiator.lance.lca.containers.plain;
import java.util.Stack;
import java.util.concurrent.atomic.AtomicReference;
import de.uniulm.omi.cloudiator.lance.container.spec.os.OperatingSystem;
import de.uniulm.omi.cloudiator.lance.lca.containers.plain.shell.PlainShell;
import de.uniulm.omi.cloudiator.lance.lca.containers.plain.shell.PlainShellImpl;
import de.uniulm.omi.cloudiator.lance.lifecycle.ExecutionResult;
final class TestPlainShellFactory implements PlainShellFactory {
private final Stack<TestPlainShell> shells = new Stack<>();
private final AtomicReference<PlainShellWrapper> reference = new AtomicReference<>();
@Override
public PlainShellWrapper createShell() {
PlainShellWrapper wrapper = reference.get();
if(wrapper == null)
throw new IllegalStateException("plain shell not set");
return wrapper;
}
@Override
public void closeShell() {
PlainShellWrapper old = reference.getAndSet(null);
if(old == null) {
throw new IllegalStateException("shell was not be set");
} else {
old.plainShell.close();
}
}
@Override
public PlainShell createAndinstallPlainShell(OperatingSystem os) {
TestPlainShell plainShell = new TestPlainShell(os);
shells.push(plainShell);
final PlainShellWrapper wrapper = new PlainShellWrapper(plainShell);
PlainShellWrapper old = reference.getAndSet(wrapper);
if(old != null) {
throw new IllegalStateException("has never been set");
}
return plainShell;
}
static class TestPlainShell implements PlainShell {
private final OperatingSystem os;
public TestPlainShell(OperatingSystem osP) {
os = osP;
}
@Override
public ExecutionResult executeCommand(String string) {
// TODO Auto-generated method stub
return null;
}
@Override
public void setEnvironmentVariable(String key, String value) {
// TODO Auto-generated method stub
}
@Override
public ExecutionResult executeBlockingCommand(String command) {
// TODO Auto-generated method stub
return null;
}
@Override
public void close() {
// TODO Auto-generated method stub
}
@Override
public void setDirectory(String directory) {
// TODO Auto-generated method stub
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment