Skip to content

Commit

Permalink
Merge pull request #1966 from pedronis/snaptest-support
Browse files Browse the repository at this point in the history
many: avoid snap.InfoFromSnapYaml in tests
  • Loading branch information
niemeyer authored Sep 21, 2016
2 parents c255f59 + 4718fd0 commit 1ca20d5
Show file tree
Hide file tree
Showing 13 changed files with 168 additions and 209 deletions.
7 changes: 3 additions & 4 deletions interfaces/apparmor/backend_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import (
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/apparmor"
"github.com/snapcore/snapd/interfaces/backendtest"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)

Expand Down Expand Up @@ -284,10 +284,9 @@ func (s *backendSuite) TestUpdatingSnapToOneWithFewerHooks(c *C) {
}

func (s *backendSuite) TestRealDefaultTemplateIsNormallyUsed(c *C) {
snapInfo, err := snap.InfoFromSnapYaml([]byte(backendtest.SambaYamlV1))
c.Assert(err, IsNil)
snapInfo := snaptest.MockInfo(c, backendtest.SambaYamlV1, nil)
// NOTE: we don't call apparmor.MockTemplate()
err = s.Backend.Setup(snapInfo, false, s.Repo)
err := s.Backend.Setup(snapInfo, false, s.Repo)
c.Assert(err, IsNil)
profile := filepath.Join(dirs.SnapAppArmorDir, "snap.samba.smbd")
data, err := ioutil.ReadFile(profile)
Expand Down
25 changes: 11 additions & 14 deletions interfaces/backendtest/backendtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
)

type BackendSuite struct {
Expand Down Expand Up @@ -143,30 +144,26 @@ slots:

// InstallSnap "installs" a snap from YAML.
func (s *BackendSuite) InstallSnap(c *C, devMode bool, snapYaml string, revision int) *snap.Info {
snapInfo, err := snap.InfoFromSnapYaml([]byte(snapYaml))
c.Assert(err, IsNil)
// this won't come from snap.yaml
snapInfo.Revision = snap.R(revision)
snapInfo.Developer = "acme"

snapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{
Revision: snap.R(revision),
Developer: "acme",
})
s.addPlugsSlots(c, snapInfo)
err = s.Backend.Setup(snapInfo, devMode, s.Repo)
err := s.Backend.Setup(snapInfo, devMode, s.Repo)
c.Assert(err, IsNil)
return snapInfo
}

// UpdateSnap "updates" an existing snap from YAML.
func (s *BackendSuite) UpdateSnap(c *C, oldSnapInfo *snap.Info, devMode bool, snapYaml string, revision int) *snap.Info {
newSnapInfo, err := snap.InfoFromSnapYaml([]byte(snapYaml))
c.Assert(err, IsNil)
// this won't come from snap.yaml
newSnapInfo.Revision = snap.R(revision)
newSnapInfo.Developer = "acme"

newSnapInfo := snaptest.MockInfo(c, snapYaml, &snap.SideInfo{
Revision: snap.R(revision),
Developer: "acme",
})
c.Assert(newSnapInfo.Name(), Equals, oldSnapInfo.Name())
s.removePlugsSlots(c, oldSnapInfo)
s.addPlugsSlots(c, newSnapInfo)
err = s.Backend.Setup(newSnapInfo, devMode, s.Repo)
err := s.Backend.Setup(newSnapInfo, devMode, s.Repo)
c.Assert(err, IsNil)
return newSnapInfo
}
Expand Down
6 changes: 3 additions & 3 deletions interfaces/builtin/bool_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)

Expand All @@ -54,7 +55,7 @@ var _ = Suite(&BoolFileInterfaceSuite{
})

func (s *BoolFileInterfaceSuite) SetUpTest(c *C) {
info, err := snap.InfoFromSnapYaml([]byte(`
info := snaptest.MockInfo(c, `
name: ubuntu-core
slots:
gpio:
Expand All @@ -74,8 +75,7 @@ slots:
plugs:
plug: bool-file
bad-interface-plug: other-interface
`))
c.Assert(err, IsNil)
`, &snap.SideInfo{})
s.gpioSlot = &interfaces.Slot{SlotInfo: info.Slots["gpio"]}
s.ledSlot = &interfaces.Slot{SlotInfo: info.Slots["led"]}
s.missingPathSlot = &interfaces.Slot{SlotInfo: info.Slots["missing-path"]}
Expand Down
38 changes: 15 additions & 23 deletions interfaces/builtin/browser_support_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
"github.com/snapcore/snapd/testutil"
)

Expand Down Expand Up @@ -67,36 +68,32 @@ func (s *BrowserSupportInterfaceSuite) TestSanitizePlugNoAttrib(c *C) {
}

func (s *BrowserSupportInterfaceSuite) TestSanitizePlugWithAttrib(c *C) {
var mockSnapYaml = []byte(`name: browser-support-plug-snap
const mockSnapYaml = `name: browser-support-plug-snap
version: 1.0
plugs:
browser-support-plug:
interface: browser-support
allow-sandbox: true
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)
`
info := snaptest.MockInfo(c, mockSnapYaml, nil)

plug := &interfaces.Plug{PlugInfo: info.Plugs["browser-support-plug"]}
err = s.iface.SanitizePlug(plug)
err := s.iface.SanitizePlug(plug)
c.Assert(err, IsNil)
}

func (s *BrowserSupportInterfaceSuite) TestSanitizePlugWithBadAttrib(c *C) {
var mockSnapYaml = []byte(`name: browser-support-plug-snap
const mockSnapYaml = `name: browser-support-plug-snap
version: 1.0
plugs:
browser-support-plug:
interface: browser-support
allow-sandbox: bad
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)
`
info := snaptest.MockInfo(c, mockSnapYaml, nil)

plug := &interfaces.Plug{PlugInfo: info.Plugs["browser-support-plug"]}
err = s.iface.SanitizePlug(plug)
err := s.iface.SanitizePlug(plug)
c.Assert(err, Not(IsNil))
c.Assert(err, ErrorMatches, "browser-support plug requires bool with 'allow-sandbox'")
}
Expand All @@ -115,17 +112,15 @@ func (s *BrowserSupportInterfaceSuite) TestConnectedPlugSnippetWithoutAttrib(c *
}

func (s *BrowserSupportInterfaceSuite) TestConnectedPlugSnippetWithAttribFalse(c *C) {
var mockSnapYaml = []byte(`name: browser-support-plug-snap
const mockSnapYaml = `name: browser-support-plug-snap
version: 1.0
plugs:
browser-support-plug:
interface: browser-support
allow-sandbox: false
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)
`

info := snaptest.MockInfo(c, mockSnapYaml, nil)
plug := &interfaces.Plug{PlugInfo: info.Plugs["browser-support-plug"]}

snippet, err := s.iface.ConnectedPlugSnippet(plug, s.slot, interfaces.SecurityAppArmor)
Expand All @@ -141,17 +136,14 @@ plugs:
}

func (s *BrowserSupportInterfaceSuite) TestConnectedPlugSnippetWithAttribTrue(c *C) {
var mockSnapYaml = []byte(`name: browser-support-plug-snap
const mockSnapYaml = `name: browser-support-plug-snap
version: 1.0
plugs:
browser-support-plug:
interface: browser-support
allow-sandbox: true
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
plug := &interfaces.Plug{PlugInfo: info.Plugs["browser-support-plug"]}

snippet, err := s.iface.ConnectedPlugSnippet(plug, s.slot, interfaces.SecurityAppArmor)
Expand Down
85 changes: 31 additions & 54 deletions interfaces/builtin/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import (
"github.com/snapcore/snapd/dirs"
"github.com/snapcore/snapd/interfaces"
"github.com/snapcore/snapd/interfaces/builtin"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snaptest"
)

type ContentSuite struct {
Expand All @@ -43,126 +43,106 @@ func (s *ContentSuite) TestName(c *C) {
}

func (s *ContentSuite) TestSanitizeSlotSimple(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
slots:
content-slot:
interface: content
read:
- shared/read
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
slot := &interfaces.Slot{SlotInfo: info.Slots["content-slot"]}
err = s.iface.SanitizeSlot(slot)
err := s.iface.SanitizeSlot(slot)
c.Assert(err, IsNil)
}

func (s *ContentSuite) TestSanitizeSlotNoPaths(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
slots:
content-slot:
interface: content
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)
`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
slot := &interfaces.Slot{SlotInfo: info.Slots["content-slot"]}

err = s.iface.SanitizeSlot(slot)
err := s.iface.SanitizeSlot(slot)
c.Assert(err, ErrorMatches, "read or write path must be set")
}

func (s *ContentSuite) TestSanitizeSlotEmptyPaths(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
slots:
content-slot:
interface: content
read: []
write: []
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
slot := &interfaces.Slot{SlotInfo: info.Slots["content-slot"]}
err = s.iface.SanitizeSlot(slot)
err := s.iface.SanitizeSlot(slot)
c.Assert(err, ErrorMatches, "read or write path must be set")
}

func (s *ContentSuite) TestSanitizeSlotHasRealtivePath(c *C) {
mockSnapYaml := `name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
slots:
content-slot:
interface: content
`
for _, rw := range []string{"read: [../foo]", "write: [../bar]"} {
info, err := snap.InfoFromSnapYaml([]byte(mockSnapYaml + " " + rw))
c.Assert(err, IsNil)

info := snaptest.MockInfo(c, mockSnapYaml+" "+rw, nil)
slot := &interfaces.Slot{SlotInfo: info.Slots["content-slot"]}
err = s.iface.SanitizeSlot(slot)
err := s.iface.SanitizeSlot(slot)
c.Assert(err, ErrorMatches, "content interface path is not clean:.*")
}
}

func (s *ContentSuite) TestSanitizePlugSimple(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
plugs:
content-plug:
interface: content
target: import
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
plug := &interfaces.Plug{PlugInfo: info.Plugs["content-plug"]}
err = s.iface.SanitizePlug(plug)
err := s.iface.SanitizePlug(plug)
c.Assert(err, IsNil)
}

func (s *ContentSuite) TestSanitizePlugSimpleNoTarget(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
plugs:
content-plug:
interface: content
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
plug := &interfaces.Plug{PlugInfo: info.Plugs["content-plug"]}
err = s.iface.SanitizePlug(plug)
err := s.iface.SanitizePlug(plug)
c.Assert(err, ErrorMatches, "content plug must contain target path")
}

func (s *ContentSuite) TestSanitizePlugSimpleTargetRelative(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
plugs:
content-plug:
interface: content
target: ../foo
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
plug := &interfaces.Plug{PlugInfo: info.Plugs["content-plug"]}
err = s.iface.SanitizePlug(plug)
err := s.iface.SanitizePlug(plug)
c.Assert(err, ErrorMatches, "content interface target path is not clean:.*")
}

func (s *ContentSuite) TestConnectedPlugSnippetSimple(c *C) {
var mockSnapYaml = []byte(`name: content-slot-snap
const mockSnapYaml = `name: content-slot-snap
version: 1.0
slots:
content-slot:
Expand All @@ -175,11 +155,8 @@ plugs:
content-plug:
interface: content
target: import
`)

info, err := snap.InfoFromSnapYaml(mockSnapYaml)
c.Assert(err, IsNil)

`
info := snaptest.MockInfo(c, mockSnapYaml, nil)
slot := &interfaces.Slot{SlotInfo: info.Slots["content-slot"]}
plug := &interfaces.Plug{PlugInfo: info.Plugs["content-plug"]}
content, err := s.iface.ConnectedPlugSnippet(plug, slot, interfaces.SecurityMount)
Expand Down
Loading

0 comments on commit 1ca20d5

Please sign in to comment.