unit test for set
This commit is contained in:
committed by
James Carnegie
parent
395b5fe114
commit
243dfbcc87
21
main.go
21
main.go
@@ -48,7 +48,7 @@ var (
|
||||
|
||||
attestationStyle string
|
||||
referrersRepo string
|
||||
parameters nameValuePairs
|
||||
parameters nameValuePairs = make(nameValuePairs)
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -65,28 +65,29 @@ var (
|
||||
|
||||
type nameValuePairs map[string]string
|
||||
|
||||
func (nvp *nameValuePairs) String() string {
|
||||
return fmt.Sprintf("%v", *nvp)
|
||||
func (nvp nameValuePairs) String() string {
|
||||
return fmt.Sprintf("%v", map[string]string(nvp))
|
||||
}
|
||||
|
||||
func (nvp *nameValuePairs) Set(value string) error {
|
||||
parts := strings.Split(value, ",")
|
||||
if len(parts) == 1 {
|
||||
return fmt.Errorf("invalid format, expected name=value")
|
||||
func (nvp nameValuePairs) Set(value string) error {
|
||||
if value == "" {
|
||||
return nil
|
||||
}
|
||||
parts := strings.Split(value, ",")
|
||||
for _, part := range parts {
|
||||
kv := strings.Split(part, "=")
|
||||
if len(kv) != 2 {
|
||||
return fmt.Errorf("invalid format, expected name=value")
|
||||
}
|
||||
(*nvp)[kv[0]] = kv[1]
|
||||
nvp[kv[0]] = kv[1]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var timeoutError = string(utils.GatekeeperError("operation timed out"))
|
||||
|
||||
func init() {
|
||||
// using initFlags to initialize the flags (standard init is a pain for testing)
|
||||
func initFlags() {
|
||||
klog.InitFlags(nil)
|
||||
flag.StringVar(&certDir, "cert-dir", "", "path to directory containing TLS certificates")
|
||||
flag.StringVar(&clientCAFile, "client-ca-file", "", "path to client CA certificate")
|
||||
@@ -105,13 +106,13 @@ func init() {
|
||||
flag.StringVar(&attestationStyle, "attestation-style", "referrers", "attestation style [referrers, attached]")
|
||||
flag.StringVar(&referrersRepo, "referrers-source", "", "repo from which to fetch Referrers for attestation lookup")
|
||||
|
||||
parameters = make(nameValuePairs)
|
||||
flag.Var(¶meters, "parameters", "policy parameters in name=value,name1,value1 format")
|
||||
|
||||
flag.Parse()
|
||||
}
|
||||
|
||||
func main() {
|
||||
initFlags()
|
||||
mux := http.NewServeMux()
|
||||
handlerTimeout := time.Duration(handlerTimeoutSeconds) * time.Second
|
||||
|
||||
|
||||
25
main_test.go
Normal file
25
main_test.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_nameValuePairs_Set(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
nvp nameValuePairs
|
||||
wantErr bool
|
||||
input string
|
||||
}{
|
||||
{name: "no value", nvp: nameValuePairs{}, input: "key=value", wantErr: false},
|
||||
{name: "invalid", nvp: nameValuePairs{}, input: "keyvalue", wantErr: true},
|
||||
{name: "empty", nvp: nameValuePairs{}, input: "", wantErr: false},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.nvp.Set(tt.input); (err != nil) != tt.wantErr {
|
||||
t.Errorf("nameValuePairs.Set() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user