github.com/skabbes/up@v0.2.1/config/dns_test.go (about)

     1  package config
     2  
     3  import (
     4  	"encoding/json"
     5  	"log"
     6  	"os"
     7  	"sort"
     8  	"testing"
     9  
    10  	"github.com/tj/assert"
    11  )
    12  
    13  func ExampleDNS() {
    14  	s := `{
    15  		"something.sh": [
    16  			{
    17  				"name": "something.com",
    18  				"type": "A",
    19  				"ttl": 300,
    20  				"value": ["35.161.83.243"]
    21  			},
    22  			{
    23  				"name": "blog.something.com",
    24  				"type": "CNAME",
    25  				"ttl": 300,
    26  				"value": ["34.209.172.67"]
    27  			},
    28  			{
    29  				"name": "api.something.com",
    30  				"type": "A",
    31  				"ttl": 300,
    32  				"value": ["54.187.185.18"]
    33  			}
    34  		]
    35  	}`
    36  
    37  	var c DNS
    38  
    39  	if err := json.Unmarshal([]byte(s), &c); err != nil {
    40  		log.Fatalf("error unmarshaling: %s", err)
    41  	}
    42  
    43  	sort.Slice(c.Zones[0].Records, func(i int, j int) bool {
    44  		a := c.Zones[0].Records[i]
    45  		b := c.Zones[0].Records[j]
    46  		return a.Name > b.Name
    47  	})
    48  
    49  	enc := json.NewEncoder(os.Stdout)
    50  	enc.SetIndent("", "  ")
    51  	enc.Encode(c)
    52  	// Output:
    53  	// 	{
    54  	//   "zones": [
    55  	//     {
    56  	//       "name": "something.sh",
    57  	//       "records": [
    58  	//         {
    59  	//           "name": "something.com",
    60  	//           "type": "A",
    61  	//           "ttl": 300,
    62  	//           "value": [
    63  	//             "35.161.83.243"
    64  	//           ]
    65  	//         },
    66  	//         {
    67  	//           "name": "blog.something.com",
    68  	//           "type": "CNAME",
    69  	//           "ttl": 300,
    70  	//           "value": [
    71  	//             "34.209.172.67"
    72  	//           ]
    73  	//         },
    74  	//         {
    75  	//           "name": "api.something.com",
    76  	//           "type": "A",
    77  	//           "ttl": 300,
    78  	//           "value": [
    79  	//             "54.187.185.18"
    80  	//           ]
    81  	//         }
    82  	//       ]
    83  	//     }
    84  	//   ]
    85  	// }
    86  }
    87  
    88  func TestDNS_Validate(t *testing.T) {
    89  	t.Run("invalid", func(t *testing.T) {
    90  		c := &DNS{
    91  			Zones: []*Zone{
    92  				{
    93  					Name: "apex.sh",
    94  					Records: []Record{
    95  						{
    96  							Name: "blog.apex.sh",
    97  							Type: "CNAME",
    98  						},
    99  					},
   100  				},
   101  			},
   102  		}
   103  
   104  		assert.EqualError(t, c.Validate(), `zone apex.sh: record 0: .value: must have at least 1 value`)
   105  	})
   106  }
   107  
   108  func TestRecord_Type(t *testing.T) {
   109  	t.Run("valid", func(t *testing.T) {
   110  		c := &Record{
   111  			Name:  "blog.apex.sh",
   112  			Type:  "A",
   113  			Value: []string{"1.1.1.1"},
   114  		}
   115  
   116  		assert.NoError(t, c.Validate(), "validate")
   117  	})
   118  
   119  	t.Run("invalid", func(t *testing.T) {
   120  		c := &Record{
   121  			Name: "blog.apex.sh",
   122  			Type: "AAA",
   123  		}
   124  
   125  		assert.EqualError(t, c.Validate(), `.type: "AAA" is invalid, must be one of:
   126  
   127    • ALIAS
   128    • A
   129    • AAAA
   130    • CNAME
   131    • MX
   132    • NAPTR
   133    • NS
   134    • PTR
   135    • SOA
   136    • SPF
   137    • SRV
   138    • TXT`)
   139  	})
   140  }
   141  
   142  func TestRecord_TTL(t *testing.T) {
   143  	c := &Record{Type: "A"}
   144  	assert.NoError(t, c.Default(), "default")
   145  	assert.Equal(t, 300, c.TTL)
   146  }
   147  
   148  func TestRecord_Value(t *testing.T) {
   149  	t.Run("empty", func(t *testing.T) {
   150  		c := &Record{
   151  			Name: "blog.apex.sh",
   152  			Type: "A",
   153  		}
   154  
   155  		assert.EqualError(t, c.Validate(), `.value: must have at least 1 value`)
   156  	})
   157  
   158  	t.Run("invalid", func(t *testing.T) {
   159  		c := &Record{
   160  			Name:  "blog.apex.sh",
   161  			Type:  "A",
   162  			Value: []string{"1.1.1.1", ""},
   163  		}
   164  
   165  		assert.EqualError(t, c.Validate(), `.value: at index 1: is required`)
   166  	})
   167  }