github.com/hexonet/dnscontrol@v0.2.8/providers/gandi/livedns_test.go (about)

     1  package gandi
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/StackExchange/dnscontrol/models"
     7  	"github.com/prasmussen/gandi-api/live_dns/record"
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  func makeRC(label, domain, target string, rc models.RecordConfig) *models.RecordConfig {
    12  	rc.SetLabel(label, domain)
    13  	rc.SetTarget(target)
    14  	return &rc
    15  }
    16  func TestRecordConfigFromInfo(t *testing.T) {
    17  
    18  	for _, data := range []struct {
    19  		info   *record.Info
    20  		config []*models.RecordConfig
    21  	}{
    22  		{
    23  			&record.Info{
    24  				Name:   "www",
    25  				Type:   "A",
    26  				TTL:    500,
    27  				Values: []string{"127.0.0.1", "127.1.0.1"},
    28  			},
    29  			[]*models.RecordConfig{
    30  				makeRC("www", "example.com", "127.0.0.1", models.RecordConfig{
    31  					Type: "A",
    32  					TTL:  500,
    33  				}),
    34  				makeRC("www", "example.com", "127.1.0.1", models.RecordConfig{
    35  					Type: "A",
    36  					TTL:  500,
    37  				}),
    38  			},
    39  		},
    40  		{
    41  			&record.Info{
    42  				Name:   "www",
    43  				Type:   "TXT",
    44  				TTL:    500,
    45  				Values: []string{"\"test 2\"", "\"test message test message test message\""},
    46  			},
    47  			[]*models.RecordConfig{
    48  				makeRC("www", "example.com", "test 2", models.RecordConfig{
    49  					Type:       "TXT",
    50  					TxtStrings: []string{"test 2", "test message test message test message"},
    51  					TTL:        500,
    52  				}),
    53  			},
    54  		},
    55  		{
    56  			&record.Info{
    57  				Name: "www",
    58  				Type: "CAA",
    59  				TTL:  500,
    60  				// examples from https://sslmate.com/caa/
    61  				Values: []string{"0 issue \"www.certinomis.com\"", "0 issuewild \"buypass.com\""},
    62  			},
    63  			[]*models.RecordConfig{
    64  				makeRC("www", "example.com", "www.certinomis.com", models.RecordConfig{
    65  					Type:    "CAA",
    66  					CaaFlag: 0,
    67  					CaaTag:  "issue",
    68  					TTL:     500,
    69  				}),
    70  				makeRC("www", "example.com", "buypass.com", models.RecordConfig{
    71  					Type:    "CAA",
    72  					CaaFlag: 0,
    73  					CaaTag:  "issuewild",
    74  					TTL:     500,
    75  				}),
    76  			},
    77  		},
    78  		{
    79  			&record.Info{
    80  				Name:   "test",
    81  				Type:   "SRV",
    82  				TTL:    500,
    83  				Values: []string{"20 0 5060 backupbox.example.com."},
    84  			},
    85  			[]*models.RecordConfig{
    86  				makeRC("test", "example.com", "backupbox.example.com.", models.RecordConfig{
    87  					Type:        "SRV",
    88  					SrvPriority: 20,
    89  					SrvWeight:   0,
    90  					SrvPort:     5060,
    91  					TTL:         500,
    92  				}),
    93  			},
    94  		},
    95  		{
    96  			&record.Info{
    97  				Name:   "mail",
    98  				Type:   "MX",
    99  				TTL:    500,
   100  				Values: []string{"50 fb.mail.gandi.net.", "10 spool.mail.gandi.net."},
   101  			},
   102  			[]*models.RecordConfig{
   103  				makeRC("mail", "example.com", "fb.mail.gandi.net.", models.RecordConfig{
   104  					Type:         "MX",
   105  					MxPreference: 50,
   106  					TTL:          500,
   107  				}),
   108  				makeRC("mail", "example.com", "spool.mail.gandi.net.", models.RecordConfig{
   109  					Type:         "MX",
   110  					MxPreference: 10,
   111  					TTL:          500,
   112  				}),
   113  			},
   114  		},
   115  	} {
   116  		t.Run("with record type "+data.info.Type, func(t *testing.T) {
   117  			c := liveClient{}
   118  			for _, c := range data.config {
   119  				c.Original = data.info
   120  			}
   121  			t.Run("Convert gandi info to record config", func(t *testing.T) {
   122  				recordConfig := c.recordConfigFromInfo([]*record.Info{data.info}, "example.com")
   123  				assert.Equal(t, data.config, recordConfig)
   124  			})
   125  			t.Run("Convert record config to gandi info", func(t *testing.T) {
   126  				_, recordInfos, err := c.recordsToInfo(data.config)
   127  				assert.NoError(t, err)
   128  				assert.Equal(t, []*record.Info{data.info}, recordInfos)
   129  			})
   130  		})
   131  	}
   132  }