Madan Jampani

Initial stc tests for distributed primitives

Change-Id: I82a2911df9a4852cb851732e220d498f9332e005
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed 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.onosproject.distributedprimitives.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.ConsistentMap;
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.StorageService;
import org.onosproject.store.service.Versioned;
/**
* CLI command to manipulate a distributed value.
*/
@Command(scope = "onos", name = "map-test",
description = "Manipulate a consistent map")
public class ConsistentMapTestCommand extends AbstractShellCommand {
@Argument(index = 0, name = "name",
description = "map name",
required = true, multiValued = false)
String name = null;
@Argument(index = 1, name = "operation",
description = "operation name",
required = true, multiValued = false)
String operation = null;
@Argument(index = 2, name = "key",
description = "first arg",
required = false, multiValued = false)
String arg1 = null;
@Argument(index = 3, name = "value1",
description = "second arg",
required = false, multiValued = false)
String arg2 = null;
@Argument(index = 4, name = "value2",
description = "third arg",
required = false, multiValued = false)
String arg3 = null;
ConsistentMap<String, String> map;
@Override
protected void execute() {
StorageService storageService = get(StorageService.class);
map = storageService.<String, String>consistentMapBuilder()
.withName(name)
.withSerializer(Serializer.using(KryoNamespaces.BASIC))
.build();
if (operation.equals("get")) {
print(map.get(arg1));
} else if (operation.equals("put")) {
print(map.put(arg1, arg2));
} else if (operation.equals("size")) {
print("%d", map.size());
} else if (operation.equals("isEmpty")) {
print("%b", map.isEmpty());
} else if (operation.equals("putIfAbsent")) {
print(map.putIfAbsent(arg1, arg2));
} else if (operation.equals("putAndGet")) {
print(map.putAndGet(arg1, arg2));
} else if (operation.equals("clear")) {
map.clear();
} else if (operation.equals("remove")) {
if (arg2 == null) {
print(map.remove(arg1));
} else {
print("%b", map.remove(arg1, arg2));
}
} else if (operation.equals("containsKey")) {
print("%b", map.containsKey(arg1));
} else if (operation.equals("containsValue")) {
print("%b", map.containsValue(arg1));
} else if (operation.equals("replace")) {
if (arg3 == null) {
print(map.replace(arg1, arg2));
} else {
print("%b", map.replace(arg1, arg2, arg3));
}
}
}
void print(Versioned<String> value) {
if (value == null) {
print("null");
} else {
print("%s", value.value());
}
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed 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.onosproject.distributedprimitives.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.store.service.AtomicCounter;
import org.onosproject.store.service.StorageService;
/**
* CLI command to increment a distributed counter.
*/
@Command(scope = "onos", name = "counter-test",
description = "Manipulate a distributed counter")
public class CounterTestCommand extends AbstractShellCommand {
@Argument(index = 0, name = "counter",
description = "Counter name",
required = true, multiValued = false)
String counter = null;
@Argument(index = 1, name = "operation",
description = "operation name",
required = true, multiValued = false)
String operation = null;
@Argument(index = 2, name = "value1",
description = "first arg",
required = false, multiValued = false)
Long value1 = null;
@Argument(index = 3, name = "value2",
description = "second arg",
required = false, multiValued = false)
Long value2 = null;
AtomicCounter atomicCounter;
@Override
protected void execute() {
StorageService storageService = get(StorageService.class);
atomicCounter = storageService.getAsyncAtomicCounter(counter).asAtomicCounter();
if (operation.equals("get")) {
print("%d", atomicCounter.get());
} else if (operation.equals("set")) {
atomicCounter.set(value1);
} else if (operation.equals("incrementAndGet")) {
print("%d", atomicCounter.incrementAndGet());
} else if (operation.equals("getAndIncrement")) {
print("%d", atomicCounter.getAndIncrement());
} else if (operation.equals("getAndAdd")) {
print("%d", atomicCounter.getAndAdd(value1));
} else if (operation.equals("addAndGet")) {
print("%d", atomicCounter.addAndGet(value1));
} else if (operation.equals("compareAndSet")) {
print("%b", atomicCounter.compareAndSet(value1, value2));
} else if (operation.equals("destroy")) {
atomicCounter.destroy();
}
}
}
package org.onosproject.distributedprimitives.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.cluster.ClusterService;
import org.onosproject.cluster.Leadership;
import org.onosproject.cluster.NodeId;
import org.onosproject.store.service.LeaderElector;
import org.onosproject.store.service.StorageService;
import com.google.common.base.Joiner;
@Command(scope = "onos", name = "leader-test",
description = "LeaderElector test cli fixture")
public class LeaderElectorTestCommand extends AbstractShellCommand {
@Argument(index = 0, name = "name",
description = "leader elector name",
required = true, multiValued = false)
String name = null;
@Argument(index = 1, name = "operation",
description = "operation",
required = true, multiValued = false)
String operation = null;
@Argument(index = 2, name = "topic",
description = "topic name",
required = false, multiValued = false)
String topic = null;
LeaderElector leaderElector;
@Override
protected void execute() {
StorageService storageService = get(StorageService.class);
ClusterService clusterService = get(ClusterService.class);
NodeId localNodeId = clusterService.getLocalNode().id();
leaderElector = storageService.leaderElectorBuilder()
.withName(name)
.build()
.asLeaderElector();
if (operation.equals("run")) {
print(leaderElector.run(topic, localNodeId));
} else if (operation.equals("withdraw")) {
leaderElector.withdraw(topic);
} else if (operation.equals("show")) {
print(leaderElector.getLeadership(topic));
}
}
private void print(Leadership leadership) {
if (leadership.leader() != null) {
print("leader=%s#term=%d#candidates=%s",
leadership.leaderNodeId(),
leadership.leader().term(),
leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
} else {
print("leader=none#candidates=%s",
leadership.candidates().isEmpty() ? "none" : Joiner.on(",").join(leadership.candidates()));
}
}
}
/*
* Copyright 2016-present Open Networking Laboratory
*
* Licensed 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.onosproject.distributedprimitives.cli;
import org.apache.karaf.shell.commands.Argument;
import org.apache.karaf.shell.commands.Command;
import org.onosproject.cli.AbstractShellCommand;
import org.onosproject.store.serializers.KryoNamespaces;
import org.onosproject.store.service.AtomicValue;
import org.onosproject.store.service.Serializer;
import org.onosproject.store.service.StorageService;
/**
* CLI command to manipulate a distributed value.
*/
@Command(scope = "onos", name = "value-test",
description = "Manipulate a distributed value")
public class ValueTestCommand extends AbstractShellCommand {
@Argument(index = 0, name = "value",
description = "Value name",
required = true, multiValued = false)
String value = null;
@Argument(index = 1, name = "operation",
description = "operation name",
required = true, multiValued = false)
String operation = null;
@Argument(index = 2, name = "value1",
description = "first arg",
required = false, multiValued = false)
String value1 = null;
@Argument(index = 3, name = "value2",
description = "second arg",
required = false, multiValued = false)
String value2 = null;
AtomicValue<String> atomicValue;
@Override
protected void execute() {
StorageService storageService = get(StorageService.class);
atomicValue = storageService.<String>atomicValueBuilder()
.withName(value)
.withSerializer(Serializer.using(KryoNamespaces.BASIC))
.build()
.asAtomicValue();
if (operation.equals("get")) {
print("%s", atomicValue.get());
} else if (operation.equals("set")) {
atomicValue.set(value1);
} else if (operation.equals("compareAndSet")) {
print("%b", atomicValue.compareAndSet(value1, value2));
} else if (operation.equals("destroy")) {
atomicValue.destroy();
}
}
}
......@@ -34,6 +34,18 @@
<command>
<action class="org.onosproject.distributedprimitives.cli.TransactionalMapTestPutCommand"/>
</command>
<command>
<action class="org.onosproject.distributedprimitives.cli.CounterTestCommand"/>
</command>
<command>
<action class="org.onosproject.distributedprimitives.cli.ConsistentMapTestCommand"/>
</command>
<command>
<action class="org.onosproject.distributedprimitives.cli.ValueTestCommand"/>
</command>
<command>
<action class="org.onosproject.distributedprimitives.cli.LeaderElectorTestCommand"/>
</command>
</command-bundle>
</blueprint>
......
<!--
~ Copyright 2016-present Open Networking Laboratory
~
~ Licensed 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.
-->
<scenario name="distributed counter test"
description="ONOS AtomicCounter distributed primitive Test">
<group name="Distributed-Primitive-Counter">
<!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
<dependency name="Setup" requires="Prerequisites"/>-->
<step name="Activate-Distributed-Primitives-App"
exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
<step name="Test-Counter-Initial-Value" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 0"/>
<step name="Test-Counter-Set" requires="^"
exec="onos ${OCI} counter-test test-counter set 1"/>
<step name="Test-Counter-Get" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 1"/>
<step name="Test-Counter-IncrementAndGet" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter incrementAndGet --expect 2"/>
<step name="Test-Counter-GetAndIncrement" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter getAndIncrement --expect 2"/>
<step name="Test-Counter-Incremented" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 3"/>
<step name="Test-Counter-AddAndGet" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter addAndGet 10 --expect 13"/>
<step name="Test-Counter-GetAndAdd" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter getAndAdd 10 --expect 13"/>
<step name="Test-Counter-Updated-After-GetAndAdd" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 23"/>
<step name="Test-Counter-CompareAndSet-False" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter compareAndSet 1 2 --expect false"/>
<step name="Test-Counter-Not-Updated-After-CAS" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 23"/>
<step name="Test-Counter-CompareAndSet-True" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter compareAndSet 23 25 --expect true"/>
<step name="Test-Counter-Updated-After-CAS" requires="^"
exec="onos-execute-expect ${OCI} counter-test test-counter get --expect 25"/>
<!--Check with check logs-->
<step name="Check-Log-Exceptions" requires="^"
exec="onos-check-logs ${OCI}"/>
<step name="Teardown-Distributed-Primitives-Test-App" requires="^"
exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
</group>
</scenario>
<!--
~ Copyright 2016-present Open Networking Laboratory
~
~ Licensed 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.
-->
<scenario name="distributed leader elector test"
description="ONOS LeaderElector distributed primitive Test">
<group name="Distributed-Primitive-Value">
<!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
<dependency name="Setup" requires="Prerequisites"/>-->
<step name="Activate-Distributed-Primitives-App"
exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
<step name="Test-Initial-No-Leader" requires="^"
exec="onos-execute-expect ${OCI} leader-test test-elector show foo --expect leader=none#candidates=none"/>
<step name="Test-Leader-Run" requires="^"
exec="onos-execute-expect ${OCI} leader-test test-elector run foo --expect leader=${OCI}#term=1#candidates=${OCI}"/>
<step name="Test-Leader-Withdraw" requires="^"
exec="onos ${OCI} leader-test test-elector withdraw foo"/>
<step name="Test-No-Leader-After-Withdraw" requires="^"
exec="onos-execute-expect ${OCI} leader-test test-elector show foo --expect leader=none#candidates=none"/>
<!--Check with check logs-->
<step name="Check-Log-Exceptions" requires="^"
exec="onos-check-logs ${OCI}"/>
<step name="Teardown-Distributed-Primitives-Test-App" requires="^"
exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
</group>
</scenario>
<!--
~ Copyright 2016-present Open Networking Laboratory
~
~ Licensed 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.
-->
<scenario name="Distributed consistent map test"
description="ONOS ConsistentMap distributed primitive Test">
<group name="Distributed-Primitive-Map">
<!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
<dependency name="Setup" requires="Prerequisites"/>-->
<step name="Activate-Distributed-Primitives-App"
exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
<step name="Test-Map-Get" requires="^"
exec="onos-execute-expect ${OCI} map-test foo get a --expect null"/>
<step name="Test-Map-Put" requires="^"
exec="onos-execute-expect ${OCI} map-test foo put a b --expect null"/>
<step name="Test-Map-Updated-After-Put" requires="^"
exec="onos-execute-expect ${OCI} map-test foo get a --expect b"/>
<step name="Test-Map-PutIfAbsent-When-Key-Present" requires="^"
exec="onos-execute-expect ${OCI} map-test foo putIfAbsent a c --expect b"/>
<step name="Test-Map-PutIfAbsent-When-Key-Absent" requires="^"
exec="onos-execute-expect ${OCI} map-test foo putIfAbsent b c --expect null"/>
<step name="Test-Map-Updated-After-PutIfAbsent" requires="^"
exec="onos-execute-expect ${OCI} map-test foo get b --expect c"/>
<step name="Test-Map-Updated-After-PutAndGet" requires="^"
exec="onos-execute-expect ${OCI} map-test foo putAndGet b d --expect d"/>
<step name="Test-Map-Replace-When-Key-Absent" requires="^"
exec="onos-execute-expect ${OCI} map-test foo replace c e --expect null"/>
<step name="Test-Map-Replace-When-Key-Present" requires="^"
exec="onos-execute-expect ${OCI} map-test foo replace b e --expect d"/>
<step name="Test-Map-Replace-When-Value-Does-Not-Match" requires="^"
exec="onos-execute-expect ${OCI} map-test foo replace b x f --expect false"/>
<step name="Test-Map-Replace-When-Value-Does-Match" requires="^"
exec="onos-execute-expect ${OCI} map-test foo replace b e f --expect true"/>
<step name="Test-Map-ContainsValue-False-Case" requires="^"
exec="onos-execute-expect ${OCI} map-test foo containsValue x --expect false"/>
<step name="Test-Map-ContainsValue-True-Case" requires="^"
exec="onos-execute-expect ${OCI} map-test foo containsValue f --expect true"/>
<step name="Test-Map-Size" requires="^"
exec="onos-execute-expect ${OCI} map-test foo size --expect 2"/>
<step name="Test-Map-IsEmpty" requires="^"
exec="onos-execute-expect ${OCI} map-test foo isEmpty --expect false"/>
<step name="Test-Map-Remove" requires="^"
exec="onos-execute-expect ${OCI} map-test foo remove b --expect f"/>
<step name="Test-Map-Remove-Key-Value-Does-Not-Match" requires="^"
exec="onos-execute-expect ${OCI} map-test foo remove a c --expect false"/>
<step name="Test-Map-Remove-Key-Value-Does-Match" requires="^"
exec="onos-execute-expect ${OCI} map-test foo remove a b --expect true"/>
<step name="Test-Map-Clear" requires="^"
exec="onos ${OCI} map-test foo clear"/>
<step name="Test-Map-Cleared" requires="^"
exec="onos-execute-expect ${OCI} map-test foo isEmpty --expect true"/>
<!--Check with check logs-->
<step name="Check-Log-Exceptions" requires="^"
exec="onos-check-logs ${OCI}"/>
<step name="Teardown-Distributed-Primitives-Test-App" requires="^"
exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
</group>
</scenario>
<!--
~ Copyright 2016-present Open Networking Laboratory
~
~ Licensed 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.
-->
<scenario name="distributed value test"
description="ONOS AtomicValue distributed primitive Test">
<group name="Distributed-Primitive-Value">
<!--<import file="${ONOS_SCENARIOS}/setup.xml"/>
<dependency name="Setup" requires="Prerequisites"/>-->
<step name="Activate-Distributed-Primitives-App"
exec="onos ${OCI} app activate org.onosproject.distributedprimitives"/>
<step name="Test-Value-Initial-Value" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value get --expect null"/>
<step name="Test-Value-Set" requires="^"
exec="onos ${OCI} value-test test-value set v0"/>
<step name="Test-Value-Get" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value get --expect v0"/>
<step name="Test-Value-CompareAndSet-False" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value compareAndSet v1 v2 --expect false"/>
<step name="Test-Value-Not-Updated-After-CAS" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value get --expect v0"/>
<step name="Test-Value-CompareAndSet-True" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value compareAndSet v0 v1 --expect true"/>
<step name="Test-Value-Updated-After-CAS" requires="^"
exec="onos-execute-expect ${OCI} value-test test-value get --expect v1"/>
<!--Check with check logs-->
<step name="Check-Log-Exceptions" requires="^"
exec="onos-check-logs ${OCI}"/>
<step name="Teardown-Distributed-Primitives-Test-App" requires="^"
exec="onos ${OCI} app deactivate org.onosproject.distributedprimitives"/>
</group>
</scenario>