How can I iterate more quickly when creating a snap?

I am working on a snap of a bigger piece of software and building the snap, removing the old one, installing the new one just takes a very long time.

I would like to iterate over creating the snap and testing things out more quickly. I know that the snap builds just fine, now I'm interested in testing things like launching the app and its integration. Is there a way to do this?


Solution 1:

Preface

One way to go about this is to use snap try. From its --help message:

The try command installs an unpacked snap into the system for testing
purposes. The unpacked snap content continues to be used even after 
installation, so non-metadata changes there go live instantly. Metadata
changes such as those performed in snap.yaml will require reinstallation 
to go live.

It also comes with an option:

--devmode     Install in development mode and disable confinement

This is particularly helpful if you want to test confinement bits and getting the use of interfaces right.

Usage

The way to use this is simple, just run:

snapcraft prime
snap try prime/

In the first step you tell snapcraft to do the

Final copy and preparation for the snap.

And in the second step snapd makes use of the contents of the directory to "install" the snap. Now you can play around with the package while still being able to make changes to it.

Example

Let's use an example from the Snappy Playpen to show how to use this.

We are using consul as it's relatively quick to build and use:

$ git clone https://github.com/ubuntu/snappy-playpen
$ cd snappy-playpen/consul/
snappy-playpen/consul$ snapcraft prime
Preparing to pull consul 
[...]
Staging consul 
Priming consul 
snappy-playpen/consul$ 

Now let's make sure that we don't have another version installed of it and then proceed with the installation.

snappy-playpen/consul$ which consul
snappy-playpen/consul$ snap try prime

Name    Version  Rev  Developer  Notes
consul  0.6.4    x1              try
snappy-playpen/consul$ consul --help
usage: consul [--version] [--help] <command> [<args>]

Available commands are:
    agent          Runs a Consul agent
[...]
snappy-playpen/consul$ 

So far so good. It seems to work just fine. Now let's replace it with /bin/echo just to see how things work:

snappy-playpen/consul$ cp /bin/echo prime/bin/consul 
snappy-playpen/consul$ consul --help
Usage: /snap/consul/x1/bin/consul [SHORT-OPTION]... [STRING]...
   or:  /snap/consul/x1/bin/consul LONG-OPTION
Echo the STRING(s) to standard output.
[...]
daniel@daydream:/tmp/test/snappy-playpen/consul$  

As you can see, we can make changes to the live system in prime while still running things under confinement. This is generally a good way to get the snap up and running and fully tested quickly.

Note: With snapd before 2.0.10 (2016-07-09), you had to temove the snap before running snapcraft clean or snapd would get into a bad state and whine about being unable to find mounted snaps. Some more fixes are lined up for 2.0.11, which should land in a few days as well.