blob: f97bd6a6187b55aee34c30e223b4975b51b7b1b8 [file] [log] [blame]
Madan Jampani18070572016-02-29 13:54:45 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
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;
Jordan Halterman07f052b2017-10-08 14:22:41 -070027import org.onosproject.core.Version;
Madan Jampani18070572016-02-29 13:54:45 -080028
29/**
30 * Unit tests for ClusterMetadataDiff.
31 */
32public 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 Halterman07f052b2017-10-08 14:22:41 -070039 Partition p1 = new DefaultPartition(pid1, Version.version("1.0.0"), ImmutableSet.of(nid1));
Madan Jampani18070572016-02-29 13:54:45 -080040 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 Halterman07f052b2017-10-08 14:22:41 -070057 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 Jampani18070572016-02-29 13:54:45 -080059 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 Halterman07f052b2017-10-08 14:22:41 -070081 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 Jampani18070572016-02-29 13:54:45 -080083 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}