Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed issue which caused provisioning of domain each time because of … #14

Merged
merged 3 commits into from Feb 12, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -86,7 +86,7 @@ public class CachedLibvirtHost extends LibvirtHost {
List<Filesystem> filesystemMappings, List<String> copySpec) {
super(libvirt, baseDomainName, ipLookupStrategy, networkName, startTimeout, bootDelay, filesystemMappings);
this.provisionUrl = checkNotNullOrEmpty(provisionUrl, "provisionUrl");
this.provisionCmd = checkNotNullOrEmpty(provisionCmd, "provisionCmd");
this.provisionCmd = checkNotNullOrEmpty(provisionCmd, "provisionCmd").trim();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't the trim be on the inside of the check?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We trim once 'checkNotNullOrEmpty' check has passed, at this point we are sure that trim operation will not throw a null pointer exception.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ric's point is that a .trim() might actually make you end up with an empty string.

this.cacheExpirationUrl = cacheExpirationUrl;
this.cacheExpirationCmd = checkNotNullOrEmpty(cacheExpirationCmd, "cacheExpirationCmd");
this.provisionedbootDelay = provisionedbootDelay;
Expand Down
Expand Up @@ -15,6 +15,9 @@
*/
package com.xebialabs.overcast.host;

import com.xebialabs.overcast.command.CommandProcessor;
import com.xebialabs.overcast.support.libvirt.DomainWrapper;
import com.xebialabs.overcast.support.libvirt.IpLookupStrategy;
import org.junit.Test;
import org.libvirt.Connect;
import org.libvirt.Domain;
Expand All @@ -23,6 +26,8 @@

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsNull.notNullValue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

public class CachedLibvirtHostTest {
Expand Down Expand Up @@ -56,6 +61,17 @@ public class CachedLibvirtHostTest {
+ "</metadata>"
+ "</domain>";

static final String PROVISION_COMMAND_DOMAIN_XML = "<domain type='kvm'>"
+"<metadata>"
+ "<overcast_metadata xmlns=\"http://www.xebialabs.com/overcast/metadata/v1\">"
+ "<parent_domain>baseDomainName</parent_domain>"
+ "<creation_time>2014-06-13T12:28:49Z</creation_time>"
+ "<provisioned_with>provcmd</provisioned_with>"
+ "<provisioned_checksum>checksum</provisioned_checksum>"
+ "</overcast_metadata>"
+ "</metadata>"
+ "</domain>";

@Test
public void shouldReturnTrueIfNoRunningDomains() throws LibvirtException {
Connect libvirt = Mockito.mock(Connect.class);
Expand Down Expand Up @@ -105,4 +121,28 @@ public void shouldReturnFalseIfRunningDomains() throws LibvirtException {

assertThat(CachedLibvirtHost.isDomainSafeToDelete(libvirt, "staleDomain"), equalTo(true));
}

@Test
public void shouldTrimAndMatchProvisionCommand() throws LibvirtException {

Connect libvirt = Mockito.mock(Connect.class);
IpLookupStrategy lookupStrategy = Mockito.mock(IpLookupStrategy.class);

Domain domain = Mockito.mock(Domain.class);
when(domain.getXMLDesc(0)).thenReturn(PROVISION_COMMAND_DOMAIN_XML);
when(domain.getName()).thenReturn("domain");

when(libvirt.listDomains()).thenReturn(new int[] { 1 });
when(libvirt.domainLookupByID(1)).thenReturn(domain);
when(libvirt.domainLookupByName(anyString())).thenReturn(domain);
when(libvirt.listDefinedDomains()).thenReturn(new String[]{"domain"});

CachedLibvirtHost host = new CachedLibvirtHost("hostLabel", libvirt,
"baseDomainName", lookupStrategy, "networkName",
"provisionUrl", "provcmd ", null, "echo checksum",
CommandProcessor.atCurrentDir(), 100, 100, 100, 100, null, null);

DomainWrapper w = host.findFirstCachedDomain();
assertThat(w, notNullValue());
}
}