Arie Bregman

Linux And Stuff

Month: October 2015

Python: Check whether a string is Palindrome or not

A palindrome is a sequence of characters which reads the same backward or forward.

For example: dad, mom, kayak, a santa at nasa, anna. Not palindromes: mario, luigi, ice cream.

There is more than one way in python to check whether given string is palindrome or not. Let’s have a look.

Method 1

defiantly one of the shortest ways to do it.

[::-1] taking care of inverting the string and all if left is simply compare between the original to inverted string.

Method 2

Slower than first method:

Method 3

My least favourite but quite common (especially in other languages)


 

Q&A

Q: How can I handle case-sensitive palindrome?

A: you can use upper() when comparing between the reversed and the original strings.

OpenDaylight: Run integration tests on CentOS/RHEL 7

Running OpenDaylight ( I’ll refer it as ODL from now on)  integration tests can be frustrating for newcomers as the documentation may be confusing or scattered in different unlinked pages.  So my purpose here is to provide you with short, clear steps to do so yourself on centos or rhel 7.

User configuration

Let’s start by creating regular user for  running the tests:

At this point I’d also recommend to change the user password.

Create public key for the user we have just created:

For mininet to operate properly you should also copy the public key to authorized_keys. If this file doesn’t exists, create it:

Our new user should also have sudo access:

Install Mininet (Optional)

Mininet allows you set up virtual network with switches, hosts and controllers in seconds.

If you already have hosts with preconfigured network environment, you can skip it but some of the tests may rely on it.

I will show you how to install mininet from the project repo, but you should be aware it’s not the only way to it.  You can view all the installation methods here: Install mininet (at the moment there is no rpm for centos/rhel 7)

First, clone the project

Now you need to edit the installation script since it supports fedora and not cenots/rhel

Now start mininet installation

-f to install openflow, -v to install ovs and -n to install mininet dependencies ( gcc, ethtool, pylint, etc)

That’s it. Now verify it’s working propely:

Install and run the OpenDaylight Controller

I won’t go into details on how to install the controller since there several types of distributions and ways to install it.

For the the absolute beginner I would recommend to download pre-built tar/zip from here: ODL Downloads (I’m using Helium)

After downloading the tar/zip file and extracting the content, go into the distribution directory and  run these commands and you are set to go:

Configure Test Environment

First, lets install Robot. This is the framework used to run the tests.

Verify the installation:

Clone the repo which contains the tests:

Run the tests

Congrats! you are here in the final and the exciting step.

All the tests are located in ‘integration/test/csit/suites’ and they are divided by project.

You can run all the tests or you can run spesific project tests.

Lets run basic Restconf tests:

The results should look like this:

Restconf test results

Please note that each project/test might require different variables that you’ll need to provide.

For exmaple for openstack neutron there are tests in ‘integration/test/csit/suites/openstack/neutron’ which require additional parameters such as {OPENSTACK}.

You can include additional variables using -v like that:


Q&A

Q: My tests are failing. why? 🙁

A: Make sure controller is running and the odl-restconf feature is installed

Q: Do I have to use odluser or can I use my own user?

A: You can use any user you want.

 

Ulimit And Maximum Number Of Open Files

Note: this is an old post from my previous blog

Sometimes you may want to set limits on some of your system resources(Processes, Files)

But other times, you may want to do the just the opposite – disable those limits or change them to higher limits like I needed to do few days ago.

1. Check what are the limits

Apparently, RedHat set the default max number of open files for users  to 1024 and and not long ago, this was change to 4096.

For some applications/users this is very low and can cause you a lot of problems.


You can check what is the current limit with ulimit using bash shell:

This will show you the hard limit of maximum number of files for your user. you can use -S to check the soft limit.

tcsh: using tcsh you can can check this limit with limit descriptors


if you wish to check the max Open file descriptors for the process your user/application running, use the following command: 


To check what is the system limit for number of files descriptors use the following command:

2. Change the limit

lets start by changing the limit for the user mario:

now change the following lines:

save the file and verify the result by using ulimit like we used it in section 1.
If you want to set maximum number of processes use nproc instead of nofile
If you want to set this setting to all users use * instead of specify user name

Now lets change the limit for the entire system:

and put the line:


update the system with::

3. List number of open files/allocated file handles

To check the number of files opened on your system, use:

You can also check the allocated file descriptors by using:

the first field is the number of total allocated files descriptors.

second field is unused file descriptors

and the third field is the maximum file descriptors that can be used

 

Test Results: convert Subnit stream to junitXML

Quick introduction: Subunit is a streaming protocol for test results. It is a binary encoding generated while you are running tests. It is also widely used in the Openstack project.

Subunit comes with a lot of useful filters. Some of them are:

  • subunit2gtk
  • subunit2pyunit
  • tap2subunit

I will focus on subunit2junitxml which converts a Subunit stream into a junitXML representation. I recently had to use it for CI as the Jenkins plugin I’m using doesn’t support publishing tests results using pure  subunit stream.

First lets start with installing the needed packages.

For RHEL/CentOS/Fedora:

Now take your subunit stream ( if you don’t know where it is, look in “Q&A” section ) and run the following command:

This command will use the subunit filters to convert the stream into junitXML.

In order to verify it worked, read the file and see if it’s in the desired format.

You can also simply run:  grep “<testcase classname=”  tests_results.xml

If it didn’t work, you may need first to convert the stream into the newer protocol version 2.  In such case simply run:

That’s it. Now you have it in junitXML format and if you are using jenkins, you can also use it to publish your tests results.


Q&A

Q: Where can I find the subunit stream files?

A: It may depend on how you run the tests. If you are using ‘tox’ you could find the files in .testrepository directory. The name of the files would be numbers. Those numbers are actually the tests run order. 1 for example, would be the second time you ran the tests.

Q: In jenkins, How do you publish tests results in Junit format?

A: I’ve installed the Junit plugin and in ‘Post-build Actions’ I’m using ‘Publish Junit test result report’

© 2017 Arie Bregman

Theme by Anders NorenUp ↑