Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

asserts: add (optional) kernel-track to model assertion #5401

Merged
merged 2 commits into from
Jul 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 14 additions & 0 deletions asserts/device_asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ func (mod *Model) Kernel() string {
return mod.HeaderString("kernel")
}

// KernelTrack returns the kernel track the model uses.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we want also a call to checkOptionalString(assert.headers, "kernel-track") in assembleModel

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pedronis fixed and pushed, please have a look

func (mod *Model) KernelTrack() string {
return mod.HeaderString("kernel-track")
}

// Base returns the base snap the model uses.
func (mod *Model) Base() string {
return mod.HeaderString("base")
Expand Down Expand Up @@ -185,6 +190,9 @@ func assembleModel(assert assertionBase) (Assertion, error) {
if _, ok := assert.headers["kernel"]; ok {
return nil, fmt.Errorf("cannot specify a kernel with a classic model")
}
if _, ok := assert.headers["kernel-track"]; ok {
return nil, fmt.Errorf("cannot specify kernel-track with a classic model")
}
if _, ok := assert.headers["base"]; ok {
return nil, fmt.Errorf("cannot specify a base with a classic model")
}
Expand All @@ -203,6 +211,12 @@ func assembleModel(assert assertionBase) (Assertion, error) {
}
}

// kernel-track is optional but must be a string.
_, err = checkOptionalString(assert.headers, "kernel-track")
if err != nil {
return nil, err
}

// store is optional but must be a string, defaults to the ubuntu store
_, err = checkOptionalString(assert.headers, "store")
if err != nil {
Expand Down
19 changes: 19 additions & 0 deletions asserts/device_asserts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const (
"gadget: brand-gadget\n" +
"base: core18\n" +
"kernel: baz-linux\n" +
"kernel-track: 4.15\n" +
"store: brand-store\n" +
sysUserAuths +
reqSnaps +
Expand Down Expand Up @@ -102,6 +103,7 @@ func (mods *modelSuite) TestDecodeOK(c *C) {
c.Check(model.Architecture(), Equals, "amd64")
c.Check(model.Gadget(), Equals, "brand-gadget")
c.Check(model.Kernel(), Equals, "baz-linux")
c.Check(model.KernelTrack(), Equals, "4.15")
c.Check(model.Base(), Equals, "core18")
c.Check(model.Store(), Equals, "brand-store")
c.Check(model.RequiredSnaps(), DeepEquals, []string{"foo", "bar"})
Expand All @@ -123,6 +125,21 @@ func (mods *modelSuite) TestDecodeStoreIsOptional(c *C) {
c.Check(model.Store(), Equals, "")
}

func (mods *modelSuite) TestDecodeKernelTrackIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "kernel-track: 4.15\n", "kernel-track: \n", 1)
a, err := asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model := a.(*asserts.Model)
c.Check(model.KernelTrack(), Equals, "")

encoded = strings.Replace(withTimestamp, "kernel-track: 4.15\n", "", 1)
a, err = asserts.Decode([]byte(encoded))
c.Assert(err, IsNil)
model = a.(*asserts.Model)
c.Check(model.KernelTrack(), Equals, "")
}

func (mods *modelSuite) TestDecodeBaseIsOptional(c *C) {
withTimestamp := strings.Replace(modelExample, "TSLINE", mods.tsLine, 1)
encoded := strings.Replace(withTimestamp, "base: core18\n", "base: \n", 1)
Expand Down Expand Up @@ -205,6 +222,7 @@ func (mods *modelSuite) TestDecodeInvalid(c *C) {
{"gadget: brand-gadget\n", "gadget: \n", `"gadget" header should not be empty`},
{"kernel: baz-linux\n", "", `"kernel" header is mandatory`},
{"kernel: baz-linux\n", "kernel: \n", `"kernel" header should not be empty`},
{"kernel-track: 4.15\n", "kernel-track:\n - 123\n", `"kernel-track" header must be a string`},
{"store: brand-store\n", "store:\n - xyz\n", `"store" header must be a string`},
{mods.tsLine, "", `"timestamp" header is mandatory`},
{mods.tsLine, "timestamp: \n", `"timestamp" header should not be empty`},
Expand Down Expand Up @@ -284,6 +302,7 @@ func (mods *modelSuite) TestClassicDecodeInvalid(c *C) {
{"architecture: amd64\n", "architecture:\n - foo\n", `"architecture" header must be a string`},
{"gadget: brand-gadget\n", "gadget:\n - foo\n", `"gadget" header must be a string`},
{"gadget: brand-gadget\n", "kernel: brand-kernel\n", `cannot specify a kernel with a classic model`},
{"gadget: brand-gadget\n", "kernel-track: edge\n", `cannot specify kernel-track with a classic model`},
{"gadget: brand-gadget\n", "base: some-base\n", `cannot specify a base with a classic model`},
}

Expand Down