mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Improve command package isolation.
This commit is contained in:
227
pkg/commands/kustfile/kustomizationfile_test.go
Normal file
227
pkg/commands/kustfile/kustomizationfile_test.go
Normal file
@@ -0,0 +1,227 @@
|
||||
/*
|
||||
Copyright 2018 The Kubernetes Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
*/
|
||||
|
||||
package kustfile
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"sigs.k8s.io/kustomize/pkg/constants"
|
||||
"sigs.k8s.io/kustomize/pkg/fs"
|
||||
"sigs.k8s.io/kustomize/pkg/types"
|
||||
)
|
||||
|
||||
func TestWriteAndRead(t *testing.T) {
|
||||
kustomization := &types.Kustomization{
|
||||
NamePrefix: "prefix",
|
||||
}
|
||||
|
||||
fsys := fs.MakeFakeFS()
|
||||
fsys.Create(constants.KustomizationFileName)
|
||||
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
|
||||
if err := mf.Write(kustomization); err != nil {
|
||||
t.Fatalf("Couldn't write kustomization file: %v\n", err)
|
||||
}
|
||||
|
||||
content, err := mf.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("Couldn't read kustomization file: %v\n", err)
|
||||
}
|
||||
if !reflect.DeepEqual(kustomization, content) {
|
||||
t.Fatal("Read kustomization is different from written kustomization")
|
||||
}
|
||||
}
|
||||
|
||||
func TestEmptyFile(t *testing.T) {
|
||||
fsys := fs.MakeFakeFS()
|
||||
_, err := NewKustomizationFile("", fsys)
|
||||
if err == nil {
|
||||
t.Fatalf("Create kustomizationFile from empty filename should fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewNotExist(t *testing.T) {
|
||||
badSuffix := "foo.bar"
|
||||
fakeFS := fs.MakeFakeFS()
|
||||
fakeFS.Mkdir(".")
|
||||
fakeFS.Create(badSuffix)
|
||||
_, err := NewKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
contained := "Missing kustomization file"
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = NewKustomizationFile(constants.KustomizationFileName, fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
_, err = NewKustomizationFile(badSuffix, fakeFS)
|
||||
if err == nil {
|
||||
t.Fatalf("expect an error")
|
||||
}
|
||||
contained = "should have .yaml suffix"
|
||||
if !strings.Contains(err.Error(), contained) {
|
||||
t.Fatalf("expect an error contains %q, but got %v", contained, err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreserveComments(t *testing.T) {
|
||||
kustomizationContentWithComments := []byte(
|
||||
`# shem qing some comments
|
||||
# This is some comment we should preserve
|
||||
# don't delete it
|
||||
resources:
|
||||
- pod.yaml
|
||||
- service.yaml
|
||||
# something you may want to keep
|
||||
vars:
|
||||
- fieldref:
|
||||
fieldPath: metadata.name
|
||||
name: MY_SERVICE_NAME
|
||||
objref:
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
name: my-service
|
||||
bases:
|
||||
- ../namespaces
|
||||
# some descriptions for the patches
|
||||
patchesStrategicMerge:
|
||||
- service.yaml
|
||||
- pod.yaml
|
||||
`)
|
||||
fsys := fs.MakeFakeFS()
|
||||
fsys.Create(constants.KustomizationFileName)
|
||||
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
||||
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
kustomization, err := mf.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if err = mf.Write(kustomization); err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
bytes, _ := fsys.ReadFile(mf.path)
|
||||
|
||||
if !reflect.DeepEqual(kustomizationContentWithComments, bytes) {
|
||||
t.Fatal("written kustomization with comments is not the same as original one")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPreserveCommentsWithAdjust(t *testing.T) {
|
||||
kustomizationContentWithComments := []byte(`
|
||||
|
||||
|
||||
|
||||
# shem qing some comments
|
||||
# This is some comment we should preserve
|
||||
# don't delete it
|
||||
resources:
|
||||
- pod.yaml
|
||||
# See which field this comment goes into
|
||||
- service.yaml
|
||||
|
||||
APIVersion: v1beta1
|
||||
kind: kustomization.yaml
|
||||
|
||||
# something you may want to keep
|
||||
vars:
|
||||
- fieldref:
|
||||
fieldPath: metadata.name
|
||||
name: MY_SERVICE_NAME
|
||||
objref:
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
name: my-service
|
||||
|
||||
BASES:
|
||||
- ../namespaces
|
||||
|
||||
# some descriptions for the patches
|
||||
|
||||
patchesStrategicMerge:
|
||||
- service.yaml
|
||||
- pod.yaml
|
||||
`)
|
||||
|
||||
expected := []byte(`
|
||||
|
||||
|
||||
|
||||
# shem qing some comments
|
||||
# This is some comment we should preserve
|
||||
# don't delete it
|
||||
# See which field this comment goes into
|
||||
resources:
|
||||
- pod.yaml
|
||||
- service.yaml
|
||||
|
||||
apiVersion: v1beta1
|
||||
kind: kustomization.yaml
|
||||
|
||||
# something you may want to keep
|
||||
vars:
|
||||
- fieldref:
|
||||
fieldPath: metadata.name
|
||||
name: MY_SERVICE_NAME
|
||||
objref:
|
||||
apiVersion: v1
|
||||
kind: Service
|
||||
name: my-service
|
||||
|
||||
bases:
|
||||
- ../namespaces
|
||||
|
||||
# some descriptions for the patches
|
||||
|
||||
patchesStrategicMerge:
|
||||
- service.yaml
|
||||
- pod.yaml
|
||||
`)
|
||||
fsys := fs.MakeFakeFS()
|
||||
fsys.Create(constants.KustomizationFileName)
|
||||
fsys.WriteFile(constants.KustomizationFileName, kustomizationContentWithComments)
|
||||
mf, err := NewKustomizationFile(constants.KustomizationFileName, fsys)
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
|
||||
kustomization, err := mf.Read()
|
||||
if err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
if err = mf.Write(kustomization); err != nil {
|
||||
t.Fatalf("Unexpected Error: %v", err)
|
||||
}
|
||||
bytes, _ := fsys.ReadFile(mf.path)
|
||||
|
||||
if !reflect.DeepEqual(expected, bytes) {
|
||||
t.Fatal("written kustomization with comments is not the same as original one\n", string(bytes))
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user