Custom Search

Monday, December 29, 2014

Gerrit error missing Change-Id in commit message footer

1)
Got Project directory and Add hook

#gitdir=$(git rev-parse --git-dir); scp -p -P 29418 sajuptpm@review.openstack.org:hooks/commit-msg ${gitdir}/hooks/

2)
Run --amend, if you already committed the changes and bring Change-Id automatically in the commit message
#git commit --amend

3)
Push the changes
#git review












Sunday, December 28, 2014

How to Access RabbitMQ Management Plugin rabbitmqadmin Command Line

1)
Enable rabbitmq_management
#sudo rabbitmq-plugins enable rabbitmq_management

2)
Restart rabbitmq-server
#sudo service rabbitmq-server restart

3)
Access GUI
http://rabbitmq-server-ip:15672/
username:guest
password:guest

4)
Access  rabbitmqadmin Command Line
http://rabbitmq-server-ip:15672/cli

a)
Download  rabbitmqadmin script
#wget http://rabbitmq-server-ip:15672/cli/rabbitmqadmin

b)
Change permission
#chmod +x rabbitmqadmin

5)
rabbitmqadmin Commands

a) 
Help 
#./rabbitmqadmin --help

b)
List all exchanges
#./rabbitmqadmin -u guest -p cloud -H localhost list exchanges
-u : username
-p : password

c)
List all queues
#./rabbitmqadmin -u guest -p cloud -H localhost list queues

d)
Get help of subcommands
#./rabbitmqadmin help subcommands
#./rabbitmqadmin help subcommands | grep queue
#./rabbitmqadmin help subcommands | grep exchnage
#./rabbitmqadmin help subcommands | grep binding
#./rabbitmqadmin help subcommands | grep channel
#./rabbitmqadmin help subcommands | grep connection

e)
Config
#./rabbitmqadmin help config




How to Access RabbitMQ Management Plugin HTTP API

1)
Enable rabbitmq_management
#sudo rabbitmq-plugins enable rabbitmq_management

2)
Restart rabbitmq-server
#sudo service rabbitmq-server restart

3)
Access GUI
http://rabbitmq-server-ip:15672/
username:guest
password:guest

4)
Access HTTP API
http://rabbitmq-server-ip:15672/api

RabbitMQ Topic Exchange Python example of Publish and Consume

1)
Install pika
#sudo pip install pika

2)
Create exchange and queue and bind it

#!/usr/bin/env python
import pika
import sys

#https://www.rabbitmq.com/tutorials/tutorial-five-python.html

credentials = pika.PlainCredentials('guest', 'cloud')#username,password

parameters = pika.ConnectionParameters(host='localhost', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

exchange_name = 'my_exchange'
##Declare/Create a topic type exchange named "my_exchange"
channel.exchange_declare(exchange=exchange_name,
                         exchange_type='topic')
#default exchange_type is 'direct'

##Declare/Create a queue
queue_name = 'my_queue'
result = channel.queue_declare(queue=queue_name)

##Bind queue to exchange with routing_key/binding_key
binding_key = "my_key_2015"
channel.queue_bind(exchange=exchange_name,
                       queue=queue_name,
                       routing_key=binding_key)


connection.close()


3)
Publish a Message to exchange named "my_exchange"

#!/usr/bin/env python
import pika
import sys

#https://www.rabbitmq.com/tutorials/tutorial-five-python.html

credentials = pika.PlainCredentials('guest', 'cloud')#username,password

parameters = pika.ConnectionParameters(host='localhost', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

##Publish/send a message to the exchange named "my_exchange" with routing_key to route the message to correct queue
message = "Hello World"
exchange_name = 'my_exchange'
routing_key = 'my_key_2015'
channel.basic_publish(exchange=exchange_name,
                      routing_key=routing_key,
                      body=message)

connection.close()


4)
Consume the published Message from the exchange named "my_exchange"

#!/usr/bin/env python
import pika
import sys

#https://www.rabbitmq.com/tutorials/tutorial-five-python.html

credentials = pika.PlainCredentials('guest', 'cloud')#username,password

parameters = pika.ConnectionParameters(host='localhost', credentials=credentials)

connection = pika.BlockingConnection(parameters)
channel = connection.channel()

##Get the exchange named 'my_exchange'
exchange_name = 'my_exchange'
channel.exchange_declare(exchange=exchange_name,
                         type='topic')

##Function to be called when there is a message in the queue named "my_queue"
def callback(ch, method, properties, body):
    print " [x] %r:%r" % (method.routing_key, body,)

##Consume the queue named "my_queue" and wait for the message
queue_name = 'my_queue'
channel.basic_consume(callback,
                      queue=queue_name,
                      no_ack=True)

channel.start_consuming()



5)
Screen shots




6)
AMQP concepts
https://www.rabbitmq.com/tutorials/amqp-concepts.html







Saturday, December 27, 2014

How to Access RabbitMQ Management Plugin GUI

1)
Enable rabbitmq_management
#sudo rabbitmq-plugins enable rabbitmq_management

2)
Restart rabbitmq-server
#sudo service rabbitmq-server restart

3)
Access GUI
http://rabbitmq-server-ip:15672/
username:guest
password:guest

Wednesday, December 24, 2014

How to Setup Static IP Address Ubuntu 14.04 LTS

How to Setup Static IP Address Ubuntu 14.04 LTS





How To Install GNS3 (Graphical Network Simulator) On Ubuntu

1)
Install gns3
#sudo apt-get update
#sudo apt-get install gns3

2)
Download IOS from following link
http://kuba.stelmaszczyk.com/tmp/dynamips/IOS/

3)
Open gns3 and upload IOS file
Edit --> "IOS images and hypervisors" --> Settings --> Image files --> Select teh downloaded IOS file *.bin --> Save

4)
Test
a)
Goto gns3 GUI and drag the router from left nav for which you uploaded the IOS file into right side.

b)
Then right click on the router and select "Configure".
Configure the Adapters/Interfaces required for the router.

c)
Then right click on the router and select "Start"

d)
Then right click on the router and select "Console". This will open a Console where you can type the commands.


Monday, December 22, 2014

How to print Unicode character in Python

>>>
>>>
>>> d = u"\u2013"
>>>
>>>
>>> print d

>>>
>>>

Tuesday, December 9, 2014

How to copy text from a copy protected web page by commenting out css code

1)
Download the web page

2)
Search for following css code in *.css file and comment out or delete

-webkit-user-select: none;
-khtml-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
-o-user-select: none;
user-select: none;

3)
Open the Downloaded html page and copy the text


Monday, December 8, 2014

OpenStack Python UnitTest with Mock Patching Library

* Mock is based on the 'action -> assertion' pattern instead of 'record -> replay'.

https://pypi.python.org/pypi/mock

http://code.google.com/p/mock/

http://www.voidspace.org.uk/python/mock/

Example:
-------------

mock.patch('requests.post').start().side_effect = FAKE_SERVER.request

Explanation:
------------------

import requests
import mock


pobj = mock.patch('requests.post') #Returns object of "mock._patch" class
type(pobj)
help(pobj)##check the definition of "mock._patch" class
dir(pobj)##Here, you can find 'start()' method


help(pobj.start)##Activate a patch, returning any created mock.
help(pobj.stop)##Stop an active patch.


mobj = pobj.start()##Returns object of "MagicMock" class
type(mobj)
help(mobj)##check the definition of "MagicMock" class
dir(mobj)##Here, you can find 'side_effect' attribute


mobj.side_effect
type(mobj.side_effect)


* Find the doc string of "side_effect"
#help(mock.Mock)
"side_effect": A function to be called whenever the Mock is called. See
the `side_effect` attribute. Useful for raising exceptions or
dynamically changing return values. The function is called with the same
arguments as the mock, and unless it returns `DEFAULT`, the return
value of this function is used as the return value.

Alternatively `side_effect` can be an exception class or instance. In
this case the exception will be raised when the mock is called.

If `side_effect` is an iterable then each call to the mock will return
the next value from the iterable. If any of the members of the iterable
are exceptions they will be raised instead of returned.

Example2:
----------------

>>> def test():
    return "Hello"

>>> mock.patch('requests.post').start().side_effect = test

>>> requests.post()
>>> Hello

Example3:
---------------
#help(mock.Mock)
* "return_value": The value returned when the mock is called. By default
this is a new Mock (created on first access). See the
`return_value` attribute.

* Auto-speccing creates mock objects that have the same attributes and methods as the objects they are replacing, and any functions and methods (including constructors) have the same call signature as the real object.Auto-speccing can be done through the autospec argument to patch.
http://www.voidspace.org.uk/python/mock/

Setting Up OpenStack Python Client Development Environment

1)
Clone the Project

#git clone https://github.com/openstack/python-keystoneclient.git

2)
Goto cloned directory

#cd python-keystoneclient



3)
Setup the Environment


3,a)
With run_tests.sh

#./run_tests.sh

OR

3,b)
With tox

#sudo pip install tox
#tox

4)
Run the client commands


4,a)
With run_tests.sh Environment


4,a,a)Goto cloned directory
#cd python-keystoneclient

4,a,b)
Create a script named "keystone" with following statements in cloned directory.
#vim keystone

#!/usr/bin/python
# PBR Generated from 'console_scripts'
import sys
from keystoneclient.shell import main
if __name__ == "__main__":
    sys.exit(main())


4,a,c)
Run the keystone client commands from run_tests.sh Environment
#source .venv/bin/activate
#.venv/bin/python keystone tenant-list
#.venv/bin/python keystone user-list


OR

4,b)
With Tox Environment

4,b,a)
Goto cloned directory
#cd python-keystoneclient

4,b,b)
Create a script named "keystone" with following statements in cloned directory.
#vim keystone

#!/usr/bin/python
# PBR Generated from 'console_scripts'
import sys
from keystoneclient.shell import main
if __name__ == "__main__":
    sys.exit(main())


4,b,c)
Run the keystone client commands from Tox Environment
#source .tox/py27/bin/activate
#.tox/py27/bin/python keystone tenant-list
#.tox/py27/bin/python keystone user-list


5)
Run the Tests


5,a)
With run_tests.sh
#./run_tests.sh --debug keystoneclient.tests.v2_0.test_tenants.TenantTests.test_list

##Only pep8 test
#./run_tests.sh --debug --pep8 keystoneclient.tests.v2_0.test_tenants

##manual pep8 test with the wrapper flake8
#source .venv/bin/activate
#flake8 keystoneclient/tests/v2_0/test_tenants.py


OR

5,b)
With tox


* Deactivate virtualenv (Important), then run tox command , otherwise tox command will try to recreate the virtualenv from scratch
#deactivate

*UnitTest
#tox -e py27 -- keystoneclient.tests.v2_0.test_tenants.TenantTests.test_list
#tox -e py27 -- keystoneclient.tests.v2_0.test_tenants
#tox -e py27

*pep8 test
#tox -e pep8 -- keystoneclient.tests.v2_0.test_tenants
#tox -e pep8

http://fosshelp.blogspot.in/2014/11/openstack-unit-testing-tox-runtestssh.html







Northbound Interface Southbound Interface

Northbound API Southbound API

Thursday, December 4, 2014

OpenStack Debug Python Unit Test using pdb

1)
Add following statements in Unit Test Code where you want to debug

import pdb
pdb.set_trace()




2)
Run "run_tests.sh" with "--debug" option

#./run_tests.sh --debug neutron.tests.unit.opencontrail.test_extension_ipam.IpamExtensionTestCase.test_create_ipam

OR

Run the test with "testtools" instead of "testr"

a)
If you are using "run_tests.sh" environment
#source .venv/bin/activate
#.venv/bin/python -m testtools.run neutron.tests.unit.opencontrail.test_extension_ipam.IpamExtensionTestCase.test_create_ipam

b)
If you are using "Tox" environment
#source .tox/py27/bin/activate
#.tox/py27/bin/python -m testtools.run neutron.tests.unit.opencontrail.test_extension_ipam.IpamExtensionTestCase.test_create_ipam


Wednesday, December 3, 2014

How to Uninstall and Reinstall Python setuptools

1)
Uninstall

#sudo apt-get remove --purge python-setuptools

2)
Reinstall

#sudo pip install --upgrade setuptools
#sudo apt-get install python-virtualenv python-dev




Monday, December 1, 2014

error: command 'gcc' failed with exit status 1

Fix
=====

#sudo apt-get install python-dev

Error
=======

  Running setup.py install for greenlet
    building 'greenlet' extension
    gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c greenlet.c -o build/temp.linux-x86_64-2.7/greenlet.o
    In file included from greenlet.c:5:0:
    greenlet.h:8:20: fatal error: Python.h: No such file or directory
    compilation terminated.
    error: command 'gcc' failed with exit status 1
    Complete output from command /home/saju/neutron/.venv/bin/python -c "import setuptools, tokenize;__file__='/home/saju/neutron/.venv/build/greenlet/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-TlW9nj-record/install-record.txt --single-version-externally-managed --compile --install-headers /home/saju/neutron/.venv/include/site/python2.7:
    running install

running build

running build_ext

building 'greenlet' extension

creating build

creating build/temp.linux-x86_64-2.7

gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c greenlet.c -o build/temp.linux-x86_64-2.7/greenlet.o

In file included from greenlet.c:5:0:

greenlet.h:8:20: fatal error: Python.h: No such file or directory

compilation terminated.

error: command 'gcc' failed with exit status 1

OpenStack Python Client Unit Testing check mocked client request Url, Method and Body

1)
Patch to debug client request Url, Method and body


git diff neutronclient/client.py neutronclient/v2_0/client.py
diff --git a/neutronclient/client.py b/neutronclient/client.py
index e41511f..c6ef62c 100644
--- a/neutronclient/client.py
+++ b/neutronclient/client.py
@@ -102,6 +102,8 @@ class HTTPClient(object):

         utils.http_log_req(_logger, args, log_kargs)
         try:
+           raise Exception(args)
+           #raise Exception(kargs)  
          
             resp, body = self.request(*args, **kargs)
         except requests.exceptions.SSLError as e:
             raise exceptions.SslCertificateValidationError(reason=e)
diff --git a/neutronclient/v2_0/client.py b/neutronclient/v2_0/client.py
index 9664520..bc555ae 100644
--- a/neutronclient/v2_0/client.py
+++ b/neutronclient/v2_0/client.py
@@ -1336,11 +1336,12 @@ class Client(object):
             try:
                 return self.do_request(method, action, body=body,
                                        headers=headers, params=params)
-            except exceptions.ConnectionFailed:
+            except exceptions.ConnectionFailed as ex:
                 # Exception has already been logged by do_request()
                 if i < self.retries:
                     _logger.debug('Retrying connection to Neutron service')
                     time.sleep(self.retry_interval)
+               raise ex

         raise exceptions.ConnectionFailed(reason=_("Maximum attempts reached"))



2)
Run following test and check the mocked client request Url, Method and body and compare with the original API request.


#cd python-neutronclient

a)
#.tox/py27/bin/python -m testtools.run neutronclient.tests.unit.test_cli20_ipam.CLITestV20Ipam.test_create_ipam

Url and Method : ConnectionFailed: Connection to neutron failed: ['localurl/v2.0/ipams.json', 'POST']

Body : ConnectionFailed: Connection to neutron failed: {'body': '{"ipam": {"name": "myname", "mgmt": {"method": "fixed"}}}', 'headers': {'X-Auth-Token': 'testtoken', 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-neutronclient'}}


b)
#.tox/py27/bin/python -m testtools.run neutronclient.tests.unit.test_cli20_ipam.CLITestV20Ipam.test_list_ipams_detail

Url and Method : ConnectionFailed: Connection to neutron failed: ['localurl/v2.0/ipams.json?verbose=True', 'GET']

Body : ConnectionFailed: Connection to neutron failed: {'body': None, 'headers': {'X-Auth-Token': 'testtoken', 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-neutronclient'}}


c)
#.tox/py27/bin/python -m testtools.run neutronclient.tests.unit.test_cli20_ipam.CLITestV20Ipam.test_show_ipam

Url and Method : ConnectionFailed: Connection to neutron failed: ['localurl/v2.0/ipams/aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa.json?fields=id&fields=name', 'GET']

Body : ConnectionFailed: Connection to neutron failed: {'body': None, 'headers': {'X-Auth-Token': 'testtoken', 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-neutronclient'}}


d)
#.tox/py27/bin/python -m testtools.run neutronclient.tests.unit.test_cli20_ipam.CLITestV20Ipam.test_delete_ipam

Url and Method : ConnectionFailed: Connection to neutron failed: ['localurl/v2.0/ipams/myid.json', 'DELETE']

Body : ConnectionFailed: Connection to neutron failed: {'body': None, 'headers': {'X-Auth-Token': 'testtoken', 'Content-Type': 'application/json', 'Accept': 'application/json', 'User-Agent': 'python-neutronclient'}}