Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Marcus Weiner <[email protected]>
  • Loading branch information
mraerino committed Jul 4, 2023
1 parent 05f99a5 commit 5e3794c
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions internal/decryptor/decryptor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func TestDecryptor_ImportKeys(t *testing.T) {
secret *corev1.Secret
wantErr bool
inspectFunc func(g *GomegaWithT, decryptor *Decryptor)
env map[string]string
}{
{
name: "PGP key",
Expand Down Expand Up @@ -169,6 +170,54 @@ func TestDecryptor_ImportKeys(t *testing.T) {
g.Expect(decryptor.ageIdentities).To(HaveLen(0))
},
},
{
name: "age key from env",
decryption: &kustomizev1.Decryption{
Provider: provider,
},
env: map[string]string{
"FLUX_SOPS_AGE_KEY": string(ageKey),
},
inspectFunc: func(g *GomegaWithT, decryptor *Decryptor) {
g.Expect(decryptor.ageIdentities).To(HaveLen(1))
},
},
{
name: "age key from env invalid",
decryption: &kustomizev1.Decryption{
Provider: provider,
},
env: map[string]string{
"FLUX_SOPS_AGE_KEY": "invalid-key",
},
inspectFunc: func(g *GomegaWithT, decryptor *Decryptor) {
g.Expect(decryptor.ageIdentities).To(HaveLen(0))
},
},
{
name: "age key from env and secret",
decryption: &kustomizev1.Decryption{
Provider: provider,
SecretRef: &meta.LocalObjectReference{
Name: "age-secret",
},
},
env: map[string]string{
"FLUX_SOPS_AGE_KEY": string(ageKey),
},
secret: &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "age-secret",
Namespace: provider,
},
Data: map[string][]byte{
"age" + DecryptionAgeExt: ageKey,
},
},
inspectFunc: func(g *GomegaWithT, decryptor *Decryptor) {
g.Expect(decryptor.ageIdentities).To(HaveLen(2))
},
},
{
name: "HC Vault token",
decryption: &kustomizev1.Decryption{
Expand Down Expand Up @@ -376,6 +425,19 @@ clientSecret: some-client-secret`),
},
}

for envName, envVal := range tt.env {
cleanName := envName
prevValue, wasPresent := os.LookupEnv(envName)
t.Cleanup(func() {
if wasPresent {
os.Setenv(cleanName, prevValue)
} else {
os.Unsetenv(cleanName)
}
})
os.Setenv(envName, envVal)
}

d, cleanup, err := NewTempDecryptor("", cb.Build(), &kustomization)
g.Expect(err).ToNot(HaveOccurred())
t.Cleanup(cleanup)
Expand Down

0 comments on commit 5e3794c

Please sign in to comment.