refactor test cases: use testNG instead of junit, as testNG can group test cases and pass parameter to test case

This commit is contained in:
Edison Su 2012-12-22 22:10:25 -08:00
parent 877e16029c
commit 4b2d9f4415
19 changed files with 465 additions and 199 deletions

View File

@ -51,11 +51,11 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
<scope>compile</scope>
</dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.2.2</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
@ -72,10 +72,22 @@
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<defaultGoal>install</defaultGoal>
<testSourceDirectory>test</testSourceDirectory>
<testSourceDirectory>${project.basedir}/test</testSourceDirectory>
<testResources>
<testResource>
<directory>${project.basedir}/test/resource</directory>
</testResource>
</testResources>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>

View File

@ -0,0 +1,68 @@
package org.apache.cloudstack.storage.test;
import java.lang.reflect.Method;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
public class CloudStackTestNGBase extends AbstractTestNGSpringContextTests {
private String hostGateway;
private String hostCidr;
private String hostIp;
private String hostGuid;
private String templateUrl;
private String localStorageUuid;
private Transaction txn;
@BeforeMethod(alwaysRun = true)
protected void injectDB(Method testMethod) throws Exception {
txn = Transaction.open(testMethod.getName());
}
@AfterMethod(alwaysRun = true)
protected void closeDB(Method testMethod) throws Exception {
if (txn != null) {
txn.close();
}
}
@BeforeMethod(alwaysRun = true)
@Parameters({"devcloud-host-uuid", "devcloud-host-gateway", "devcloud-host-cidr", "devcloud-host-ip", "template-url", "devcloud-local-storage-uuid"})
protected void setup(String hostuuid, String gateway, String cidr, String hostIp, String templateUrl, String localStorageUuid) {
this.hostGuid = hostuuid;
this.hostGateway = gateway;
this.hostCidr = cidr;
this.hostIp = hostIp;
this.templateUrl = templateUrl;
this.localStorageUuid = localStorageUuid;
}
protected String getHostGuid() {
return this.hostGuid;
}
protected String getHostGateway() {
return this.hostGateway;
}
protected String getHostCidr() {
return this.hostCidr;
}
protected String getHostIp() {
return this.hostIp;
}
protected String getTemplateUrl() {
return this.templateUrl;
}
protected String getLocalStorageUuid() {
return this.localStorageUuid;
}
}

View File

@ -18,6 +18,7 @@
*/
package org.apache.cloudstack.storage.test;
import java.lang.reflect.Method;
import java.util.UUID;
import javax.inject.Inject;
@ -27,12 +28,16 @@ import org.apache.cloudstack.storage.to.ImageDataStoreTO;
import org.apache.cloudstack.storage.to.ImageOnPrimayDataStoreTO;
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
import org.apache.cloudstack.storage.to.TemplateTO;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.cloud.agent.AgentManager;
import com.cloud.agent.api.ReadyCommand;
@ -52,10 +57,11 @@ import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.org.Cluster.ClusterType;
import com.cloud.org.Managed.ManagedState;
import com.cloud.resource.ResourceState;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/resource/storageContext.xml")
public class DirectAgentTest {
@ContextConfiguration(locations="classpath:/storageContext.xml")
public class DirectAgentTest extends CloudStackTestNGBase {
@Inject
AgentManager agentMgr;
@Inject
@ -69,10 +75,10 @@ public class DirectAgentTest {
private long dcId;
private long clusterId;
private long hostId;
private String hostGuid = "9d4c9db8-32f7-25c3-0435-eab4bf3adcea";
@Before
@Test(priority = -1)
public void setUp() {
HostVO host = hostDao.findByGuid(hostGuid);
HostVO host = hostDao.findByGuid(this.getHostGuid());
if (host != null) {
hostId = host.getId();
dcId = host.getDataCenterId();
@ -86,7 +92,7 @@ public class DirectAgentTest {
dcId = dc.getId();
//create pod
HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), dc.getId(), "192.168.56.1", "192.168.56.0/24", 8, "test");
HostPodVO pod = new HostPodVO(UUID.randomUUID().toString(), dc.getId(), this.getHostGateway(), this.getHostCidr(), 8, "test");
pod = podDao.persist(pod);
//create xen cluster
ClusterVO cluster = new ClusterVO(dc.getId(), pod.getId(), "devcloud cluster");
@ -97,12 +103,11 @@ public class DirectAgentTest {
clusterId = cluster.getId();
//create xen host
//TODO: this hardcode host uuid in devcloud
host = new HostVO(hostGuid);
host = new HostVO(this.getHostGuid());
host.setName("devcloud xen host");
host.setType(Host.Type.Routing);
host.setHypervisorType(HypervisorType.XenServer);
host.setPrivateIpAddress("192.168.56.10");
host.setPrivateIpAddress(this.getHostIp());
host.setDataCenterId(dc.getId());
host.setVersion("6.0.1");
host.setAvailable(true);
@ -133,14 +138,14 @@ public class DirectAgentTest {
public void testDownloadTemplate() {
ImageOnPrimayDataStoreTO image = Mockito.mock(ImageOnPrimayDataStoreTO.class);
PrimaryDataStoreTO primaryStore = Mockito.mock(PrimaryDataStoreTO.class);
Mockito.when(primaryStore.getUuid()).thenReturn("9f3f9262-3f77-09cc-2df7-0d8475676260");
Mockito.when(primaryStore.getUuid()).thenReturn(this.getLocalStorageUuid());
Mockito.when(image.getPrimaryDataStore()).thenReturn(primaryStore);
ImageDataStoreTO imageStore = Mockito.mock(ImageDataStoreTO.class);
Mockito.when(imageStore.getType()).thenReturn("http");
TemplateTO template = Mockito.mock(TemplateTO.class);
Mockito.when(template.getPath()).thenReturn("http://download.cloud.com/templates/devcloud/defaulttemplates/5/ce5b212e-215a-3461-94fb-814a635b2215.vhd");
Mockito.when(template.getPath()).thenReturn(this.getTemplateUrl());
Mockito.when(template.getImageDataStore()).thenReturn(imageStore);
Mockito.when(image.getTemplate()).thenReturn(template);

View File

@ -1,114 +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.cloudstack.storage.test;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
public class Future2<T> {
private Callable<T> callable;
private Callable<T> success;
private Callable<T> failed;
private ExecutorService executor = Executors.newFixedThreadPool(2);
private Future<T> f;
public class Func<T> implements Callable<T> {
private Callable<T> f;
private Callable<T> fs;
public T func() throws Exception {
return f.call();
}
public T success() throws Exception {
return fs.call();
}
public Func (Callable<T> f, Callable<T> s) {
this.f = f;
this.fs = s;
}
@Override
public T call() throws Exception {
func();
success();
return null;
}
}
public Future2 (Callable<T> callable) {
this.callable = callable;
}
public void onSuccess(Callable<T> s) {
this.success = s;
}
public void go() {
Func<T> ft = new Func<T>(this.callable, this.success);
f = executor.submit(ft);
}
public T get() {
try {
return this.f.get();
} catch (InterruptedException e) {
return null;
} catch (ExecutionException e) {
// TODO Auto-generated catch block
return null;
}
}
public void shutdown() {
this.executor.shutdown();
}
public static void main(String[] args) {
Callable<String> fun = new Callable<String> () {
@Override
public String call() throws Exception {
System.out.println("execing");
return "test";
}
};
Future2<String> f2 = new Future2<String>(fun);
f2.onSuccess(new Callable<String>() {
@Override
public String call() throws Exception {
Thread.sleep(1000);
System.out.println("success");
return null;
}
});
f2.go();
//f2.get();
f2.shutdown();
}
}

View File

@ -1,41 +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.cloudstack.storage.test;
import static org.junit.Assert.*;
import java.net.URI;
import java.net.URISyntaxException;
import org.junit.Test;
public class SimpleTest {
@Test
public void test() {
try {
URI u = new URI("http://myproxy.domain.com:3128");
System.out.print(u.getHost());
} catch (URISyntaxException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

View File

@ -40,13 +40,15 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.testng.annotations.Parameters;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/resource/storageContext.xml")
public class TestHttp {
@ContextConfiguration(locations="classpath:/storageContext.xml")
public class TestHttp extends AbstractTestNGSpringContextTests {
@Test
public void testHttpclient() {
HttpHead method = new HttpHead("http://download.cloud.com/templates/devcloud/defaulttemplates/5/ce5b212e-215a-3461-94fb-814a635b2215.vhd");
@Parameters("template-url")
public void testHttpclient(String templateUrl) {
HttpHead method = new HttpHead(templateUrl);
DefaultHttpClient client = new DefaultHttpClient();
OutputStream output = null;
@ -60,7 +62,7 @@ public class TestHttp {
localFile.createNewFile();
}
HttpGet getMethod = new HttpGet("http://download.cloud.com/templates/devcloud/defaulttemplates/5/ce5b212e-215a-3461-94fb-814a635b2215.vhd");
HttpGet getMethod = new HttpGet(templateUrl);
response = client.execute(getMethod);
HttpEntity entity = response.getEntity();

View File

@ -0,0 +1,19 @@
package org.apache.cloudstack.storage.test;
import junit.framework.Assert;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests;
import org.springframework.test.context.testng.AbstractTransactionalTestNGSpringContextTests;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import com.cloud.utils.db.DB;
@ContextConfiguration(locations="classpath:/storageContext.xml")
public class TestNG extends AbstractTestNGSpringContextTests {
@Test
@DB
public void test1() {
Assert.assertEquals("", "192.168.56.2");
}
}

View File

@ -0,0 +1,37 @@
package org.apache.cloudstack.storage.test;
import java.lang.reflect.Method;
import java.util.List;
import org.testng.IMethodInstance;
import org.testng.IMethodInterceptor;
import org.testng.ITestContext;
import org.testng.ITestNGMethod;
import org.testng.internal.ConstructorOrMethod;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
public class TestNGAop implements IMethodInterceptor {
@Override
public List<IMethodInstance> intercept(List<IMethodInstance> methods,
ITestContext context) {
for (IMethodInstance methodIns : methods) {
ITestNGMethod method = methodIns.getMethod();
ConstructorOrMethod meth = method.getConstructorOrMethod();
Method m = meth.getMethod();
if (m != null) {
DB db = m.getAnnotation(DB.class);
if (db != null) {
Transaction txn = Transaction.open(m.getName());
}
}
}
// TODO Auto-generated method stub
return methods;
}
}

View File

@ -105,7 +105,7 @@ import com.cloud.utils.db.DB;
import com.cloud.utils.db.Transaction;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:/resource/storageContext.xml")
@ContextConfiguration(locations="classpath:/storageContext.xml")
public class volumeServiceTest {
@Inject
ImageDataStoreProviderManager imageProviderMgr;
@ -158,7 +158,7 @@ public class volumeServiceTest {
HostVO host = new HostVO(UUID.randomUUID().toString());
host.setName("devcloud xen host");
host.setType(Host.Type.Routing);
host.setPrivateIpAddress("192.168.56.10");
host.setPrivateIpAddress("192.168.56.2");
host.setDataCenterId(dc.getId());
host.setVersion("6.0.1");
host.setAvailable(true);

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite thread-count="1" verbose="1" name="storage test" annotations="JDK" parallel="methods">
<!--describe your test environment-->
<parameter name="devcloud-host-ip" value="192.168.56.2"/>
<parameter name="devcloud-host-gateway" value="192.168.56.1"/>
<parameter name="devcloud-host-cidr" value="192.168.56.0/24"/>
<parameter name="template-url" value="http://192.168.56.1/ce5b212e-215a-3461-94fb-814a635b2215.vhd"/>
<parameter name="primary-storage-want-to-add" value="nfs://192.168.56.2/opt/storage/primarynfs"/>
<parameter name="devcloud-local-storage-uuid" value="cd10cac1-4772-92e5-5da6-c2bc16b1ce1b"/>
<parameter name="devcloud-host-uuid" value="759ee4c9-a15a-297b-67c6-ac267d8aa429"/>
<listeners>
<listener class-name="org.apache.cloudstack.storage.test.TestNGAop" />
</listeners>
<test name="integration-tests">
<packages>
<package name="org.apache.cloudstack.storage.test"/>
</packages>
</test>
</suite>

View File

@ -0,0 +1,23 @@
package org.apache.cloudstack.storage.command;
import org.apache.cloudstack.storage.to.PrimaryDataStoreTO;
import com.cloud.agent.api.Command;
public class CreatePrimaryDataStoreCmd extends Command implements StorageSubSystemCommand {
private final PrimaryDataStoreTO dataStore;
public CreatePrimaryDataStoreCmd(PrimaryDataStoreTO dataStore) {
this.dataStore = dataStore;
}
public PrimaryDataStoreTO getDataStore() {
return this.dataStore;
}
@Override
public boolean executeInSequence() {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -23,6 +23,7 @@ import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import com.cloud.utils.exception.CloudRuntimeException;
@ -48,6 +49,7 @@ public class NfsValidator implements ProtocolValidator {
params.put("path", hostPath);
params.put("user", userInfo);
params.put("port", String.valueOf(port));
params.put("uuid", UUID.fromString(storageHost + hostPath).toString());
} catch (URISyntaxException e) {
throw new CloudRuntimeException("invalid url: " + e.toString());

View File

@ -53,7 +53,7 @@ public class PrimaryDataStoreVO implements Identity {
private String uuid = null;
@Column(name = "pool_type", updatable = false, nullable = false, length = 32)
private String protocol;
private String poolType;
@Column(name = GenericDao.CREATED_COLUMN)
Date created;
@ -123,11 +123,11 @@ public class PrimaryDataStoreVO implements Identity {
}
public String getPoolType() {
return protocol;
return poolType;
}
public void setPoolType(String protocol) {
this.protocol = protocol;
this.poolType = protocol;
}
public Date getCreated() {
@ -262,6 +262,6 @@ public class PrimaryDataStoreVO implements Identity {
@Override
public String toString() {
return new StringBuilder("Pool[").append(id).append("|").append(protocol).append("]").toString();
return new StringBuilder("Pool[").append(id).append("|").append(poolType).append("]").toString();
}
}

View File

@ -51,21 +51,25 @@ public class DefaultPrimaryDataStoreLifeCycleImpl implements PrimaryDataStoreLif
return true;
}
protected void attachCluster() {
//send down createStoragePool command to all the hosts in the cluster
AttachPrimaryDataStoreCmd cmd = new AttachPrimaryDataStoreCmd(this.dataStore);
for (EndPoint ep : dataStore.getEndPoints()) {
ep.sendMessage(cmd);
}
}
@Override
public boolean attachCluster(ClusterScope scope) {
attachCluster();
PrimaryDataStoreVO dataStoreVO = dataStoreDao.findById(this.dataStore.getId());
dataStoreVO.setDataCenterId(scope.getZoneId());
dataStoreVO.setPodId(scope.getPodId());
dataStoreVO.setClusterId(scope.getScopeId());
dataStoreVO.setStatus(DataStoreStatus.Up);
dataStoreDao.update(dataStoreVO.getId(), dataStoreVO);
//send down createStoragePool command to all the hosts in the cluster
AttachPrimaryDataStoreCmd cmd = new AttachPrimaryDataStoreCmd(this.dataStore);
for (EndPoint ep : dataStore.getEndPoints()) {
ep.sendMessage(cmd);
}
return false;
return true;
}
@Override

View File

@ -18,8 +18,13 @@
*/
package org.apache.cloudstack.storage.datastore.lifecycle;
import org.apache.cloudstack.storage.EndPoint;
import org.apache.cloudstack.storage.command.AttachPrimaryDataStoreCmd;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import com.cloud.agent.api.Answer;
import com.cloud.utils.exception.CloudRuntimeException;
public class DefaultXenPrimaryDataStoreLifeCycle extends DefaultPrimaryDataStoreLifeCycleImpl {
/**
@ -30,5 +35,22 @@ public class DefaultXenPrimaryDataStoreLifeCycle extends DefaultPrimaryDataStore
super(dataStoreDao);
// TODO Auto-generated constructor stub
}
@Override
public void attachCluster() {
//send one time is enough, as xenserver is clustered
AttachPrimaryDataStoreCmd cmd = new AttachPrimaryDataStoreCmd(this.dataStore);
String result = null;
for (EndPoint ep : dataStore.getEndPoints()) {
Answer answer = ep.sendMessage(cmd);
if (answer.getResult()) {
return;
}
result = answer.getDetails();
}
throw new CloudRuntimeException("AttachPrimaryDataStoreCmd failed: " + result);
}
}

View File

@ -125,6 +125,7 @@ public class DefaultPrimaryDatastoreProviderImpl implements PrimaryDataStoreProv
dataStoreVO.setPort(Integer.parseInt(dsInfos.get("port")));
dataStoreVO.setKey(key);
dataStoreVO.setName(dsInfos.get("name"));
dataStoreVO.setUuid(dsInfos.get("uuid"));
dataStoreVO = dataStoreDao.persist(dataStoreVO);
DefaultPrimaryDataStore dataStore = (DefaultPrimaryDataStore)configurator.getDataStore(dataStoreVO.getId());

View File

@ -32,6 +32,7 @@ import org.apache.cloudstack.storage.command.AttachPrimaryDataStoreAnswer;
import org.apache.cloudstack.storage.command.AttachPrimaryDataStoreCmd;
import org.apache.cloudstack.storage.command.CopyTemplateToPrimaryStorageCmd;
import org.apache.cloudstack.storage.command.CopyTemplateToPrimaryStorageAnswer;
import org.apache.cloudstack.storage.command.CreatePrimaryDataStoreCmd;
import org.apache.cloudstack.storage.command.StorageSubSystemCommand;
import org.apache.cloudstack.storage.to.ImageDataStoreTO;
import org.apache.cloudstack.storage.to.ImageOnPrimayDataStoreTO;
@ -56,10 +57,14 @@ import com.cloud.agent.api.Command;
import com.cloud.agent.api.ModifyStoragePoolAnswer;
import com.cloud.agent.api.storage.PrimaryStorageDownloadAnswer;
import com.cloud.agent.api.to.StorageFilerTO;
import com.cloud.hypervisor.xen.resource.CitrixResourceBase.SRType;
import com.cloud.storage.Storage.StoragePoolType;
import com.cloud.storage.template.TemplateInfo;
import com.cloud.utils.exception.CloudRuntimeException;
import com.xensource.xenapi.Connection;
import com.xensource.xenapi.Host;
import com.xensource.xenapi.PBD;
import com.xensource.xenapi.Pool;
import com.xensource.xenapi.SR;
import com.xensource.xenapi.Types;
import com.xensource.xenapi.Types.BadServerResponse;
@ -79,13 +84,212 @@ public class XenServerStorageResource {
return this.execute((CopyTemplateToPrimaryStorageCmd)command);
} else if (command instanceof AttachPrimaryDataStoreCmd) {
return this.execute((AttachPrimaryDataStoreCmd)command);
} else if (command instanceof CreatePrimaryDataStoreCmd) {
return execute((CreatePrimaryDataStoreCmd) command);
}
return new Answer((Command)command, false, "not implemented yet");
}
/*
protected SR getNfsSR(Connection conn, PrimaryDataStoreTO pool) {
Map<String, String> deviceConfig = new HashMap<String, String>();
try {
String server = pool.getHost();
String serverpath = pool.getPath();
serverpath = serverpath.replace("//", "/");
Set<SR> srs = SR.getAll(conn);
for (SR sr : srs) {
if (!SRType.NFS.equals(sr.getType(conn))) {
continue;
}
Set<PBD> pbds = sr.getPBDs(conn);
if (pbds.isEmpty()) {
continue;
}
PBD pbd = pbds.iterator().next();
Map<String, String> dc = pbd.getDeviceConfig(conn);
if (dc == null) {
continue;
}
if (dc.get("server") == null) {
continue;
}
if (dc.get("serverpath") == null) {
continue;
}
if (server.equals(dc.get("server")) && serverpath.equals(dc.get("serverpath"))) {
throw new CloudRuntimeException("There is a SR using the same configuration server:" + dc.get("server") + ", serverpath:"
+ dc.get("serverpath") + " for pool " + pool.getUuid() + "on host:" + _host.uuid);
}
}
deviceConfig.put("server", server);
deviceConfig.put("serverpath", serverpath);
Host host = Host.getByUuid(conn, _host.uuid);
SR sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), Long.toString(pool.getId()), SRType.NFS.toString(), "user", true,
new HashMap<String, String>());
sr.scan(conn);
return sr;
} catch (XenAPIException e) {
throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
} catch (XmlRpcException e) {
throw new CloudRuntimeException("Unable to create NFS SR " + pool.toString(), e);
}
}
protected SR getIscsiSR(Connection conn, PrimaryDataStoreTO pool) {
synchronized (pool.getUuid().intern()) {
Map<String, String> deviceConfig = new HashMap<String, String>();
try {
String target = pool.getHost();
String path = pool.getPath();
if (path.endsWith("/")) {
path = path.substring(0, path.length() - 1);
}
String tmp[] = path.split("/");
if (tmp.length != 3) {
String msg = "Wrong iscsi path " + pool.getPath() + " it should be /targetIQN/LUN";
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
String targetiqn = tmp[1].trim();
String lunid = tmp[2].trim();
String scsiid = "";
Set<SR> srs = SR.getByNameLabel(conn, pool.getUuid());
for (SR sr : srs) {
if (!SRType.LVMOISCSI.equals(sr.getType(conn))) {
continue;
}
Set<PBD> pbds = sr.getPBDs(conn);
if (pbds.isEmpty()) {
continue;
}
PBD pbd = pbds.iterator().next();
Map<String, String> dc = pbd.getDeviceConfig(conn);
if (dc == null) {
continue;
}
if (dc.get("target") == null) {
continue;
}
if (dc.get("targetIQN") == null) {
continue;
}
if (dc.get("lunid") == null) {
continue;
}
if (target.equals(dc.get("target")) && targetiqn.equals(dc.get("targetIQN")) && lunid.equals(dc.get("lunid"))) {
throw new CloudRuntimeException("There is a SR using the same configuration target:" + dc.get("target") + ", targetIQN:"
+ dc.get("targetIQN") + ", lunid:" + dc.get("lunid") + " for pool " + pool.getUuid() + "on host:" + _host.uuid);
}
}
deviceConfig.put("target", target);
deviceConfig.put("targetIQN", targetiqn);
Host host = Host.getByUuid(conn, _host.uuid);
Map<String, String> smConfig = new HashMap<String, String>();
String type = SRType.LVMOISCSI.toString();
String poolId = Long.toString(pool.getId());
SR sr = null;
try {
sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), poolId, type, "user", true,
smConfig);
} catch (XenAPIException e) {
String errmsg = e.toString();
if (errmsg.contains("SR_BACKEND_FAILURE_107")) {
String lun[] = errmsg.split("<LUN>");
boolean found = false;
for (int i = 1; i < lun.length; i++) {
int blunindex = lun[i].indexOf("<LUNid>") + 7;
int elunindex = lun[i].indexOf("</LUNid>");
String ilun = lun[i].substring(blunindex, elunindex);
ilun = ilun.trim();
if (ilun.equals(lunid)) {
int bscsiindex = lun[i].indexOf("<SCSIid>") + 8;
int escsiindex = lun[i].indexOf("</SCSIid>");
scsiid = lun[i].substring(bscsiindex, escsiindex);
scsiid = scsiid.trim();
found = true;
break;
}
}
if (!found) {
String msg = "can not find LUN " + lunid + " in " + errmsg;
s_logger.warn(msg);
throw new CloudRuntimeException(msg);
}
} else {
String msg = "Unable to create Iscsi SR " + deviceConfig + " due to " + e.toString();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
}
}
deviceConfig.put("SCSIid", scsiid);
String result = SR.probe(conn, host, deviceConfig, type , smConfig);
String pooluuid = null;
if( result.indexOf("<UUID>") != -1) {
pooluuid = result.substring(result.indexOf("<UUID>") + 6, result.indexOf("</UUID>")).trim();
}
if( pooluuid == null || pooluuid.length() != 36) {
sr = SR.create(conn, host, deviceConfig, new Long(0), pool.getUuid(), poolId, type, "user", true,
smConfig);
} else {
sr = SR.introduce(conn, pooluuid, pool.getUuid(), poolId,
type, "user", true, smConfig);
Pool.Record pRec = XenServerConnectionPool.getPoolRecord(conn);
PBD.Record rec = new PBD.Record();
rec.deviceConfig = deviceConfig;
rec.host = pRec.master;
rec.SR = sr;
PBD pbd = PBD.create(conn, rec);
pbd.plug(conn);
}
sr.scan(conn);
return sr;
} catch (XenAPIException e) {
String msg = "Unable to create Iscsi SR " + deviceConfig + " due to " + e.toString();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
} catch (Exception e) {
String msg = "Unable to create Iscsi SR " + deviceConfig + " due to " + e.getMessage();
s_logger.warn(msg, e);
throw new CloudRuntimeException(msg, e);
}
}
}*/
protected Answer execute(CreatePrimaryDataStoreCmd cmd) {
Connection conn = hypervisorResource.getConnection();
PrimaryDataStoreTO dataStore = cmd.getDataStore();
try {
if (dataStore.getType() == StoragePoolType.NetworkFilesystem.toString()) {
//getNfsSR(conn, dataStore);
} else if (dataStore.getType() == StoragePoolType.IscsiLUN.toString()) {
//getIscsiSR(conn, dataStore);
} else if (dataStore.getType() == StoragePoolType.PreSetup.toString()) {
} else {
//return new Answer(cmd, false, "The pool type: " + pool.getType().name() + " is not supported.");
}
return new Answer(cmd, true, "success");
} catch (Exception e) {
// String msg = "Catch Exception " + e.getClass().getName() + ", create StoragePool failed due to " + e.toString() + " on host:" + _host.uuid + " pool: " + pool.getHost() + pool.getPath();
//s_logger.warn(msg, e);
return new Answer(cmd, false, null);
}
}
private long getTemplateSize(Connection conn, String url) {
String size = hypervisorResource.callHostPlugin(conn, "storagePlugin", "getTemplateSize", "srcUrl", url);
if (size == "" || size == null) {
if (size.equalsIgnoreCase("") || size == null) {
throw new CloudRuntimeException("Can't get template size");
}

View File

@ -22,7 +22,7 @@
import os, sys, time
import XenAPIPlugin
sys.path.extend(["/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
sys.path.extend(["/opt/xensource/sm/", "/usr/lib/xcp/sm/", "/usr/local/sbin/", "/sbin/"])
import util
import base64
import socket

View File

@ -64,4 +64,4 @@ cloud-prepare-upgrade.sh=..,0755,/usr/lib/xcp/bin
getRouterStatus.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
bumpUpPriority.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
getDomRVersion.sh=../../../../network/domr/,0755,/usr/lib/xcp/bin
storagePlugin=.,0755,/usr/lib/xcp/plugins
storagePlugin=..,0755,/usr/lib/xcp/plugins