Android Studio Mac OSX Installation Error — NoClassDefFoundError: Could not initialize class javax.crypto.SunJCE_b

Today I decided to play with Android and build a simple app. I downloaded the latest version of Android Studio for Mac from the official website http://developer.android.com/sdk/index.html. As soon as I tried to install the Android Studio on my Mac book (OSX YoseMite 10.10.2) I was greeted with a weird error message as shown below.

Internal error. Please report to https://code.google.com/p/android/issues

java.lang.NoClassDefFoundError: Could not initialize class javax.crypto.SunJCE_b
at javax.crypto.KeyGenerator.a(DashoA13*..)
at javax.crypto.KeyGenerator.(DashoA13*..)
at javax.crypto.KeyGenerator.getInstance(DashoA13*..)

You get this error because you need to have Java 6 installed on your Mac. To get rid of this error, just download the Java from this link https://support.apple.com/kb/DL1572?locale=en_US. Once Java 6 is installed, open the Android Studio app again and you will be able to work with Android Studio :)

How to resize a Virtual Box virtual machine hard disk size

Today while working on a project I had to resize hard disk size of one of the virtual machines. The Windows virtual machine was created using veewee. To resize a VM hard disk size, you have to perform couple of steps:

1. Run the $ VBoxManage modifyhd ~/VirtualBox\ VMs/windows2008/windows20081.vdi –resize 20480 command.

2. Follow the steps mentioned in this post http://derekmolloy.ie/resize-a-virtualbox-disk/ to understand how you can use the gparted to allocate the unallocated space.

Writing Test Automation Framework for Installers

Last few weeks I have spent considerable time writing integration test suite for one of our product installers. The goal of this test suite is to automatically test functionality of the product on various operating systems. Currently, we are running our automated test suite on Windows 2008, Windows 7, and Windows 8 systems. In this blog, I will first talk about the technology stack that we used to build our automation framework and then I explain  how you can write such test suite using Java installer as an example.

Automation Framework Technology Stack

1. VirtualBox: VirtualBox is a cross platform powerful virtualization tool.

2. Veewee: Veewee is a ruby gem that helps you to easily build repeatable Vagrant base boxes from the command-line. It provides templates for various operating system that you can modify to configure your base box.

3. Vagrant: Vagrant is another Ruby tool that allows you to easily create and manage virtual machines from command-line. You can provision virtual machines using configuration management tools like Chef or Puppet.

4. Ovethere: Overthere is a Java library from XebiaLabs that allows you manipulate files and execute processes on remote hosts. It supports various protocols(local, SSH, CIFS) to communicate with remote machines. Refer to README.md https://github.com/xebialabs/overthere/blob/master/README.md for more information.

5. Overcast: Overcast is another great library from XebiaLabs that allows you test against various hosts. It supports Vagrant hosts, KVM hosts, Amazon EC2, etc. For more information refer to README.md https://github.com/xebialabs/overcast/blob/master/README.markdown.

6. JUnit: JUnit is a Java library that we used to write test cases.

Installing Veewee and Vagrant

Our test automation framework needs veewee and vagrant to work so lets install them before we can start working on our test cases. Both veewee and vagrant requires Ruby programming language. So, before installing them you need to have Ruby installed on your machine. Please install Ruby by following instructions mentioned in official documentation https://www.ruby-lang.org/en/documentation/installation/.

Installing Veewee

To install veewee run the following command.

$ gem install veewee

You might also need to install couple more dependencies depending on your operating system.

$ gem install em-winrm log4r

Installing Vagrant

Refer to the official Vagrant documentation for installation instructions http://www.vagrantup.com/downloads.

Please download package for your operating system.

Creating Base box configuration

Veewee provides a predefined set of templates for various operating systems that you can use to create your base box. To view list of all templates run the following command.

$ veewee vbox templates

It will list down all the available template as shown below. I am only showing part of the output for brevity.

veewee vbox define ‘<box_name>’ ‘windows-2008R1-serverstandard-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-2008R2-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-2008R2-serverstandard-amd64-winrm’ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-2012-serverstandard-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-2012R2-serverdatacenter-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-7-enterprise-amd64-winrm’ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-8-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-8-i386′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

veewee vbox define ‘<box_name>’ ‘windows-8-preview-amd64′ –workdir=/Users/shekhargulati/dev/blog/automation-framework

Next choose the basebox definition that you need for your test cases. For this blog, I will go with “`windows-2008R2-serverstandard-amd64-winrm“` configuration. Now navigate to a convinient location where you want to create your base box configuration and run the following command.

$ mkdir windows2008R2 && veewee vbox define ‘windows2008R2′ ‘windows-2008R2-serverstandard-amd64-winrm’ –cwd=windows2008R2

The command shown above will create a new directory **windows2008R2** and place base box configuration in that directory. You can view the base box configuration by opening the **windows2008R2** directory in your favorite editor.

Lets look at the files generated by veewee.

— definitions

    — windows2008R2

        |– Autounattend.xml

        |– README.md

        |– definition.rb

        |– install-chef.bat

        |– install-vbox.bat

        |– oracle-cert.cer

         — postinstall.sh

2 directories, 7 files

We will discuss two files — definition.rb and Autounattended.xml.

The **definition.rb** is a ruby file that defines from where Veewee should download ISO images. It is also used to configure disk space, RAM, Number of CPUs that this VM should use. This also specifies the winrm user and other configurations for the base box.

**Tip**: If you already have an ISO image then create a directory named **iso** next to **definitions** directory and place your ISO image in that directory. You also have to tell Veewee about your ISO image. In the **definition.rb** file comment out “`iso_md5“` and “`iso_src“` as shown file change the iso_file value to your image name.

    :iso_file => “my_image.iso”,

    #:iso_md5 => “4263be2cf3c59177c45085c0a7bc6ca5″,

    #:iso_src => “http://care.dlservice.microsoft.com//dl/download/7/5/E/75EC4E54-5B02-42D6-8879-D8D3A25FBEF7/7601.17514.101119-1850_x64fre_server_eval_en-us-GRMSXEVAL_EN_DVD.iso&#8221;,

The Autounattended.xml allows you to fully automate Windows installation. It can be used to:

1. Create a single partition for the boot, system, crash dump and primary partition

2. Enter the product key

3. Configure WinRm

4. Open ports so that you can access them

5. Enable the ‘Administrator’ account

6. Set the ‘Administrator’ password to ‘password’

7. Create a local administrator account called ‘UserName’ with the password of ‘password’

In our test cases, we updated WinRm configuration so we modified this file to meet our needs. For example, we updated WinRm “`MaxMemoryPerShellMB“` property from “`300MB“` to “`1024MB“` so we updated “`Autounattend.xml“` file.

<SynchronousCommand wcm:action=”add”>

    <CommandLine>cmd.exe /c winrm set winrm/config/winrs @{MaxMemoryPerShellMB=”1024″}</CommandLine>

    <Description>Win RM MaxMemoryPerShellMB</Description>

    <Order>4</Order>

    <RequiresUserInput>true</RequiresUserInput>

</SynchronousCommand>

You can also open ports using this file as shown below.

<SynchronousCommand wcm:action=”add”>

  <CommandLine>cmd.exe /c winrm set winrm/config/listener?Address=*+Transport=HTTP @{Port=”8080″} </CommandLine>

  <Description>My application port</Description>

  <Order>9</Order>

  <RequiresUserInput>true</RequiresUserInput>

</SynchronousCommand>

Once you are done with configuration changes, you can create a VirtualBox VM build by executing the command shown below.

$ veewee vbox build ‘windows2008R2′

This will download the ISO image and then create a VirtualBox image for Windows 2008. Once installation is complete, you can export the Vagrant box by running the following command.

$ veewee vbox export ‘windows2008R2′

This will create a new Vagrant box in windows2008R2 directory. The name of the box will be windows2008R2.box.

Importing the Vagrant base box

Now that you have created the Vagrant box, you can add it Vagrant local registry by running the following command.

$ vagrant add windows2008R2 windows2008R2.box

Now you can test your Vagrant box by creating a Vagrantfile and launching the newly created Vagrant box. Go to a convenient location and create a new file with name Vagrantfile as shown below.

$ mkdir vagrant-windows2008R2

$ cd vagrant-windows2008R2

$ touch Vagrantfile

Put the following contents in the Vagrantfile as shown below.

Vagrant.configure(“2″) do |config|

  config.vm.box = “windows2008R2″

  config.vm.guest = :windows

  config.vm.communicator = “winrm”

  config.winrm.username = “vagrant”

  config.winrm.password = “vagrant”

  config.vm.provider “virtualbox” do |v|

    v.gui = true

    v.cpus = 2

    v.memory = 2048

  end

end

Fire the new Vagrant box by running the following command.

$ vagrant up

This will launch the Windows 2008 R2 virtual machine and you can use it for your work. To destroy the newly created box, run the following command.

$ vagrant destroy –force

Creating a Java project

Now that all prerequisites are take care off, lets start writing our test cases. Create a new Java Maven project using your favorite IDE or Maven archetype. Replace your application pom.xml with the one shown below:

<?xml version=”1.0″ encoding=”UTF-8″?>

<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243;

         xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;

         xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd”&gt;

    <modelVersion>4.0.0</modelVersion>

    <groupId>org.shekhar</groupId>

    <artifactId>installer-tests</artifactId>

    <version>1.0-SNAPSHOT</version>

    <properties>

        <maven.compiler.source>1.8</maven.compiler.source>

        <maven.compiler.target>1.8</maven.compiler.target>

    </properties>

    <dependencies>

        <dependency>

            <groupId>com.xebialabs.overthere</groupId>

            <artifactId>overthere</artifactId>

            <version>2.4.5</version>

        </dependency>

        <dependency>

            <groupId>com.xebialabs.cloud</groupId>

            <artifactId>overcast</artifactId>

            <version>2.4.0</version>

        </dependency>

        <dependency>

            <groupId>junit</groupId>

            <artifactId>junit</artifactId>

            <version>4.12</version>

            <scope>test</scope>

        </dependency>

    </dependencies>

</project>

Create configuration files

Create a new file with name “`overcast.conf“` in your Java project root directory and populate with following contents.

vagrantHost {

  vagrantDir = “vagrant”

  vagrantIp = “10.10.200.200”

  vagrantVm = “mybox”

}

Also, create a directory called vagrant in the project root directory and inside that create Vagrantfile configuration file as shown below.

# -*- mode: ruby -*-

# vi: set ft=ruby :

Vagrant.configure(“2″) do |config|

  config.vm.provider :virtualbox do |vb|

    vb.customize [“modifyvm”, :id, “–name”, “mybox”]

  end

  config.vm.box = “windows2008R2″

  config.vm.guest = :windows

  config.vm.define “mybox” do |oi|

    oi.vm.network :private_network, ip: “10.10.200.200”

  end

  config.vm.communicator = “winrm”

  config.winrm.username = “vagrant”

  config.winrm.password = “vagrant”

  config.vm.provider “virtualbox” do |v|

    v.gui = true

    v.cpus = 2

    v.memory = 2048

  end

end

Write JUnit test case for VM initialization

Create a new JUnit test case in the test directory as shown below.

package org.shekhar.installer.tests;

import com.xebialabs.overcast.host.CloudHost;

import com.xebialabs.overcast.host.CloudHostFactory;

import org.junit.AfterClass;

import org.junit.Assert;

import org.junit.BeforeClass;

import org.junit.Test;

import static org.hamcrest.core.Is.is;

import static org.hamcrest.core.IsNull.notNullValue;

import static org.junit.Assert.assertThat;

public class JavaInstallerTest {

    private static CloudHost vagrantHost;

    @BeforeClass

    public static void setup() {

        vagrantHost = CloudHostFactory.getCloudHost(“vagrantHost”);

        vagrantHost.setup();

    }

    @AfterClass

    public static void teardown() {

        vagrantHost.teardown();

    }

    @Test

    public void shouldLaunchVagrantBox() throws Exception {

        assertThat(vagrantHost, is(notNullValue()));

    }

}

Write Java installation test case

Now we will write a test case that would first copy the Java installer from local machine to the Vagrant box, installs it, and then checks if Java is installed.

package org.shekhar.installer.tests;

import com.xebialabs.overcast.OverthereUtil;

import com.xebialabs.overcast.host.CloudHost;

import com.xebialabs.overcast.host.CloudHostFactory;

import com.xebialabs.overthere.CmdLine;

import com.xebialabs.overthere.OverthereConnection;

import com.xebialabs.overthere.OverthereExecutionOutputHandler;

import com.xebialabs.overthere.local.LocalConnection;

import com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler;

import org.junit.AfterClass;

import org.junit.BeforeClass;

import org.junit.Test;

import org.slf4j.Logger;

import org.slf4j.LoggerFactory;

import java.io.UnsupportedEncodingException;

import java.net.URLEncoder;

import java.text.MessageFormat;

import java.util.Arrays;

import static com.xebialabs.overcast.OvercastProperties.getOvercastProperty;

import static com.xebialabs.overcast.OverthereUtil.overthereConnectionFromURI;

import static com.xebialabs.overthere.util.CapturingOverthereExecutionOutputHandler.capturingHandler;

import static org.hamcrest.core.Is.is;

import static org.hamcrest.core.IsEqual.equalTo;

import static org.hamcrest.core.IsNull.notNullValue;

import static org.junit.Assert.assertThat;

public class JavaInstallerTest {

    private static final String JDK_FILE_SOURCE = “jdk”;

    private static final String JDK_DESTINATION_PATH = “c:\\”;

    private static final String JDK_EXE_PATH = JDK_DESTINATION_PATH + “jdk-7u75-windows-x64.exe”;

    public static final String VAGRANT_HOST = “vagrantHost”;

    private static CloudHost vagrantHost;

    private static final Logger logger = LoggerFactory.getLogger(JavaInstallerTest.class);

    @BeforeClass

    public static void setup() {

        vagrantHost = CloudHostFactory.getCloudHost(VAGRANT_HOST);

        vagrantHost.setup();

    }

    @AfterClass

    public static void teardown() {

        vagrantHost.teardown();

    }

    @Test

    public void shouldInstallJDK7() throws Exception {

        assertThat(vagrantHost, is(notNullValue()));

        String url = makeWinrmConnectionUrl(VAGRANT_HOST);

        String hostName = vagrantHost.getHostName();

        OverthereConnection dest = getOverthereConnection(url, hostName);

        copyFiles(LocalConnection.getLocalConnection(), dest);

        CmdLine jdkInstallCommand = new CmdLine().addRaw(String.format(“%s /quiet”, JDK_EXE_PATH));

        CapturingOverthereExecutionOutputHandler stdOutCapture = capturingHandler();

        CapturingOverthereExecutionOutputHandler stdErrCapture = capturingHandler();

        OverthereExecutionOutputHandler stdOutHandler = stdOutCapture;

        OverthereExecutionOutputHandler stdErrHandler = stdErrCapture;

        int exitCode = dest.execute(stdOutHandler, stdErrHandler, jdkInstallCommand);

        assertThat(exitCode, is(equalTo(0)));

        CmdLine setJavaInPath = new CmdLine().addRaw(“set PATH=\”c:\\Program Files\\Java\\jdk1.7.0_75\\bin\”;%PATH%”);

        exitCode = dest.execute(stdOutHandler, stdErrHandler, setJavaInPath);

        assertThat(exitCode, is(equalTo(0)));

    }

}

That’s it for today. You can view the full sourcecode of the application on Github https://github.com/shekhargulati/blog-installers-automation-test.

Writing your First Technology Book

So I wrote my first book. It is a great feeling to find your book available on Amazon http://www.amazon.com/OpenShift-Cookbook-Shekhar-Gulati/dp/1783981202/. I have read many motivational books or quotes that suggest everyone should write at least one book in their lifetime even if no one reads it. I hope people will find my book useful and learn from it. This blog is not about my book but it is about the lessons I learnt while writing the book.

  1. Limit book scope: As a first time writer it is very tempting to include a lot of related topics in the book outline. I made this mistake in my book outline but as I started writing the book it became clear that I should drop few topics to write a cohesive book. Pay a lot of attention to the book outline and don’t try to put everything in the book. For example, my book is about OpenShift, a platform as a service that supports Java, Python, Node, Ruby, PHP, and Perl runtimes. In my book outline, I proposed to include everything except Perl and Ruby.  After writing few chapters it became clear that we can’t include everything in one book as it would lead to a book that covers lot of topics but none of the topic would be covered in entirety. Keep in mind book outline will change so always try to keep publisher updated on the same.
  2. Your Estimates will be wrong: The book started with the initial estimate of 250 pages and 6 months writing time. We ended with 430 pages and it took more than 10 months to finish. I suggest you take book writing as another software development project. Like most software development projects, book estimates can also be wrong. The better way to write a book is by taking an iterative approach where you re-estimate after writing each chapter. Consider book writing as an Agile software development project rather than a waterfall project.
  3. Plan your book: I suggest if you wish to write a book on a topic then you should first write a blog series on that topic. I have written close to 70 blogs on OpenShift and that helped me a lot. This not only help in content creation but also gives you an audience that would like to read your book. Make sure to tell the publisher in advance that you would use your blogs.
  4. Managing your time: Writing is a very tedious and time consuming task. Writing a technology book involves various tasks like writing book text, creating sample applications, learning new changes introduced in the topic etc.There would be days when you will be productive and write a lot of content and there will be days when you will not be able to contribute much. Be prepared for lean patches as book writing can tire you more quickly than writing code. Initially I used to write book only on weekends but that didn’t worked for me as my writing flow suffered during the week. I tried writing on weekdays after office hours but that too very quickly became tiring. It is very difficult to write a book with office work. Try to plan few weeks holiday to work on the book. I took a couple of weeks off and only worked on the book. This really helped me finish book and maintain my writing flow.
  5. Read books: As a first time writer there will be times when you will be unsure about a topic or how to correctly convey it to the readers. Reading books can help you learn some tricks or improve your writing style that will help you convey your point better to the readers. Try to steal ideas from good books and apply to your book.
  6. Become comfortable with the book format: I wrote a cookbook that required me to use a predefined template. This sometimes restricts you and slow you down.
  7. Keep all book artifacts in a version control system: I used a private Github repository to store all my book artifacts  including content, images, invoices, source code etc.This not only helps as backup and version control system but also helps to analyze your writing habits. For example, the Github repository contribution graph shown below clearly show my writing flow throughout book writing period.book_contributions_over_timeI also used a separate public Github organization https://github.com/OpenShift-Cookbook for the web applications I developed in the book. This helped me keep all book related application source separate from my own Github account.
  8. Plan for software updates: The biggest problem in writing technology books is that technology changes/deprecates very rapidly. There could be a significant change in the software library that would impact your book quality and correctness.

Hope you find these useful when you write your first book.

How to Write Node.js Applications in Python using PythonJS on OpenShift

Today I came across an interesting library called PythonJS that converts Python to JavaScript. It converts to JavaScript and generates JavaScript, CoffeeScript, and Dart code for the given Python code. In this blog you will learn how you can use PythonJS to deploy Python’s Tornado web framework application to OpenShift’s Node 0.10 cartridge. Read the full blog here https://www.openshift.com/blogs/how-to-write-nodejs-applications-in-python-using-pythonjs-on-openshift

Java EE7 Development and Hosting on OpenShift with Wildfly: Part 1

This is the first blog post in a series where I will cover how to build web applications in Java using Java EE 7. Java EE 7 is the latest release of the Java Enterprise Platform, with a focus on HTML 5 and developer productivity.

Java EE 7 introduced five new specifications covering WebSockets (JSR-356), JSON Processing (JSR-353), Batch Applications (JSR-352), Concurrency Utilities (JSR-236), and Caching (JSR-107). Apart from these new specifications, three existing specifications (JAX-RS 2.0JMS 2.0, and EL 3.0) have major updates and a few other specifications have minor updates. We will cover most of these specifications in detail in this blog post series.

Read full blog here https://www.openshift.com/blogs/java-ee7-development-and-hosting-on-openshift-with-wildfly-part-1