mirror of
https://github.com/kubernetes-sigs/kustomize.git
synced 2026-06-11 17:12:51 +00:00
support for adding setter substitution
- refactor add setter to include file updates - support add substitution file updates
This commit is contained in:
@@ -4,6 +4,9 @@
|
||||
package setters2
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
@@ -204,3 +207,152 @@ spec:
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
var resourcefile = `apiVersion: resource.dev/v1alpha1
|
||||
kind: resourcefile
|
||||
metadata:
|
||||
name: hello-world-set
|
||||
upstream:
|
||||
type: git
|
||||
git:
|
||||
commit: 5c1c019b59299a4f6c7edd1ff5ff54d720621bbe
|
||||
directory: /package-examples/helloworld-set
|
||||
ref: v0.1.0
|
||||
packageMetadata:
|
||||
shortDescription: example package using setters`
|
||||
|
||||
func TestAdd_Filter2(t *testing.T) {
|
||||
path := filepath.Join(os.TempDir(), "resourcefile")
|
||||
|
||||
//write initial resourcefile to temp path
|
||||
err := ioutil.WriteFile(path, []byte(resourcefile), 0666)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
//add a setter definition
|
||||
sd := SetterDefinition{
|
||||
Name: "image",
|
||||
Value: "1",
|
||||
}
|
||||
|
||||
err = sd.AddToFile(path)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// update setter definition
|
||||
sd2 := SetterDefinition{
|
||||
Name: "image",
|
||||
Value: "2",
|
||||
}
|
||||
|
||||
err = sd2.AddToFile(path)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expected := `apiVersion: resource.dev/v1alpha1
|
||||
kind: resourcefile
|
||||
metadata:
|
||||
name: hello-world-set
|
||||
upstream:
|
||||
type: git
|
||||
git:
|
||||
commit: 5c1c019b59299a4f6c7edd1ff5ff54d720621bbe
|
||||
directory: /package-examples/helloworld-set
|
||||
ref: v0.1.0
|
||||
packageMetadata:
|
||||
shortDescription: example package using setters
|
||||
openAPI:
|
||||
definitions:
|
||||
io.k8s.cli.setters.image:
|
||||
x-k8s-cli:
|
||||
setter:
|
||||
name: image
|
||||
value: "2"
|
||||
`
|
||||
assert.Equal(t, expected, string(b))
|
||||
}
|
||||
|
||||
func TestAddUpdateSubstitution(t *testing.T) {
|
||||
path := filepath.Join(os.TempDir(), "resourcefile")
|
||||
|
||||
//write initial resourcefile to temp path
|
||||
err := ioutil.WriteFile(path, []byte(resourcefile), 0666)
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
value1 := Value{
|
||||
Marker: "IMAGE_NAME",
|
||||
Ref: "#/definitions/io.k8s.cli.setters.image-name",
|
||||
}
|
||||
|
||||
value2 := Value{
|
||||
Marker: "IMAGE_TAG",
|
||||
Ref: "#/definitions/io.k8s.cli.setters.image-tag",
|
||||
}
|
||||
|
||||
values := []Value{value1, value2}
|
||||
|
||||
//add a setter definition
|
||||
subd := SubstitutionDefinition{
|
||||
Name: "image",
|
||||
Pattern: "IMAGE_NAME:IMAGE_TAG",
|
||||
Values: values,
|
||||
}
|
||||
|
||||
err = subd.AddToFile(path)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
// update setter definition
|
||||
subd2 := SubstitutionDefinition{
|
||||
Name: "image",
|
||||
Pattern: "IMAGE_NAME:IMAGE_TAG2",
|
||||
}
|
||||
|
||||
err = subd2.AddToFile(path)
|
||||
|
||||
if !assert.NoError(t, err) {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
b, err := ioutil.ReadFile(path)
|
||||
if err != nil {
|
||||
t.FailNow()
|
||||
}
|
||||
|
||||
expected := `apiVersion: resource.dev/v1alpha1
|
||||
kind: resourcefile
|
||||
metadata:
|
||||
name: hello-world-set
|
||||
upstream:
|
||||
type: git
|
||||
git:
|
||||
commit: 5c1c019b59299a4f6c7edd1ff5ff54d720621bbe
|
||||
directory: /package-examples/helloworld-set
|
||||
ref: v0.1.0
|
||||
packageMetadata:
|
||||
shortDescription: example package using setters
|
||||
openAPI:
|
||||
definitions:
|
||||
io.k8s.cli.substitutions.image:
|
||||
x-k8s-cli:
|
||||
substitution:
|
||||
name: image
|
||||
pattern: IMAGE_NAME:IMAGE_TAG2
|
||||
values: []
|
||||
`
|
||||
assert.Equal(t, expected, string(b))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user