mardi 16 décembre 2014

Mageia 5 systemd-nspawn

A killer feature of systemd is systemd-nspawn. From the man page, "Spawn a namespace container for debugging, testing and building", it is a kind of chroot on steroids, I feel, not limited to "testing and building", but with some care, can be used for production apps. There is little or no resources on how to use this tool on Mageia linux distribution which integrates systemd since a long time (Mandriva integrated Systemd before Mageia fork even exists).

Archlinux provides a rich set of docs which are a good practical starting point. This blog entry mostly adapt the systemd container pages to Mageia


The main differences between Archlinux and Mageia regarding these documents are the packaging system and the network configuration. The Mageia chroot page explain how to populate a directory (lets use a btrfs one) with base system with minimum configuration.

Lets suppose you have a directory named "mageia", first add all media:
  # urpmi.addmedia --distrib --urpmi-root mageia 
  ftp://mirror.netcologne.de/mageia/distrib/cauldron/x86_64/

Then install basesystem and other packages needed for your application:
  # urpmi  --urpmi-root mageia basesystem
  # urpmi  --urpmi-root mageia java

You have to create a regular user (i.e. other than root, which you can log with) and set the root passwd in the chroot environment (I did not succeed to log directly as root).

Log as root without booting
  # cd mageia
  # systemd-nspawn -D .

Change the root passwd in the chroot 
  # passwd

Add regular user in chroot and exit
  # createuser test
  # exit

You can now launch the container and log in using the regular user you just created
   # systemd-nspawn -bD .

To adapt network configuration from the Archlinux page, I do have to remove the default route that was created for br0 on Magiea. This is depending on your configuration, so it is better to check if nothing strange appears in your route after having started systemd-networkd service.

You can even copy the chroot directory on a Debian installation to use your Mageia chroot on your Debian (and vice-versa). 

Grails webflow plugin integration test sample

It is difficult to find resources up to date about how to write Grails Webflow integration tests. You would lose precious time reading internet entries you would better invest in writing integration tests.

I am using Grails 2.4.3, the latest Webflow plugin and Spring security plugin which I want to test along with the flow.

package reportbyorigin

import grails.test.mixin.TestMixin
import grails.test.mixin.webflow.WebFlowUnitTestMixin

import org.springframework.security.authentication.UsernamePasswordAuthenticationToken
import org.springframework.security.core.Authentication
import org.springframework.security.core.context.SecurityContextHolder

import spock.lang.*
import taackaccess.User

@TestMixin(WebFlowUnitTestMixin)
class AllControllerSpec extends GroovyTestCase {

    void setUp() {
        User admin = User.findByUsername("admin")
        Authentication auth =
          new UsernamePasswordAuthenticationToken(admin, null)
          SecurityContextHolder.context.authentication = auth
    }

    def cleanup() {
    }

    void "test flow user instance"() {
        mockController(AllController)
  
        when: "user login"
            statEntryFlow.putDatasOnFlow.action()

        then: "test flow values"
            flow.loggedUser instanceof User
    }
}

The tricky parts are "void setUp()" instead of "def setup()", "extends GroovyTestCase" instead of all you can read on the internet, "mockController(<yourcontroller>)".

Hope this sample will help someone.