github.com/teknogeek/dnscontrol@v0.2.8/providers/octodns/octoyaml/rw_test.go (about)

     1  package octoyaml
     2  
     3  /*
     4  
     5  Test
     6  	DSL -> yaml
     7  	YAML -> JSON
     8  
     9  	001-test.js
    10  	001-test.yaml
    11  	001-test.json
    12  */
    13  
    14  import (
    15  	"bytes"
    16  	"encoding/json"
    17  	"fmt"
    18  	"io/ioutil"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  	"unicode"
    24  
    25  	"github.com/tdewolff/minify"
    26  	minjson "github.com/tdewolff/minify/json"
    27  )
    28  
    29  const (
    30  	testDir  = "octodns/octoyaml/parse_tests"
    31  	errorDir = "octodns/octoyaml/error_tests"
    32  )
    33  
    34  func init() {
    35  	os.Chdir("../..") // go up a directory so we helpers.js is in a consistent place.
    36  }
    37  
    38  func TestYamlWrite(t *testing.T) {
    39  
    40  	// Read a .JS and make sure we can generate the expected YAML.
    41  
    42  	files, err := ioutil.ReadDir(testDir)
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	for _, f := range files {
    47  
    48  		// Run JS -> conf
    49  
    50  		// run all js files that start with a number. Skip others.
    51  		if filepath.Ext(f.Name()) != ".js" || !unicode.IsNumber(rune(f.Name()[0])) {
    52  			continue
    53  		}
    54  
    55  		m := minify.New()
    56  		m.AddFunc("json", minjson.Minify)
    57  
    58  		t.Run(f.Name(), func(t *testing.T) {
    59  			fname := filepath.Join(testDir, f.Name())
    60  			fmt.Printf("Filename: %v\n", fname)
    61  			content, err := ioutil.ReadFile(fname)
    62  			if err != nil {
    63  				t.Fatal(err)
    64  			}
    65  			conf, err := ExecuteJavascript(string(content), true)
    66  			if err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			basename := f.Name()[:len(f.Name())-3] // Remove ".js"
    70  
    71  			// Run conf -> YAML
    72  
    73  			actualYAML := bytes.NewBuffer([]byte{})
    74  			dom := conf.FindDomain("example.tld")
    75  			if dom == nil {
    76  				panic(fmt.Sprintf("FILE %s does not mention domain '%s'", f.Name(), "example.tld"))
    77  			}
    78  
    79  			err = WriteYaml(
    80  				actualYAML, conf.FindDomain("example.tld").Records, "example.tld")
    81  
    82  			// Read expected YAML
    83  
    84  			expectedFile := filepath.Join(testDir, basename+".yaml")
    85  			expectedData, err := ioutil.ReadFile(expectedFile)
    86  			if err != nil {
    87  				t.Fatal(err)
    88  			}
    89  			expectedYAML := expectedData
    90  
    91  			// Compare YAML and expectedData
    92  
    93  			if string(expectedYAML) != actualYAML.String() {
    94  				t.Error("Expected and actual YAML don't match")
    95  				t.Log("Expected:", string(expectedYAML))
    96  				t.Log("Actual  :", actualYAML.String())
    97  			}
    98  
    99  		})
   100  	}
   101  }
   102  
   103  func TestYamlRead(t *testing.T) {
   104  
   105  	// Read a .YAML and make sure it matches the RecordConfig (.JSON).
   106  
   107  	minifyFlag := true
   108  
   109  	files, err := ioutil.ReadDir(testDir)
   110  	if err != nil {
   111  		t.Fatal(err)
   112  	}
   113  	for _, f := range files {
   114  		// run all yaml files that start with a number. Skip others.
   115  		if filepath.Ext(f.Name()) != ".yaml" || !unicode.IsNumber(rune(f.Name()[0])) {
   116  			continue
   117  		}
   118  		basename := f.Name()[:len(f.Name())-5] // remove ".yaml"
   119  
   120  		m := minify.New()
   121  		m.AddFunc("json", minjson.Minify)
   122  
   123  		t.Run(f.Name(), func(t *testing.T) {
   124  
   125  			// Parse YAML
   126  
   127  			content, err := ioutil.ReadFile(filepath.Join(testDir, f.Name()))
   128  			if err != nil {
   129  				if os.IsNotExist(err) {
   130  					content = nil
   131  				} else {
   132  					t.Fatal(err)
   133  				}
   134  			}
   135  			recs, err := ReadYaml(bytes.NewBufferString(string(content)), "example.tld")
   136  			if err != nil {
   137  				t.Fatal(err)
   138  			}
   139  			//fmt.Printf("DEBUG: CONTENT=%s\n", string(content))
   140  			//fmt.Printf("DEBUG: RECS=%v\n", recs)
   141  
   142  			// YAML -> JSON
   143  
   144  			actualJSON, err := json.MarshalIndent(recs, "", "  ")
   145  			if err != nil {
   146  				t.Fatal(err)
   147  			}
   148  			if minifyFlag {
   149  				// fmt.Printf("DEBUG: actualJSON-full: %s\n", actualJSON)
   150  				actualJSON, err = m.Bytes("json", actualJSON)
   151  			}
   152  			if err != nil {
   153  				t.Fatal(err)
   154  			}
   155  			// fmt.Printf("DEBUG: actualJSON-mini: %s\n", actualJSON)
   156  
   157  			// Read expected JSON
   158  			expectedFile := filepath.Join(testDir, basename+".json")
   159  			expectedData, err := ioutil.ReadFile(expectedFile)
   160  			if err != nil {
   161  				if os.IsNotExist(err) {
   162  					fmt.Println("SKIPPING")
   163  					t.Log("Skipping (no .json)")
   164  					return
   165  				}
   166  				t.Fatal(err)
   167  			}
   168  			var expectedJSON string
   169  			if minifyFlag {
   170  				expectedJSON, err = m.String("json", string(expectedData))
   171  			} else {
   172  				expectedJSON = string(expectedData)
   173  			}
   174  			if err != nil {
   175  				t.Fatal(err)
   176  			}
   177  
   178  			//fmt.Printf("DEBUG: EXPECTED=%s\n", string(expectedJSON))
   179  			//fmt.Printf("DEBUG: ACTUAL  =%s\n", string(actualJSON))
   180  
   181  			if strings.TrimSpace(string(expectedJSON)) != strings.TrimSpace(string(actualJSON)) {
   182  				t.Error("Expected and actual json don't match")
   183  				t.Log("Expected:", string(expectedJSON))
   184  				t.Log("Actual  :", string(actualJSON))
   185  			}
   186  		})
   187  	}
   188  }