Skip to content

Commit 27d6f00

Browse files
committed
Confirm format detection of file sources via tests
1 parent 8bcf7b7 commit 27d6f00

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

controllers/kustomization_decryptor_test.go

+22-12
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func TestKustomizeDecryptor_DecryptResource(t *testing.T) {
695695
},
696696
}
697697

698-
t.Run("SOPS encrypted resource", func(t *testing.T) {
698+
t.Run("SOPS-encrypted Secret resource", func(t *testing.T) {
699699
g := NewWithT(t)
700700

701701
kus := kustomization.DeepCopy()
@@ -736,7 +736,7 @@ func TestKustomizeDecryptor_DecryptResource(t *testing.T) {
736736
g.Expect(got.MarshalJSON()).To(Equal(secretData))
737737
})
738738

739-
t.Run("SOPS encrypted binary Secret data field", func(t *testing.T) {
739+
t.Run("SOPS-encrypted binary-format Secret data field", func(t *testing.T) {
740740
g := NewWithT(t)
741741

742742
kus := kustomization.DeepCopy()
@@ -771,7 +771,7 @@ func TestKustomizeDecryptor_DecryptResource(t *testing.T) {
771771
g.Expect(got.GetDataMap()).To(HaveKeyWithValue("file.ini", base64.StdEncoding.EncodeToString(plainData)))
772772
})
773773

774-
t.Run("SOPS encrypted YAML Secret data field", func(t *testing.T) {
774+
t.Run("SOPS-encrypted YAML-format Secret data field", func(t *testing.T) {
775775
g := NewWithT(t)
776776

777777
kus := kustomization.DeepCopy()
@@ -849,12 +849,14 @@ func TestKustomizeDecryptor_DecryptResource(t *testing.T) {
849849

850850
func TestKustomizeDecryptor_decryptKustomizationEnvSources(t *testing.T) {
851851
type file struct {
852-
name string
853-
symlink string
854-
data []byte
855-
encrypt bool
856-
expectData bool
852+
name string
853+
symlink string
854+
data []byte
855+
originalFormat *formats.Format
856+
encrypt bool
857+
expectData bool
857858
}
859+
binaryFormat := formats.Binary
858860
tests := []struct {
859861
name string
860862
wordirSuffix string
@@ -869,6 +871,9 @@ func TestKustomizeDecryptor_decryptKustomizationEnvSources(t *testing.T) {
869871
path: "subdir",
870872
files: []file{
871873
{name: "subdir/app.env", data: []byte("var1=value1\n"), encrypt: true, expectData: true},
874+
// NB: Despite the file extension representing the SOPS-encrypted JSON output
875+
// format, the original data is plain text, or "binary."
876+
{name: "subdir/combination.json", data: []byte("The safe combination is ..."), originalFormat: &binaryFormat, encrypt: true, expectData: true},
872877
{name: "subdir/file.txt", data: []byte("file"), encrypt: true, expectData: true},
873878
{name: "secret.env", data: []byte("var2=value2\n"), encrypt: true, expectData: true},
874879
},
@@ -877,13 +882,13 @@ func TestKustomizeDecryptor_decryptKustomizationEnvSources(t *testing.T) {
877882
GeneratorArgs: kustypes.GeneratorArgs{
878883
Name: "envSecret",
879884
KvPairSources: kustypes.KvPairSources{
880-
FileSources: []string{"file.txt"},
885+
FileSources: []string{"file.txt", "combo=combination.json"},
881886
EnvSources: []string{"app.env", "../secret.env"},
882887
},
883888
},
884889
},
885890
},
886-
expectVisited: []string{"subdir/app.env", "subdir/file.txt", "secret.env"},
891+
expectVisited: []string{"subdir/app.env", "subdir/combination.json", "subdir/file.txt", "secret.env"},
887892
},
888893
{
889894
name: "decryption error",
@@ -987,7 +992,12 @@ func TestKustomizeDecryptor_decryptKustomizationEnvSources(t *testing.T) {
987992
}
988993
data := f.data
989994
if f.encrypt {
990-
format := formats.FormatForPath(f.name)
995+
var format formats.Format
996+
if f.originalFormat != nil {
997+
format = *f.originalFormat
998+
} else {
999+
format = formats.FormatForPath(f.name)
1000+
}
9911001
data, err = d.sopsEncryptWithFormat(sops.Metadata{
9921002
KeyGroups: []sops.KeyGroup{
9931003
{&sopsage.MasterKey{Recipient: id.Recipient().String()}},
@@ -1159,7 +1169,7 @@ func TestKustomizeDecryptor_decryptSopsFile(t *testing.T) {
11591169

11601170
b, err := os.ReadFile(filepath.Join(tmpDir, f.name))
11611171
g.Expect(err).ToNot(HaveOccurred())
1162-
g.Expect(bytes.Compare(f.data, b) == 0).To(Equal(f.expectData))
1172+
g.Expect(bytes.Equal(f.data, b)).To(Equal(f.expectData))
11631173
}
11641174
})
11651175
}

controllers/suite_test.go

-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ import (
4343
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
4444
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
4545
"k8s.io/client-go/kubernetes/scheme"
46-
kuberecorder "k8s.io/client-go/tools/record"
4746
ctrl "sigs.k8s.io/controller-runtime"
4847
"sigs.k8s.io/controller-runtime/pkg/client"
4948
"sigs.k8s.io/controller-runtime/pkg/envtest"
@@ -68,7 +67,6 @@ var (
6867
k8sClient client.Client
6968
testEnv *testenv.Environment
7069
testServer *testserver.ArtifactServer
71-
testEventsH kuberecorder.EventRecorder
7270
testMetricsH controller.Metrics
7371
ctx = ctrl.SetupSignalHandler()
7472
kubeConfig []byte

0 commit comments

Comments
 (0)