| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | package metrics
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-07-03 09:04:47 +08:00
										 |  |  | 	"github.com/caddyserver/caddy"
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-10-08 16:44:43 +01:00
										 |  |  | func TestPrometheusParse(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 	tests := []struct {
 | 
					
						
							|  |  |  | 		input     string
 | 
					
						
							|  |  |  | 		shouldErr bool
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 		addr      string
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 	}{
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 		// oks
 | 
					
						
							|  |  |  | 		{`prometheus`, false, "localhost:9153"},
 | 
					
						
							|  |  |  | 		{`prometheus localhost:53`, false, "localhost:53"},
 | 
					
						
							|  |  |  | 		// fails
 | 
					
						
							|  |  |  | 		{`prometheus {}`, true, ""},
 | 
					
						
							|  |  |  | 		{`prometheus /foo`, true, ""},
 | 
					
						
							| 
									
										
										
										
											2017-08-22 14:21:42 +01:00
										 |  |  | 		{`prometheus a b c`, true, ""},
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 	for i, test := range tests {
 | 
					
						
							|  |  |  | 		c := caddy.NewTestController("dns", test.input)
 | 
					
						
							| 
									
										
										
										
											2019-05-13 12:26:05 +01:00
										 |  |  | 		m, err := parse(c)
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 		if test.shouldErr && err == nil {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %v: Expected error but found nil", i)
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 			continue
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 		} else if !test.shouldErr && err != nil {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %v: Expected no error but found error: %v", i, err)
 | 
					
						
							| 
									
										
										
										
											2016-10-04 11:05:04 +01:00
										 |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.shouldErr {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if test.addr != m.Addr {
 | 
					
						
							|  |  |  | 			t.Errorf("Test %v: Expected address %s but found: %s", i, test.addr, m.Addr)
 | 
					
						
							| 
									
										
										
										
											2016-09-24 22:47:38 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |