Skip to content

Commit

Permalink
planstate unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Oct 14, 2024
1 parent 11e246e commit f683d93
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions internals/overlord/planstate/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@ func (m *PlanManager) appendLayer(newLayer *plan.Layer, inner bool) (*plan.Plan,
if layerSubLabel == newSubLabel {
newOrder = layer.Order + 1
newIndex = lastIndex - i + 1
break
}
}

Expand Down
79 changes: 79 additions & 0 deletions internals/overlord/planstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -509,3 +509,82 @@ services:

c.Assert(calls, Equals, 5)
}

func (ps *planSuite) TestAppendLayersNoInner(c *C) {
plan.RegisterSectionExtension(testField, testExtension{})
defer plan.UnregisterSectionExtension(testField)
var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)

layer := ps.parseLayer(c, 0, "foo/bar", "")
err = ps.planMgr.AppendLayer(layer, false)
c.Assert(err, IsNil)
layer = ps.parseLayer(c, 0, "baz", "")
err = ps.planMgr.AppendLayer(layer, false)
c.Assert(err, IsNil)
layer = ps.parseLayer(c, 0, "foo/baz", "")
err = ps.planMgr.AppendLayer(layer, false)
c.Assert(err, ErrorMatches, ".*cannot insert sub-directory.*")
}

func (ps *planSuite) TestAppendLayersOrderAllocation(c *C) {
plan.RegisterSectionExtension(testField, testExtension{})
defer plan.UnregisterSectionExtension(testField)
var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)

appendLabels := []string{
"foo",
"baz/aaa",
"something",
"baz/bbb",
"else",
"baz/ccc",
"zzz/zzz",
"final",
}

for _, label := range appendLabels {
layer := ps.parseLayer(c, 0, label, "")
err = ps.planMgr.AppendLayer(layer, true)
c.Assert(err, IsNil)
}

layersResult := []struct {
label string
order int
}{{
label: "foo",
order: 1000,
}, {
label: "baz/aaa",
order: 2001,
}, {
label: "baz/bbb",
order: 2002,
}, {
label: "baz/ccc",
order: 2003,
}, {
label: "something",
order: 3000,
}, {
label: "else",
order: 4000,
}, {
label: "zzz/zzz",
order: 5001,
}, {
label: "final",
order: 6000,
}}

plan := ps.planMgr.Plan()
// Check the layers order and each layer's order is correct.
for i, layer := range layersResult {
c.Assert(plan.Layers[i].Order, Equals, layer.order)
c.Assert(plan.Layers[i].Label, Equals, layer.label)
}
}

0 comments on commit f683d93

Please sign in to comment.