tests: Refactor CURLPrefixArgs

Signed-off-by: Marek Siarkowicz <siarkowicz@google.com>
This commit is contained in:
Marek Siarkowicz 2023-03-14 12:12:32 +01:00
parent 67e3c784ba
commit e87abf3f13
4 changed files with 18 additions and 15 deletions

View File

@ -232,7 +232,7 @@ func compareMemberVersion(expect version.Versions, target version.Versions) erro
} }
func getMemberVersionByCurl(cfg *e2e.EtcdProcessClusterConfig, member e2e.EtcdProcess) (version.Versions, error) { func getMemberVersionByCurl(cfg *e2e.EtcdProcessClusterConfig, member e2e.EtcdProcess) (version.Versions, error) {
args := e2e.CURLPrefixArgs(cfg, member, "GET", e2e.CURLReq{Endpoint: "/version"}) args := e2e.CURLPrefixArgsCluster(cfg, member, "GET", e2e.CURLReq{Endpoint: "/version"})
lines, err := e2e.RunUtilCompletion(args, nil) lines, err := e2e.RunUtilCompletion(args, nil)
if err != nil { if err != nil {
return version.Versions{}, err return version.Versions{}, err

View File

@ -157,7 +157,7 @@ func submitConcurrentWatch(cx ctlCtx, number int, wgDone *sync.WaitGroup, closeC
member := cluster.Procs[rand.Intn(cluster.Cfg.ClusterSize)] member := cluster.Procs[rand.Intn(cluster.Cfg.ClusterSize)]
curlReq := e2e.CURLReq{Endpoint: "/v3/watch", Value: string(watchData)} curlReq := e2e.CURLReq{Endpoint: "/v3/watch", Value: string(watchData)}
args := e2e.CURLPrefixArgs(cluster.Cfg, member, "POST", curlReq) args := e2e.CURLPrefixArgsCluster(cluster.Cfg, member, "POST", curlReq)
proc, err := e2e.SpawnCmd(args, nil) proc, err := e2e.SpawnCmd(args, nil)
if err != nil { if err != nil {
return fmt.Errorf("failed to spawn: %w", err) return fmt.Errorf("failed to spawn: %w", err)

View File

@ -246,7 +246,7 @@ func testV3CurlAuth(cx ctlCtx) {
lineFunc = func(txt string) bool { return true } lineFunc = func(txt string) bool { return true }
) )
cmdArgs = e2e.CURLPrefixArgs(cx.epc.Cfg, cx.epc.Procs[rand.Intn(cx.epc.Cfg.ClusterSize)], "POST", e2e.CURLReq{Endpoint: path.Join(p, "/auth/authenticate"), Value: string(authreq)}) cmdArgs = e2e.CURLPrefixArgsCluster(cx.epc.Cfg, cx.epc.Procs[rand.Intn(cx.epc.Cfg.ClusterSize)], "POST", e2e.CURLReq{Endpoint: path.Join(p, "/auth/authenticate"), Value: string(authreq)})
proc, err := e2e.SpawnCmd(cmdArgs, cx.envMap) proc, err := e2e.SpawnCmd(cmdArgs, cx.envMap)
testutil.AssertNil(cx.t, err) testutil.AssertNil(cx.t, err)
defer proc.Close() defer proc.Close()
@ -285,7 +285,7 @@ func testV3CurlCampaign(cx ctlCtx) {
if err != nil { if err != nil {
cx.t.Fatal(err) cx.t.Fatal(err)
} }
cargs := e2e.CURLPrefixArgs(cx.epc.Cfg, cx.epc.Procs[rand.Intn(cx.epc.Cfg.ClusterSize)], "POST", e2e.CURLReq{ cargs := e2e.CURLPrefixArgsCluster(cx.epc.Cfg, cx.epc.Procs[rand.Intn(cx.epc.Cfg.ClusterSize)], "POST", e2e.CURLReq{
Endpoint: path.Join(cx.apiPrefix, "/election/campaign"), Endpoint: path.Join(cx.apiPrefix, "/election/campaign"),
Value: string(cdata), Value: string(cdata),
}) })

View File

@ -47,27 +47,30 @@ func (r CURLReq) timeoutDuration() time.Duration {
return 5 * time.Second return 5 * time.Second
} }
// CURLPrefixArgs builds the beginning of a curl command for a given key // CURLPrefixArgsCluster builds the beginning of a curl command for a given key
// addressed to a random URL in the given cluster. // addressed to a random URL in the given cluster.
func CURLPrefixArgs(cfg *EtcdProcessClusterConfig, member EtcdProcess, method string, req CURLReq) []string { func CURLPrefixArgsCluster(cfg *EtcdProcessClusterConfig, member EtcdProcess, method string, req CURLReq) []string {
return CURLPrefixArgs(member.Config().ClientURL, cfg.Client, cfg.CN, method, req)
}
func CURLPrefixArgs(clientURL string, cfg ClientConfig, CN bool, method string, req CURLReq) []string {
var ( var (
cmdArgs = []string{"curl"} cmdArgs = []string{"curl"}
acurl = member.Config().ClientURL
) )
if req.IsTLS { if req.IsTLS {
if cfg.Client.ConnectionType != ClientTLSAndNonTLS { if cfg.ConnectionType != ClientTLSAndNonTLS {
panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS") panic("should not use cURLPrefixArgsUseTLS when serving only TLS or non-TLS")
} }
cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath) cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath)
acurl = ToTLS(member.Config().ClientURL) clientURL = ToTLS(clientURL)
} else if cfg.Client.ConnectionType == ClientTLS { } else if cfg.ConnectionType == ClientTLS {
if cfg.CN { if CN {
cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath) cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath, "--key", PrivateKeyPath)
} else { } else {
cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath3, "--key", PrivateKeyPath3) cmdArgs = append(cmdArgs, "--cacert", CaPath, "--cert", CertPath3, "--key", PrivateKeyPath3)
} }
} }
ep := acurl + req.Endpoint ep := clientURL + req.Endpoint
if req.Username != "" || req.Password != "" { if req.Username != "" || req.Password != "" {
cmdArgs = append(cmdArgs, "-L", "-u", fmt.Sprintf("%s:%s", req.Username, req.Password), ep) cmdArgs = append(cmdArgs, "-L", "-u", fmt.Sprintf("%s:%s", req.Username, req.Password), ep)
@ -100,18 +103,18 @@ func CURLPrefixArgs(cfg *EtcdProcessClusterConfig, member EtcdProcess, method st
func CURLPost(clus *EtcdProcessCluster, req CURLReq) error { func CURLPost(clus *EtcdProcessCluster, req CURLReq) error {
ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration()) ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration())
defer cancel() defer cancel()
return SpawnWithExpectsContext(ctx, CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", req), nil, req.Expected) return SpawnWithExpectsContext(ctx, CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "POST", req), nil, req.Expected)
} }
func CURLPut(clus *EtcdProcessCluster, req CURLReq) error { func CURLPut(clus *EtcdProcessCluster, req CURLReq) error {
ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration()) ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration())
defer cancel() defer cancel()
return SpawnWithExpectsContext(ctx, CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "PUT", req), nil, req.Expected) return SpawnWithExpectsContext(ctx, CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "PUT", req), nil, req.Expected)
} }
func CURLGet(clus *EtcdProcessCluster, req CURLReq) error { func CURLGet(clus *EtcdProcessCluster, req CURLReq) error {
ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration()) ctx, cancel := context.WithTimeout(context.Background(), req.timeoutDuration())
defer cancel() defer cancel()
return SpawnWithExpectsContext(ctx, CURLPrefixArgs(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "GET", req), nil, req.Expected) return SpawnWithExpectsContext(ctx, CURLPrefixArgsCluster(clus.Cfg, clus.Procs[rand.Intn(clus.Cfg.ClusterSize)], "GET", req), nil, req.Expected)
} }