Merge pull request #1589 from monopole/moreTestsAroundKustFileName

improve tests for alternative kustomization file names
This commit is contained in:
Jeff Regan
2019-10-04 10:22:04 -07:00
committed by GitHub
15 changed files with 61 additions and 43 deletions

View File

@@ -68,7 +68,8 @@ func NewCmdBuild(
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "build {path}", Use: "build {path}",
Short: "Print configuration per contents of " + pgmconfig.KustomizationFileName0, Short: "Print configuration per contents of " +
pgmconfig.DefaultKustomizationFileName(),
Example: examples, Example: examples,
SilenceUsage: true, SilenceUsage: true,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -96,7 +97,8 @@ func NewCmdBuild(
func (o *Options) Validate(args []string) (err error) { func (o *Options) Validate(args []string) (err error) {
if len(args) > 1 { if len(args) > 1 {
return errors.New( return errors.New(
"specify one path to " + pgmconfig.KustomizationFileName0) "specify one path to " +
pgmconfig.DefaultKustomizationFileName())
} }
if len(args) == 0 { if len(args) == 0 {
o.kustomizationPath = loader.CWD o.kustomizationPath = loader.CWD

View File

@@ -26,7 +26,9 @@ func TestBuildValidate(t *testing.T) {
{"file", []string{"beans"}, "beans", ""}, {"file", []string{"beans"}, "beans", ""},
{"path", []string{"a/b/c"}, "a/b/c", ""}, {"path", []string{"a/b/c"}, "a/b/c", ""},
{"path", []string{"too", "many"}, {"path", []string{"too", "many"},
"", "specify one path to " + pgmconfig.KustomizationFileName0}, "",
"specify one path to " +
pgmconfig.DefaultKustomizationFileName()},
} }
for _, mycase := range cases { for _, mycase := range cases {
opts := Options{} opts := Options{}

View File

@@ -162,7 +162,7 @@ func detectResources(fSys fs.FileSystem, uf ifc.KunstructuredFactory, base strin
} }
// If a sub-directory contains an existing kustomization file add the // If a sub-directory contains an existing kustomization file add the
// directory as a resource and do not decend into it. // directory as a resource and do not decend into it.
for _, kfilename := range pgmconfig.KustomizationFileNames() { for _, kfilename := range pgmconfig.RecognizedKustomizationFileNames() {
if fSys.Exists(filepath.Join(path, kfilename)) { if fSys.Exists(filepath.Join(path, kfilename)) {
paths = append(paths, path) paths = append(paths, path)
return filepath.SkipDir return filepath.SkipDir

View File

@@ -47,7 +47,8 @@ func newCmdAddAnnotation(fSys fs.FileSystem, v func(map[string]string) error) *c
o.mapValidator = v o.mapValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "annotation", Use: "annotation",
Short: "Adds one or more commonAnnotations to " + pgmconfig.KustomizationFileName0, Short: "Adds one or more commonAnnotations to " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`, add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -67,7 +68,8 @@ func newCmdAddLabel(fSys fs.FileSystem, v func(map[string]string) error) *cobra.
o.mapValidator = v o.mapValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "label", Use: "label",
Short: "Adds one or more commonLabels to " + pgmconfig.KustomizationFileName0, Short: "Adds one or more commonLabels to " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
add label {labelKey1:labelValue1},{labelKey2:labelValue2}`, add label {labelKey1:labelValue1},{labelKey2:labelValue2}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -47,7 +47,8 @@ func newCmdRemoveAnnotation(fSys fs.FileSystem, v func([]string) error) *cobra.C
o.arrayValidator = v o.arrayValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "annotation", Use: "annotation",
Short: "Removes one or more commonAnnotations from " + pgmconfig.KustomizationFileName0, Short: "Removes one or more commonAnnotations from " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
remove annotation {annotationKey1},{annotationKey2}`, remove annotation {annotationKey1},{annotationKey2}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {
@@ -67,7 +68,8 @@ func newCmdRemoveLabel(fSys fs.FileSystem, v func([]string) error) *cobra.Comman
o.arrayValidator = v o.arrayValidator = v
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "label", Use: "label",
Short: "Removes one or more commonLabels from " + pgmconfig.KustomizationFileName0, Short: "Removes one or more commonLabels from " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
remove label {labelKey1},{labelKey2}`, remove label {labelKey1},{labelKey2}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -25,7 +25,8 @@ func newCmdRemovePatch(fsys fs.FileSystem) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "patch", Use: "patch",
Short: "Removes one or more patches from " + pgmconfig.KustomizationFileName0, Short: "Removes one or more patches from " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
remove patch {filepath}`, remove patch {filepath}`,
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {

View File

@@ -23,7 +23,8 @@ func newCmdRemoveResource(fsys fs.FileSystem) *cobra.Command {
cmd := &cobra.Command{ cmd := &cobra.Command{
Use: "resource", Use: "resource",
Short: "Removes one or more resource file paths from " + pgmconfig.KustomizationFileName0, Short: "Removes one or more resource file paths from " +
pgmconfig.DefaultKustomizationFileName(),
Example: ` Example: `
remove resource my-resource.yml remove resource my-resource.yml
remove resource resource1.yml resource2.yml resource3.yml remove resource resource1.yml resource2.yml resource3.yml

View File

@@ -119,7 +119,7 @@ func NewKustomizationFile(fSys fs.FileSystem) (*kustomizationFile, error) { // n
func (mf *kustomizationFile) validate() error { func (mf *kustomizationFile) validate() error {
match := 0 match := 0
var path []string var path []string
for _, kfilename := range pgmconfig.KustomizationFileNames() { for _, kfilename := range pgmconfig.RecognizedKustomizationFileNames() {
if mf.fSys.Exists(kfilename) { if mf.fSys.Exists(kfilename) {
match += 1 match += 1
path = append(path, kfilename) path = append(path, kfilename)
@@ -128,7 +128,9 @@ func (mf *kustomizationFile) validate() error {
switch match { switch match {
case 0: case 0:
return fmt.Errorf("Missing kustomization file '%s'.\n", pgmconfig.KustomizationFileName0) return fmt.Errorf(
"Missing kustomization file '%s'.\n",
pgmconfig.DefaultKustomizationFileName())
case 1: case 1:
mf.path = path[0] mf.path = path[0]
default: default:
@@ -228,7 +230,6 @@ func (mf *kustomizationFile) marshal(kustomization *types.Kustomization) ([]byte
return content, nil return content, nil
} }
output = append(output, content...) output = append(output, content...)
} }
return output, nil return output, nil
} }

View File

@@ -97,7 +97,7 @@ func TestNewNotExist(t *testing.T) {
} }
} }
func TestSecondarySuffix(t *testing.T) { func TestAllKustomizationFileNames(t *testing.T) {
kcontent := ` kcontent := `
configMapGenerator: configMapGenerator:
- literals: - literals:
@@ -105,16 +105,18 @@ configMapGenerator:
- baz=qux - baz=qux
name: my-configmap name: my-configmap
` `
for _, n := range pgmconfig.RecognizedKustomizationFileNames() {
fSys := fs.MakeFsInMemory() fSys := fs.MakeFsInMemory()
fSys.WriteFile(pgmconfig.KustomizationFileName1, []byte(kcontent)) fSys.WriteFile(n, []byte(kcontent))
k, err := NewKustomizationFile(fSys) k, err := NewKustomizationFile(fSys)
if err != nil { if err != nil {
t.Fatalf("Unexpected Error: %v", err) t.Fatalf("Unexpected Error: %v", err)
} }
if k.path != pgmconfig.KustomizationFileName1 { if k.path != n {
t.Fatalf("Load incorrect file path %s", k.path) t.Fatalf("Load incorrect file path %s", k.path)
} }
} }
}
func TestPreserveComments(t *testing.T) { func TestPreserveComments(t *testing.T) {
kustomizationContentWithComments := []byte( kustomizationContentWithComments := []byte(

View File

@@ -38,10 +38,10 @@ func WriteTestKustomization(fSys fs.FileSystem) {
// WriteTestKustomizationWith writes content to a well known file name. // WriteTestKustomizationWith writes content to a well known file name.
func WriteTestKustomizationWith(fSys fs.FileSystem, bytes []byte) { func WriteTestKustomizationWith(fSys fs.FileSystem, bytes []byte) {
fSys.WriteFile(pgmconfig.KustomizationFileName0, bytes) fSys.WriteFile(pgmconfig.DefaultKustomizationFileName(), bytes)
} }
// ReadTestKustomization reads content from a well known file name. // ReadTestKustomization reads content from a well known file name.
func ReadTestKustomization(fSys fs.FileSystem) ([]byte, error) { func ReadTestKustomization(fSys fs.FileSystem) ([]byte, error) {
return fSys.ReadFile(pgmconfig.KustomizationFileName0) return fSys.ReadFile(pgmconfig.DefaultKustomizationFileName())
} }

View File

@@ -74,7 +74,10 @@ func (th *KustTestHarness) WriteF(dir string, content string) {
} }
func (th *KustTestHarness) WriteK(dir string, content string) { func (th *KustTestHarness) WriteK(dir string, content string) {
th.WriteF(filepath.Join(dir, pgmconfig.KustomizationFileName0), ` th.WriteF(
filepath.Join(
dir,
pgmconfig.DefaultKustomizationFileName()), `
apiVersion: kustomize.config.k8s.io/v1beta1 apiVersion: kustomize.config.k8s.io/v1beta1
kind: Kustomization kind: Kustomization
`+content) `+content)

View File

@@ -389,7 +389,8 @@ func TestNewLoaderAtGitClone(t *testing.T) {
fSys.MkdirAll(coRoot) fSys.MkdirAll(coRoot)
fSys.MkdirAll(coRoot + "/" + pathInRepo) fSys.MkdirAll(coRoot + "/" + pathInRepo)
fSys.WriteFile( fSys.WriteFile(
coRoot+"/"+pathInRepo+"/"+pgmconfig.KustomizationFileName0, coRoot+"/"+pathInRepo+"/"+
pgmconfig.DefaultKustomizationFileName(),
[]byte(` []byte(`
whatever whatever
`)) `))

View File

@@ -4,22 +4,23 @@
// Package pgmconfig holds global constants for the kustomize tool. // Package pgmconfig holds global constants for the kustomize tool.
package pgmconfig package pgmconfig
// KustomizationFileNames is a list of filenames // RecognizedKustomizationFileNames is a list of file names
// that kustomize recognizes. // that kustomize recognizes.
// To avoid ambiguity, a directory cannot contain // To avoid ambiguity, a kustomization directory may not
// more than one match to this list. // contain more than one match to this list.
func KustomizationFileNames() []string { func RecognizedKustomizationFileNames() []string {
return []string{ return []string{
KustomizationFileName0, "kustomization.yaml",
KustomizationFileName1, "kustomization.yml",
KustomizationFileName2} "Kustomization",
}
}
func DefaultKustomizationFileName() string {
return RecognizedKustomizationFileNames()[0]
} }
const ( const (
KustomizationFileName0 = "kustomization.yaml"
KustomizationFileName1 = "kustomization.yml"
KustomizationFileName2 = "Kustomization"
// An environment variable to consult for kustomization // An environment variable to consult for kustomization
// configuration data. See: // configuration data. See:
// https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html

View File

@@ -81,7 +81,7 @@ func commaOr(q []string) string {
func loadKustFile(ldr ifc.Loader) ([]byte, error) { func loadKustFile(ldr ifc.Loader) ([]byte, error) {
var content []byte var content []byte
match := 0 match := 0
for _, kf := range pgmconfig.KustomizationFileNames() { for _, kf := range pgmconfig.RecognizedKustomizationFileNames() {
c, err := ldr.Load(kf) c, err := ldr.Load(kf)
if err == nil { if err == nil {
match += 1 match += 1
@@ -92,7 +92,7 @@ func loadKustFile(ldr ifc.Loader) ([]byte, error) {
case 0: case 0:
return nil, fmt.Errorf( return nil, fmt.Errorf(
"unable to find one of %v in directory '%s'", "unable to find one of %v in directory '%s'",
commaOr(quoted(pgmconfig.KustomizationFileNames())), commaOr(quoted(pgmconfig.RecognizedKustomizationFileNames())),
ldr.Root()) ldr.Root())
case 1: case 1:
return content, nil return content, nil