Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | package org.onosproject.cluster; |
| 17 | |
| 18 | import static org.junit.Assert.assertEquals; |
| 19 | import static org.junit.Assert.assertFalse; |
| 20 | import static org.junit.Assert.assertTrue; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | |
| 25 | import com.google.common.collect.ImmutableSet; |
| 26 | import com.google.common.collect.Sets; |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 27 | import org.onosproject.core.Version; |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 28 | |
| 29 | /** |
| 30 | * Unit tests for ClusterMetadataDiff. |
| 31 | */ |
| 32 | public class ClusterMetadataDiffTest { |
| 33 | |
| 34 | @Test |
| 35 | public void testDiffNoChange() { |
| 36 | PartitionId pid1 = PartitionId.from(1); |
| 37 | NodeId nid1 = NodeId.nodeId("10.0.0.1"); |
| 38 | ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876); |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 39 | Partition p1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1)); |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 40 | ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1)); |
| 41 | ClusterMetadataDiff diff = new ClusterMetadataDiff(md1, md1); |
| 42 | assertTrue(diff.nodesAdded().isEmpty()); |
| 43 | assertTrue(diff.nodesRemoved().isEmpty()); |
| 44 | assertEquals(diff.partitionDiffs().size(), 1); |
| 45 | assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1)); |
| 46 | PartitionDiff pdiff = diff.partitionDiffs().get(pid1); |
| 47 | assertFalse(pdiff.hasChanged()); |
| 48 | } |
| 49 | |
| 50 | @Test |
| 51 | public void testDiffForScaleUp() { |
| 52 | PartitionId pid1 = PartitionId.from(1); |
| 53 | NodeId nid1 = NodeId.nodeId("10.0.0.1"); |
| 54 | NodeId nid2 = NodeId.nodeId("10.0.0.2"); |
| 55 | ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876); |
| 56 | ControllerNode n2 = new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876); |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 57 | Partition p1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1)); |
| 58 | Partition p12 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1, nid2)); |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 59 | ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1)); |
| 60 | ClusterMetadata md12 = new ClusterMetadata("foo", ImmutableSet.of(n1, n2), ImmutableSet.of(p12)); |
| 61 | ClusterMetadataDiff diff = new ClusterMetadataDiff(md1, md12); |
| 62 | assertEquals(diff.nodesAdded(), Sets.newHashSet(n2)); |
| 63 | assertTrue(diff.nodesRemoved().isEmpty()); |
| 64 | assertEquals(diff.partitionDiffs().size(), 1); |
| 65 | assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1)); |
| 66 | PartitionDiff pdiff = diff.partitionDiffs().get(pid1); |
| 67 | assertTrue(pdiff.hasChanged()); |
| 68 | assertFalse(pdiff.isAdded(nid1)); |
| 69 | assertTrue(pdiff.isAdded(nid2)); |
| 70 | assertFalse(pdiff.isRemoved(nid1)); |
| 71 | assertFalse(pdiff.isAdded(nid1)); |
| 72 | } |
| 73 | |
| 74 | @Test |
| 75 | public void testDiffForScaleDown() { |
| 76 | PartitionId pid1 = PartitionId.from(1); |
| 77 | NodeId nid1 = NodeId.nodeId("10.0.0.1"); |
| 78 | NodeId nid2 = NodeId.nodeId("10.0.0.2"); |
| 79 | ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876); |
| 80 | ControllerNode n2 = new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876); |
Jordan Halterman | 07f052b | 2017-10-08 14:22:41 -0700 | [diff] [blame] | 81 | Partition p1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1)); |
| 82 | Partition p12 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1, nid2)); |
Madan Jampani | 1807057 | 2016-02-29 13:54:45 -0800 | [diff] [blame] | 83 | ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1)); |
| 84 | ClusterMetadata md12 = new ClusterMetadata("foo", ImmutableSet.of(n1, n2), ImmutableSet.of(p12)); |
| 85 | ClusterMetadataDiff diff = new ClusterMetadataDiff(md12, md1); |
| 86 | assertEquals(diff.nodesRemoved(), Sets.newHashSet(nid2)); |
| 87 | assertTrue(diff.nodesAdded().isEmpty()); |
| 88 | assertEquals(diff.partitionDiffs().size(), 1); |
| 89 | assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1)); |
| 90 | PartitionDiff pdiff = diff.partitionDiffs().get(pid1); |
| 91 | assertTrue(pdiff.hasChanged()); |
| 92 | assertTrue(pdiff.isRemoved(nid2)); |
| 93 | assertFalse(pdiff.isAdded(nid2)); |
| 94 | assertFalse(pdiff.isRemoved(nid1)); |
| 95 | assertFalse(pdiff.isAdded(nid1)); |
| 96 | } |
| 97 | } |