Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Brian O'Connor | 7cbbbb7 | 2016-04-09 02:13:23 -0700 | [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 | */ |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 16 | package org.onosproject.store.cluster.impl; |
| 17 | |
| 18 | import org.junit.After; |
| 19 | import org.junit.Before; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.IpAddress; |
| 22 | import org.onosproject.cfg.ComponentConfigAdapter; |
| 23 | import org.onosproject.cluster.ClusterEvent; |
| 24 | import org.onosproject.cluster.ClusterMetadataServiceAdapter; |
| 25 | import org.onosproject.cluster.ClusterStore; |
| 26 | import org.onosproject.cluster.ClusterStoreDelegate; |
| 27 | import org.onosproject.cluster.ControllerNode; |
| 28 | import org.onosproject.cluster.DefaultControllerNode; |
| 29 | import org.onosproject.cluster.NodeId; |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 30 | import org.onosproject.core.Version; |
| 31 | import org.onosproject.core.VersionServiceAdapter; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 32 | import org.onosproject.store.cluster.messaging.impl.NettyMessagingManager; |
| 33 | |
| 34 | import java.util.Set; |
| 35 | |
| 36 | import static org.hamcrest.MatcherAssert.assertThat; |
| 37 | import static org.hamcrest.Matchers.*; |
| 38 | import static org.junit.Assert.assertFalse; |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 39 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 40 | /** |
| 41 | * Unit test for DistributedClusterStore. |
| 42 | */ |
| 43 | public class DistributedClusterStoreTest { |
| 44 | DistributedClusterStore distributedClusterStore; |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 45 | ClusterStore clusterStore; |
| 46 | NodeId nodeId; |
| 47 | ControllerNode local; |
| 48 | private static final NodeId NID1 = new NodeId("foo"); |
| 49 | private static final NodeId NID2 = new NodeId("bar"); |
| 50 | private static final NodeId NID3 = new NodeId("buz"); |
| 51 | |
| 52 | private static final IpAddress IP1 = IpAddress.valueOf("127.0.0.1"); |
| 53 | private static final IpAddress IP2 = IpAddress.valueOf("127.0.0.2"); |
| 54 | private static final IpAddress IP3 = IpAddress.valueOf("127.0.0.3"); |
| 55 | |
| 56 | private static final int PORT1 = 1; |
| 57 | private static final int PORT2 = 2; |
| 58 | private static Set<ControllerNode> nodes; |
| 59 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 60 | private TestDelegate delegate = new TestDelegate(); |
| 61 | private class TestDelegate implements ClusterStoreDelegate { |
| 62 | private ClusterEvent event; |
| 63 | @Override |
| 64 | public void notify(ClusterEvent event) { |
| 65 | this.event = event; |
| 66 | } |
| 67 | } |
| 68 | |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 69 | @Before |
| 70 | public void setUp() throws Exception { |
| 71 | distributedClusterStore = new DistributedClusterStore(); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 72 | distributedClusterStore.clusterMetadataService = new ClusterMetadataServiceAdapter() { |
| 73 | @Override |
| 74 | public ControllerNode getLocalNode() { |
| 75 | return new DefaultControllerNode(NID1, IP1); |
| 76 | } |
| 77 | }; |
| 78 | distributedClusterStore.messagingService = new NettyMessagingManager(); |
| 79 | distributedClusterStore.cfgService = new ComponentConfigAdapter(); |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 80 | distributedClusterStore.versionService = new VersionServiceAdapter() { |
| 81 | @Override |
| 82 | public Version version() { |
| 83 | return Version.version("1.1.1"); |
| 84 | } |
| 85 | }; |
Thomas Vachuska | 1ca7e9f | 2016-08-05 10:21:41 -0700 | [diff] [blame] | 86 | distributedClusterStore.activate(); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 87 | clusterStore = distributedClusterStore; |
sangyun-han | f98df54 | 2016-03-24 20:28:03 +0900 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @After |
| 91 | public void tearDown() throws Exception { |
| 92 | distributedClusterStore.deactivate(); |
| 93 | } |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 94 | |
| 95 | @Test |
| 96 | public void testEmpty() { |
| 97 | nodeId = new NodeId("newNode"); |
| 98 | assertThat(clusterStore.getNode((nodeId)), is(nullValue())); |
| 99 | assertFalse(clusterStore.hasDelegate()); |
| 100 | assertThat(clusterStore.getState(nodeId), is(ControllerNode.State.INACTIVE)); |
Jordan Halterman | f70bf46 | 2017-07-29 13:12:00 -0700 | [diff] [blame] | 101 | assertThat(clusterStore.getVersion(nodeId), is(nullValue())); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | @Test |
| 105 | public void addNodes() { |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 106 | clusterStore.setDelegate(delegate); |
Jordan Halterman | c61232b | 2018-07-29 03:06:48 -0700 | [diff] [blame^] | 107 | nodes = clusterStore.getNodes(); |
| 108 | assertThat(nodes.size(), is(1)); |
| 109 | |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 110 | assertThat(clusterStore.hasDelegate(), is(true)); |
| 111 | clusterStore.addNode(NID1, IP1, PORT1); |
| 112 | clusterStore.addNode(NID2, IP2, PORT2); |
| 113 | clusterStore.removeNode(NID1); |
| 114 | |
| 115 | assertThat(clusterStore.getNode(NID1), is(nullValue())); |
| 116 | clusterStore.addNode(NID3, IP3, PORT2); |
| 117 | |
| 118 | clusterStore.markFullyStarted(true); |
| 119 | assertThat(clusterStore.getState(clusterStore.getLocalNode().id()), |
| 120 | is(ControllerNode.State.READY)); |
| 121 | clusterStore.markFullyStarted(false); |
| 122 | assertThat(clusterStore.getState(clusterStore.getLocalNode().id()), |
| 123 | is(ControllerNode.State.ACTIVE)); |
| 124 | nodes = clusterStore.getNodes(); |
Jordan Halterman | c61232b | 2018-07-29 03:06:48 -0700 | [diff] [blame^] | 125 | assertThat(nodes.size(), is(3)); |
Sbhat35 | 3548bb2 | 2017-07-13 10:27:10 -0700 | [diff] [blame] | 126 | clusterStore.markFullyStarted(true); |
| 127 | |
| 128 | clusterStore.unsetDelegate(delegate); |
| 129 | assertThat(clusterStore.hasDelegate(), is(false)); |
| 130 | } |
Sho SHIMIZU | e0b7d16 | 2016-08-12 15:46:48 -0700 | [diff] [blame] | 131 | } |