summaryrefslogtreecommitdiff
path: root/snap
diff options
authorPawel Stolowski <stolowski@gmail.com>2019-08-19 11:16:36 +0200
committerPawel Stolowski <stolowski@gmail.com>2019-08-19 11:16:36 +0200
commitca5026caad50bb10cd75283b410e3943708a5137 (patch)
tree051bfed76ea4d4272539f1ea9f1829c6a865715a /snap
parentb60b05e6dddaca79a8fd602511d44c7e69146357 (diff)
Moved the method to validate bases and poviders to snap package and renamed to ValidateBasesAndProviders.
Diffstat (limited to 'snap')
-rw-r--r--snap/validate.go40
-rw-r--r--snap/validate_test.go16
2 files changed, 56 insertions, 0 deletions
diff --git a/snap/validate.go b/snap/validate.go
index 148fa2578a..7c24b56821 100644
--- a/snap/validate.go
+++ b/snap/validate.go
@@ -951,3 +951,43 @@ func ValidateSystemUsernames(info *Info) error {
}
return nil
}
+
+// neededDefaultProviders returns the names of all default-providers for
+// the content plugs that the given snap.Info needs.
+func NeededDefaultProviders(info *Info) (cps []string) {
+ for _, plug := range info.Plugs {
+ if plug.Interface == "content" {
+ var dprovider string
+ if err := plug.Attr("default-provider", &dprovider); err == nil && dprovider != "" {
+ cps = append(cps, dprovider)
+ }
+ }
+ }
+ return cps
+}
+
+// ValidateBasesAndProviders checks that all bases/default-providers are part of the seed
+func ValidateBasesAndProviders(snapInfos map[string]*Info) []error {
+ var errs []error
+ for _, info := range snapInfos {
+ // ensure base is available
+ if info.Base != "" && info.Base != "none" {
+ if _, ok := snapInfos[info.Base]; !ok {
+ errs = append(errs, fmt.Errorf("cannot use snap %q: base %q is missing", info.InstanceName(), info.Base))
+ }
+ }
+ // ensure core is available
+ if info.Base == "" && info.SnapType == TypeApp && info.InstanceName() != "snapd" {
+ if _, ok := snapInfos["core"]; !ok {
+ errs = append(errs, fmt.Errorf(`cannot use snap %q: required snap "core" missing`, info.InstanceName()))
+ }
+ }
+ // ensure default-providers are available
+ for _, dp := range NeededDefaultProviders(info) {
+ if _, ok := snapInfos[dp]; !ok {
+ errs = append(errs, fmt.Errorf("cannot use snap %q: default provider %q is missing", info.InstanceName(), dp))
+ }
+ }
+ }
+ return errs
+}
diff --git a/snap/validate_test.go b/snap/validate_test.go
index ecd73bd61b..de74e2c856 100644
--- a/snap/validate_test.go
+++ b/snap/validate_test.go
@@ -1574,3 +1574,19 @@ system-usernames:
err = Validate(info)
c.Assert(err, ErrorMatches, `invalid system username "b@d"`)
}
+
+func (s *ValidateSuite) TestNeededDefaultProviders(c *C) {
+ const yaml = `name: need-df
+version: 1.0
+plugs:
+ gtk-3-themes:
+ interface: content
+ default-provider: gtk-common-themes
+`
+ strk := NewScopedTracker()
+ info, err := InfoFromSnapYamlWithSideInfo([]byte(yaml), nil, strk)
+ c.Assert(err, IsNil)
+
+ dps := NeededDefaultProviders(info)
+ c.Check(dps, DeepEquals, []string{"gtk-common-themes"})
+}