forked from openfaas/certifier
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain_test.go
117 lines (94 loc) · 3.12 KB
/
main_test.go
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package tests
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strings"
"testing"
"time"
"github.com/openfaas/faas-cli/commands"
sdkConfig "github.com/openfaas/faas-cli/config"
sdk "github.com/openfaas/faas-cli/proxy"
)
var (
config = Config{}
defaultNamespace = ""
swarm = flag.Bool("swarm", false, "helper flag to run only swarm-compatible tests only")
token = flag.String("token", "", "authentication Bearer token override, enables auth automatically")
)
func init() {
flag.StringVar(&config.Gateway, "gateway", "", "set the gateway URL, if empty use the gateway_url env variable")
flag.BoolVar(
&config.AuthEnabled,
"enableAuth",
false,
fmt.Sprintf("enable/disable authentication. The auth will be parsed from the default config in %s", filepath.Join(sdkConfig.DefaultDir, sdkConfig.DefaultFile)),
)
flag.BoolVar(&config.SecretUpdate, "secretUpdate", true, "enable/disable secret update tests")
flag.BoolVar(&config.ScaleToZero, "scaleToZero", true, "enable/disable scale from zero tests")
FromEnv(&config)
}
func TestMain(m *testing.M) {
// flag parsing here
flag.Parse()
if config.Gateway == "" {
uri, err := url.Parse(os.Getenv("gateway_url"))
if err != nil {
log.Fatalf("invalid gateway url %s", err)
}
config.Gateway = uri.String()
}
// make sure to trim any trailing slash because this is how the gateway is modified when
// saved to the config. if we don't do this, we wont find the saved auth.
config.Gateway = strings.TrimRight(config.Gateway, "/")
if *swarm {
config.SecretUpdate = false
config.ScaleToZero = false
}
config.Auth = &Unauthenticated{}
if config.AuthEnabled || *token != "" {
// TODO : NewCLIAuth should return the error from LookupAuthConfig!
config.Auth = commands.NewCLIAuth(*token, config.Gateway)
}
timeout := 5 * time.Second
config.Client = sdk.NewClient(config.Auth, config.Gateway, nil, &timeout)
prettyConfig, err := json.MarshalIndent(config, "", "\t")
if err != nil {
log.Fatalf("Config Pretty Print Failed with %s", err)
}
log.Println(string(prettyConfig))
os.Exit(m.Run())
}
// Config contains the configuration values for the certifier tests
// This includes the gateway and auth parameters as well as the feature
// flags to control skipping specific tests.
type Config struct {
// Gateway is the URL for the gateway that will be tested
Gateway string
// Auth contains the parsed proxy client auth
Auth sdk.ClientAuth
// Client is a preconfigured gateway client, including auth
Client *sdk.Client
// AuthEnabled
AuthEnabled bool
// SecretUpdate enables/disables the secret update test
SecretUpdate bool
// ScaleToZero enables/disables the scale from zero test
ScaleToZero bool
// Namespaces to verfiy OpenFaaS provider
Namespaces []string
}
func FromEnv(config *Config) {
// read CERTIFIER_NAMESPACES variable, parse as csv string
namespaces, present := os.LookupEnv("CERTIFIER_NAMESPACES")
if present {
config.Namespaces = strings.Split(namespaces, ",")
for index := range config.Namespaces {
config.Namespaces[index] = strings.TrimSpace(config.Namespaces[index])
}
}
}