-
Notifications
You must be signed in to change notification settings - Fork 601
/
Copy pathvalidate.go
134 lines (118 loc) · 3.4 KB
/
validate.go
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
// -*- Mode: Go; indent-tabs-mode: t -*-
/*
* Copyright (C) 2014-2020 Canonical Ltd
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package seed
import (
"bytes"
"fmt"
"path/filepath"
"sort"
"github.com/snapcore/snapd/snap"
"github.com/snapcore/snapd/snap/snapfile"
"github.com/snapcore/snapd/timings"
)
type ValidationError struct {
// SystemErrors maps system labels ("" for UC16/18) to their validation errors.
SystemErrors map[string][]error
}
func newValidationError(label string, err error) *ValidationError {
return &ValidationError{SystemErrors: map[string][]error{
label: {err},
}}
}
func (e *ValidationError) addErr(label string, errs ...error) {
if e.SystemErrors == nil {
e.SystemErrors = make(map[string][]error)
}
for _, err := range errs {
e.SystemErrors[label] = append(e.SystemErrors[label], err)
}
}
func (e ValidationError) hasErrors() bool {
return len(e.SystemErrors) != 0
}
func (e *ValidationError) Error() string {
systems := make([]string, 0, len(e.SystemErrors))
for s := range e.SystemErrors {
systems = append(systems, s)
}
sort.Strings(systems)
var buf bytes.Buffer
first := true
for _, s := range systems {
if first {
if s == "" {
fmt.Fprintf(&buf, "cannot validate seed:")
} else {
fmt.Fprintf(&buf, "cannot validate seed system %q:", s)
}
} else {
fmt.Fprintf(&buf, "\nand seed system %q:", s)
}
for _, err := range e.SystemErrors[s] {
fmt.Fprintf(&buf, "\n - %s", err)
}
}
return buf.String()
}
// ValidateFromYaml validates the given seed.yaml file and surrounding seed.
func ValidateFromYaml(seedYamlFile string) error {
// TODO:UC20: support validating also one or multiple UC20 seed systems
// introduce ListSystems ?
// What about full empty seed dir?
seedDir := filepath.Dir(seedYamlFile)
seed, err := Open(seedDir, "")
if err != nil {
return newValidationError("", err)
}
if err := seed.LoadAssertions(nil, nil); err != nil {
return newValidationError("", err)
}
tm := timings.New(nil)
if err := seed.LoadMeta(AllModes, nil, tm); err != nil {
if missingErr, ok := err.(*essentialSnapMissingError); ok {
if seed.Model().Classic() && missingErr.SnapName == "core" {
err = fmt.Errorf("essential snap core or snapd must be part of the seed")
}
}
return newValidationError("", err)
}
ve := &ValidationError{}
// read the snap infos
snapInfos := make([]*snap.Info, 0, seed.NumSnaps())
seed.Iter(func(sn *Snap) error {
snapf, err := snapfile.Open(sn.Path)
if err != nil {
ve.addErr("", err)
} else {
info, err := snap.ReadInfoFromSnapFile(snapf, sn.SideInfo)
if err != nil {
ve.addErr("", fmt.Errorf("cannot use snap %q: %v", sn.Path, err))
} else {
snapInfos = append(snapInfos, info)
}
}
return nil
})
if errs2 := snap.ValidateBasesAndProviders(snapInfos); errs2 != nil {
ve.addErr("", errs2...)
}
if ve.hasErrors() {
return ve
}
return nil
}