mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
Tests test Validate function
This commit is contained in:
@@ -91,7 +91,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
|
|||||||
|
|
||||||
// Validate validates addLabel and addAnnotation commands.
|
// Validate validates addLabel and addAnnotation commands.
|
||||||
func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
|
func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
|
||||||
|
o.metadata = make(map[string]string)
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return fmt.Errorf("must specify %s", k)
|
return fmt.Errorf("must specify %s", k)
|
||||||
}
|
}
|
||||||
@@ -110,7 +110,6 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
|
|||||||
}
|
}
|
||||||
//parse annotation keys and values into metadata
|
//parse annotation keys and values into metadata
|
||||||
entries := strings.Split(args[0], ",")
|
entries := strings.Split(args[0], ",")
|
||||||
o.metadata = make(map[string]string)
|
|
||||||
for _, entry := range entries {
|
for _, entry := range entries {
|
||||||
kv := strings.Split(entry, ":")
|
kv := strings.Split(entry, ":")
|
||||||
o.metadata[kv[0]] = kv[1]
|
o.metadata[kv[0]] = kv[1]
|
||||||
|
|||||||
@@ -17,34 +17,36 @@ limitations under the License.
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestLabelsValid(t *testing.T) {
|
func TestInputValid(t *testing.T) {
|
||||||
var testcases = []struct {
|
var testcases = []struct {
|
||||||
input string
|
input string
|
||||||
valid bool
|
valid bool
|
||||||
name string
|
name string
|
||||||
|
expectedData map[string]string
|
||||||
}{
|
}{
|
||||||
{
|
|
||||||
input: "owls:great,unicorns:magical",
|
|
||||||
valid: true,
|
|
||||||
name: "Adds two labels",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "owls:great",
|
|
||||||
valid: false,
|
|
||||||
name: "Label keys must be unique",
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
input: "otters:cute",
|
input: "otters:cute",
|
||||||
valid: true,
|
valid: true,
|
||||||
name: "Adds single label",
|
name: "Adds single input",
|
||||||
|
expectedData: map[string]string{
|
||||||
|
"otters": "cute",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "owls:great,unicorns:magical",
|
||||||
|
valid: true,
|
||||||
|
name: "Adds two items",
|
||||||
|
expectedData: map[string]string{
|
||||||
|
"owls": "great",
|
||||||
|
"unicorns": "magical",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "dogs,cats",
|
input: "dogs,cats",
|
||||||
@@ -61,41 +63,52 @@ func TestLabelsValid(t *testing.T) {
|
|||||||
valid: false,
|
valid: false,
|
||||||
name: "Missing value",
|
name: "Missing value",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
input: "exclamation!:point",
|
||||||
|
valid: false,
|
||||||
|
name: "Non-alphanumeric input",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "123:45",
|
||||||
|
valid: true,
|
||||||
|
name: "Numeric input is allowed",
|
||||||
|
expectedData: map[string]string{
|
||||||
|
"123": "45",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "",
|
||||||
|
valid: false,
|
||||||
|
name: "Empty input",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
fakeFS := fs.MakeFakeFS()
|
var o addMetadataOptions
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
|
||||||
cmd := newCmdAddLabel(fakeFS)
|
|
||||||
|
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
labels := strings.Split(tc.input, ",")
|
|
||||||
//run command with test input
|
|
||||||
args := []string{tc.input}
|
args := []string{tc.input}
|
||||||
err := cmd.RunE(cmd, args)
|
err := o.Validate(args, label) //use label since in Validate kindofAdd is only used for error messages
|
||||||
|
|
||||||
if err != nil && tc.valid {
|
if err != nil && tc.valid {
|
||||||
t.Errorf("for test case %s, unexpected cmd error: %v", tc.name, err)
|
t.Errorf("for test case %s, unexpected cmd error: %v", tc.name, err)
|
||||||
}
|
}
|
||||||
if err == nil && !tc.valid {
|
if err == nil && !tc.valid {
|
||||||
t.Errorf("unexpected error: expected invalid annotation format error for test case %v", tc.name)
|
t.Errorf("unexpected error: expected invalid format error for test case %v", tc.name)
|
||||||
}
|
}
|
||||||
if err == nil && (tc.name == "Label keys must be unique") {
|
if err == nil && (tc.name == "Metadata keys must be unique") {
|
||||||
t.Errorf("unexpected error: for test case %s, expected already there problem", tc.name)
|
t.Errorf("unexpected error: for test case %s, expected already there problem", tc.name)
|
||||||
}
|
}
|
||||||
|
|
||||||
content, readErr := fakeFS.ReadFile(constants.KustomizationFileName)
|
//o.metadata should be the same as expectedData
|
||||||
if readErr != nil {
|
if tc.valid {
|
||||||
t.Errorf("unexpected read error: %v", readErr)
|
if !reflect.DeepEqual(o.metadata, tc.expectedData) {
|
||||||
}
|
t.Errorf("unexpeceted error: for test case %s, unexpected data was added", tc.name)
|
||||||
//check if valid input was added to commonLabels
|
}
|
||||||
for _, label := range labels {
|
} else {
|
||||||
key := strings.Split(label, ":")[0]
|
if len(o.metadata) != 0 {
|
||||||
if !strings.Contains(string(content), key) && tc.valid {
|
t.Errorf("unexpeceted error: for test case %s, expected no data to be added", tc.name)
|
||||||
t.Errorf("unexpected error: for test case %s, expected key to be in file.", tc.name)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddLabelNoArgs(t *testing.T) {
|
func TestAddLabelNoArgs(t *testing.T) {
|
||||||
@@ -125,79 +138,6 @@ func TestAddLabelMultipleArgs(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAnnotationsValid(t *testing.T) {
|
|
||||||
var testcases = []struct {
|
|
||||||
input string
|
|
||||||
valid bool
|
|
||||||
name string
|
|
||||||
}{
|
|
||||||
{
|
|
||||||
input: "cats:great,dogs:okay",
|
|
||||||
valid: true,
|
|
||||||
name: "Adds two annotations",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "cats:great",
|
|
||||||
valid: false,
|
|
||||||
name: "Annotation keys must be unique",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "owls:best",
|
|
||||||
valid: true,
|
|
||||||
name: "Adds single annotation",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "cake,cookies",
|
|
||||||
valid: false,
|
|
||||||
name: "Does not contain colon",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: ":hasNoKey",
|
|
||||||
valid: false,
|
|
||||||
name: "Missing key",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "hasNoValue:",
|
|
||||||
valid: false,
|
|
||||||
name: "Missing value",
|
|
||||||
},
|
|
||||||
}
|
|
||||||
fakeFS := fs.MakeFakeFS()
|
|
||||||
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
|
||||||
cmd := newCmdAddAnnotation(fakeFS)
|
|
||||||
|
|
||||||
for _, tc := range testcases {
|
|
||||||
annotations := strings.Split(tc.input, ",")
|
|
||||||
//run command with test input
|
|
||||||
args := []string{tc.input}
|
|
||||||
err := cmd.RunE(cmd, args)
|
|
||||||
|
|
||||||
if err != nil && tc.valid {
|
|
||||||
t.Errorf("for test case %s, unexpected cmd error: %v", tc.name, err)
|
|
||||||
}
|
|
||||||
if err == nil && !tc.valid {
|
|
||||||
t.Errorf("unexpected error: expected invalid annotation format error for test case %v", tc.name)
|
|
||||||
}
|
|
||||||
if err == nil && (tc.name == "Annotation keys must be unique") {
|
|
||||||
t.Errorf("unexpected error: for test case %s, expected already there problem", tc.name)
|
|
||||||
}
|
|
||||||
|
|
||||||
content, readErr := fakeFS.ReadFile(constants.KustomizationFileName)
|
|
||||||
if readErr != nil {
|
|
||||||
t.Errorf("unexpected read error: %v", readErr)
|
|
||||||
}
|
|
||||||
//check if valid input was added to commonAnnotations
|
|
||||||
for _, annotation := range annotations {
|
|
||||||
key := strings.Split(annotation, ":")[0]
|
|
||||||
if !strings.Contains(string(content), key) && tc.valid {
|
|
||||||
t.Errorf("unexpected error: for test case %s, expected key to be in file.", tc.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestAddAnnotationNoArgs(t *testing.T) {
|
func TestAddAnnotationNoArgs(t *testing.T) {
|
||||||
fakeFS := fs.MakeFakeFS()
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user