grim/convey

Add a few additional tests.

2017-09-14, Eric Fritz
c265ab0c2248
Parents 93df1b650a9f
Children 0fc998fd142f
Add a few additional tests.
--- a/loaders/convey/convey.go Thu Sep 14 19:31:03 2017 -0500
+++ b/loaders/convey/convey.go Thu Sep 14 19:38:52 2017 -0500
@@ -51,6 +51,7 @@
type Loader struct {
defaultPlan string
+ depth int
fileLoader func(string, *Loader) (*cConfig.Config, error)
}
@@ -69,7 +70,13 @@
var baseConfig *cConfig.Config
if cfg.Extends != "" {
+ if c.depth >= MaxExtendsDepth {
+ return nil, ErrMaxExtendsDepth
+ }
+
+ c.depth++
baseConfig, err = c.loadFile(cfg.Extends)
+ c.depth--
// We can safely ignore no plans and no tasks errors here
// as we're ensured to also get a valid base config back.
--- a/loaders/convey/errors.go Thu Sep 14 19:31:03 2017 -0500
+++ b/loaders/convey/errors.go Thu Sep 14 19:38:52 2017 -0500
@@ -21,7 +21,12 @@
"errors"
)
+const (
+ MaxExtendsDepth = 50
+)
+
var (
- ErrNoTasks = errors.New("no tasks specified")
- ErrNoPlans = errors.New("no plans specified")
+ ErrNoTasks = errors.New("no tasks specified")
+ ErrNoPlans = errors.New("no plans specified")
+ ErrMaxExtendsDepth = errors.New("exceeded allowed depth for extended configs")
)
--- a/loaders/convey/extends_test.go Thu Sep 14 19:31:03 2017 -0500
+++ b/loaders/convey/extends_test.go Thu Sep 14 19:38:52 2017 -0500
@@ -18,6 +18,8 @@
package convey
import (
+ "fmt"
+
"bitbucket.org/rw_grim/convey/docker"
"github.com/aphistic/sweet"
. "github.com/onsi/gomega"
@@ -168,3 +170,87 @@
Expect(err).To(BeNil())
Expect(loader.defaultPlan).To(Equal("plan2"))
}
+
+func (d *extendsSuite) ExtendTasksOnly(t sweet.T) {
+ baseData := `
+default: plan1
+tasks:
+ foo:
+ image: imaginary1
+`
+
+ extendedData := `
+extends: base.yaml
+plans:
+ plan:
+ stages:
+ - tasks: [foo]
+`
+
+ loader := &Loader{
+ fileLoader: func(name string, c *Loader) (*cConfig.Config, error) {
+ return c.Load(".", name, []byte(baseData))
+ },
+ }
+
+ _, err := loader.Load(".", ".", []byte(extendedData))
+ Expect(err).To(BeNil())
+}
+
+func (d *extendsSuite) ExtendNoPlans(t sweet.T) {
+ baseData := `
+default: plan1
+tasks:
+ foo:
+ image: imaginary1
+`
+
+ extendedData := `
+extends: base.yaml
+tasks:
+ bar:
+ image: imaginary2
+`
+
+ loader := &Loader{
+ fileLoader: func(name string, c *Loader) (*cConfig.Config, error) {
+ return c.Load(".", name, []byte(baseData))
+ },
+ }
+
+ _, err := loader.Load(".", ".", []byte(extendedData))
+ Expect(err).To(Equal(ErrNoTasks))
+}
+
+func (d *extendsSuite) TestLoop(t sweet.T) {
+ sameData := `
+extends: convey.yaml
+`
+
+ loader := &Loader{
+ fileLoader: func(name string, c *Loader) (*cConfig.Config, error) {
+ return c.Load(".", name, []byte(sameData))
+ },
+ }
+
+ _, err := loader.Load(".", ".", []byte(sameData))
+ Expect(err).To(Equal(ErrMaxExtendsDepth))
+}
+
+func (d *extendsSuite) TestFilename(t sweet.T) {
+ data := `
+extends: ../base.yaml
+`
+
+ calledWith := ""
+
+ loader := &Loader{
+ fileLoader: func(name string, c *Loader) (*cConfig.Config, error) {
+ calledWith = name
+ return nil, fmt.Errorf("early out")
+ },
+ }
+
+ loader.Load(".", ".", []byte(data))
+ Expect(calledWith).To(Equal("../base.yaml"))
+}