| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | package test
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bufio"
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | 	"fmt"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	"os"
 | 
					
						
							|  |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 20:29:38 +01:00
										 |  |  | 	"strconv"
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | 	"strings"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	"testing"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-09-24 18:14:41 +02:00
										 |  |  | 	"github.com/coredns/caddy"
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	"github.com/coredns/coredns/core/dnsserver"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 07:14:49 +00:00
										 |  |  | // As we use the filesystem as-is, these files need to exist ON DISK for the readme test to work. This is especially
 | 
					
						
							|  |  |  | // useful for the *file* and *dnssec* plugins as their Corefiles are now tested as well. We create files in the
 | 
					
						
							|  |  |  | // current dir for all these, meaning the example READMEs MUST use relative path in their READMEs.
 | 
					
						
							|  |  |  | var contents = map[string]string{
 | 
					
						
							|  |  |  | 	"Kexample.org.+013+45330.key":     examplePub,
 | 
					
						
							|  |  |  | 	"Kexample.org.+013+45330.private": examplePriv,
 | 
					
						
							|  |  |  | 	"example.org.signed":              exampleOrg, // not signed, but does not matter for this test.
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const (
 | 
					
						
							|  |  |  | 	examplePub = `example.org. IN DNSKEY 256 3 13 eNMYFZYb6e0oJOV47IPo5f/UHy7wY9aBebotvcKakIYLyyGscBmXJQhbKLt/LhrMNDE2Q96hQnI5PdTBeOLzhQ==
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | 	examplePriv = `Private-key-format: v1.3
 | 
					
						
							|  |  |  | Algorithm: 13 (ECDSAP256SHA256)
 | 
					
						
							|  |  |  | PrivateKey: f03VplaIEA+KHI9uizlemUSbUJH86hPBPjmcUninPoM=
 | 
					
						
							|  |  |  | `
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // TestReadme parses all README.mds of the plugins and checks if every example Corefile
 | 
					
						
							| 
									
										
										
										
											2017-10-18 17:20:27 +01:00
										 |  |  | // actually works. Each corefile snippet is only used if the language is set to 'corefile':
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // ~~~ corefile
 | 
					
						
							|  |  |  | // . {
 | 
					
						
							|  |  |  | //	# check-this-please
 | 
					
						
							|  |  |  | // }
 | 
					
						
							|  |  |  | // ~~~
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | //
 | 
					
						
							|  |  |  | // While we're at it - we also check the README.md itself. It should at least have the sections:
 | 
					
						
							|  |  |  | // Name, Description, Syntax and Examples. See plugin.md for more details.
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 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-10-31 07:14:49 +00:00
										 |  |  | 	create(contents)
 | 
					
						
							|  |  |  | 	defer remove(contents)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 09:36:06 +01:00
										 |  |  | 	middle := filepath.Join("..", "plugin")
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 	dirs, err := os.ReadDir(middle)
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 	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")
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | 		if err := sectionsFromReadme(readme); err != nil {
 | 
					
						
							|  |  |  | 			t.Fatal(err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 		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 {
 | 
					
						
							| 
									
										
										
										
											2018-08-14 17:55:55 +02:00
										 |  |  | 			temp += line + "\n" // read newline stripped by s.Text()
 | 
					
						
							| 
									
										
										
										
											2017-09-10 19:52:15 +01:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if err := s.Err(); err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return input, nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							| 
									
										
										
										
											2017-10-31 07:14:49 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | // sectionsFromReadme returns an error if the readme doesn't contains all
 | 
					
						
							|  |  |  | // mandatory sections. The check is basic, as we match each line, this mostly
 | 
					
						
							|  |  |  | // works, because markdown is such a simple format.
 | 
					
						
							|  |  |  | // We want: Name, Description, Syntax, Examples - in this order.
 | 
					
						
							|  |  |  | func sectionsFromReadme(readme string) error {
 | 
					
						
							|  |  |  | 	f, err := os.Open(readme)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil // don't error when we can read the file
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	defer f.Close()
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	section := 0
 | 
					
						
							|  |  |  | 	s := bufio.NewScanner(f)
 | 
					
						
							|  |  |  | 	for s.Scan() {
 | 
					
						
							|  |  |  | 		line := s.Text()
 | 
					
						
							| 
									
										
										
										
											2020-11-05 11:52:24 +01:00
										 |  |  | 		if strings.HasPrefix(line, "## Also See") {
 | 
					
						
							|  |  |  | 			return fmt.Errorf("Please use %q instead of %q", "See Also", "Also See")
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-31 12:37:24 +01:00
										 |  |  | 		switch section {
 | 
					
						
							|  |  |  | 		case 0:
 | 
					
						
							|  |  |  | 			if strings.HasPrefix(line, "## Name") {
 | 
					
						
							|  |  |  | 				section++
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		case 1:
 | 
					
						
							|  |  |  | 			if strings.HasPrefix(line, "## Description") {
 | 
					
						
							|  |  |  | 				section++
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		case 2:
 | 
					
						
							|  |  |  | 			if strings.HasPrefix(line, "## Syntax") {
 | 
					
						
							|  |  |  | 				section++
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		case 3:
 | 
					
						
							|  |  |  | 			if strings.HasPrefix(line, "## Examples") {
 | 
					
						
							|  |  |  | 				section++
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if section != 4 {
 | 
					
						
							|  |  |  | 		return fmt.Errorf("Sections incomplete or ordered wrong: %q, want (at least): Name, Descripion, Syntax and Examples", readme)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-31 07:14:49 +00:00
										 |  |  | func create(c map[string]string) {
 | 
					
						
							|  |  |  | 	for name, content := range c {
 | 
					
						
							| 
									
										
										
										
											2021-10-13 15:30:31 +08:00
										 |  |  | 		os.WriteFile(name, []byte(content), 0644)
 | 
					
						
							| 
									
										
										
										
											2017-10-31 07:14:49 +00:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func remove(c map[string]string) {
 | 
					
						
							|  |  |  | 	for name := range c {
 | 
					
						
							|  |  |  | 		os.Remove(name)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | }
 |