Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Commit

Permalink
Test that duplicates are detected in more scenarios
Browse files Browse the repository at this point in the history
To wit:
 - when a manifest appears in a YAML file, and is also generated
 - when a manifest is generated by two different configs
  • Loading branch information
squaremo committed Nov 13, 2019
1 parent 0475fe6 commit ad813e8
Showing 1 changed file with 95 additions and 18 deletions.
113 changes: 95 additions & 18 deletions pkg/manifests/configaware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,49 @@ import (
"github.com/fluxcd/flux/pkg/resource"
)

// represents, for the purpose of setting a test up, either a
// - path to a .flux.yaml: `config{path: ..., fluxyaml: ...}`
// - a .flux.yaml for the whole "repo": `config{fluxyaml: ...}`
type config struct {
path string
fluxyaml string
}

// set a directory up with the given `--git-path` arguments
// (subpaths), and locations in which to put config files. The paths
// and the config locations don't necessarily have to line up. You can
// pass `nil` for the paths, to indicate "just use the base path".
func setup(t *testing.T, paths []string, configs ...config) (*configAware, string, func()) {
manifests := kubernetes.NewManifests(kubernetes.ConstNamespacer("default"), log.NewLogfmtLogger(os.Stdout))
baseDir, cleanup := testfiles.TempDir(t)

// te constructor NewConfigAware expects at least one absolute path.
var searchPaths []string
for _, p := range paths {
searchPaths = append(searchPaths, filepath.Join(baseDir, p))
}
if len(paths) == 0 {
searchPaths = []string{baseDir}
}

for _, c := range configs {
p := c.path
if p == "" {
p = "."
}
if len(c.fluxyaml) > 0 {
err := os.MkdirAll(filepath.Join(baseDir, p), 0777)
assert.NoError(t, err)
ioutil.WriteFile(filepath.Join(baseDir, p, ConfigFilename), []byte(c.fluxyaml), 0600)
}
}
frs, err := NewConfigAware(baseDir, searchPaths, manifests)
assert.NoError(t, err)
return frs, baseDir, cleanup
}

// ---

func TestFindConfigFilePaths(t *testing.T) {
baseDir, clean := testfiles.TempDir(t)
defer clean()
Expand Down Expand Up @@ -103,17 +146,6 @@ commandUpdated:
assert.Equal(t, configFiles[0].CommandUpdated.Generators, configFiles[0].CommandUpdated.Generators)
}

func setup(t *testing.T, configFileBody string) (*configAware, func()) {
manifests := kubernetes.NewManifests(kubernetes.ConstNamespacer("default"), log.NewLogfmtLogger(os.Stdout))
baseDir, cleanup := testfiles.TempDir(t)
if len(configFileBody) > 0 {
ioutil.WriteFile(filepath.Join(baseDir, ConfigFilename), []byte(configFileBody), 0600)
}
frs, err := NewConfigAware(baseDir, []string{baseDir}, manifests)
assert.NoError(t, err)
return frs, cleanup
}

const commandUpdatedEchoConfigFile = `---
version: 1
commandUpdated:
Expand All @@ -140,7 +172,7 @@ commandUpdated:
`

func TestCommandUpdatedConfigFile(t *testing.T) {
frs, cleanup := setup(t, commandUpdatedEchoConfigFile)
frs, _, cleanup := setup(t, nil, config{fluxyaml: commandUpdatedEchoConfigFile})
defer cleanup()
ctx := context.Background()
resources, err := frs.GetAllResourcesByID(ctx)
Expand Down Expand Up @@ -185,7 +217,7 @@ patchUpdated:
`

func TestPatchUpdatedConfigFile(t *testing.T) {
frs, cleanup := setup(t, patchUpdatedEchoConfigFile)
frs, _, cleanup := setup(t, nil, config{fluxyaml: patchUpdatedEchoConfigFile})
defer cleanup()
ctx := context.Background()
resources, err := frs.GetAllResourcesByID(ctx)
Expand Down Expand Up @@ -258,7 +290,7 @@ commandUpdated: # <-- because this is commandUpdated, patchFile is ignored
// update operation results in an error, rather than a silent failure
// to make any changes.
func TestMistakenConfigFile(t *testing.T) {
frs, cleanup := setup(t, mistakenConf)
frs, _, cleanup := setup(t, nil, config{fluxyaml: mistakenConf})
defer cleanup()

deploymentID := resource.MustParseID("default:deployment/helloworld")
Expand All @@ -269,9 +301,7 @@ func TestMistakenConfigFile(t *testing.T) {
assert.Error(t, err)
}

func TestDuplicateDetection(t *testing.T) {
// this one has the same resource twice in the generated manifests
conf, cleanup := setup(t, `
const duplicateGeneration = `
version: 1
commandUpdated:
generators:
Expand Down Expand Up @@ -300,11 +330,58 @@ commandUpdated:
metadata:
name: demo
"
`)
`

func TestDuplicateDetection(t *testing.T) {
// this one has the same resource twice in the generated manifests
conf, _, cleanup := setup(t, nil, config{fluxyaml: duplicateGeneration})
defer cleanup()

res, err := conf.GetAllResourcesByID(context.Background())
assert.Nil(t, res)
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate")
}

// A de-indented version of the deployment manifest echoed in the
// patchUpdatedEchoConfigFile above, to be written to a file.
const helloManifest = `
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: helloworld
spec:
template:
metadata:
labels:
name: helloworld
spec:
containers:
- name: greeter
image: quay.io/weaveworks/helloworld:master-a000001
`

func TestDuplicateInFiles(t *testing.T) {
// this one tests that a manifest that's in a file as well as generated is detected as a duplicate
frs, baseDir, cleanup := setup(t, []string{".", "echo"}, config{path: "echo", fluxyaml: patchUpdatedEchoConfigFile})
defer cleanup()
ioutil.WriteFile(filepath.Join(baseDir, "manifest.yaml"), []byte(helloManifest), 0666)

res, err := frs.GetAllResourcesByID(context.Background())
assert.Nil(t, res)
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate")
}

func TestDuplicateInGenerators(t *testing.T) {
// this one tests that a manifest that's generated by two different generator configs
frs, _, cleanup := setup(t, []string{"echo1", "echo2"},
config{path: "echo1", fluxyaml: patchUpdatedEchoConfigFile},
config{path: "echo2", fluxyaml: patchUpdatedEchoConfigFile})
defer cleanup()

res, err := frs.GetAllResourcesByID(context.Background())
assert.Nil(t, res)
assert.Error(t, err)
assert.Contains(t, err.Error(), "duplicate")
}

0 comments on commit ad813e8

Please sign in to comment.