pkg: authorization: cleanup tests
- do use use log pkg - do not t.Fatal in goroutine - cleanups Signed-off-by: Antonio Murdaca <runcom@redhat.com>
This commit is contained in:
parent
8a7fc5c59d
commit
e2b595b73b
1 changed files with 9 additions and 23 deletions
|
@ -8,7 +8,6 @@ package authorization
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"log"
|
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
|
@ -139,7 +138,6 @@ func TestResponseModifier(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestDrainBody(t *testing.T) {
|
func TestDrainBody(t *testing.T) {
|
||||||
|
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
length int // length is the message length send to drainBody
|
length int // length is the message length send to drainBody
|
||||||
expectedBodyLength int // expectedBodyLength is the expected body length after drainBody is called
|
expectedBodyLength int // expectedBodyLength is the expected body length after drainBody is called
|
||||||
|
@ -151,18 +149,17 @@ func TestDrainBody(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
|
|
||||||
msg := strings.Repeat("a", test.length)
|
msg := strings.Repeat("a", test.length)
|
||||||
body, closer, err := drainBody(ioutil.NopCloser(bytes.NewReader([]byte(msg))))
|
body, closer, err := drainBody(ioutil.NopCloser(bytes.NewReader([]byte(msg))))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
if len(body) != test.expectedBodyLength {
|
if len(body) != test.expectedBodyLength {
|
||||||
t.Fatalf("Body must be copied, actual length: '%d'", len(body))
|
t.Fatalf("Body must be copied, actual length: '%d'", len(body))
|
||||||
}
|
}
|
||||||
if closer == nil {
|
if closer == nil {
|
||||||
t.Fatalf("Closer must not be nil")
|
t.Fatalf("Closer must not be nil")
|
||||||
}
|
}
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Error must not be nil: '%v'", err)
|
|
||||||
}
|
|
||||||
modified, err := ioutil.ReadAll(closer)
|
modified, err := ioutil.ReadAll(closer)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Error must not be nil: '%v'", err)
|
t.Fatalf("Error must not be nil: '%v'", err)
|
||||||
|
@ -207,7 +204,7 @@ func createTestPlugin(t *testing.T) *authorizationPlugin {
|
||||||
plugin := &plugins.Plugin{Name: "authz"}
|
plugin := &plugins.Plugin{Name: "authz"}
|
||||||
pwd, err := os.Getwd()
|
pwd, err := os.Getwd()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
t.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.Client, err = plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), tlsconfig.Options{InsecureSkipVerify: true})
|
plugin.Client, err = plugins.NewClient("unix:///"+path.Join(pwd, pluginAddress), tlsconfig.Options{InsecureSkipVerify: true})
|
||||||
|
@ -232,16 +229,11 @@ type authZPluginTestServer struct {
|
||||||
func (t *authZPluginTestServer) start() {
|
func (t *authZPluginTestServer) start() {
|
||||||
r := mux.NewRouter()
|
r := mux.NewRouter()
|
||||||
os.Remove(pluginAddress)
|
os.Remove(pluginAddress)
|
||||||
l, err := net.ListenUnix("unix", &net.UnixAddr{Name: pluginAddress, Net: "unix"})
|
l, _ := net.ListenUnix("unix", &net.UnixAddr{Name: pluginAddress, Net: "unix"})
|
||||||
if err != nil {
|
|
||||||
t.t.Fatalf("Failed to listen %v", err)
|
|
||||||
}
|
|
||||||
t.listener = l
|
t.listener = l
|
||||||
|
|
||||||
r.HandleFunc("/Plugin.Activate", t.activate)
|
r.HandleFunc("/Plugin.Activate", t.activate)
|
||||||
r.HandleFunc("/"+AuthZApiRequest, t.auth)
|
r.HandleFunc("/"+AuthZApiRequest, t.auth)
|
||||||
r.HandleFunc("/"+AuthZApiResponse, t.auth)
|
r.HandleFunc("/"+AuthZApiResponse, t.auth)
|
||||||
t.listener, _ = net.Listen("tcp", pluginAddress)
|
|
||||||
server := http.Server{Handler: r, Addr: pluginAddress}
|
server := http.Server{Handler: r, Addr: pluginAddress}
|
||||||
server.Serve(l)
|
server.Serve(l)
|
||||||
}
|
}
|
||||||
|
@ -257,20 +249,14 @@ func (t *authZPluginTestServer) stop() {
|
||||||
// auth is a used to record/replay the authentication api messages
|
// auth is a used to record/replay the authentication api messages
|
||||||
func (t *authZPluginTestServer) auth(w http.ResponseWriter, r *http.Request) {
|
func (t *authZPluginTestServer) auth(w http.ResponseWriter, r *http.Request) {
|
||||||
t.recordedRequest = Request{}
|
t.recordedRequest = Request{}
|
||||||
defer r.Body.Close()
|
body, _ := ioutil.ReadAll(r.Body)
|
||||||
body, err := ioutil.ReadAll(r.Body)
|
r.Body.Close()
|
||||||
json.Unmarshal(body, &t.recordedRequest)
|
json.Unmarshal(body, &t.recordedRequest)
|
||||||
b, err := json.Marshal(t.replayResponse)
|
b, _ := json.Marshal(t.replayResponse)
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *authZPluginTestServer) activate(w http.ResponseWriter, r *http.Request) {
|
func (t *authZPluginTestServer) activate(w http.ResponseWriter, r *http.Request) {
|
||||||
b, err := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
|
b, _ := json.Marshal(plugins.Manifest{Implements: []string{AuthZApiImplements}})
|
||||||
if err != nil {
|
|
||||||
log.Fatal(err)
|
|
||||||
}
|
|
||||||
w.Write(b)
|
w.Write(b)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue