blob: 63e9af9d31e4533117e7ff87660184b00a595b06 [file] [log] [blame]
Madan Jampani18070572016-02-29 13:54:45 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Madan Jampani18070572016-02-29 13:54:45 -08003 *
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 */
16package org.onosproject.cluster;
17
18import static org.junit.Assert.assertEquals;
19import static org.junit.Assert.assertFalse;
20import static org.junit.Assert.assertTrue;
21
22import org.junit.Test;
23import org.onlab.packet.IpAddress;
24
25import com.google.common.collect.ImmutableSet;
26import com.google.common.collect.Sets;
27
28/**
29 * Unit tests for ClusterMetadataDiff.
30 */
31public class ClusterMetadataDiffTest {
32
33 @Test
34 public void testDiffNoChange() {
35 PartitionId pid1 = PartitionId.from(1);
36 NodeId nid1 = NodeId.nodeId("10.0.0.1");
37 ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
38 Partition p1 = new DefaultPartition(pid1, ImmutableSet.of(nid1));
39 ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
40 ClusterMetadataDiff diff = new ClusterMetadataDiff(md1, md1);
41 assertTrue(diff.nodesAdded().isEmpty());
42 assertTrue(diff.nodesRemoved().isEmpty());
43 assertEquals(diff.partitionDiffs().size(), 1);
44 assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1));
45 PartitionDiff pdiff = diff.partitionDiffs().get(pid1);
46 assertFalse(pdiff.hasChanged());
47 }
48
49 @Test
50 public void testDiffForScaleUp() {
51 PartitionId pid1 = PartitionId.from(1);
52 NodeId nid1 = NodeId.nodeId("10.0.0.1");
53 NodeId nid2 = NodeId.nodeId("10.0.0.2");
54 ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
55 ControllerNode n2 = new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
56 Partition p1 = new DefaultPartition(pid1, ImmutableSet.of(nid1));
57 Partition p12 = new DefaultPartition(pid1, ImmutableSet.of(nid1, nid2));
58 ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
59 ClusterMetadata md12 = new ClusterMetadata("foo", ImmutableSet.of(n1, n2), ImmutableSet.of(p12));
60 ClusterMetadataDiff diff = new ClusterMetadataDiff(md1, md12);
61 assertEquals(diff.nodesAdded(), Sets.newHashSet(n2));
62 assertTrue(diff.nodesRemoved().isEmpty());
63 assertEquals(diff.partitionDiffs().size(), 1);
64 assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1));
65 PartitionDiff pdiff = diff.partitionDiffs().get(pid1);
66 assertTrue(pdiff.hasChanged());
67 assertFalse(pdiff.isAdded(nid1));
68 assertTrue(pdiff.isAdded(nid2));
69 assertFalse(pdiff.isRemoved(nid1));
70 assertFalse(pdiff.isAdded(nid1));
71 }
72
73 @Test
74 public void testDiffForScaleDown() {
75 PartitionId pid1 = PartitionId.from(1);
76 NodeId nid1 = NodeId.nodeId("10.0.0.1");
77 NodeId nid2 = NodeId.nodeId("10.0.0.2");
78 ControllerNode n1 = new DefaultControllerNode(nid1, IpAddress.valueOf("10.0.0.1"), 9876);
79 ControllerNode n2 = new DefaultControllerNode(nid2, IpAddress.valueOf("10.0.0.2"), 9876);
80 Partition p1 = new DefaultPartition(pid1, ImmutableSet.of(nid1));
81 Partition p12 = new DefaultPartition(pid1, ImmutableSet.of(nid1, nid2));
82 ClusterMetadata md1 = new ClusterMetadata("foo", ImmutableSet.of(n1), ImmutableSet.of(p1));
83 ClusterMetadata md12 = new ClusterMetadata("foo", ImmutableSet.of(n1, n2), ImmutableSet.of(p12));
84 ClusterMetadataDiff diff = new ClusterMetadataDiff(md12, md1);
85 assertEquals(diff.nodesRemoved(), Sets.newHashSet(nid2));
86 assertTrue(diff.nodesAdded().isEmpty());
87 assertEquals(diff.partitionDiffs().size(), 1);
88 assertEquals(diff.partitionDiffs().keySet(), Sets.newHashSet(pid1));
89 PartitionDiff pdiff = diff.partitionDiffs().get(pid1);
90 assertTrue(pdiff.hasChanged());
91 assertTrue(pdiff.isRemoved(nid2));
92 assertFalse(pdiff.isAdded(nid2));
93 assertFalse(pdiff.isRemoved(nid1));
94 assertFalse(pdiff.isAdded(nid1));
95 }
96}