mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-05-21 14:32:03 +00:00
[doc]: https://github.com/golang/go/wiki/Modules#releasing-modules-v2-or-higher Per this Go modules [doc] a repo or branch that's already tagged v2 or higher should increment the major version (e.g. go to v3) when releasing their first Go module-based packages. At the moment, the kustomize repo has these top level packages in the sigs.k8s.io/kustomize module: - `cmd` - holds main program for kustomize Conceivably someone can depend on this package for integration tests. - `internal` - intentionally unreleased subpackages - `k8sdeps` - an adapter wrapping k8s dependencies This exists only for use in pre-Go-modules kustomize-into-kubectl integration and won't live much longer (as everything involved is switching to Go modules). - `pkg` - kustomize packages for export This should shrink in later versions, since the surface area is too large, containing sub-packages that should be in 'internal'. - `plugin` - holds main programs for plugins This PR changes the top level go.mod file from ``` module sigs.k8s.io/kustomize ``` to ``` module sigs.k8s.io/kustomize/v3 ``` and adjusts all import statements to reflect the change.
181 lines
4.7 KiB
Go
181 lines
4.7 KiB
Go
/*
|
|
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 transformer
|
|
|
|
import (
|
|
"reflect"
|
|
"strings"
|
|
"testing"
|
|
|
|
"sigs.k8s.io/kustomize/v3/k8sdeps/kunstruct"
|
|
"sigs.k8s.io/kustomize/v3/pkg/gvk"
|
|
"sigs.k8s.io/kustomize/v3/pkg/resid"
|
|
"sigs.k8s.io/kustomize/v3/pkg/resmaptest"
|
|
"sigs.k8s.io/kustomize/v3/pkg/resource"
|
|
)
|
|
|
|
var deploy = gvk.Gvk{Group: "apps", Version: "v1", Kind: "Deployment"}
|
|
|
|
func TestJsonPatchJSONTransformer_Transform(t *testing.T) {
|
|
rf := resource.NewFactory(
|
|
kunstruct.NewKunstructuredFactoryImpl())
|
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
|
Add(map[string]interface{}{
|
|
"apiVersion": "apps/v1",
|
|
"kind": "Deployment",
|
|
"metadata": map[string]interface{}{
|
|
"name": "deploy1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"template": map[string]interface{}{
|
|
"metadata": map[string]interface{}{
|
|
"labels": map[string]interface{}{
|
|
"old-label": "old-value",
|
|
},
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"containers": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "nginx",
|
|
"image": "nginx",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}).ResMap()
|
|
|
|
operations := []byte(`[
|
|
{"op": "replace", "path": "/spec/template/spec/containers/0/name", "value": "my-nginx"},
|
|
{"op": "add", "path": "/spec/replica", "value": "3"},
|
|
{"op": "add", "path": "/spec/template/spec/containers/0/command", "value": ["arg1", "arg2", "arg3"]}
|
|
]`)
|
|
|
|
expected := resmaptest_test.NewRmBuilder(t, rf).
|
|
Add(map[string]interface{}{
|
|
"apiVersion": "apps/v1",
|
|
"kind": "Deployment",
|
|
"metadata": map[string]interface{}{
|
|
"name": "deploy1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"replica": "3",
|
|
"template": map[string]interface{}{
|
|
"metadata": map[string]interface{}{
|
|
"labels": map[string]interface{}{
|
|
"old-label": "old-value",
|
|
},
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"containers": []interface{}{
|
|
map[string]interface{}{
|
|
"image": "nginx",
|
|
"name": "my-nginx",
|
|
"command": []interface{}{
|
|
"arg1",
|
|
"arg2",
|
|
"arg3",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}).ResMap()
|
|
|
|
patchId := m.GetByIndex(0).OrgId()
|
|
|
|
jpt, err := newPatchJson6902JSONTransformer(patchId, operations)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error : %v", err)
|
|
}
|
|
err = jpt.Transform(m)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if !reflect.DeepEqual(m, expected) {
|
|
err = expected.ErrorIfNotEqualSets(m)
|
|
t.Fatalf("actual doesn't match expected: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestJsonPatchJSONTransformer_UnHappyTransform(t *testing.T) {
|
|
rf := resource.NewFactory(
|
|
kunstruct.NewKunstructuredFactoryImpl())
|
|
m := resmaptest_test.NewRmBuilder(t, rf).
|
|
Add(map[string]interface{}{
|
|
"apiVersion": "apps/v1",
|
|
"kind": "Deployment",
|
|
"metadata": map[string]interface{}{
|
|
"name": "deploy1",
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"template": map[string]interface{}{
|
|
"metadata": map[string]interface{}{
|
|
"labels": map[string]interface{}{
|
|
"old-label": "old-value",
|
|
},
|
|
},
|
|
"spec": map[string]interface{}{
|
|
"containers": []interface{}{
|
|
map[string]interface{}{
|
|
"name": "nginx",
|
|
"image": "nginx",
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}).ResMap()
|
|
|
|
operations := []byte(`[
|
|
{"op": "add", "path": "/spec/template/spec/containers/0/command/", "value": ["arg1", "arg2", "arg3"]}
|
|
]`)
|
|
|
|
jpt, err := newPatchJson6902JSONTransformer(
|
|
m.GetByIndex(0).OrgId(), operations)
|
|
if err != nil {
|
|
t.Fatalf("unexpected error : %v", err)
|
|
}
|
|
err = jpt.Transform(m)
|
|
if err == nil {
|
|
t.Fatalf("expected error didn't happen")
|
|
}
|
|
if !strings.HasPrefix(
|
|
err.Error(), "failed to apply json patch") ||
|
|
!strings.Contains(err.Error(), string(operations)) {
|
|
t.Fatalf("expected error didn't happen, but got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestJsonPatchJSONTransformer_EmptyPatchFile(t *testing.T) {
|
|
id := resid.NewResId(deploy, "deploy1")
|
|
operations := []byte(``)
|
|
|
|
_, err := newPatchJson6902JSONTransformer(id, operations)
|
|
|
|
if err == nil {
|
|
t.Fatalf("expected an error")
|
|
}
|
|
|
|
if err != nil {
|
|
if !strings.HasPrefix(err.Error(), "json patch file is empty") {
|
|
t.Fatalf("expected %s, but got %v", "json patch file is empty", err)
|
|
}
|
|
}
|
|
}
|