| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bufio"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"log"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/mholt/caddy"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | // Pasrse all README.md's of the plugin and check if every example Corefile
 | 
					
						
							| 
									
										
										
										
											2017-09-28 09:46:41 +01:00
										 |  |  | // actually works. Each corefile is only used if the language is set to 'corefile':
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // ~~~ corefile
 | 
					
						
							|  |  |  | // . {
 | 
					
						
							|  |  |  | //	# check-this-please
 | 
					
						
							|  |  |  | // }
 | 
					
						
							|  |  |  | // ~~~
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func TestReadme(t *testing.T) {
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 	port := 30053
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	caddy.Quiet = true
 | 
					
						
							|  |  |  | 	dnsserver.Quiet = true
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	log.SetOutput(ioutil.Discard)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	middle := filepath.Join("..", "plugin")
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	dirs, err := ioutil.ReadDir(middle)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		t.Fatalf("Could not read %s: %q", middle, err)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	for _, d := range dirs {
 | 
					
						
							|  |  |  | 		if !d.IsDir() {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		readme := filepath.Join(middle, d.Name())
 | 
					
						
							|  |  |  | 		readme = filepath.Join(readme, "README.md")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		inputs, err := corefileFromReadme(readme)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		// Test each snippet.
 | 
					
						
							|  |  |  | 		for _, in := range inputs {
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 			dnsserver.Port = strconv.Itoa(port)
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 			server, err := caddy.Start(in)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							| 
									
										
										
										
											2017-09-28 09:46:41 +01:00
										 |  |  | 				t.Errorf("Failed to start server with %s, for input %q:\n%s", readme, err, in.Body())
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 			}
 | 
					
						
							|  |  |  | 			server.Stop()
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 			port++
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // corefileFromReadme parses a readme and returns all fragments that
 | 
					
						
							|  |  |  | // have ~~~ corefile (or ``` corefile).
 | 
					
						
							|  |  |  | func corefileFromReadme(readme string) ([]*Input, error) {
 | 
					
						
							|  |  |  | 	f, err := os.Open(readme)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer f.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	s := bufio.NewScanner(f)
 | 
					
						
							|  |  |  | 	input := []*Input{}
 | 
					
						
							|  |  |  | 	corefile := false
 | 
					
						
							|  |  |  | 	temp := ""
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for s.Scan() {
 | 
					
						
							|  |  |  | 		line := s.Text()
 | 
					
						
							|  |  |  | 		if line == "~~~ corefile" || line == "``` corefile" {
 | 
					
						
							|  |  |  | 			corefile = true
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if corefile && (line == "~~~" || line == "```") {
 | 
					
						
							|  |  |  | 			// last line
 | 
					
						
							|  |  |  | 			input = append(input, NewInput(temp))
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			temp = ""
 | 
					
						
							|  |  |  | 			corefile = false
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if corefile {
 | 
					
						
							|  |  |  | 			temp += line + "\n" // readd newline stripped by s.Text()
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := s.Err(); err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return input, nil
 | 
					
						
							|  |  |  | }
 |