github.com/rigado/snapd@v2.42.5-go-mod+incompatible/cmd/snap/cmd_pack_test.go (about)

     1  package main_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	"gopkg.in/check.v1"
     9  
    10  	snaprun "github.com/snapcore/snapd/cmd/snap"
    11  	"github.com/snapcore/snapd/logger"
    12  )
    13  
    14  const packSnapYaml = `name: hello
    15  version: 1.0.1
    16  apps:
    17   app:
    18    command: bin/hello
    19  `
    20  
    21  func makeSnapDirForPack(c *check.C, snapYaml string) string {
    22  	tempdir := c.MkDir()
    23  	c.Assert(os.Chmod(tempdir, 0755), check.IsNil)
    24  
    25  	// use meta/snap.yaml
    26  	metaDir := filepath.Join(tempdir, "meta")
    27  	err := os.Mkdir(metaDir, 0755)
    28  	c.Assert(err, check.IsNil)
    29  	err = ioutil.WriteFile(filepath.Join(metaDir, "snap.yaml"), []byte(snapYaml), 0644)
    30  	c.Assert(err, check.IsNil)
    31  
    32  	return tempdir
    33  }
    34  
    35  func (s *SnapSuite) TestPackCheckSkeletonNoAppFiles(c *check.C) {
    36  	_, r := logger.MockLogger()
    37  	defer r()
    38  
    39  	snapDir := makeSnapDirForPack(c, packSnapYaml)
    40  
    41  	// check-skeleton does not fail due to missing files
    42  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    43  	c.Assert(err, check.IsNil)
    44  }
    45  
    46  func (s *SnapSuite) TestPackCheckSkeletonBadMeta(c *check.C) {
    47  	// no snap name
    48  	snapYaml := `
    49  version: foobar
    50  apps:
    51  `
    52  	snapDir := makeSnapDirForPack(c, snapYaml)
    53  
    54  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    55  	c.Assert(err, check.ErrorMatches, `cannot validate snap "": snap name cannot be empty`)
    56  }
    57  
    58  func (s *SnapSuite) TestPackCheckSkeletonConflictingCommonID(c *check.C) {
    59  	// conflicting common-id
    60  	snapYaml := `name: foo
    61  version: foobar
    62  apps:
    63    foo:
    64      common-id: org.foo.foo
    65    bar:
    66      common-id: org.foo.foo
    67  `
    68  	snapDir := makeSnapDirForPack(c, snapYaml)
    69  
    70  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
    71  	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")`)
    72  }
    73  
    74  func (s *SnapSuite) TestPackPacksFailsForMissingPaths(c *check.C) {
    75  	_, r := logger.MockLogger()
    76  	defer r()
    77  
    78  	snapDir := makeSnapDirForPack(c, packSnapYaml)
    79  
    80  	_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
    81  	c.Assert(err, check.ErrorMatches, ".* snap is unusable due to missing files")
    82  }
    83  
    84  func (s *SnapSuite) TestPackPacksASnap(c *check.C) {
    85  	snapDir := makeSnapDirForPack(c, packSnapYaml)
    86  
    87  	const helloBinContent = `#!/bin/sh
    88  printf "hello world"
    89  `
    90  	// an example binary
    91  	binDir := filepath.Join(snapDir, "bin")
    92  	err := os.Mkdir(binDir, 0755)
    93  	c.Assert(err, check.IsNil)
    94  	err = ioutil.WriteFile(filepath.Join(binDir, "hello"), []byte(helloBinContent), 0755)
    95  	c.Assert(err, check.IsNil)
    96  
    97  	_, err = snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
    98  	c.Assert(err, check.IsNil)
    99  
   100  	matches, err := filepath.Glob(snapDir + "/hello*.snap")
   101  	c.Assert(err, check.IsNil)
   102  	c.Assert(matches, check.HasLen, 1)
   103  }