-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from dcarley/dcarley/handle-empty-secrets
[fixes #11] Improve handling of empty secrets
- Loading branch information
Showing
3 changed files
with
126 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package cmd | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
|
||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime/schema" | ||
"k8s.io/client-go/kubernetes/fake" | ||
) | ||
|
||
func TestModifySecrets(t *testing.T) { | ||
const ( | ||
name = "mysecret" | ||
namespace = "mynamespace" | ||
) | ||
|
||
logrus.SetOutput(ioutil.Discard) | ||
origEditor := os.Getenv("EDITOR") | ||
if origEditor != "" { | ||
defer os.Setenv("EDITOR", origEditor) | ||
} else { | ||
defer os.Unsetenv("EDITOR") | ||
} | ||
|
||
testcases := []struct { | ||
name string | ||
command string | ||
secret map[string][]byte | ||
expected map[string][]byte | ||
}{ | ||
{ | ||
name: "no changes to empty secret", | ||
command: "touch", | ||
secret: map[string][]byte{}, | ||
expected: map[string][]byte{}, | ||
}, { | ||
name: "no changes to populated secret", | ||
command: "touch", | ||
secret: map[string][]byte{"key": []byte("value")}, | ||
expected: map[string][]byte{"key": []byte("value")}, | ||
}, { | ||
name: "changes to populated secret", | ||
command: "sed -i= s/value/updated/", | ||
secret: map[string][]byte{"key": []byte("value")}, | ||
expected: map[string][]byte{"key": []byte("updated")}, | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
os.Setenv("EDITOR", tc.command) | ||
|
||
client := fake.NewSimpleClientset(&v1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
Annotations: map[string]string{}, | ||
}, | ||
Data: tc.secret, | ||
}) | ||
|
||
modify := ModifySecretOptions{ | ||
kubeclient: client, | ||
secretName: name, | ||
namespace: namespace, | ||
} | ||
require.NoError(t, modify.Run()) | ||
|
||
object, err := client.Tracker().Get( | ||
schema.GroupVersionResource{ | ||
Version: "v1", | ||
Resource: "secrets", | ||
}, | ||
namespace, name, | ||
) | ||
require.NoError(t, err) | ||
assert.Equal(t, tc.expected, object.(*v1.Secret).Data) | ||
}) | ||
} | ||
} | ||
|
||
func TestYamlOrEmpty(t *testing.T) { | ||
testcases := []struct { | ||
name string | ||
input map[string]string | ||
expected []byte | ||
}{ | ||
{ | ||
name: "empty map", | ||
input: map[string]string{}, | ||
expected: []byte{}, | ||
}, { | ||
name: "populated map", | ||
input: map[string]string{"foo": "bar"}, | ||
expected: []byte("foo: bar\n"), | ||
}, | ||
} | ||
|
||
for _, tc := range testcases { | ||
t.Run(tc.name, func(t *testing.T) { | ||
output, err := yamlOrEmpty(tc.input) | ||
assert.NoError(t, err) | ||
assert.Equal(t, tc.expected, output) | ||
}) | ||
} | ||
} |
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