mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-12 01:14:22 +00:00
Add tests
This commit is contained in:
@@ -19,8 +19,8 @@ package commands
|
|||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"strings"
|
|
||||||
"regexp"
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
|
||||||
@@ -64,7 +64,7 @@ func (o *addAnnotationOptions) Validate(args []string) error {
|
|||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
return errors.New("annotations must be comma-separated, with no spaces. See help text for example.")
|
return errors.New("annotations must be comma-separated, with no spaces. See help text for example.")
|
||||||
}
|
}
|
||||||
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)
|
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -95,7 +95,7 @@ func (o *addAnnotationOptions) RunAddAnnotation(fsys fs.FileSystem) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.CommonAnnotations == nil{
|
if m.CommonAnnotations == nil {
|
||||||
m.CommonAnnotations = make(map[string]string)
|
m.CommonAnnotations = make(map[string]string)
|
||||||
}
|
}
|
||||||
annotations := strings.Split(o.annotations, ",")
|
annotations := strings.Split(o.annotations, ",")
|
||||||
|
|||||||
@@ -15,3 +15,112 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestAnnotationsValid(t *testing.T) {
|
||||||
|
var testcases = []struct {
|
||||||
|
input string
|
||||||
|
valid bool
|
||||||
|
name string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
input: "owls:great,unicorns:magical",
|
||||||
|
valid: true,
|
||||||
|
name: "Adds two annotations",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "owls:great",
|
||||||
|
valid: false,
|
||||||
|
name: "Annotation keys must be unique",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "otters:cute",
|
||||||
|
valid: true,
|
||||||
|
name: "Adds single annotation",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "dogs,cats",
|
||||||
|
valid: false,
|
||||||
|
name: "Does not contain colon",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: ":noKey",
|
||||||
|
valid: false,
|
||||||
|
name: "Missing key",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
input: "noValue:",
|
||||||
|
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) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
|
||||||
|
cmd := newCmdAddAnnotation(fakeFS)
|
||||||
|
err := cmd.Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error but error is %v", err)
|
||||||
|
}
|
||||||
|
if err != nil && err.Error() != "must specify an annotation" {
|
||||||
|
t.Errorf("incorrect error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddAnnotationMultipleArgs(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
cmd := newCmdAddAnnotation(fakeFS)
|
||||||
|
args := []string{"this:input", "has:spaces"}
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error but error is %v", err)
|
||||||
|
}
|
||||||
|
if err != nil && err.Error() != "annotations must be comma-separated, with no spaces. See help text for example." {
|
||||||
|
t.Errorf("incorrect error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -17,9 +17,9 @@ limitations under the License.
|
|||||||
package commands
|
package commands
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"regexp"
|
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@@ -57,19 +57,14 @@ func newCmdAddLabel(fsys fs.FileSystem) *cobra.Command {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Validate validates addLabel command.
|
// Validate validates addLabel command.
|
||||||
// TODO: make sure label is of correct format key:value
|
|
||||||
func (o *addLabelOptions) Validate(args []string) error {
|
func (o *addLabelOptions) Validate(args []string) error {
|
||||||
for _, arg := range args {
|
|
||||||
fmt.Println(arg + "****")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(args) < 1 {
|
if len(args) < 1 {
|
||||||
return errors.New("must specify a label")
|
return errors.New("must specify a label")
|
||||||
}
|
}
|
||||||
if len(args) > 1 {
|
if len(args) > 1 {
|
||||||
return errors.New("labels must be comma-separated, with no spaces. See help text for example.")
|
return errors.New("labels must be comma-separated, with no spaces. See help text for example.")
|
||||||
}
|
}
|
||||||
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)
|
ok, err := regexp.MatchString(`\A([a-zA-Z0-9_.-]+):([a-zA-Z0-9_.-]+)\z`, input)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -102,7 +97,7 @@ func (o *addLabelOptions) RunAddLabel(fsys fs.FileSystem) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if m.CommonLabels == nil{
|
if m.CommonLabels == nil {
|
||||||
m.CommonLabels = make(map[string]string)
|
m.CommonLabels = make(map[string]string)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,3 +15,112 @@ limitations under the License.
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
package commands
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/constants"
|
||||||
|
"github.com/kubernetes-sigs/kustomize/pkg/fs"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestLabelsValid(t *testing.T) {
|
||||||
|
var testcases = []struct {
|
||||||
|
input string
|
||||||
|
valid bool
|
||||||
|
name 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",
|
||||||
|
valid: true,
|
||||||
|
name: "Adds single 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",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
cmd := newCmdAddLabel(fakeFS)
|
||||||
|
|
||||||
|
for _, tc := range testcases {
|
||||||
|
labels := 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 == "Label 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 commonLabels
|
||||||
|
for _, label := range labels {
|
||||||
|
key := strings.Split(label, ":")[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 TestAddLabelNoArgs(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
|
||||||
|
cmd := newCmdAddLabel(fakeFS)
|
||||||
|
err := cmd.Execute()
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error but error is: %v", err)
|
||||||
|
}
|
||||||
|
if err != nil && err.Error() != "must specify a label" {
|
||||||
|
t.Errorf("incorrect error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestAddLabelMultipleArgs(t *testing.T) {
|
||||||
|
fakeFS := fs.MakeFakeFS()
|
||||||
|
fakeFS.WriteFile(constants.KustomizationFileName, []byte(kustomizationContent))
|
||||||
|
cmd := newCmdAddLabel(fakeFS)
|
||||||
|
args := []string{"this:input", "has:spaces"}
|
||||||
|
err := cmd.RunE(cmd, args)
|
||||||
|
if err == nil {
|
||||||
|
t.Errorf("expected an error but error is: %v", err)
|
||||||
|
}
|
||||||
|
if err != nil && err.Error() != "labels must be comma-separated, with no spaces. See help text for example." {
|
||||||
|
t.Errorf("incorrect error: %v", err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -96,15 +96,12 @@ func newCmdAdd(fsys fs.FileSystem) *cobra.Command {
|
|||||||
# Adds one or more base directories to the kustomization
|
# Adds one or more base directories to the kustomization
|
||||||
kustomize edit add base <filepath>
|
kustomize edit add base <filepath>
|
||||||
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
|
kustomize edit add base <filepath1>,<filepath2>,<filepath3>
|
||||||
<<<<<<< HEAD
|
|
||||||
=======
|
|
||||||
|
|
||||||
# Adds one or more commonLabels to the kustomization
|
# Adds one or more commonLabels to the kustomization
|
||||||
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
|
kustomize edit add label {labelKey1:labelValue1},{labelKey2:labelValue2}
|
||||||
|
|
||||||
# Adds one or more commonAnnotations to the kustomization
|
# Adds one or more commonAnnotations to the kustomization
|
||||||
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
|
kustomize edit add annotation {annotationKey1:annotationValue1},{annotationKey2:annotationValue2}
|
||||||
>>>>>>> Implements labels and annotations as subcommands of edit
|
|
||||||
`,
|
`,
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user