maanantai 13. huhtikuuta 2015

Linux aliases

You can create aliases to your linux system by editing file:

~/.bash_aliases

Update aliases by running:

sudo apt-get update

For example if you install composer:

curl -sS https://getcomposer.org/installer | php

https://getcomposer.org/download/

You can then create alias for composer by editing the bash file and typing:

alias composer='/usr/local/bin/composer'

Now you can run composer anywhere just by typing composer

Sources:
http://ss64.com/bash/syntax-bashrc.html

Crontab timed mysql dump and commands

Crontab is timing program included in Linux systems. With crontab you can execute commands automatically on specific times and dates.

To open crontab give command $crontab -e 

You can run this command with root or non root person. If you want to do root things or you need to do command as root use root.

* * * * * command
┬ ┬ ┬ ┬ ┬
│ │ │ │ │
│ │ │ │ └──── day of the week (0–7) (sunday = 0 or 7)
│ │ │ └────── month (1–12)
│ │ └──────── day (1–31)
│ └────────── hours (0–23)
└──────────── minutes (0–59)


Here are few examples how to use crontab. For example I want to make a tarball from my homepage. This command would do the tarball under /home/shnigi and tarball name would be mysite.tar.gz. The tarball is created from /home/shnigi/public_html the command would run every day 15:12

12 15 * * * tar -czvf /home/shnigi/mysite.tar.gz /home/shnigi/public_html

To backup your MySQL database use command:

30 15 * * * /usr/bin/mysqldump --all-databases > /home/shnigi/dump.sql -u root -pPASSWORD

This would run every day 15:30 and take backup about all databases to /home/shnigi/dump.sql. Note that password is inserted right after the -p without space. The code under this is the same, but it generates date for every mysql dump file.

12 16 * * * /usr/bin/mysqldump --all-databases > /home/dump-$(date +\%d\%m\%y).sql -u root -p

You can try these commands without crontab, just use these:

#SQL dump
32 16 * * * /usr/bin/mysqldump --routines --flush-privileges --all-databases > /home/dump-$(date +\%d\%m\%y).sql -u root -pPASSWORD

#backup all home folders
26 16 * * * tar -czvf /home/homefolders.tar.gz /home

Sources:
https://www.linode.com/docs/databases/mysql/back-up-your-mysql-databases
http://www.thegeekstuff.com/2011/07/cron-every-5-minutes/

Remove .php extension

To remove .php extension from your site url, for example testsite.com/blog.php to just testsite.com/blog you need to do the following in your system:

$sudo a2enmod rewrite which allows apache to rewrite names. Then create hidden .htaccess file in your website root folder and type:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Edit your navigation page and remove all extensions. Like this: 


You also need to edit your virtualhost files which allows this. 



keskiviikko 25. maaliskuuta 2015

Robot Framework : Installation on local Linux and VPS. XML + Selenium2Library. Jenkins script.

http://robotframework.org/
Robot framework is generic test automation framework for acceptance testing, ATDD.

In this tutorial I am going to use Selenium2Library. Robot framework is based on libraries. If there is no library for your needs you can just create your own.

Test cases are written in a .txt or .robot files. Test cases are keyword based and can be parameterized. Language is Python, so think creating a test case like row of tables. Tabulator or two spaces between code.


Keyword Argument Data
Click Button for example locator (id, name etc.)
Insert text name Jack

Selenium library documentation: http://rtomac.github.io/robotframework-selenium2library/doc/Selenium2Library.html#Click%20Button

To install Robot Framework on Xubuntu:

$ sudo apt-get install python-pip
$sudo pip install robotframework
$sudo pip install robotframework-selenium2library
$ python
>> import Selenium2Library
>>exit()

And now you should have working Robot Framework including Selenium2Library. 

Create new test case:

$mkdir test
$cd tests
$nano test.robot

*** Settings ***
Library Selenium2Library
*** Variables ***
${PAGE} http://www.amazon.com
*** Test Cases ***
User must be able to open amazon.com
[Documentation] Type in Laptop and search
[Tags] Smoke
Open Browser ${PAGE} ff
Input Text twotabsearchtextbox Laptop
Click Button Go
Close Browser
*** Keywords ***

Under settings we have imported the selenium library. In the variables I have inserted one variable that can be used anywhere in the test case. Under test cases the first one is test name, documentation is about the test what will be done etc and with tags tests can be linked together and it is easier to found them in report files. So "Open Browser" is selenium library keyword, page is my variable and ff means firefox.

And now to run your test:
$pybot test.robot


My computer opened browser and went to amazon OK! Everything working. Robot Framework created some log and report files to the same folder that our test case lies. 


Green is good!

pybot -d /some/path will move logfiles elsewhere. $pybot -d results tutorial.robot

To run multiple test sets at one, do $pybot foldername

example and results in a tree view: pybot -d /some/path tests (this will run all test sets under tests folder and put results to results folder, which keeps things separate. It will be easier to maintain things.




Check my example script:
 https://github.com/shnigi/robotframework

Install on VPS / Vagrant


To run robotframework on a vagrantbox or VPS you need to run browser headless. This means robotframework can't run without faking screen. In order to do this install firefox:

$sudo apt-get install firefox

Install software to fake screen: $sudo apt-get install xvfb

Add a "display": $sudo Xvfb :10 -ac

Run firefox on the fake display:  $export DISPLAY=:10 firefox

And now you should be able to run robotframework tests in your vagrant / vps.

Closer look to Robot files


Robot framework should be installed to CI server as well as for developer computer. This means developer can run test sets and when CI build is triggered it will also run the tests. 

Create file resource.robot to the tests folder containing all test sets. This file will act as general settings file, which contains all basic variables, urls and tasks like open browser, maximize it etc. 

Then you can use those keywords in a test file. Here is my example:

Robot Framework doesnt create very sleek reports, so it would be useful to use some test case management software for project reporting and test case planning, like Quality Center or TestLink. Of course good old excel sheet will also do. Some people use only robot framework test files because they are pretty easy to understand if you understand robot and programming basics.

Reading XML files


Robotframework has built in library for reading xml. The principles are same than in the selenium2library, but you dont have to import it in python console beforehand. It is already there. 

XML documentation can be found here where all the keywords are explained: http://robotframework.org/robotframework/latest/libraries/XML.html

XML .robot file looks like this:

*** Settings ***
Library           XML

*** Variables ***
${XmlFile}        lol.xml

*** Test Cases ***
Parse-Xml-Test
    ${root}=    Parse XML    ${XmlFile}
    Should Be Equal    ${root.tag}    example
    ${first}  Get Element  ${root} first
    Should Be Equal  ${first.text}  text

And xml file is the same than in xml documentation:

<example>
  <first id="1">text</first>
  <second id="2">
    <child/>
  </second>
  <third>
    <child>more text</child>
    <second id="child"/>
    <child><grandchild/></child>
  </third>
  <html>
    <p>
      Text with <b>bold</b> and <i>italics</i>.
    </p>
  </html>
</example>

It goes through the xml file, checks that the xml root tag is "example" and first element text is "text" simple isn't it?


Jenkins


I created small shellscript to do Jenkins build. My system has user called "jenkka" the only thing Jenkins does is that it first goes to jenkkas home folder where the test.robot file lies, does all tests and puts results to var/www. Simple as that.

#!/bin/sh
cd /home/jenkka
pybot -d /var/www test.robot

Best Practices


RobotFramework best practises are to create ShellScript, that will trigger wanted tests. For example we could do a shell script named "run_qa_tests.sh" which has code:

#!/bin/bash
source executerobot.sh
runQAAcceptanceTests

Then we would have script named "executerobot.sh" which has a function named runQAAcceptanceTests.

#!/bin/bash
# executerobot.sh: a  shell script to run robot tests

function runQAAcceptanceTests() {
  pybot some code here -d results .
}

Trigger the first script with command: ./run_qa_tests.sh which will then run our function. The dot . after the pybot command will run all tests recursive under our main robot test folder. By doing this, we can run tests in our local environment and test server. We can define parameters for both cases and set jenkins properly.

To run a single test use shell command:  pybot -d results --test "testname" . Which will recursively find test case named testname. Last dot does the recursive search.

To add a variable file, which may contain for example test data numbers etc, you can give parameter like: --variablefile ./resources/testdata.py and the python file contains data which then can be used as variable ${variable}

When doing ATDD (Acceptance Test Driven Development) or BDD (Behaviour Driven Development), the following grammar is widely spread to formulate pre- and postconditions of a test:
  1. Given: The static preconditions
  2. When: The behaviour under test (or that, which should be specified)
  3. Then: The expected results of the behaviour under the given preconditions

Tutorial end


Notice for myself: There is syntax highlighter for ATOM editor: https://atom.io/packages/language-robot-framework

Ride is testdata editor for robotframework. Install it with: pip install robotframework-ride

Read more about creating test case: http://en.wikipedia.org/wiki/Robot_Framework

Sources:
http://datakurre.pandala.org/2014/03/cross-browser-selenium-testing-with.html
http://testingknols.blogspot.fi/2014/05/robot-framework-installation-on-ubuntu.html
http://seleniummaster.com/sitecontent/index.php/selenium-robot-framework-menu/selenium-robot-framework-python-menu/221-read-xml-file-in-robot-framework-python


keskiviikko 25. helmikuuta 2015

How To Install MediaWiki on user directory

MediaWiki is a free software open source wiki package written in PHP, originally for use on Wikipedia. It is now also used by several other projects of the non-profit Wikimedia Foundation and by many other wikis, including this website, the home of MediaWiki.
http://www.mediawiki.org/

Now I am going to install MediaWiki on my personal server. To install MediaWiki you will need a LAMP stack = Linux, Apache, MySQL, PHP. Possibly ssh connection.

Step 1


First head over to
http://releases.wikimedia.org/mediawiki/

Find the latest version available and copy its address. Currently the newest version is 1.24. http://releases.wikimedia.org/mediawiki//1.24/mediawiki-1.24.1.tar.gz

Copy that address and then head over to your user public_html folder. Use commands wget to download and tar to untar the file:

$ wget http://releases.wikimedia.org/mediawiki//1.24/mediawiki-1.24.1.tar.gz
$ tar xvfz mediawiki-1.24.1.tar.gz

You may need to restart your apache

$ sudo service apache2 restart

Step 2

Create MySQL table and user.

$ mysql -u root -p
$ create database my_wiki;
$ grant index, create, select, insert, update, delete, alter, lock tables on my_wiki.* to 'wikiuser'@'localhost' identified by 'password';
$ flush privileges;
$ exit;

step 3

head over to your MediaWiki site www.yoursite.com/~username/mediawiki-1.24.1
Click install and insert all things asked. 
When your install is finished MediaWiki will automatically download LocalSettings.php file. Upload this file to the root of your MediaWiki installation.

Step 4 Enjoy your MediaWiki site

Step 5 Images / Uploads

To allow image uploads to your mediawiki site you need to edit LocalSettings.php file and give chmod 777 to images folder. 

$nano LocalSettings.php 
Set line $weEnableUploads = true;
$ chmod 777 images/

 

keskiviikko 4. helmikuuta 2015

Trying Bitbucket

Introduction

Bitbucket is like Github. Which one should I use? Both GitHub and Bitbucket offer great Git services, but each has its own features and pricing plans.

Answer, use both. Use GitHub for open source and public repos and Bitbucket for private repos.

Interface

Bitbucket dashboard


Github dashboard
To start using Git service with Linux operating system, install Git first.

$ sudo apt-get install git

Step 1: Check for SSH keys
First, we need to check for existing SSH keys on your computer. Open up your Terminal and type:
ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist
Check the directory listing to see if you already have a public SSH key. The default public key file names are:
  • id_dsa.pub
  • id_ecdsa.pub
  • id_ed25519.pub
  • id_rsa.pub
Step 2: Generate a new SSH key
To generate a new SSH key, copy and paste the text below, making sure to substitute in your email address. The default settings are preferred, so when you're prompted to "Enter a file in which to save the key", just press Enter to continue.
ssh-keygen -t rsa -C "your_email@example.com"
# Creates a new ssh key, using the provided email as a label
# Generating public/private rsa key pair.
# Enter file in which to save the key (/home/you/.ssh/id_rsa):

Next, you'll be asked to enter a passphrase.
# Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
Which should give you something like this:
# Your identification has been saved in /home/you/.ssh/id_rsa.
# Your public key has been saved in /home/you/.ssh/id_rsa.pub.
# The key fingerprint is:
# 01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com
Install xclip
sudo apt-get install xclip
Copy your ssh key
xclip -sel clip < ~/.ssh/id_rsa.pub
Enter it to Bitbucket page. 


Then configure your Git repo.

Create folder 
$mkdir code 
$cd code
$git init
$git config --global user.email "yourmail"
$git config --global user.name "yourname"

Then add your repository
$git remote add origin git@bitbucket.org:yourname/repo.git

Add all files to git in your local repo. Then commit changes.
$git add .
$git commit -m "testing git"

Push files to Bitbucket
$git push -u origin master

If you somewhere encounter problem "fatal remote origin exists" set it again with command:

git remote set-url origin git://new.url.here

keskiviikko 22. lokakuuta 2014

Starting Android developement with Linux and Phonegap / CocoonJS, Sign and zipalign. Upload to Google PlayStore.

Installing Phonegap, creating HelloWorld project and running it on Android emulator.

Links related:
http://phonegap.com/
http://developer.android.com/index.html
http://nodejs.org/

1. Start by downloading Android SDK from https://developer.android.com/sdk/index.html?hl=i



2. Unzip the file and move it under /opt/adt-bundle

Instructions for unzipping: http://www.cyberciti.biz/tips/how-can-i-zipping-and-unzipping-files-under-linux.html

$ sudo mv adt-bundle-linux-x86_64-20140702 /opt/adt-bundle
3. Configure SDK to terminal by giving command nano ~/.bashrc and add lines to the end of file:

#Android stuff  
export ANDROID_HOME=/opt/adt-bundle/sdk
export PATH=$ANDROID_HOME/tools:$PATH
export PATH=$ANDROID_HOME/platform-tools:$PATH




4. Give command android to terminal and download all required files. For example tools, android api and extras.



5. Install some libraries needed for developing (notice that these are needed also in 64bit system): sudo apt-get install libstdc++6:i386 libgcc1:i386 zlib1g:i386 libncurses5:i386
6. Install NodeJS, ANT and NPM: sudo apt-get install npm ant nodejs nodejs-legacy
7. Install PhoneGap: sudo npm install -g phonegap
8. Install Cordova: sudo npm install -g cordova

9. Then create new folder for your project for example /home/username/phonegap
10. Cd to that folder
11. Create new project by typing: phonegap create helloworld
12. Give command: android avd and install virtual Android emulator

HINT: create custom project: $ phonegap create hello com.example.hello HelloWorld



13. Run the emulator with command: phonegap run android to run the HelloWorld project in android emulator. Notice that this may take very long. 10 minutes even.



14. After successful run, type  phonegap build android to build your project which can then be imported to Eclipse and Android phone.
15. You may need to install "Android Development tools" from the marketplace.


16. Setup your SDK location to Eclipse. In this tutorial SDK is set to /opt/adt-bundle/sdk


17. Import your HelloWorld project to eclipse. File -> Import -> Android -> Existing Android Code Into Workspace. Select your generated HelloWorld root folder to import. I have selected another project here.


18. Project loaded to Eclipse and ready for Developing.


19. To run your project right click it and select "Run as -> Android Application" You can also find SDK and AVD Managers in Eclipse menu.

20. If you need to run your project again in emulator, DO NOT CLOSE IT. Android emulator may take even 10 minutes to load and therefore you don't need to re-open it every time. You can just run your project again while emulator is running and it installs the app on it.

To use a real  physical Android phone


1. Log in as root and create this file: sudoedit /etc/udev/rules.d/51-android.rules.

2. Insert line SUBSYSTEM=="usb", ATTR{idVendor}=="0bb4", MODE="0666", GROUP="plugdev"

3. Check your phones Vendor ID from the list: http://developer.android.com/tools/device.html
and paste it to Vendor {idVendor}=="YOUR PHONE CODE HERE"

4. Execute command: chmod a+r /etc/udev/rules.d/51-android.rules

5. Enable Developer Options from your phone and activate USB-debugging
(How to enable developer options http://www.androidcentral.com/how-enable-developer-settings-android-42)


6. Connect your phone to computer with usb and now if you run the project from Eclipse, it will ask if you want to run it with your phone or emulator. If you use terminal Phonegap run android it will launch the project on your phone. What this actually does is that it installs the app on your phone. It will stay there even if you unplug the cable. You need to uninstall it to get rid of it. This should do no harm to your phone and should be safe to test applications. Unless you are trying to manipulate your android software itself.

Signing the application


To be able to upload your application to Google Play you need to Sign and zipalign your app.
Google link: http://developer.android.com/tools/publishing/app-signing.html

In Eclipse:

1. Right click your project and select Export 

2. Search for Android application



3. Click next


4. Select create a "new key store" as you are making a key for the first time. Next time onwards you can use the same key to sign all of your projects.
Now you have to save the key to a file, so click browse and type a file name and click save.
You have to give a password for the key, you have to remember it, as it will be asked when next time an APK is signed. Type the password again to confirm.



5.
Alias name: you can give your name
Password: just the same password you typed before
Validity: Give a big number like 100 Years.
Organisational Unit: Give any name like your family name
Organisation: Give some name as above.
Country code: Search for it in Google.


6. On the next screen click browse and save your signed APK file in to a folder or desktop. Click finish to complete the setup.Congrats!! You can now publish it in Google Play.


To build from terminal type: cordova build android --release inside the project folder. Then you will find under platforms/android/ant-build/HelloWorld-release-unsigned.apk for example.

CocoonJS: Build your HTML5 crossplatform app in cloud and skip all the hazzle with phonegap.


"CocoonJS is a platform to test, accelerate, deploy and monetize your HTML5 apps and games on all mobile devices with many interesting features to help you deliver great web products faster:"


What CocoonJS actually does, it converts your HTML5 app to mobile ready file. You can upload a ZIP file containing your HTML5 project to CocoonJS and it will convert it for you almost ready to deploy on Google Play. CocoonJS also has use full application in play store to install on your phone. https://play.google.com/store/apps/details?id=com.ideateca.cocoonjslauncher You can upload your app to a web-server and try your app straight from the phone. Just copy paste the web address to CocoonJS launcher and see how your app works.

1. Sign up at CocoonJS

2. Click Dev Portal


3. Select "New Project"

If I understood correctly "TYPE": CocoonJS: if you want to create game running with HTML5 or Canvas, Application that doesn't need any sensors from the phone. Cordova: if you want to access your phones sensors, like proximity sensor, GPS, etc. However this is just a guess. I am not sure what is the difference between these. I also think that CocoonJS somehow accelerates the app so it should work faster theoretically compared to Cordova build.  


4. Type in your project Name, Bundle Id and Version. Especially Bundle Id is important. If your package name start with com.example, com.google or com.android they will not accept your file. better you can give a starting name as "com.yourname" because it is to be unique in Google Play. Version number should start from 0.1 and always grow up. It can also start from 1.0.0 and grow but I prefer versioning from 0.1


5. Select phone orientation, in which orientation it can be used and scaling method how it should show up on the phone screen. Then click Create Project

6. Next head up to Configuration tab. Select the minimum Android version needed to run your app and Max version should be set to All versions of course. Before you update the configurations go ahead and upload your app icons. Notice that Android needs different sized images, because the screen sizes vary. You can also add your splash screen if wanted.

7. Go to compile project window. Now it is time to zip your project. Open your project folder which contains the index.html file, select all and create a zip file. (If you want to upload your phonegap project select everything from "www" directory. Upload it. Then select "Compile project for" for which platforms you want to compile. Pick Google PlayStore (Android) for this project. And run using your preferred type mentioned earlier. Pick newest compiler version and accept the terms.


8. After you click "Compile Project" it may take 5-10 minutes before your project is compiled, because this is a free service. You will receive email containing the download link when your project is ready. Click the Android icon to download your .APK file.


9. The apk comes in a zip file. Unzip it and you will end up with two different files
a. Debug_signed
b. Release_unsigned
If you want to try your app before going online you can allow your phone to run applications from untrusted sources (Link how to allow untrusted sources: http://www.tech-recipes.com/rx/22616/android-allow-installation-of-non-market-apps/) Then connect your phone to computer and copy paste the Debug_signed.APK to your phone.

10. Find your Debug_signed.APK file with file manager (For example: https://play.google.com/store/apps/details?id=com.rhmsoft.fm)

11. Install and try how it works.

Signing CocoonJS apk with command line, ready to publish on Google Play


To upload your CocoonJS file to Google Play, you need to sign and zipalign it also. Now the file is in APK format and you can't export it straight from the Eclipse.


To sign apk:

1. Create new folder and put your Release_signed.APK there

2. Open terminal and use command:

$ keytool -genkey -v -keystore my-release-key.keystore
-alias alias_name -keyalg RSA -keysize 2048 -validity 10000

Where my-release-key is your key name and alias_name is your name . For example "androidkey" and "shnigi". REMEMBER to keep your signature key in safe place and password also. If you want to update your app in future you need to sign it with this same key file and password. Also if you want to sign other projects, you can use this same key as well.

$ keytool -genkey -v -keystore androidkey.keystore
-alias shnigi -keyalg RSA -keysize 2048 -validity 10000

Answer some questions to generate the file.



3. Then sign your APK with command (modify your key name and alias):

$ jarsigner -verbose -sigalg SHA1withRSA -digestalg SHA1
-keystore androidkey.keystore Release_signed.apk shnigi

Example of signing process:


4. Now your .APK is signed but it needs to be zipaligned also.

You can try to zipalign your file with: $ zipalign -v 4 your_project_name-unaligned.apk your_project_name.apk But if you get "Command not found" continue.

5. Head over to your SDK folder and find zipalign file under build-tools
(In my case /opt/adt-bundle/sdk/build-tools/21.1.1/)

6. Copy it and paste to the folder whre your .APK file is

7. Execute command:

$ ./zipalign -v 4 Release_unsigned.apk release_signed.apk




8. Congrats! Your CocoonJS file is now ready to be deployed on Google PlayStore!

Publishing your app to Google Play


1. Go to https://play.google.com/apps/publish/ and login with your Google account. If you don't have one go ahead and register to Google. 

2. You have to give a one time developer fee of $25/- for publishing in Google play. 


3. Before publishing you have to make screenshot of any of these types 320x480,480x800,480x854,1280x720,1280x800 , PNG or JPEG. Also a high resolution Application icon of 512x512, Max.1024KB You may also create optional youtube promotional videos. All these optional items will make your app more attractive.


Important! Once your App is published you can NEVER delete it. You can only "cancel publish" but Google will keep all apk files in the store. You can use filters to hide these canceled publishes. You can delete apps that are not yet published. To update your app just upload new .APK file. And now if I understood correctly, the version number must be higher than previously. Otherwise Google won't accept it.