Skip to content

Commit

Permalink
Added sleep and renamed change completion check.
Browse files Browse the repository at this point in the history
Moved integration test to a separate package and adjusted accordingly.
Added missing fails.
  • Loading branch information
mderka committed Mar 1, 2016
1 parent 75269ff commit f069c58
Showing 1 changed file with 64 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package com.google.gcloud.dns;
package com.google.gcloud.dns.it;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
Expand All @@ -25,6 +25,14 @@

import com.google.common.collect.ImmutableList;
import com.google.gcloud.Page;
import com.google.gcloud.dns.ChangeRequest;
import com.google.gcloud.dns.Dns;
import com.google.gcloud.dns.DnsException;
import com.google.gcloud.dns.DnsOptions;
import com.google.gcloud.dns.DnsRecord;
import com.google.gcloud.dns.ProjectInfo;
import com.google.gcloud.dns.Zone;
import com.google.gcloud.dns.ZoneInfo;

import org.junit.AfterClass;
import org.junit.BeforeClass;
Expand Down Expand Up @@ -62,24 +70,20 @@ public class ITDnsTest {
.description(ZONE_DESCRIPTION1)
.dnsName(ZONE_DNS_NAME1)
.build();
public static final ZoneInfo ZONE_NAME_ERROR =
ZoneInfo.builder(ZONE_NAME_TOO_LONG)
.description(ZONE_DESCRIPTION1)
.dnsName(ZONE_DNS_NAME1)
.build();
public static final ZoneInfo ZONE_MISSING_DESCRIPTION =
ZoneInfo.builder(ZONE_NAME1)
.dnsName(ZONE_DNS_NAME1)
.build();
public static final ZoneInfo ZONE_MISSING_DNS_NAME =
ZoneInfo.builder(ZONE_NAME1)
.description(ZONE_DESCRIPTION1)
.build();
public static final ZoneInfo ZONE_DNS_NO_PERIOD =
ZoneInfo.builder(ZONE_NAME1)
.description(ZONE_DESCRIPTION1)
.dnsName(ZONE_DNS_NAME_NO_PERIOD)
.build();
public static final ZoneInfo ZONE_NAME_ERROR = ZoneInfo.builder(ZONE_NAME_TOO_LONG)
.description(ZONE_DESCRIPTION1)
.dnsName(ZONE_DNS_NAME1)
.build();
public static final ZoneInfo ZONE_MISSING_DESCRIPTION = ZoneInfo.builder(ZONE_NAME1)
.dnsName(ZONE_DNS_NAME1)
.build();
public static final ZoneInfo ZONE_MISSING_DNS_NAME = ZoneInfo.builder(ZONE_NAME1)
.description(ZONE_DESCRIPTION1)
.build();
public static final ZoneInfo ZONE_DNS_NO_PERIOD = ZoneInfo.builder(ZONE_NAME1)
.description(ZONE_DESCRIPTION1)
.dnsName(ZONE_DNS_NAME_NO_PERIOD)
.build();
public static final DnsRecord A_RECORD_ZONE1 =
DnsRecord.builder("www." + ZONE1.dnsName(), DnsRecord.Type.A)
.records(ImmutableList.of("123.123.55.1"))
Expand Down Expand Up @@ -116,7 +120,7 @@ public static void clear() {
if (!toDelete.isEmpty()) {
ChangeRequest deletion =
zone.applyChangeRequest(ChangeRequest.builder().deletions(toDelete).build());
checkChangeComplete(zone.name(), deletion.id());
waitUntilComplete(zone.name(), deletion.id());
}
zone.delete();
}
Expand Down Expand Up @@ -145,17 +149,23 @@ public static void after() {
}

private static void assertEqChangesIgnoreStatus(ChangeRequest expected, ChangeRequest actual) {
ChangeRequest unifiedEx = ChangeRequest.fromPb(expected.toPb().setStatus("pending"));
ChangeRequest unifiedAct = ChangeRequest.fromPb(actual.toPb().setStatus("pending"));
assertEquals(unifiedEx, unifiedAct);
assertEquals(expected.additions(), actual.additions());
assertEquals(expected.deletions(), actual.deletions());
assertEquals(expected.id(), actual.id());
assertEquals(expected.startTimeMillis(), actual.startTimeMillis());
}

private static void checkChangeComplete(String zoneName, String changeId) {
private static void waitUntilComplete(String zoneName, String changeId) {
while (true) {
ChangeRequest changeRequest = DNS.getChangeRequest(zoneName, changeId,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS));
if (ChangeRequest.Status.DONE.equals(changeRequest.status())) {
break;
return;
}
try {
Thread.sleep(10);
} catch (InterruptedException e) {
fail("Thread was interrupted while waiting for change proceesing.");
}
}
}
Expand Down Expand Up @@ -404,13 +414,15 @@ public void testListZones() {
// error in options
try {
DNS.listZones(Dns.ZoneListOption.pageSize(0));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
// todo(mderka) test not-retryable
}
try {
DNS.listZones(Dns.ZoneListOption.pageSize(-1));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
Expand All @@ -422,6 +434,7 @@ public void testListZones() {
// dns name problems
try {
DNS.listZones(Dns.ZoneListOption.dnsName("aaaaa"));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
Expand Down Expand Up @@ -529,7 +542,6 @@ public void testDeleteZone() {
}
}


@Test
public void testCreateChange() {
try {
Expand All @@ -542,9 +554,9 @@ public void testCreateChange() {
assertTrue(ImmutableList.of(ChangeRequest.Status.PENDING, ChangeRequest.Status.DONE)
.contains(created.status()));
assertEqChangesIgnoreStatus(created, DNS.getChangeRequest(ZONE1.name(), "1"));
checkChangeComplete(ZONE1.name(), "1");
waitUntilComplete(ZONE1.name(), "1");
DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), "2");
waitUntilComplete(ZONE1.name(), "2");
// with options
created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ID));
Expand All @@ -553,29 +565,29 @@ public void testCreateChange() {
assertTrue(created.deletions().isEmpty());
assertEquals("3", created.id());
assertNull(created.status());
checkChangeComplete(ZONE1.name(), "3");
waitUntilComplete(ZONE1.name(), "3");
DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), "4");
waitUntilComplete(ZONE1.name(), "4");
created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.STATUS));
assertTrue(created.additions().isEmpty());
assertNull(created.startTimeMillis());
assertTrue(created.deletions().isEmpty());
assertEquals("5", created.id());
assertNotNull(created.status());
checkChangeComplete(ZONE1.name(), "5");
waitUntilComplete(ZONE1.name(), "5");
DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), "6");
waitUntilComplete(ZONE1.name(), "6");
created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.START_TIME));
assertTrue(created.additions().isEmpty());
assertNotNull(created.startTimeMillis());
assertTrue(created.deletions().isEmpty());
assertEquals("7", created.id());
assertNull(created.status());
checkChangeComplete(ZONE1.name(), "7");
waitUntilComplete(ZONE1.name(), "7");
DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), "8");
waitUntilComplete(ZONE1.name(), "8");
created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.ADDITIONS));
assertEquals(CHANGE_ADD_ZONE1.additions(), created.additions());
Expand All @@ -584,16 +596,16 @@ public void testCreateChange() {
assertEquals("9", created.id());
assertNull(created.status());
// finishes with delete otherwise we cannot delete the zone
checkChangeComplete(ZONE1.name(), "9");
waitUntilComplete(ZONE1.name(), "9");
created = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1,
Dns.ChangeRequestOption.fields(Dns.ChangeRequestField.DELETIONS));
checkChangeComplete(ZONE1.name(), "10");
waitUntilComplete(ZONE1.name(), "10");
assertEquals(CHANGE_DELETE_ZONE1.deletions(), created.deletions());
assertNull(created.startTimeMillis());
assertTrue(created.additions().isEmpty());
assertEquals("10", created.id());
assertNull(created.status());
checkChangeComplete(ZONE1.name(), "10");
waitUntilComplete(ZONE1.name(), "10");
} finally {
clear();
}
Expand All @@ -618,25 +630,27 @@ public void testListChanges() {
assertEquals(1, changes.size()); // default change creating SOA and NS
// zone has changes
ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1);
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1);
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_DELETE_ZONE1);
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
changes = ImmutableList.copyOf(DNS.listChangeRequests(ZONE1.name()).iterateAll());
assertEquals(5, changes.size());
// error in options
try {
DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(0));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
// todo(mderka) test retry functionality
}
try {
DNS.listChangeRequests(ZONE1.name(), Dns.ChangeRequestListOption.pageSize(-1));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
Expand Down Expand Up @@ -754,27 +768,16 @@ public void testGetProject() {
// fetches all fields
ProjectInfo project = DNS.getProject();
assertNotNull(project.quota());
assertNotNull(project.number());
assertNotNull(project.id());
assertEquals(PROJECT_ID, project.id());
// options
project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.QUOTA));
assertNotNull(project.quota());
assertNull(project.number());
assertNotNull(project.id()); // id is always returned
project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_ID));
assertNull(project.quota());
assertNull(project.number());
assertNotNull(project.id());
project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER));
assertNull(project.quota());
assertNotNull(project.number());
assertNotNull(project.id());
project = DNS.getProject(Dns.ProjectOption.fields(Dns.ProjectField.PROJECT_NUMBER,
Dns.ProjectField.QUOTA, Dns.ProjectField.PROJECT_ID));
assertNotNull(project.quota());
assertNotNull(project.number());
assertNotNull(project.id());
}

@Test
Expand Down Expand Up @@ -846,7 +849,7 @@ public void testListDnsRecords() {
assertEquals(1, ImmutableList.copyOf(dnsRecordPage.values().iterator()).size());
// test name filter
ChangeRequest change = DNS.applyChangeRequest(ZONE1.name(), CHANGE_ADD_ZONE1);
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(),
Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name())).iterateAll();
counter = 0;
Expand All @@ -858,7 +861,7 @@ public void testListDnsRecords() {
}
assertEquals(2, counter);
// test type filter
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
dnsRecordIterator = DNS.listDnsRecords(ZONE1.name(),
Dns.DnsRecordListOption.dnsName(A_RECORD_ZONE1.name()),
Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type()))
Expand All @@ -874,30 +877,30 @@ public void testListDnsRecords() {
// check wrong arguments
try {
// name is not set
DNS.listDnsRecords(ZONE1.name(),
Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type()));
DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.type(A_RECORD_ZONE1.type()));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
// todo(mderka) test retry functionality when available
}
try {
DNS.listDnsRecords(ZONE1.name(),
Dns.DnsRecordListOption.pageSize(0));
DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(0));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
// todo(mderka) test retry functionality when available
}
try {
DNS.listDnsRecords(ZONE1.name(),
Dns.DnsRecordListOption.pageSize(-1));
DNS.listDnsRecords(ZONE1.name(), Dns.DnsRecordListOption.pageSize(-1));
fail();
} catch (DnsException ex) {
// expected
assertEquals(400, ex.code());
// todo(mderka) test retry functionality when available
}
checkChangeComplete(ZONE1.name(), change.id());
waitUntilComplete(ZONE1.name(), change.id());
} finally {
clear();
}
Expand Down

0 comments on commit f069c58

Please sign in to comment.