-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathflags_test.go
More file actions
97 lines (88 loc) · 3.15 KB
/
Copy pathflags_test.go
File metadata and controls
97 lines (88 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package config
import (
"path/filepath"
"testing"
"github.com/spf13/pflag"
"github.com/stretchr/testify/assert"
)
func TestGetFlagSet(t *testing.T) {
fs := getFlagSet(pflag.ExitOnError)
// Check if each flag exists
assert.NotNil(t, fs.Lookup(configFilePathFlagName), "Flag %s not found", configFilePathFlagName)
assert.NotNil(t, fs.Lookup(kubeConfigPathFlagName), "Flag %s not found", kubeConfigPathFlagName)
assert.NotNil(t, fs.Lookup(prometheusCREnabledFlagName), "Flag %s not found", prometheusCREnabledFlagName)
}
func TestFlagGetters(t *testing.T) {
tests := []struct {
name string
flagArgs []string
expectedValue interface{}
expectedErr bool
getterFunc func(*pflag.FlagSet) (interface{}, error)
}{
{
name: "GetConfigFilePath",
flagArgs: []string{"--" + configFilePathFlagName, "/path/to/config"},
expectedValue: "/path/to/config",
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getConfigFilePath(fs) },
},
{
name: "GetKubeConfigFilePath",
flagArgs: []string{"--" + kubeConfigPathFlagName, filepath.Join("~", ".kube", "config")},
expectedValue: filepath.Join("~", ".kube", "config"),
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getKubeConfigFilePath(fs) },
},
{
name: "GetConfigReloadEnabled",
flagArgs: []string{"--" + reloadConfigFlagName, "true"},
expectedValue: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getConfigReloadEnabled(fs) },
},
{
name: "GetPrometheusCREnabled",
flagArgs: []string{"--" + prometheusCREnabledFlagName},
expectedValue: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getPrometheusCREnabled(fs) },
},
{
name: "GetPrometheusCREnabledDefault",
flagArgs: []string{},
expectedValue: false,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getPrometheusCREnabled(fs) },
},
{
name: "InvalidFlag",
flagArgs: []string{"--invalid-flag", "value"},
expectedErr: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getConfigFilePath(fs) },
},
{
name: "HttpsServer",
flagArgs: []string{"--" + httpsEnabledFlagName, "true"},
expectedValue: true,
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getHttpsEnabled(fs) },
},
{
name: "HttpsServerKey",
flagArgs: []string{"--" + httpsTLSKeyFilePathFlagName, "/path/to/tls.key"},
expectedValue: "/path/to/tls.key",
getterFunc: func(fs *pflag.FlagSet) (interface{}, error) { return getHttpsTLSKeyFilePath(fs) },
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
fs := getFlagSet(pflag.ContinueOnError)
err := fs.Parse(tt.flagArgs)
// If an error is expected during parsing, we check it here.
if tt.expectedErr {
assert.Error(t, err)
return
}
got, err := tt.getterFunc(fs)
assert.NoError(t, err)
assert.Equal(t, tt.expectedValue, got)
})
}
}