Rainbond/gateway/store/store_test.go

71 lines
1.6 KiB
Go
Raw Normal View History

2018-11-08 17:22:26 +08:00
package store
import (
"fmt"
2019-08-29 12:59:44 +08:00
"net/http"
"testing"
2018-12-25 18:51:44 +08:00
"github.com/goodrain/rainbond/gateway/annotations/parser"
api "k8s.io/api/core/v1"
2018-11-08 17:22:26 +08:00
extensions "k8s.io/api/extensions/v1beta1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
2018-12-25 18:51:44 +08:00
func TestRbdStore_checkIngress(t *testing.T) {
ing := buildIngress()
2018-11-08 17:22:26 +08:00
2018-12-25 18:51:44 +08:00
type foo struct {
data map[string]string
expected bool
}
2018-11-08 17:22:26 +08:00
2018-12-25 18:51:44 +08:00
getFoo := func(expected bool, enable, host, port string) foo {
return foo{
expected: expected,
data: map[string]string{
parser.GetAnnotationWithPrefix("l4-enable"): enable,
parser.GetAnnotationWithPrefix("l4-host"): host,
parser.GetAnnotationWithPrefix("l4-port"): port,
2018-11-08 17:22:26 +08:00
},
}
2018-12-25 18:51:44 +08:00
}
2018-11-08 17:22:26 +08:00
2018-12-25 18:51:44 +08:00
testCases := []foo{
getFoo(true, "true", "0.0.0.0", "12345"),
getFoo(true, "true", "", "12345"),
getFoo(false, "true", "0.0.0.0", "0"),
getFoo(false, "true", "0.0.0.0", "65536"),
getFoo(false, "true", "0.0.0.0", "65535"),
{
expected: true,
data: nil,
2018-11-08 17:22:26 +08:00
},
}
2018-12-25 18:51:44 +08:00
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {})
server := &http.Server{
Addr: fmt.Sprintf("0.0.0.0:%v", 65535),
Handler: mux,
2018-11-08 17:22:26 +08:00
}
2018-12-25 18:51:44 +08:00
go server.ListenAndServe()
2018-11-08 17:22:26 +08:00
2018-12-25 18:51:44 +08:00
for _, testCase := range testCases {
ing.SetAnnotations(testCase.data)
2019-08-29 12:59:44 +08:00
s := k8sStore{}
2018-12-25 18:51:44 +08:00
if s.checkIngress(ing) != testCase.expected {
t.Errorf("Expected %v for s.checkIngress(ing), but returned %v. data: %v", testCase.expected,
s.checkIngress(ing), testCase.data)
}
2018-11-08 17:22:26 +08:00
}
}
2018-12-25 18:51:44 +08:00
func buildIngress() *extensions.Ingress {
return &extensions.Ingress{
ObjectMeta: metav1.ObjectMeta{
Name: "foobar",
Namespace: api.NamespaceDefault,
},
2018-11-08 17:22:26 +08:00
}
2018-12-25 18:51:44 +08:00
}