2019-08-01 14:01:40 -04:00
|
|
|
/*
|
|
|
|
|
Copyright 2017 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 diff
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
2022-07-27 12:53:32 -04:00
|
|
|
"fmt"
|
2019-08-01 14:01:40 -04:00
|
|
|
"os"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
|
2021-05-06 00:51:15 -04:00
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
|
|
|
2019-08-01 14:01:40 -04:00
|
|
|
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
2023-04-05 07:07:46 -04:00
|
|
|
"k8s.io/cli-runtime/pkg/genericiooptions"
|
2019-08-01 14:01:40 -04:00
|
|
|
"k8s.io/utils/exec"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type FakeObject struct {
|
|
|
|
|
name string
|
|
|
|
|
merged map[string]interface{}
|
|
|
|
|
live map[string]interface{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var _ Object = &FakeObject{}
|
|
|
|
|
|
|
|
|
|
func (f *FakeObject) Name() string {
|
|
|
|
|
return f.name
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FakeObject) Merged() (runtime.Object, error) {
|
2021-05-06 00:51:15 -04:00
|
|
|
// Return nil if merged object does not exist
|
|
|
|
|
if f.merged == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
2019-08-01 14:01:40 -04:00
|
|
|
return &unstructured.Unstructured{Object: f.merged}, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *FakeObject) Live() runtime.Object {
|
2021-05-06 00:51:15 -04:00
|
|
|
// Return nil if live object does not exist
|
|
|
|
|
if f.live == nil {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
2019-08-01 14:01:40 -04:00
|
|
|
return &unstructured.Unstructured{Object: f.live}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDiffProgram(t *testing.T) {
|
2020-10-04 23:29:27 -04:00
|
|
|
externalDiffCommands := [3]string{"diff", "diff -ruN", "diff --report-identical-files"}
|
|
|
|
|
|
2023-04-15 11:09:47 -04:00
|
|
|
t.Setenv("LANG", "C")
|
2025-03-21 13:28:41 -04:00
|
|
|
t.Setenv("LANGUAGE", "en_US")
|
2021-04-02 02:59:47 -04:00
|
|
|
|
2020-10-04 23:29:27 -04:00
|
|
|
for i, c := range externalDiffCommands {
|
2023-04-15 11:09:47 -04:00
|
|
|
t.Setenv("KUBECTL_EXTERNAL_DIFF", c)
|
2023-04-05 07:07:46 -04:00
|
|
|
streams, _, stdout, _ := genericiooptions.NewTestIOStreams()
|
2020-10-04 23:29:27 -04:00
|
|
|
diff := DiffProgram{
|
|
|
|
|
IOStreams: streams,
|
|
|
|
|
Exec: exec.New(),
|
|
|
|
|
}
|
|
|
|
|
err := diff.Run("/dev/zero", "/dev/zero")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Testing diff --report-identical-files
|
|
|
|
|
if i == 2 {
|
|
|
|
|
output_msg := "Files /dev/zero and /dev/zero are identical\n"
|
|
|
|
|
if output := stdout.String(); output != output_msg {
|
|
|
|
|
t.Fatalf(`stdout = %q, expected = %s"`, output, output_msg)
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-01 14:01:40 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestPrinter(t *testing.T) {
|
|
|
|
|
printer := Printer{}
|
|
|
|
|
|
|
|
|
|
obj := &unstructured.Unstructured{Object: map[string]interface{}{
|
|
|
|
|
"string": "string",
|
|
|
|
|
"list": []int{1, 2, 3},
|
|
|
|
|
"int": 12,
|
|
|
|
|
}}
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
printer.Print(obj, &buf)
|
|
|
|
|
want := `int: 12
|
|
|
|
|
list:
|
|
|
|
|
- 1
|
|
|
|
|
- 2
|
|
|
|
|
- 3
|
|
|
|
|
string: string
|
|
|
|
|
`
|
|
|
|
|
if buf.String() != want {
|
|
|
|
|
t.Errorf("Print() = %q, want %q", buf.String(), want)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDiffVersion(t *testing.T) {
|
|
|
|
|
diff, err := NewDiffVersion("MERGED")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer diff.Dir.Delete()
|
|
|
|
|
|
|
|
|
|
obj := FakeObject{
|
|
|
|
|
name: "bla",
|
|
|
|
|
live: map[string]interface{}{"live": true},
|
|
|
|
|
merged: map[string]interface{}{"merged": true},
|
|
|
|
|
}
|
2021-05-06 00:51:15 -04:00
|
|
|
rObj, err := obj.Merged()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
err = diff.Print(obj.Name(), rObj, Printer{})
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions:
Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not
take into account the OS-specific path separator, meaning that they
won't behave as intended on Windows.
For example, Dir is supposed to return all but the last element of the
path. For the path "C:\some\dir\somewhere", it is supposed to return
"C:\some\dir\", however, it returns ".".
Instead of these functions, the ones in filepath should be used instead.
Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7
2022-06-15 08:17:24 -04:00
|
|
|
fcontent, err := os.ReadFile(filepath.Join(diff.Dir.Name, obj.Name()))
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
econtent := "merged: true\n"
|
|
|
|
|
if string(fcontent) != econtent {
|
|
|
|
|
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDirectory(t *testing.T) {
|
|
|
|
|
dir, err := CreateDirectory("prefix")
|
|
|
|
|
defer dir.Delete()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
_, err = os.Stat(dir.Name)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.HasPrefix(filepath.Base(dir.Name), "prefix") {
|
|
|
|
|
t.Fatalf(`Directory doesn't start with "prefix": %q`, dir.Name)
|
|
|
|
|
}
|
2022-07-30 09:31:16 -04:00
|
|
|
entries, err := os.ReadDir(dir.Name)
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(entries) != 0 {
|
|
|
|
|
t.Fatalf("Directory should be empty, has %d elements", len(entries))
|
|
|
|
|
}
|
|
|
|
|
_, err = dir.NewFile("ONE")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
_, err = dir.NewFile("TWO")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2022-07-30 09:31:16 -04:00
|
|
|
entries, err = os.ReadDir(dir.Name)
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if len(entries) != 2 {
|
|
|
|
|
t.Fatalf("ReadDir should have two elements, has %d elements", len(entries))
|
|
|
|
|
}
|
|
|
|
|
err = dir.Delete()
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
_, err = os.Stat(dir.Name)
|
|
|
|
|
if err == nil {
|
|
|
|
|
t.Fatal("Directory should be gone, still present.")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestDiffer(t *testing.T) {
|
|
|
|
|
diff, err := NewDiffer("LIVE", "MERGED")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer diff.TearDown()
|
|
|
|
|
|
|
|
|
|
obj := FakeObject{
|
|
|
|
|
name: "bla",
|
|
|
|
|
live: map[string]interface{}{"live": true},
|
|
|
|
|
merged: map[string]interface{}{"merged": true},
|
|
|
|
|
}
|
2022-07-27 12:53:32 -04:00
|
|
|
err = diff.Diff(&obj, Printer{}, true)
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions:
Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not
take into account the OS-specific path separator, meaning that they
won't behave as intended on Windows.
For example, Dir is supposed to return all but the last element of the
path. For the path "C:\some\dir\somewhere", it is supposed to return
"C:\some\dir\", however, it returns ".".
Instead of these functions, the ones in filepath should be used instead.
Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7
2022-06-15 08:17:24 -04:00
|
|
|
fcontent, err := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name()))
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
econtent := "live: true\n"
|
|
|
|
|
if string(fcontent) != econtent {
|
|
|
|
|
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
|
|
|
|
|
}
|
|
|
|
|
|
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions:
Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not
take into account the OS-specific path separator, meaning that they
won't behave as intended on Windows.
For example, Dir is supposed to return all but the last element of the
path. For the path "C:\some\dir\somewhere", it is supposed to return
"C:\some\dir\", however, it returns ".".
Instead of these functions, the ones in filepath should be used instead.
Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7
2022-06-15 08:17:24 -04:00
|
|
|
fcontent, err = os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name()))
|
2019-08-01 14:01:40 -04:00
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
econtent = "merged: true\n"
|
|
|
|
|
if string(fcontent) != econtent {
|
|
|
|
|
t.Fatalf("File has %q, expected %q", string(fcontent), econtent)
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-05-06 00:51:15 -04:00
|
|
|
|
2022-07-27 12:53:32 -04:00
|
|
|
func TestShowManagedFields(t *testing.T) {
|
|
|
|
|
diff, err := NewDiffer("LIVE", "MERGED")
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
defer diff.TearDown()
|
|
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
showManagedFields bool
|
|
|
|
|
expectedFromContent string
|
|
|
|
|
expectedToContent string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "without managed fields",
|
|
|
|
|
showManagedFields: false,
|
|
|
|
|
expectedFromContent: `live: true
|
|
|
|
|
metadata:
|
|
|
|
|
name: foo
|
|
|
|
|
`,
|
|
|
|
|
expectedToContent: `merged: true
|
|
|
|
|
metadata:
|
|
|
|
|
name: foo
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "with managed fields",
|
|
|
|
|
showManagedFields: true,
|
|
|
|
|
expectedFromContent: `live: true
|
|
|
|
|
metadata:
|
|
|
|
|
managedFields: mf-data
|
|
|
|
|
name: foo
|
|
|
|
|
`,
|
|
|
|
|
expectedToContent: `merged: true
|
|
|
|
|
metadata:
|
|
|
|
|
managedFields: mf-data
|
|
|
|
|
name: foo
|
|
|
|
|
`,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for i, tc := range testCases {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
obj := FakeObject{
|
|
|
|
|
name: fmt.Sprintf("TestCase%d", i),
|
|
|
|
|
live: map[string]interface{}{
|
|
|
|
|
"live": true,
|
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
|
"managedFields": "mf-data",
|
|
|
|
|
"name": "foo",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
merged: map[string]interface{}{
|
|
|
|
|
"merged": true,
|
|
|
|
|
"metadata": map[string]interface{}{
|
|
|
|
|
"managedFields": "mf-data",
|
|
|
|
|
"name": "foo",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err = diff.Diff(&obj, Printer{}, tc.showManagedFields)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
|
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions:
Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not
take into account the OS-specific path separator, meaning that they
won't behave as intended on Windows.
For example, Dir is supposed to return all but the last element of the
path. For the path "C:\some\dir\somewhere", it is supposed to return
"C:\some\dir\", however, it returns ".".
Instead of these functions, the ones in filepath should be used instead.
Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7
2022-06-15 08:17:24 -04:00
|
|
|
actualFromContent, _ := os.ReadFile(filepath.Join(diff.From.Dir.Name, obj.Name()))
|
2022-07-27 12:53:32 -04:00
|
|
|
if string(actualFromContent) != tc.expectedFromContent {
|
|
|
|
|
t.Fatalf("File has %q, expected %q", string(actualFromContent), tc.expectedFromContent)
|
|
|
|
|
}
|
|
|
|
|
|
Replaces path.Operation with filepath.Operation (staging)
The path module has a few different functions:
Clean, Split, Join, Ext, Dir, Base, IsAbs. These functions do not
take into account the OS-specific path separator, meaning that they
won't behave as intended on Windows.
For example, Dir is supposed to return all but the last element of the
path. For the path "C:\some\dir\somewhere", it is supposed to return
"C:\some\dir\", however, it returns ".".
Instead of these functions, the ones in filepath should be used instead.
Kubernetes-commit: 856bb5c8f266f5276f1a576f47be622d7cb384e7
2022-06-15 08:17:24 -04:00
|
|
|
actualToContent, _ := os.ReadFile(filepath.Join(diff.To.Dir.Name, obj.Name()))
|
2022-07-27 12:53:32 -04:00
|
|
|
if string(actualToContent) != tc.expectedToContent {
|
|
|
|
|
t.Fatalf("File has %q, expected %q", string(actualToContent), tc.expectedToContent)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-05-06 00:51:15 -04:00
|
|
|
func TestMasker(t *testing.T) {
|
|
|
|
|
type diff struct {
|
|
|
|
|
from runtime.Object
|
|
|
|
|
to runtime.Object
|
|
|
|
|
}
|
|
|
|
|
cases := []struct {
|
|
|
|
|
name string
|
|
|
|
|
input diff
|
|
|
|
|
want diff
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "no_changes",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***", // still masked
|
|
|
|
|
"password": "***", // still masked
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***", // still masked
|
|
|
|
|
"password": "***", // still masked
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "object_created",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: nil, // does not exist yet
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: nil, // does not exist yet
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***", // no suffix needed
|
|
|
|
|
"password": "***", // no suffix needed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "object_removed",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: nil, // removed
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***", // no suffix needed
|
|
|
|
|
"password": "***", // no suffix needed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: nil, // removed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "data_key_added",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123", // added
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "***", // no suffix needed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "data_key_changed",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "456", // changed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "*** (before)", // added suffix for diff
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "*** (after)", // added suffix for diff
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "data_key_removed",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
// "password": "123", // removed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "***", // no suffix needed
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
// "password": "***",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "empty_secret_from",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{}, // no data key
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{}, // no data key
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "***",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "empty_secret_to",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{}, // no data key
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"data": map[string]interface{}{
|
|
|
|
|
"username": "***",
|
|
|
|
|
"password": "***",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{}, // no data key
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "invalid_data_key",
|
|
|
|
|
input: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"some_other_key": map[string]interface{}{ // invalid key
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"some_other_key": map[string]interface{}{ // invalid key
|
|
|
|
|
"username": "abc",
|
|
|
|
|
"password": "123",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
want: diff{
|
|
|
|
|
from: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"some_other_key": map[string]interface{}{
|
|
|
|
|
"username": "abc", // skipped
|
|
|
|
|
"password": "123", // skipped
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
to: &unstructured.Unstructured{
|
|
|
|
|
Object: map[string]interface{}{
|
|
|
|
|
"some_other_key": map[string]interface{}{
|
|
|
|
|
"username": "abc", // skipped
|
|
|
|
|
"password": "123", // skipped
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
|
tc := tc // capture range variable
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
m, err := NewMasker(tc.input.from, tc.input.to)
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
from, to := m.From(), m.To()
|
|
|
|
|
if from != nil && tc.want.from != nil {
|
|
|
|
|
if diff := cmp.Diff(from, tc.want.from); diff != "" {
|
|
|
|
|
t.Errorf("from: (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if to != nil && tc.want.to != nil {
|
|
|
|
|
if diff := cmp.Diff(to, tc.want.to); diff != "" {
|
|
|
|
|
t.Errorf("to: (-want +got):\n%s", diff)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|