|
|
@@ -24,6 +24,7 @@ import (
|
|
|
|
|
|
"github.com/google/go-cmp/cmp"
|
|
|
vault "github.com/hashicorp/vault/api"
|
|
|
+ "github.com/spf13/pflag"
|
|
|
corev1 "k8s.io/api/core/v1"
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
|
|
@@ -942,3 +943,77 @@ func TestValidateTokenExpiry(t *testing.T) {
|
|
|
}
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+func TestRegisterVaultCacheFlagsDefaults(t *testing.T) {
|
|
|
+ _, flags := registerVaultCacheFlags(pflag.ContinueOnError)
|
|
|
+
|
|
|
+ // The deprecated size flag shares the non-zero default of the flag that
|
|
|
+ // replaces it. That is why resolveVaultCacheConfig has to look at
|
|
|
+ // fs.Changed instead of the value.
|
|
|
+ if flags.size != defaultCacheSize {
|
|
|
+ t.Errorf("vault-token-cache-size default = %d, want %d", flags.size, defaultCacheSize)
|
|
|
+ }
|
|
|
+ if flags.expSize != defaultCacheSize {
|
|
|
+ t.Errorf("experimental-vault-token-cache-size default = %d, want %d", flags.expSize, defaultCacheSize)
|
|
|
+ }
|
|
|
+ if flags.enable || flags.expEnable {
|
|
|
+ t.Errorf("cache enable flags should default to false, got enable=%v expEnable=%v", flags.enable, flags.expEnable)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func TestResolveVaultCacheConfig(t *testing.T) {
|
|
|
+ cases := map[string]struct {
|
|
|
+ reason string
|
|
|
+ args []string
|
|
|
+ wantEnable bool
|
|
|
+ wantSize int
|
|
|
+ }{
|
|
|
+ "NoFlagSet": {
|
|
|
+ reason: "Should keep the supported defaults when no flag is passed",
|
|
|
+ args: []string{},
|
|
|
+ wantEnable: false,
|
|
|
+ wantSize: defaultCacheSize,
|
|
|
+ },
|
|
|
+ "SupportedFlagsOnly": {
|
|
|
+ reason: "Should honor --vault-token-cache-size, which the deprecated default used to overwrite",
|
|
|
+ args: []string{"--enable-vault-token-cache", "--vault-token-cache-size=4096"},
|
|
|
+ wantEnable: true,
|
|
|
+ wantSize: 4096,
|
|
|
+ },
|
|
|
+ "DeprecatedFlagsOnly": {
|
|
|
+ reason: "Should apply the deprecated flags when they are explicitly passed",
|
|
|
+ args: []string{"--experimental-enable-vault-token-cache", "--experimental-vault-token-cache-size=8192"},
|
|
|
+ wantEnable: true,
|
|
|
+ wantSize: 8192,
|
|
|
+ },
|
|
|
+ "BothSizeFlagsSet": {
|
|
|
+ reason: "Should let the deprecated size win, which is the existing precedence",
|
|
|
+ args: []string{"--vault-token-cache-size=4096", "--experimental-vault-token-cache-size=8192"},
|
|
|
+ wantEnable: false,
|
|
|
+ wantSize: 8192,
|
|
|
+ },
|
|
|
+ "DeprecatedEnableExplicitlyFalse": {
|
|
|
+ reason: "Should honor an explicit false instead of forcing the cache on",
|
|
|
+ args: []string{"--enable-vault-token-cache", "--experimental-enable-vault-token-cache=false"},
|
|
|
+ wantEnable: false,
|
|
|
+ wantSize: defaultCacheSize,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ for name, tc := range cases {
|
|
|
+ t.Run(name, func(t *testing.T) {
|
|
|
+ fs, flags := registerVaultCacheFlags(pflag.ContinueOnError)
|
|
|
+ if err := fs.Parse(tc.args); err != nil {
|
|
|
+ t.Fatalf("%s: unexpected parse error: %v", tc.reason, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ gotEnable, gotSize := resolveVaultCacheConfig(fs, flags)
|
|
|
+ if gotEnable != tc.wantEnable {
|
|
|
+ t.Errorf("%s: enable = %v, want %v", tc.reason, gotEnable, tc.wantEnable)
|
|
|
+ }
|
|
|
+ if gotSize != tc.wantSize {
|
|
|
+ t.Errorf("%s: size = %d, want %d", tc.reason, gotSize, tc.wantSize)
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|