Updated Branches: refs/heads/master 7295f34e5 -> 97c911ca3
http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs-google/blob/97c911ca/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiLiveTest.java ---------------------------------------------------------------------- diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiLiveTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiLiveTest.java index 8c550b7..ca18883 100644 --- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiLiveTest.java +++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/features/FirewallApiLiveTest.java @@ -17,7 +17,6 @@ package org.jclouds.googlecomputeengine.features; import static com.google.common.collect.Iterables.getOnlyElement; -import static org.jclouds.googlecomputeengine.domain.Firewall.Rule.IPProtocol; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertNotNull; @@ -28,6 +27,7 @@ import org.jclouds.googlecomputeengine.domain.Firewall; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineApiLiveTest; import org.jclouds.googlecomputeengine.options.FirewallOptions; import org.jclouds.googlecomputeengine.options.ListOptions; +import org.jclouds.net.domain.IpProtocol; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @@ -57,7 +57,7 @@ public class FirewallApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { FirewallOptions firewall = new FirewallOptions() .addAllowedRule( Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(22).build()) .addSourceRange("10.0.0.0/8") .addSourceTag("tag1") @@ -79,7 +79,7 @@ public class FirewallApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { .addTargetTag("tag2") .allowedRules(ImmutableSet.of( Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(23) .build())); @@ -96,11 +96,11 @@ public class FirewallApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { .network(getNetworkUrl(userProject.get(), FIREWALL_NETWORK_NAME)) .allowedRules(ImmutableSet.of( Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(22) .build(), Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(23) .build())) .addSourceRange("10.0.0.0/8") @@ -119,11 +119,11 @@ public class FirewallApiLiveTest extends BaseGoogleComputeEngineApiLiveTest { .network(getNetworkUrl(userProject.get(), FIREWALL_NETWORK_NAME)) .allowedRules(ImmutableSet.of( Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(22) .build(), Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(23) .build())) .addSourceRange("10.0.0.0/8") http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs-google/blob/97c911ca/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/functions/CreateNetworkIfNeededTest.java ---------------------------------------------------------------------- diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/functions/CreateNetworkIfNeededTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/functions/CreateNetworkIfNeededTest.java new file mode 100644 index 0000000..4bbff5e --- /dev/null +++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/functions/CreateNetworkIfNeededTest.java @@ -0,0 +1,133 @@ +/* + * 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.jclouds.googlecomputeengine.functions; + +import static com.google.common.base.Optional.fromNullable; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.easymock.EasyMock.verify; +import static org.testng.Assert.assertEquals; + +import java.net.URI; + +import org.jclouds.googlecomputeengine.GoogleComputeEngineApi; +import org.jclouds.googlecomputeengine.domain.Network; +import org.jclouds.googlecomputeengine.domain.Operation; +import org.jclouds.googlecomputeengine.domain.internal.NetworkAndAddressRange; +import org.jclouds.googlecomputeengine.features.GlobalOperationApi; +import org.jclouds.googlecomputeengine.features.NetworkApi; +import org.jclouds.googlecomputeengine.predicates.GlobalOperationDonePredicate; +import org.jclouds.http.HttpResponse; +import org.testng.annotations.Test; + +import com.google.common.base.Supplier; + +/** + * @author Andrew Bayer + */ +public class CreateNetworkIfNeededTest { + + @Test + public void testApply() { + final GoogleComputeEngineApi api = createMock(GoogleComputeEngineApi.class); + final NetworkApi nwApi = createMock(NetworkApi.class); + final GlobalOperationApi globalApi = createMock(GlobalOperationApi.class); + + Network network = Network.builder().IPv4Range("0.0.0.0/0") + .id("abcd").name("this-network") + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/networks/this-network")) + .build(); + + Operation createOp = createMock(Operation.class); + + final Supplier<String> userProject = new Supplier<String>() { + @Override + public String get() { + return "myproject"; + } + }; + + expect(api.getNetworkApiForProject(userProject.get())).andReturn(nwApi).atLeastOnce(); + expect(api.getGlobalOperationApiForProject(userProject.get())).andReturn(globalApi).atLeastOnce(); + + expect(nwApi.createInIPv4Range("this-network", "0.0.0.0/0")) + .andReturn(createOp); + expect(globalApi.get("create-op")).andReturn(createOp); + expect(nwApi.get("this-network")).andReturn(network); + + expect(createOp.getName()).andReturn("create-op"); + expect(createOp.getStatus()).andReturn(Operation.Status.DONE); + expect(createOp.getHttpError()).andReturn(fromNullable((HttpResponse)null)); + replay(api, nwApi, createOp, globalApi); + + NetworkAndAddressRange input = new NetworkAndAddressRange("this-network", "0.0.0.0/0", null); + + GlobalOperationDonePredicate pred = new GlobalOperationDonePredicate(api, userProject); + + CreateNetworkIfNeeded creator = new CreateNetworkIfNeeded(api, userProject, pred, 100l, 100l); + + assertEquals(creator.apply(input), network); + + verify(api, nwApi, globalApi, createOp); + } + + @Test + public void testApplyWithGateway() { + final GoogleComputeEngineApi api = createMock(GoogleComputeEngineApi.class); + final NetworkApi nwApi = createMock(NetworkApi.class); + final GlobalOperationApi globalApi = createMock(GlobalOperationApi.class); + + Network network = Network.builder().IPv4Range("0.0.0.0/0") + .id("abcd").name("this-network").gatewayIPv4("1.2.3.4") + .selfLink(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/networks/this-network")) + .build(); + + Operation createOp = createMock(Operation.class); + + final Supplier<String> userProject = new Supplier<String>() { + @Override + public String get() { + return "myproject"; + } + }; + + expect(api.getNetworkApiForProject(userProject.get())).andReturn(nwApi).atLeastOnce(); + expect(api.getGlobalOperationApiForProject(userProject.get())).andReturn(globalApi).atLeastOnce(); + + expect(nwApi.createInIPv4RangeWithGateway("this-network", "0.0.0.0/0", "1.2.3.4")) + .andReturn(createOp); + expect(globalApi.get("create-op")).andReturn(createOp); + expect(nwApi.get("this-network")).andReturn(network); + + expect(createOp.getName()).andReturn("create-op"); + expect(createOp.getStatus()).andReturn(Operation.Status.DONE); + expect(createOp.getHttpError()).andReturn(fromNullable((HttpResponse)null)); + replay(api, nwApi, createOp, globalApi); + + NetworkAndAddressRange input = new NetworkAndAddressRange("this-network", "0.0.0.0/0", "1.2.3.4"); + + GlobalOperationDonePredicate pred = new GlobalOperationDonePredicate(api, userProject); + + CreateNetworkIfNeeded creator = new CreateNetworkIfNeeded(api, userProject, pred, 100l, 100l); + + assertEquals(creator.apply(input), network); + + verify(api, nwApi, globalApi, createOp); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs-google/blob/97c911ca/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java ---------------------------------------------------------------------- diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java index a90f17b..1bbf753 100644 --- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java +++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallListTest.java @@ -26,6 +26,7 @@ import org.jclouds.googlecomputeengine.domain.Firewall; import org.jclouds.googlecomputeengine.domain.ListPage; import org.jclouds.googlecomputeengine.domain.Resource; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineParseTest; +import org.jclouds.net.domain.IpProtocol; import org.testng.annotations.Test; import com.google.common.collect.ImmutableSet; @@ -61,7 +62,7 @@ public class ParseFirewallListTest extends BaseGoogleComputeEngineParseTest<List ".com/compute/v1beta15/projects/google/global/networks/default")) .addSourceRange("0.0.0.0/0") .addAllowed(Firewall.Rule.builder() - .IPProtocol(Firewall.Rule.IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPort(22).build()) .build() )) http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs-google/blob/97c911ca/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java ---------------------------------------------------------------------- diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java index 187380f..49012fb 100644 --- a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java +++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/parse/ParseFirewallTest.java @@ -16,8 +16,6 @@ */ package org.jclouds.googlecomputeengine.parse; -import static org.jclouds.googlecomputeengine.domain.Firewall.Rule.IPProtocol; - import java.net.URI; import javax.ws.rs.Consumes; @@ -26,6 +24,7 @@ import javax.ws.rs.core.MediaType; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.googlecomputeengine.domain.Firewall; import org.jclouds.googlecomputeengine.internal.BaseGoogleComputeEngineParseTest; +import org.jclouds.net.domain.IpProtocol; import org.testng.annotations.Test; /** @@ -51,13 +50,13 @@ public class ParseFirewallTest extends BaseGoogleComputeEngineParseTest<Firewall .network(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/networks/jclouds-test")) .addSourceRange("10.0.0.0/8") .addAllowed(Firewall.Rule.builder() - .IPProtocol(IPProtocol.TCP) + .IpProtocol(IpProtocol.TCP) .addPortRange(1, 65535).build()) .addAllowed(Firewall.Rule.builder() - .IPProtocol(IPProtocol.UDP) + .IpProtocol(IpProtocol.UDP) .addPortRange(1, 65535).build()) .addAllowed(Firewall.Rule.builder() - .IPProtocol(IPProtocol.ICMP).build()) + .IpProtocol(IpProtocol.ICMP).build()) .build(); } http://git-wip-us.apache.org/repos/asf/incubator-jclouds-labs-google/blob/97c911ca/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/predicates/NetworkFirewallPredicatesTest.java ---------------------------------------------------------------------- diff --git a/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/predicates/NetworkFirewallPredicatesTest.java b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/predicates/NetworkFirewallPredicatesTest.java new file mode 100644 index 0000000..a7c43f2 --- /dev/null +++ b/google-compute-engine/src/test/java/org/jclouds/googlecomputeengine/predicates/NetworkFirewallPredicatesTest.java @@ -0,0 +1,162 @@ +/* + * 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.jclouds.googlecomputeengine.predicates; + +import static org.jclouds.googlecomputeengine.compute.functions.FirewallToIpPermissionTest.fwForTest; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.equalsIpPermission; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.hasPortRange; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.hasProtocol; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.hasSourceRange; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.hasSourceTag; +import static org.jclouds.googlecomputeengine.predicates.NetworkFirewallPredicates.providesIpPermission; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +import java.net.URI; +import java.util.Date; + +import org.jclouds.googlecomputeengine.domain.Firewall; +import org.jclouds.net.domain.IpPermission; +import org.jclouds.net.domain.IpProtocol; +import org.testng.annotations.Test; + +import com.google.common.collect.Range; + +@Test(groups = "unit") +public class NetworkFirewallPredicatesTest { + + public static Firewall getFwForTestSourceTags() { + Firewall.Builder builder = Firewall.builder(); + + builder.network(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/networks/jclouds-test")); + builder.selfLink(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/firewalls/jclouds-test")); + builder.addSourceTag("tag-1"); + builder.addAllowed(Firewall.Rule.builder().IpProtocol(IpProtocol.TCP) + .addPortRange(1, 10).build()); + builder.addAllowed(Firewall.Rule.builder().IpProtocol(IpProtocol.TCP) + .addPort(33).build()); + builder.addAllowed(Firewall.Rule.builder().IpProtocol(IpProtocol.ICMP).build()); + builder.id("abcd"); + builder.creationTimestamp(new Date()); + builder.name("jclouds-test"); + + return builder.build(); + } + + public static Firewall getFwForTestSourceTagsExact() { + Firewall.Builder builder = Firewall.builder(); + + builder.network(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/networks/jclouds-test")); + builder.selfLink(URI.create("https://www.googleapis.com/compute/v1beta15/projects/myproject/global/firewalls/jclouds-test")); + builder.addSourceTag("tag-1"); + builder.addAllowed(Firewall.Rule.builder().IpProtocol(IpProtocol.TCP) + .addPortRange(1, 10).build()); + builder.id("abcd"); + builder.creationTimestamp(new Date()); + builder.name("jclouds-test"); + + return builder.build(); + } + + @Test + public void testHasProtocol() { + assertTrue(hasProtocol(IpProtocol.TCP).apply(fwForTest()), + "Firewall " + fwForTest() + " should contain a TCP rule."); + } + + @Test + public void testHasProtocolFails() { + assertFalse(hasProtocol(IpProtocol.UDP).apply(fwForTest()), + "Firewall " + fwForTest() + " should NOT contain a UDP rule."); + } + + @Test + public void testHasPortRange() { + assertTrue(hasPortRange(Range.closed(2, 9)).apply(fwForTest()), + "Firewall " + fwForTest() + " should contain the port range 2-9."); + } + + @Test + public void testHasPortRangeFails() { + assertFalse(hasPortRange(Range.closed(11, 15)).apply(fwForTest()), + "Firewall " + fwForTest() + " should NOT contain the port range 11-15."); + } + + @Test + public void testHasSourceTag() { + assertTrue(hasSourceTag("tag-1").apply(getFwForTestSourceTags()), + "Firewall " + getFwForTestSourceTags() + " should contain the source tag 'tag-1'."); + } + + @Test + public void testHasSourceTagFails() { + assertFalse(hasSourceTag("tag-1").apply(fwForTest()), + "Firewall " + fwForTest() + " should NOT contain the source tag 'tag-1'."); + } + + @Test + public void testHasSourceRange() { + assertTrue(hasSourceRange("0.0.0.0/0").apply(fwForTest()), + "Firewall " + fwForTest() + " should contain the source range '0.0.0.0/0'."); + } + + @Test + public void testHasSourceRangeFails() { + assertFalse(hasSourceRange("0.0.0.0/0").apply(getFwForTestSourceTags()), + "Firewall " + getFwForTestSourceTags() + " should NOT contain the source range '0.0.0.0/0'."); + } + + @Test + public void testEqualsIpPermission() { + IpPermission perm = IpPermission.builder().groupId("tag-1") + .fromPort(1).toPort(10).ipProtocol(IpProtocol.TCP).build(); + + assertTrue(equalsIpPermission(perm).apply(getFwForTestSourceTagsExact()), + "Firewall " + getFwForTestSourceTagsExact() + " should match IpPermission " + perm + " but does not."); + } + + @Test + public void testEqualsIpPermissionFails() { + IpPermission perm = IpPermission.builder().groupId("tag-1") + .fromPort(1).toPort(10).ipProtocol(IpProtocol.TCP).build(); + + assertFalse(equalsIpPermission(perm).apply(getFwForTestSourceTags()), + "Firewall " + getFwForTestSourceTags() + " should not match IpPermission " + perm + " but does."); + } + + @Test + public void testProvidesIpPermission() { + IpPermission perm = IpPermission.builder().groupId("tag-1") + .fromPort(1).toPort(10).ipProtocol(IpProtocol.TCP).build(); + + assertTrue(providesIpPermission(perm).apply(getFwForTestSourceTagsExact()), + "Firewall " + getFwForTestSourceTagsExact() + " should provide IpPermission " + perm + " but does not."); + + assertTrue(providesIpPermission(perm).apply(getFwForTestSourceTags()), + "Firewall " + getFwForTestSourceTags() + " should inexactly provide IpPermission " + perm + " but does not."); + } + + @Test + public void testProvidesIpPermissionFails() { + IpPermission perm = IpPermission.builder().groupId("tag-1") + .fromPort(1).toPort(10).ipProtocol(IpProtocol.TCP).build(); + + assertFalse(providesIpPermission(perm).apply(fwForTest()), + "Firewall " + fwForTest() + " should not provide IpPermission " + perm + " but does."); + } +} +
