1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
|
package main_test
import (
"fmt"
"os"
"path/filepath"
"gopkg.in/check.v1"
snaprun "github.com/snapcore/snapd/cmd/snap"
"github.com/snapcore/snapd/logger"
)
const packSnapYaml = `name: hello
version: 1.0.1
apps:
app:
command: bin/hello
`
func makeSnapDirForPack(c *check.C, snapYaml string) string {
tempdir := c.MkDir()
c.Assert(os.Chmod(tempdir, 0755), check.IsNil)
// use meta/snap.yaml
metaDir := filepath.Join(tempdir, "meta")
err := os.Mkdir(metaDir, 0755)
c.Assert(err, check.IsNil)
err = os.WriteFile(filepath.Join(metaDir, "snap.yaml"), []byte(snapYaml), 0644)
c.Assert(err, check.IsNil)
return tempdir
}
func makeComponentDirForPack(c *check.C, compYaml string) string {
tempdir := c.MkDir()
c.Assert(os.Chmod(tempdir, 0755), check.IsNil)
metaDir := filepath.Join(tempdir, "meta")
err := os.Mkdir(metaDir, 0755)
c.Assert(err, check.IsNil)
err = os.WriteFile(filepath.Join(metaDir, "component.yaml"), []byte(compYaml), 0644)
c.Assert(err, check.IsNil)
return tempdir
}
func (s *SnapSuite) TestPackCheckSkeletonNoAppFiles(c *check.C) {
_, r := logger.MockLogger()
defer r()
snapDir := makeSnapDirForPack(c, packSnapYaml)
// check-skeleton does not fail due to missing files
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
c.Assert(err, check.IsNil)
}
func (s *SnapSuite) TestPackCheckSkeletonBadMeta(c *check.C) {
// no snap name
snapYaml := `
version: foobar
apps:
`
snapDir := makeSnapDirForPack(c, snapYaml)
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
c.Assert(err, check.ErrorMatches, `cannot validate snap "": snap name cannot be empty`)
}
func (s *SnapSuite) TestPackCheckSkeletonConflictingCommonID(c *check.C) {
// conflicting common-id
snapYaml := `name: foo
version: foobar
apps:
foo:
common-id: org.foo.foo
bar:
common-id: org.foo.foo
`
snapDir := makeSnapDirForPack(c, snapYaml)
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
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")`)
}
func (s *SnapSuite) TestPackCheckSkeletonWonkyInterfaces(c *check.C) {
snapYaml := `
name: foo
version: 1.0.1
slots:
kale:
`
snapDir := makeSnapDirForPack(c, snapYaml)
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--check-skeleton", snapDir})
c.Assert(err, check.IsNil)
c.Check(s.stderr.String(), check.Equals, "snap \"foo\" has bad plugs or slots: kale (unknown interface \"kale\")\n")
}
func (s *SnapSuite) TestPackPacksFailsForMissingPaths(c *check.C) {
_, r := logger.MockLogger()
defer r()
snapDir := makeSnapDirForPack(c, packSnapYaml)
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
// needed files/dirs are tracked in map so order of first error is not guaranteed
c.Assert(err, check.ErrorMatches, `.* snap is unusable due to missing files: path "(bin/hello|bin)" does not exist`)
}
func (s *SnapSuite) TestPackPacksASnap(c *check.C) {
snapDir := makeSnapDirForPack(c, packSnapYaml)
const helloBinContent = `#!/bin/sh
printf "hello world"
`
// an example binary
binDir := filepath.Join(snapDir, "bin")
err := os.Mkdir(binDir, 0755)
c.Assert(err, check.IsNil)
err = os.WriteFile(filepath.Join(binDir, "hello"), []byte(helloBinContent), 0755)
c.Assert(err, check.IsNil)
_, err = snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir, snapDir})
c.Assert(err, check.IsNil)
matches, err := filepath.Glob(snapDir + "/hello*.snap")
c.Assert(err, check.IsNil)
c.Assert(matches, check.HasLen, 1)
}
func (s *SnapSuite) TestPackPacksASnapWithCompressionHappy(c *check.C) {
snapDir := makeSnapDirForPack(c, "name: hello\nversion: 1.0")
for _, comp := range []string{"xz", "lzo"} {
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--compression", comp, snapDir, snapDir})
c.Assert(err, check.IsNil)
matches, err := filepath.Glob(snapDir + "/hello*.snap")
c.Assert(err, check.IsNil)
c.Assert(matches, check.HasLen, 1)
err = os.Remove(matches[0])
c.Assert(err, check.IsNil)
}
}
func (s *SnapSuite) TestPackPacksASnapWithCompressionUnhappy(c *check.C) {
snapDir := makeSnapDirForPack(c, "name: hello\nversion: 1.0")
for _, comp := range []string{"gzip", "zstd", "silly"} {
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", "--compression", comp, snapDir, snapDir})
c.Assert(err, check.ErrorMatches, fmt.Sprintf(`cannot pack "/.*": cannot use compression %q`, comp))
}
}
func (s *SnapSuite) TestPackComponentHappy(c *check.C) {
const compYaml = `component: snap+comp
version: 12a
type: standard
`
_, r := logger.MockLogger()
defer r()
snapDir := makeComponentDirForPack(c, compYaml)
// check-skeleton does not fail due to missing files
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir})
c.Assert(err, check.IsNil)
err = os.Remove("snap+comp_12a.comp")
c.Assert(err, check.IsNil)
}
func (s *SnapSuite) TestPackComponentBadName(c *check.C) {
const compYaml = `component: snapcomp
version: 12a
type: standard
`
_, r := logger.MockLogger()
defer r()
snapDir := makeComponentDirForPack(c, compYaml)
// check-skeleton does not fail due to missing files
_, err := snaprun.Parser(snaprun.Client()).ParseArgs([]string{"pack", snapDir})
c.Assert(err, check.ErrorMatches, `.*: cannot parse component.yaml: incorrect component name "snapcomp"`)
}
|