github.com/chipaca/snappy@v0.0.0-20210104084008-1f06296fe8ad/cmd/snap/cmd_pack_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  
     9  	"gopkg.in/check.v1"
    10  
    11  	snaprun "github.com/snapcore/snapd/cmd/snap"
    12  	"github.com/snapcore/snapd/logger"
    13  )
    14  
    15  const packSnapYaml = `name: hello
    16  version: 1.0.1
    17  apps:
    18   app:
    19    command: bin/hello
    20  `
    21  
    22  func makeSnapDirForPack(c *check.C, snapYaml string) string {
    23  	tempdir := c.MkDir()
    24  	c.Assert(os.Chmod(tempdir, 0755), check.IsNil)
    25  
    26  	// use meta/snap.yaml
    27  	metaDir := filepath.Join(tempdir, "meta")
    28  	err := os.Mkdir(metaDir, 0755)
    29  	c.Assert(err, check.IsNil)
    30  	err = ioutil.WriteFile(filepath.Join(metaDir, "snap.yaml"), []byte(snapYaml), 0644)
    31  	c.Assert(err, check.IsNil)
    32  
    33  	return tempdir
    34  }
    35  
    36  func (s *SnapSuite) TestPackCheckSkeletonNoAppFiles(c *check.C) {
    37  	_, r := logger.MockLogger()
    38  	defer r()
    39  
    40  	snapDir := makeSnapDirForPack(c, packSnapYaml)
    41  
    42  	// check-skeleton does not fail due to missing files
    43  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    44  	c.Assert(err, check.IsNil)
    45  }
    46  
    47  func (s *SnapSuite) TestPackCheckSkeletonBadMeta(c *check.C) {
    48  	// no snap name
    49  	snapYaml := `
    50  version: foobar
    51  apps:
    52  `
    53  	snapDir := makeSnapDirForPack(c, snapYaml)
    54  
    55  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    56  	c.Assert(err, check.ErrorMatches, `cannot validate snap "": snap name cannot be empty`)
    57  }
    58  
    59  func (s *SnapSuite) TestPackCheckSkeletonConflictingCommonID(c *check.C) {
    60  	// conflicting common-id
    61  	snapYaml := `name: foo
    62  version: foobar
    63  apps:
    64    foo:
    65      common-id: org.foo.foo
    66    bar:
    67      common-id: org.foo.foo
    68  `
    69  	snapDir := makeSnapDirForPack(c, snapYaml)
    70  
    71  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    72  	c.Assert(err, check.ErrorMatches, `cannot validate snap "foo": application ("bar" common-id "org.foo.foo" must be unique, already used by application "foo"|"foo" common-id "org.foo.foo" must be unique, already used by application "bar")`)
    73  }
    74  
    75  func (s *SnapSuite) TestPackCheckSkeletonWonkyInterfaces(c *check.C) {
    76  	snapYaml := `
    77  name: foo
    78  version: 1.0.1
    79  slots:
    80    kale:
    81  `
    82  	snapDir := makeSnapDirForPack(c, snapYaml)
    83  
    84  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    85  	c.Assert(err, check.IsNil)
    86  	c.Check(s.stderr.String(), check.Equals, "snap \"foo\" has bad plugs or slots: kale (unknown interface \"kale\")\n")
    87  }
    88  
    89  func (s *SnapSuite) TestPackPacksFailsForMissingPaths(c *check.C) {
    90  	_, r := logger.MockLogger()
    91  	defer r()
    92  
    93  	snapDir := makeSnapDirForPack(c, packSnapYaml)
    94  
    95  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
    96  	c.Assert(err, check.ErrorMatches, ".* snap is unusable due to missing files")
    97  }
    98  
    99  func (s *SnapSuite) TestPackPacksASnap(c *check.C) {
   100  	snapDir := makeSnapDirForPack(c, packSnapYaml)
   101  
   102  	const helloBinContent = `#!/bin/sh
   103  printf "hello world"
   104  `
   105  	// an example binary
   106  	binDir := filepath.Join(snapDir, "bin")
   107  	err := os.Mkdir(binDir, 0755)
   108  	c.Assert(err, check.IsNil)
   109  	err = ioutil.WriteFile(filepath.Join(binDir, "hello"), []byte(helloBinContent), 0755)
   110  	c.Assert(err, check.IsNil)
   111  
   112  	_, err = snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
   113  	c.Assert(err, check.IsNil)
   114  
   115  	matches, err := filepath.Glob(snapDir + "/hello*.snap")
   116  	c.Assert(err, check.IsNil)
   117  	c.Assert(matches, check.HasLen, 1)
   118  }
   119  
   120  func (s *SnapSuite) TestPackPacksASnapWithCompressionHappy(c *check.C) {
   121  	snapDir := makeSnapDirForPack(c, "name: hello\nversion: 1.0")
   122  
   123  	for _, comp := range []string{"xz", "lzo"} {
   124  		_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--compression", comp, snapDir, snapDir})
   125  		c.Assert(err, check.IsNil)
   126  
   127  		matches, err := filepath.Glob(snapDir + "/hello*.snap")
   128  		c.Assert(err, check.IsNil)
   129  		c.Assert(matches, check.HasLen, 1)
   130  		err = os.Remove(matches[0])
   131  		c.Assert(err, check.IsNil)
   132  	}
   133  }
   134  
   135  func (s *SnapSuite) TestPackPacksASnapWithCompressionUnhappy(c *check.C) {
   136  	snapDir := makeSnapDirForPack(c, "name: hello\nversion: 1.0")
   137  
   138  	for _, comp := range []string{"gzip", "zstd", "silly"} {
   139  		_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--compression", comp, snapDir, snapDir})
   140  		c.Assert(err, check.ErrorMatches, fmt.Sprintf(`cannot pack "/.*": cannot use compression %q`, comp))
   141  	}
   142  }