From 519666a97c2454784502a322183c113218b2030e Mon Sep 17 00:00:00 2001 From: James Phillips Date: Tue, 10 Nov 2015 11:33:00 -0800 Subject: [PATCH] Adds query parsing unit tests. --- consul/prepared_query_endpoint_test.go | 42 ++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/consul/prepared_query_endpoint_test.go b/consul/prepared_query_endpoint_test.go index bb5b0f231b..c393a77c79 100644 --- a/consul/prepared_query_endpoint_test.go +++ b/consul/prepared_query_endpoint_test.go @@ -493,3 +493,45 @@ func TestPreparedQuery_Apply_ACLDeny(t *testing.T) { } } } + +func TestPreparedQuery_parseQuery(t *testing.T) { + query := &structs.PreparedQuery{} + + err := parseQuery(query) + if err == nil || !strings.Contains(err.Error(), "Must provide a service") { + t.Fatalf("bad: %v", err) + } + + query.Service.Service = "foo" + if err := parseQuery(query); err != nil { + t.Fatalf("err: %v", err) + } + + query.Service.Failover.NearestN = -1 + err = parseQuery(query) + if err == nil || !strings.Contains(err.Error(), "Bad NearestN") { + t.Fatalf("bad: %v", err) + } + + query.Service.Failover.NearestN = 3 + if err := parseQuery(query); err != nil { + t.Fatalf("err: %v", err) + } + + query.DNS.TTL = "two fortnights" + err = parseQuery(query) + if err == nil || !strings.Contains(err.Error(), "Bad DNS TTL") { + t.Fatalf("bad: %v", err) + } + + query.DNS.TTL = "-3s" + err = parseQuery(query) + if err == nil || !strings.Contains(err.Error(), "must be >=0") { + t.Fatalf("bad: %v", err) + } + + query.DNS.TTL = "3s" + if err := parseQuery(query); err != nil { + t.Fatalf("err: %v", err) + } +}