Skip to content

Commit

Permalink
add test case
Browse files Browse the repository at this point in the history
  • Loading branch information
fate0 committed May 31, 2020
1 parent dab6a69 commit 1d34252
Showing 1 changed file with 66 additions and 0 deletions.
66 changes: 66 additions & 0 deletions pkg/client/load_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package client

import (
"crypto/tls"
"crypto/x509"
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
Expand Down Expand Up @@ -73,3 +76,66 @@ func TestLoadCoordinateURLPaths(t *testing.T) {
t.Errorf("Wanted %d but got %d", 30, len(cass.URLPaths))
}
}

func TestCoordinateTLSConfig(t *testing.T) {
srv := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
w.Write([]byte("ok"))
}))

pemCerts, err := ioutil.ReadFile("testdata/ca.pem")
if err != nil {
t.Fatal("Invalid ca.pem path")
}

ca := x509.NewCertPool()
if !ca.AppendCertsFromPEM(pemCerts) {
t.Fatal("Failed to read CA from PEM")
}

cert, err := tls.LoadX509KeyPair("testdata/server.pem", "testdata/server-key.pem")
if err != nil {
t.Fatal("Invalid server.pem/server-key.pem path")
}

srv.TLS = &tls.Config{
ClientCAs: ca,
ClientAuth: tls.RequireAndVerifyClientCert,
Certificates: []tls.Certificate{cert},
}
srv.StartTLS()

cert, err = tls.LoadX509KeyPair("testdata/client.pem", "testdata/client-key.pem")
if err != nil {
t.Fatal("Invalid client.pem/client-key.pem path")
}
clientTLSConfig := &tls.Config{
RootCAs: ca,
Certificates: []tls.Certificate{cert},
}

cass := Cassowary{
BaseURL: srv.URL,
ConcurrencyLevel: 1,
Requests: 10,
TLSConfig: clientTLSConfig,
DisableTerminalOutput: true,
}

metrics, err := cass.Coordinate()
if err != nil {
t.Error(err)
}

if metrics.BaseURL != srv.URL {
t.Errorf("Wanted %s but got %s", srv.URL, metrics.BaseURL)
}

if metrics.TotalRequests != 10 {
t.Errorf("Wanted %d but got %d", 1, metrics.TotalRequests)
}

if metrics.FailedRequests != 0 {
t.Errorf("Wanted %d but got %d", 0, metrics.FailedRequests)
}
}

0 comments on commit 1d34252

Please sign in to comment.