Skip to content

Commit

Permalink
Merge pull request #5401 from mvo5/kernel-track-in-model
Browse files Browse the repository at this point in the history
asserts: add (optional) kernel-track to model assertion
  • Loading branch information
mvo5 committed Jul 16, 2018
2 parents 19d3614 + 13f39ca commit 0c91754
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
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.
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

0 comments on commit 0c91754

Please sign in to comment.