Skip to content

Commit

Permalink
simplify test code
Browse files Browse the repository at this point in the history
  • Loading branch information
flotter committed Aug 29, 2024
1 parent a1d6300 commit 5d9bef2
Showing 1 changed file with 2 additions and 253 deletions.
255 changes: 2 additions & 253 deletions internals/overlord/planstate/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,52 +45,6 @@ var loadLayers = []string{`
summary: Svc1
override: replace
command: echo svc1
`, `
summary: Layer 2
description: Layer 2 desc.
services:
svc2:
summary: Svc2
override: replace
command: echo svc2
`}

func (ps *planSuite) TestLoadLayers(c *C) {
var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)
// Write layers
for _, l := range loadLayers {
ps.writeLayer(c, string(reindent(l)))
}
// Load the plan from the <pebble-dir>/layers directory
err = ps.planMgr.Load()
c.Assert(err, IsNil)
plan := ps.planMgr.Plan()
out, err := yaml.Marshal(plan)
c.Assert(err, IsNil)
c.Assert(len(plan.Layers), Equals, 2)
c.Assert(string(out), Equals, `
services:
svc1:
summary: Svc1
override: replace
command: echo svc1
svc2:
summary: Svc2
override: replace
command: echo svc2
`[1:])
}

var extLoadLayers = []string{`
summary: Layer 1
description: Layer 1 desc.
services:
svc1:
summary: Svc1
override: replace
command: echo svc1
test-field:
test1:
override: merge
Expand All @@ -109,15 +63,14 @@ var extLoadLayers = []string{`
b: something else
`}

func (ps *planSuite) TestExtLoadLayers(c *C) {
func (ps *planSuite) TestLoadLayers(c *C) {
plan.RegisterExtension(testField, testExtension{})
defer plan.UnregisterExtension(testField)

var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)
// Write layers
for _, l := range extLoadLayers {
for _, l := range loadLayers {
ps.writeLayer(c, string(reindent(l)))
}
// Load the plan from the <pebble-dir>/layers directory
Expand Down Expand Up @@ -146,89 +99,8 @@ test-field:
}

func (ps *planSuite) TestAppendLayers(c *C) {
var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)

// Append a layer when there are no layers.
layer := ps.parseLayer(c, 0, "label1", `
services:
svc1:
override: replace
command: /bin/sh
`)
err = ps.planMgr.AppendLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 1)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/sh
`[1:])
ps.planLayersHasLen(c, 1)

// Try to append a layer when that label already exists.
layer = ps.parseLayer(c, 0, "label1", `
services:
svc1:
override: foobar
command: /bin/bar
`)
err = ps.planMgr.AppendLayer(layer)
c.Assert(err.(*planstate.LabelExists).Label, Equals, "label1")
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/sh
`[1:])
ps.planLayersHasLen(c, 1)

// Append another layer on top.
layer = ps.parseLayer(c, 0, "label2", `
services:
svc1:
override: replace
command: /bin/bash
`)
err = ps.planMgr.AppendLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 2)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/bash
`[1:])
ps.planLayersHasLen(c, 2)

// Append a layer with a different service.
layer = ps.parseLayer(c, 0, "label3", `
services:
svc2:
override: replace
command: /bin/foo
`)
err = ps.planMgr.AppendLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 3)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/bash
svc2:
override: replace
command: /bin/foo
`[1:])
ps.planLayersHasLen(c, 3)
}

func (ps *planSuite) TestExtAppendLayers(c *C) {
plan.RegisterExtension(testField, testExtension{})
defer plan.UnregisterExtension(testField)

var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)
Expand Down Expand Up @@ -344,131 +216,8 @@ test-field:
}

func (ps *planSuite) TestCombineLayers(c *C) {
var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)

// "Combine" layer with no layers should just append.
layer := ps.parseLayer(c, 0, "label1", `
services:
svc1:
override: replace
command: /bin/sh
`)
err = ps.planMgr.CombineLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 1)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/sh
`[1:])
ps.planLayersHasLen(c, 1)

// Combine layer with different label should just append.
layer = ps.parseLayer(c, 0, "label2", `
services:
svc2:
override: replace
command: /bin/foo
`)
err = ps.planMgr.CombineLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 2)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/sh
svc2:
override: replace
command: /bin/foo
`[1:])
ps.planLayersHasLen(c, 2)

// Combine layer with first layer.
layer = ps.parseLayer(c, 0, "label1", `
services:
svc1:
override: replace
command: /bin/bash
`)
err = ps.planMgr.CombineLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 1)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/bash
svc2:
override: replace
command: /bin/foo
`[1:])
ps.planLayersHasLen(c, 2)

// Combine layer with second layer.
layer = ps.parseLayer(c, 0, "label2", `
services:
svc2:
override: replace
command: /bin/bar
`)
err = ps.planMgr.CombineLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 2)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/bash
svc2:
override: replace
command: /bin/bar
`[1:])
ps.planLayersHasLen(c, 2)

// One last append for good measure.
layer = ps.parseLayer(c, 0, "label3", `
services:
svc1:
override: replace
command: /bin/a
svc2:
override: replace
command: /bin/b
`)
err = ps.planMgr.CombineLayer(layer)
c.Assert(err, IsNil)
c.Assert(layer.Order, Equals, 3)
c.Assert(ps.planYAML(c), Equals, `
services:
svc1:
override: replace
command: /bin/a
svc2:
override: replace
command: /bin/b
`[1:])
ps.planLayersHasLen(c, 3)

// Make sure that layer validation is happening.
layer, err = plan.ParseLayer(0, "label4", []byte(`
checks:
bad-check:
override: replace
level: invalid
tcp:
port: 8080
`))
c.Check(err, ErrorMatches, `(?s).*plan check.*must be "alive" or "ready".*`)
}

func (ps *planSuite) TestExtCombineLayers(c *C) {
plan.RegisterExtension(testField, testExtension{})
defer plan.UnregisterExtension(testField)

var err error
ps.planMgr, err = planstate.NewManager(ps.layersDir)
c.Assert(err, IsNil)
Expand Down

0 comments on commit 5d9bef2

Please sign in to comment.