-
Notifications
You must be signed in to change notification settings - Fork 231
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Heal Comments on Package Resources Update (#3084)
- Loading branch information
1 parent
a97673b
commit c6bc428
Showing
9 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
// Copyright 2022 Google LLC | ||
// | ||
// 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 engine | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"path/filepath" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/kpt/porch/api/porch/v1alpha1" | ||
"github.com/GoogleContainerTools/kpt/porch/pkg/repository" | ||
"github.com/google/go-cmp/cmp" | ||
"sigs.k8s.io/kustomize/kyaml/yaml" | ||
) | ||
|
||
func TestReplaceResources(t *testing.T) { | ||
ctx := context.Background() | ||
|
||
input := readPackage(t, filepath.Join("testdata", "replace")) | ||
nocomment := removeComments(t, input) | ||
|
||
replace := &mutationReplaceResources{ | ||
newResources: &v1alpha1.PackageRevisionResources{ | ||
Spec: v1alpha1.PackageRevisionResourcesSpec{ | ||
Resources: nocomment.Contents, | ||
}, | ||
}, | ||
oldResources: &v1alpha1.PackageRevisionResources{ | ||
Spec: v1alpha1.PackageRevisionResourcesSpec{ | ||
Resources: input.Contents, | ||
}, | ||
}, | ||
} | ||
|
||
output, _, err := replace.Apply(ctx, input) | ||
if err != nil { | ||
t.Fatalf("mutationReplaceResources.Apply failed: %v", err) | ||
} | ||
|
||
if !cmp.Equal(input, output) { | ||
t.Errorf("Diff: (-want,+got): %s", cmp.Diff(input, output)) | ||
} | ||
} | ||
|
||
func removeComments(t *testing.T, r repository.PackageResources) repository.PackageResources { | ||
t.Helper() | ||
|
||
out := repository.PackageResources{ | ||
Contents: map[string]string{}, | ||
} | ||
|
||
for k, v := range r.Contents { | ||
var data interface{} | ||
if err := yaml.Unmarshal([]byte(v), &data); err != nil { | ||
t.Fatalf("Failed to unmarshal %q: %v", k, err) | ||
} | ||
|
||
var nocomment bytes.Buffer | ||
encoder := yaml.NewEncoder(&nocomment) | ||
encoder.SetIndent(0) | ||
if err := encoder.Encode(data); err != nil { | ||
t.Fatalf("Failed to re-encode yaml output: %v", err) | ||
} | ||
|
||
out.Contents[k] = nocomment.String() | ||
} | ||
|
||
return out | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# top comment | ||
apiVersion: kpt.dev/v1 | ||
# Kptfile info | ||
info: | ||
# Kptfile description | ||
description: A Google Cloud Storage bucket | ||
# Kptfile kind | ||
kind: Kptfile | ||
# Kptfile metadata | ||
metadata: | ||
annotations: | ||
blueprints.cloud.google.com/title: Google Cloud Storage Bucket blueprint | ||
name: simple-bucket | ||
# Kptfile pipeline | ||
pipeline: | ||
# Kptfile mutators | ||
mutators: | ||
- configMap: | ||
name: updated-bucket-name | ||
namespace: updated-namespace | ||
project-id: updated-project-id | ||
storage-class: updated-storage-class | ||
image: gcr.io/kpt-fn/apply-setters:v0.2.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# top comment | ||
apiVersion: storage.cnrm.cloud.google.com/v1beta1 | ||
kind: StorageBucket | ||
# metadata comment | ||
metadata: # kpt-merge: config-control/blueprints-project-bucket | ||
# annotations comment | ||
annotations: | ||
cnrm.cloud.google.com/force-destroy: "false" | ||
cnrm.cloud.google.com/project-id: blueprints-project # kpt-set: ${project-id} | ||
name: blueprints-project-bucket # kpt-set: ${project-id}-${name} | ||
namespace: config-control # kpt-set: ${namespace} | ||
# spec comment | ||
spec: | ||
storageClass: standard # kpt-set: ${storage-class} | ||
uniformBucketLevelAccess: true | ||
# Versioning is enabled | ||
versioning: | ||
enabled: false |