mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Removes semantic validation from addmetadata.go and tests.
Due to moving some input parsing to the Validate method, it was renamed to reflect this additional purpose. Tests were removed where appropriate.
This commit is contained in:
@@ -18,11 +18,11 @@ package commands
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
|
||||||
"strings"
|
"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"
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/validate"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -59,7 +59,7 @@ func newCmdAddAnnotation(fsys fs.FileSystem) *cobra.Command {
|
|||||||
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 {
|
||||||
err := o.Validate(args, annotation)
|
err := o.ValidateAndParse(args, annotation)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -79,7 +79,7 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
|
|||||||
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 {
|
||||||
err := o.Validate(args, label)
|
err := o.ValidateAndParse(args, label)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -89,8 +89,8 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
|
|||||||
return cmd
|
return cmd
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate validates addLabel and addAnnotation commands.
|
// ValidateAndParse validates addLabel and addAnnotation commands and parses them into o.metadata
|
||||||
func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
|
func (o *addMetadataOptions) ValidateAndParse(args []string, k KindOfAdd) error {
|
||||||
o.metadata = make(map[string]string)
|
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)
|
||||||
@@ -100,18 +100,22 @@ func (o *addMetadataOptions) Validate(args []string, k KindOfAdd) error {
|
|||||||
}
|
}
|
||||||
inputs := strings.Split(args[0], ",")
|
inputs := strings.Split(args[0], ",")
|
||||||
for _, input := range inputs {
|
for _, input := range inputs {
|
||||||
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
|
switch k {
|
||||||
if err != nil {
|
case label:
|
||||||
|
valid, err := validate.IsValidLabel(input)
|
||||||
|
if !valid {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if !ok {
|
case annotation:
|
||||||
return fmt.Errorf("invalid %s format: %s", k, input)
|
valid, err := validate.IsValidAnnotation(input)
|
||||||
|
if !valid {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return fmt.Errorf("unknown metadata kind %s", k)
|
||||||
}
|
}
|
||||||
//parse annotation keys and values into metadata
|
//parse annotation keys and values into metadata
|
||||||
entries := strings.Split(args[0], ",")
|
kv := strings.Split(input, ":")
|
||||||
for _, entry := range entries {
|
|
||||||
kv := strings.Split(entry, ":")
|
|
||||||
o.metadata[kv[0]] = kv[1]
|
o.metadata[kv[0]] = kv[1]
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
|||||||
@@ -17,7 +17,6 @@ limitations under the License.
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
@@ -25,12 +24,13 @@ import (
|
|||||||
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestInputValid(t *testing.T) {
|
func TestParseValidateInput(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
|
expectedData map[string]string
|
||||||
|
kind KindOfAdd
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
input: "otters:cute",
|
input: "otters:cute",
|
||||||
@@ -39,6 +39,7 @@ func TestInputValid(t *testing.T) {
|
|||||||
expectedData: map[string]string{
|
expectedData: map[string]string{
|
||||||
"otters": "cute",
|
"otters": "cute",
|
||||||
},
|
},
|
||||||
|
kind: label,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "owls:great,unicorns:magical",
|
input: "owls:great,unicorns:magical",
|
||||||
@@ -48,26 +49,7 @@ func TestInputValid(t *testing.T) {
|
|||||||
"owls": "great",
|
"owls": "great",
|
||||||
"unicorns": "magical",
|
"unicorns": "magical",
|
||||||
},
|
},
|
||||||
},
|
kind: label,
|
||||||
{
|
|
||||||
input: "dogs,cats",
|
|
||||||
valid: false,
|
|
||||||
name: "Does not contain colon",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: ":noKey",
|
|
||||||
valid: false,
|
|
||||||
name: "Missing key",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "noValue:",
|
|
||||||
valid: false,
|
|
||||||
name: "Missing value",
|
|
||||||
},
|
|
||||||
{
|
|
||||||
input: "exclamation!:point",
|
|
||||||
valid: false,
|
|
||||||
name: "Non-alphanumeric input",
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
input: "123:45",
|
input: "123:45",
|
||||||
@@ -76,30 +58,26 @@ func TestInputValid(t *testing.T) {
|
|||||||
expectedData: map[string]string{
|
expectedData: map[string]string{
|
||||||
"123": "45",
|
"123": "45",
|
||||||
},
|
},
|
||||||
|
kind: annotation,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: " ",
|
||||||
|
valid: false,
|
||||||
|
name: "Empty space input",
|
||||||
|
expectedData: nil,
|
||||||
|
kind: annotation,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
var o addMetadataOptions
|
var o addMetadataOptions
|
||||||
for _, tc := range testcases {
|
for _, tc := range testcases {
|
||||||
|
|
||||||
args := []string{tc.input}
|
args := []string{tc.input}
|
||||||
err := o.Validate(args, label) //use label since in Validate kindofAdd is only used for error messages
|
err := o.ValidateAndParse(args, tc.kind)
|
||||||
|
|
||||||
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 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 == "Metadata keys must be unique") {
|
|
||||||
t.Errorf("unexpected error: for test case %s, expected already there problem", tc.name)
|
|
||||||
}
|
|
||||||
if err != nil && (tc.name == "Multiple args") {
|
|
||||||
fmt.Println(err.Error())
|
|
||||||
if err.Error() != "must specify label" {
|
|
||||||
t.Errorf("unexpected error: for test case %s, expected must specify label error", tc.name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
//o.metadata should be the same as expectedData
|
//o.metadata should be the same as expectedData
|
||||||
if tc.valid {
|
if tc.valid {
|
||||||
if !reflect.DeepEqual(o.metadata, tc.expectedData) {
|
if !reflect.DeepEqual(o.metadata, tc.expectedData) {
|
||||||
|
|||||||
Reference in New Issue
Block a user