Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests to ensure functions preserve comments #324

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions examples/format/imperative/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
diff --git a/resources.yaml b/resources.yaml
index 968a0f4..a33d07a 100644
index 2ce8de6..6c12c43 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -1,22 +1,22 @@
@@ -2,24 +2,24 @@
apiVersion: apps/v1
kind: Deployment
metadata:
- labels:
- foo: bar
- env: dev
- env: dev # must be one of prod, staging and dev
name: my-nginx
# namespace for team foo
namespace: my-space
+ labels:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you explain the formatting changes in the PR description.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This diff is from the format function. The formatting change is desired.
And also this line is not changed in this PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phanimarupaka I think we disabled the formatting now? Not sure.

+ env: dev
+ env: dev # must be one of prod, staging and dev
+ foo: bar
spec:
+ # The desired replicas
+ replicas: 3
selector:
- matchLabels:
- app: nginx
spec:
containers:
- - image: nginx:1.14.2
- - image: nginx:1.14.2 # version should be >= 1.14.2
- name: nginx
- ports:
- - containerPort: 80
- # The desired replicas
- replicas: 3
+ - name: nginx
+ image: nginx:1.14.2
+ image: nginx:1.14.2 # version should be >= 1.14.2
+ ports:
+ - containerPort: 80
+ matchLabels:
+ app: nginx
template:
template: # pod template
metadata:
labels:
9 changes: 6 additions & 3 deletions examples/format/imperative/resources.yaml
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
# This is a Deployment for app foo
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
foo: bar
env: dev
env: dev # must be one of prod, staging and dev
name: my-nginx
# namespace for team foo
namespace: my-space
spec:
selector:
matchLabels:
app: nginx
spec:
containers:
- image: nginx:1.14.2
- image: nginx:1.14.2 # version should be >= 1.14.2
name: nginx
ports:
- containerPort: 80
# The desired replicas
replicas: 3
template:
template: # pod template
metadata:
labels:
app: nginx
29 changes: 29 additions & 0 deletions tests/ensure-name-substring/preserve-comments/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
diff --git a/resources.yaml b/resources.yaml
index a1830c3..ce16eb1 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -3,7 +3,7 @@
apiVersion: v1
kind: ConfigMap
metadata: # comment 3
- name: the-map # comment 4
+ name: prod-the-map # comment 4
namespace: the-namespace
data:
some-key: some-value
@@ -12,7 +12,7 @@ data:
apiVersion: v1
kind: Pod
metadata:
- name: the-pod
+ name: prod-the-pod
namespace: the-namespace
spec:
containers: # comment 6
@@ -26,5 +26,5 @@ spec:
- name: SOME_KEY
valueFrom:
configMapKeyRef: # comment 7
- name: the-map # comment 8
+ name: prod-the-map # comment 8
key: some-key
9 changes: 9 additions & 0 deletions tests/ensure-name-substring/preserve-comments/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: example
pipeline:
mutators:
- image: gcr.io/kpt-fn/ensure-name-substring:unstable
configMap:
prepend: prod-
30 changes: 30 additions & 0 deletions tests/ensure-name-substring/preserve-comments/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# comment 1
# comment 2
apiVersion: v1
kind: ConfigMap
metadata: # comment 3
name: the-map # comment 4
namespace: the-namespace
data:
some-key: some-value
---
# comment 5
apiVersion: v1
kind: Pod
metadata:
name: the-pod
namespace: the-namespace
spec:
containers: # comment 6
- name: test-container
image: k8s.gcr.io/busybox
command:
- /bin/sh
- -c
- env
env:
- name: SOME_KEY
valueFrom:
configMapKeyRef: # comment 7
name: the-map # comment 8
key: some-key
21 changes: 21 additions & 0 deletions tests/set-annotations/preserve-comments/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/resources.yaml b/resources.yaml
index bc3856e..41a8456 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -5,7 +5,8 @@ kind: ConfigMap
metadata: # comment 3
name: the-map
annotations:
- color: red # comment 4
+ color: orange # comment 4
+ fruit: apple
# comment 5
data:
some-key: some-value
@@ -15,3 +16,6 @@ apiVersion: v1
kind: Namespace
metadata:
name: ns
+ annotations:
+ color: orange
+ fruit: apple
1 change: 1 addition & 0 deletions tests/set-annotations/preserve-comments/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
10 changes: 10 additions & 0 deletions tests/set-annotations/preserve-comments/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: example
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-annotations:unstable
configMap:
color: orange
fruit: apple
17 changes: 17 additions & 0 deletions tests/set-annotations/preserve-comments/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# comment 1
# comment 2
apiVersion: v1
kind: ConfigMap
metadata: # comment 3
name: the-map
annotations:
color: red # comment 4
# comment 5
data:
some-key: some-value
---
# comment 6
apiVersion: v1
kind: Namespace
metadata:
name: ns
24 changes: 24 additions & 0 deletions tests/set-labels/preserve-comments/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
diff --git a/resources.yaml b/resources.yaml
index 0bf78e1..e14316d 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -5,7 +5,8 @@ kind: ConfigMap
metadata: # comment 4
name: the-map
labels: # comment 5
- color: red # comment 6
+ color: orange # comment 6
+ fruit: apple
data:
some-key: some-value
---
@@ -15,6 +16,9 @@ kind: MyResource
metadata:
name: the-service
namespace: the-namespace
+ labels:
+ color: orange
+ fruit: apple
spec: # comment 8
selector:
labels:
1 change: 1 addition & 0 deletions tests/set-labels/preserve-comments/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
10 changes: 10 additions & 0 deletions tests/set-labels/preserve-comments/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: example
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-labels:unstable
configMap:
color: orange
fruit: apple
21 changes: 21 additions & 0 deletions tests/set-labels/preserve-comments/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# comment 1
# comment 2
apiVersion: v1
kind: ConfigMap
metadata: # comment 4
name: the-map
labels: # comment 5
color: red # comment 6
data:
some-key: some-value
---
# comment 7
apiVersion: dev.example.com/v1
kind: MyResource
metadata:
name: the-service
namespace: the-namespace
spec: # comment 8
selector:
labels:
foo: bar
14 changes: 0 additions & 14 deletions tests/set-namespace/crd/.expected/diff.patch

This file was deleted.

10 changes: 0 additions & 10 deletions tests/set-namespace/crd/fn-config.yaml

This file was deleted.

8 changes: 0 additions & 8 deletions tests/set-namespace/crd/resources.yaml

This file was deleted.

21 changes: 21 additions & 0 deletions tests/set-namespace/preserve-comments/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
diff --git a/resources.yaml b/resources.yaml
index d964587..24d1386 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -4,6 +4,7 @@ apiVersion: v1
kind: ConfigMap
metadata: # comment 3
name: the-map
+ namespace: example-ns
data:
some-key: some-value # comment 4
---
@@ -12,7 +13,7 @@ apiVersion: v1
kind: Service
metadata:
name: the-service
- namespace: the-namespace
+ namespace: example-ns
spec: # comment 6
ports:
- name: etcd-server-ssl
1 change: 1 addition & 0 deletions tests/set-namespace/preserve-comments/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ metadata:
pipeline:
mutators:
- image: gcr.io/kpt-fn/set-namespace:unstable
configPath: fn-config.yaml
configMap:
namespace: example-ns
23 changes: 23 additions & 0 deletions tests/set-namespace/preserve-comments/resources.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# comment 1
# comment 2
apiVersion: v1
kind: ConfigMap
metadata: # comment 3
name: the-map
data:
some-key: some-value # comment 4
---
# comment 5
apiVersion: v1
kind: Service
metadata:
name: the-service
namespace: the-namespace
spec: # comment 6
ports:
- name: etcd-server-ssl
port: 2380
- name: etcd-client-ssl
port: 2379
clusterIP: None
publishNotReadyAddresses: true
20 changes: 20 additions & 0 deletions tests/starlark/preserve-comments/.expected/diff.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
diff --git a/resources.yaml b/resources.yaml
index 4a2f098..5d6b87a 100644
--- a/resources.yaml
+++ b/resources.yaml
@@ -4,7 +4,7 @@ apiVersion: v1
kind: ConfigMap
metadata:
name: my-cm
- namespace: my-ns # comment 3
+ namespace: prod # comment 3
data:
# comment 4
some-key: some-value
@@ -14,5 +14,6 @@ apiVersion: foo.com/v1
kind: Bar
metadata:
name: my-bar
+ namespace: prod
spec:
featureA: true # comment 6
1 change: 1 addition & 0 deletions tests/starlark/preserve-comments/.krmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.expected
8 changes: 8 additions & 0 deletions tests/starlark/preserve-comments/Kptfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: example
pipeline:
mutators:
- image: gcr.io/kpt-fn/starlark:unstable
configPath: fn-config.yaml
11 changes: 11 additions & 0 deletions tests/starlark/preserve-comments/fn-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
apiVersion: fn.kpt.dev/v1alpha1
kind: StarlarkRun
metadata:
name: set-namespace-to-prod
source: |
# set the namespace on all resources
def setnamespace(resources, namespace):
for resource in resources:
# mutate the resource
resource["metadata"]["namespace"] = namespace
setnamespace(ctx.resource_list["items"], "prod")
Loading