Add more tests and explain some strange quotes.

This commit is contained in:
monopole
2021-01-13 13:03:22 -08:00
parent cf8815b0a0
commit bb41d018b5
11 changed files with 111 additions and 92 deletions

View File

@@ -4,13 +4,33 @@
package krusty_test
import (
"fmt"
"testing"
kusttest_test "sigs.k8s.io/kustomize/api/testutils/kusttest"
)
func TestBasicIO1(t *testing.T) {
func TestBasicIO_1(t *testing.T) {
th := kusttest_test.MakeHarness(t)
opts := th.MakeDefaultOptions()
if !opts.UseKyaml {
// This test won't pass under apimachinery, because in the bowels of
// that code (see GetAnnotations in v0.17.0 of
// k8s.io/apimachinery/pkg/apis/meta/v1/unstructured/unstructured.go)
// an error returned from NestedStringMap is discarded, and an
// empty annotation map is silently returned, making this test fail
// The swallowed error arises from code like:
// var v interface{}
// v = true
// if str, ok := v.(string); ok {
// save the value in a map (doesn't happen)
// } else {
// return an error (that is then ignored by GetAnnotations)
// }
// The error happens when any annotation value can be interpreted as
// a boolean or number.
t.SkipNow()
}
th.WriteK(".", `
resources:
- service.yaml
@@ -27,7 +47,8 @@ metadata:
spec:
clusterIP: None
`)
m := th.Run(".", th.MakeDefaultOptions())
m := th.Run(".", opts)
// The annotations are sorted by key, hence the order change.
th.AssertActualEqualsExpected(
m, `
apiVersion: v1
@@ -42,3 +63,45 @@ spec:
clusterIP: None
`)
}
func TestBasicIO_2(t *testing.T) {
th := kusttest_test.MakeHarness(t)
opts := th.MakeDefaultOptions()
th.WriteK(".", `
resources:
- service.yaml
`)
// All the annotation values are quoted.
// The apimachinery code path retains the quotes, the kyaml
// path drops them at the moment.
th.WriteF("service.yaml", `
apiVersion: v1
kind: Service
metadata:
annotations:
port: "8080"
happy: "true"
color: green
name: demo
spec:
clusterIP: None
`)
m := th.Run(".", opts)
// The annotations are sorted by key, hence the order change.
expFmt := `
apiVersion: v1
kind: Service
metadata:
annotations:
color: green
happy: %s
port: %s
name: demo
spec:
clusterIP: None
`
th.AssertActualEqualsExpected(
m, opts.IfApiMachineryElseKyaml(
fmt.Sprintf(expFmt, `"true"`, `"8080"`),
fmt.Sprintf(expFmt, `true`, `8080`)))
}

View File

@@ -27,10 +27,6 @@ configMapGenerator:
apiVersion: v1
kind: Service
metadata:
annotations:
port: 8080
happy: true
color: green
name: demo
spec:
clusterIP: None
@@ -41,10 +37,6 @@ spec:
apiVersion: v1
kind: Service
metadata:
annotations:
color: green
happy: true
port: 8080
name: demo
spec:
clusterIP: None

View File

@@ -93,9 +93,6 @@ func (b *Kustomizer) Run(path string) (resmap.ResMap, error) {
}
t.Transform(m)
}
err = m.RemoveIdAnnotations()
if err != nil {
return nil, err
}
m.RemoveIdAnnotations()
return m, nil
}

View File

@@ -4,6 +4,7 @@
package krusty_test
import (
"fmt"
"strings"
"testing"
@@ -360,7 +361,7 @@ resources:
}
}
// TODO: varref has some quote issues
// TODO(#3449): varref has some quote issues
// https://github.com/kubernetes-sigs/kustomize/issues/3449
func TestVarRefBig(t *testing.T) {
th := kusttest_test.MakeHarness(t)
@@ -682,7 +683,7 @@ resources:
- ../../base
`)
m := th.Run("/app/overlay/staging", opts)
th.AssertActualEqualsExpected(m, `
expFmt := `
apiVersion: v1
kind: ServiceAccount
metadata:
@@ -756,9 +757,9 @@ kind: Service
metadata:
annotations:
prometheus.io/path: _status/vars
prometheus.io/port: 8080
prometheus.io/scrape: true
service.alpha.kubernetes.io/tolerate-unready-endpoints: true
prometheus.io/port: %s
prometheus.io/scrape: %s
service.alpha.kubernetes.io/tolerate-unready-endpoints: %s
labels:
app: cockroachdb
name: dev-base-cockroachdb
@@ -769,8 +770,8 @@ spec:
port: 26257
targetPort: 26257
- name: http
port: "8080"
targetPort: "8080"
port: %s
targetPort: %s
selector:
app: cockroachdb
---
@@ -786,8 +787,8 @@ spec:
port: 26257
targetPort: 26257
- name: http
port: "8080"
targetPort: "8080"
port: %s
targetPort: %s
selector:
app: cockroachdb
---
@@ -824,8 +825,8 @@ spec:
- --host $(hostname -f)
- --http-host 0.0.0.0
- --join dev-base-cockroachdb-0.dev-base-cockroachdb,dev-base-cockroachdb-1.dev-base-cockroachdb,dev-base-cockroachdb-2.dev-base-cockroachdb
- --cache 25%
- --max-sql-memory 25%
- --cache 25%%
- --max-sql-memory 25%%
image: cockroachdb/cockroach:v1.1.5
imagePullPolicy: IfNotPresent
name: cockroachdb
@@ -925,7 +926,16 @@ data:
kind: ConfigMap
metadata:
name: dev-base-test-config-map-6b85g79g7g
`)
`
th.AssertActualEqualsExpected(m,
opts.IfApiMachineryElseKyaml(
fmt.Sprintf(
expFmt,
`"8080"`, `"true"`, `"true"`, `8080`, `8080`, `8080`, `8080`),
fmt.Sprintf(
expFmt,
`8080`, `true`, `true`, `"8080"`, `"8080"`, `"8080"`, `"8080"`),
))
}
func TestVariableRefIngressBasic(t *testing.T) {