| 1 |
package dynect |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"strconv" |
| 6 |
"strings" |
| 7 |
"testing" |
| 8 |
|
| 9 |
"github.com/dnaeon/go-vcr/recorder" |
| 10 |
) |
| 11 |
|
| 12 |
// test helper to setup convenient client with vcr cassette |
| 13 |
func withConvenientClient(cassetteName string, f func(*ConvenientClient)) { |
| 14 |
withCassette(cassetteName, func(r *recorder.Recorder) { |
| 15 |
c := NewConvenientClient(DynCustomerName) |
| 16 |
c.SetTransport(r) |
| 17 |
c.Verbose(true) |
| 18 |
|
| 19 |
f(c) |
| 20 |
}) |
| 21 |
} |
| 22 |
|
| 23 |
// test helper to setup authenticated convenient client with vcr cassette |
| 24 |
func testWithConvenientClientSession(cassetteName string, t *testing.T, f func(*ConvenientClient)) { |
| 25 |
withConvenientClient(cassetteName, func(c *ConvenientClient) { |
| 26 |
if err := c.Login(DynUsername, DynPassword); err != nil { |
| 27 |
t.Error(err) |
| 28 |
} |
| 29 |
|
| 30 |
defer func() { |
| 31 |
if err := c.Logout(); err != nil { |
| 32 |
t.Error(err) |
| 33 |
} |
| 34 |
}() |
| 35 |
|
| 36 |
f(c) |
| 37 |
}) |
| 38 |
} |
| 39 |
|
| 40 |
func TestConvenientLoginLogout(t *testing.T) { |
| 41 |
withConvenientClient("fixtures/login_logout", func(c *ConvenientClient) { |
| 42 |
if err := c.Login(DynUsername, DynPassword); err != nil { |
| 43 |
t.Error(err) |
| 44 |
} |
| 45 |
|
| 46 |
if err := c.Logout(); err != nil { |
| 47 |
t.Error(err) |
| 48 |
} |
| 49 |
}) |
| 50 |
} |
| 51 |
|
| 52 |
func TestConvenientGetA(t *testing.T) { |
| 53 |
testWithConvenientClientSession("fixtures/convenient_get_a", t, func(c *ConvenientClient) { |
| 54 |
actual := Record{ |
| 55 |
Zone: testZone, |
| 56 |
Type: "A", |
| 57 |
FQDN: "foobar." + testZone, |
| 58 |
} |
| 59 |
|
| 60 |
if err := c.GetRecordID(&actual); err != nil { |
| 61 |
t.Fatal(err) |
| 62 |
} |
| 63 |
|
| 64 |
if err := c.GetRecord(&actual); err != nil { |
| 65 |
t.Fatal(err) |
| 66 |
} |
| 67 |
|
| 68 |
if actual.Value != "10.9.8.7" { |
| 69 |
t.Fatalf("Incorrect value %q for %q (expected %q)", actual.Value, actual.FQDN, "foobar.go-dynect.test.") |
| 70 |
} |
| 71 |
|
| 72 |
t.Log("OK") |
| 73 |
}) |
| 74 |
} |
| 75 |
|
| 76 |
func TestConvenientGetANotFound(t *testing.T) { |
| 77 |
testWithConvenientClientSession("fixtures/convenient_get_a_not_found", t, func(c *ConvenientClient) { |
| 78 |
actual := Record{ |
| 79 |
Zone: testZone, |
| 80 |
Type: "A", |
| 81 |
FQDN: "unknown." + testZone, |
| 82 |
} |
| 83 |
|
| 84 |
if err := c.GetRecordID(&actual); err == nil { |
| 85 |
t.Fatalf("Expected error getting %q", actual.FQDN) |
| 86 |
} else if !strings.HasPrefix(err.Error(), "Failed to find Dyn record id:") { |
| 87 |
t.Fatalf("Expected error %q for %q (actual error %q)", "Failed to find Dyn record id:", actual.FQDN, err.Error()) |
| 88 |
} |
| 89 |
|
| 90 |
t.Log("OK") |
| 91 |
}) |
| 92 |
} |
| 93 |
|
| 94 |
func TestConvenientGetCNAME(t *testing.T) { |
| 95 |
testWithConvenientClientSession("fixtures/convenient_get_cname", t, func(c *ConvenientClient) { |
| 96 |
actual := Record{ |
| 97 |
Zone: testZone, |
| 98 |
Type: "CNAME", |
| 99 |
FQDN: "foo." + testZone, |
| 100 |
} |
| 101 |
|
| 102 |
if err := c.GetRecordID(&actual); err != nil { |
| 103 |
t.Fatal(err) |
| 104 |
} |
| 105 |
|
| 106 |
if err := c.GetRecord(&actual); err != nil { |
| 107 |
t.Fatal(err) |
| 108 |
} |
| 109 |
|
| 110 |
if actual.Value != "foobar.go-dynect.test." { |
| 111 |
t.Fatalf("Incorrect value %q (expected %q)", actual.Value, "foobar.go-dynect.test.") |
| 112 |
} |
| 113 |
|
| 114 |
t.Log("OK") |
| 115 |
}) |
| 116 |
} |
| 117 |
|
| 118 |
func TestConvenientCreateMX(t *testing.T) { |
| 119 |
testWithConvenientClientSession("fixtures/convenient_create_mx", t, func(c *ConvenientClient) { |
| 120 |
record := Record{ |
| 121 |
Zone: testZone, |
| 122 |
Type: "MX", |
| 123 |
Value: "123 mx.example.com.", |
| 124 |
TTL: "12345", |
| 125 |
} |
| 126 |
|
| 127 |
if err := c.CreateRecord(&record); err != nil { |
| 128 |
t.Fatal(err) |
| 129 |
} |
| 130 |
|
| 131 |
if err := c.PublishZone(testZone); err != nil { |
| 132 |
t.Fatal(err) |
| 133 |
} |
| 134 |
|
| 135 |
if err := c.GetRecordID(&record); err != nil { |
| 136 |
t.Fatal(err) |
| 137 |
} |
| 138 |
|
| 139 |
if err := c.GetRecord(&record); err != nil { |
| 140 |
t.Fatal(err) |
| 141 |
} |
| 142 |
|
| 143 |
if record.FQDN != testZone { |
| 144 |
t.Fatalf("Expected FQDN %q (actual %q)", testZone, record.FQDN) |
| 145 |
} |
| 146 |
|
| 147 |
id, err := strconv.Atoi(record.ID) |
| 148 |
if err != nil || id <= 0 { |
| 149 |
t.Fatalf("Expected ID to be positive integer (actual %q)", record.ID) |
| 150 |
} |
| 151 |
|
| 152 |
ttl, err := strconv.Atoi(record.TTL) |
| 153 |
if err != nil || ttl != 12345 { |
| 154 |
t.Fatalf("Expected ID to be 12345 (actual %q)", record.TTL) |
| 155 |
} |
| 156 |
|
| 157 |
t.Log("OK") |
| 158 |
}) |
| 159 |
} |
| 160 |
|
| 161 |
func TestConvenientCreateZone(t *testing.T) { |
| 162 |
testWithConvenientClientSession("fixtures/convenient_create_zone", t, func(c *ConvenientClient) { |
| 163 |
subZone := fmt.Sprintf("subzone.%s", testZone) |
| 164 |
|
| 165 |
if err := c.CreateZone(subZone, "[email protected]", "day", "1800"); err != nil { |
| 166 |
t.Fatal(err) |
| 167 |
} |
| 168 |
|
| 169 |
if err := c.PublishZone(subZone); err != nil { |
| 170 |
t.Fatal(err) |
| 171 |
} |
| 172 |
|
| 173 |
z := &Zone{Zone: subZone} |
| 174 |
|
| 175 |
if err := c.GetZone(z); err != nil { |
| 176 |
t.Fatal(err) |
| 177 |
} |
| 178 |
|
| 179 |
if z.Zone != subZone { |
| 180 |
t.Fatalf("Expected Zone of %q (actual %q)", subZone, z.Zone) |
| 181 |
} |
| 182 |
|
| 183 |
if z.Type != "Primary" { |
| 184 |
t.Fatalf("Expected Zone Type of %q (actual %q)", "Primary", z.Type) |
| 185 |
} |
| 186 |
|
| 187 |
if z.SerialStyle != "day" { |
| 188 |
t.Fatalf("Expected SerialStyle of %q (actual %q)", "day", z.SerialStyle) |
| 189 |
} |
| 190 |
|
| 191 |
if z.Serial == "" { |
| 192 |
t.Fatalf("Expected non-empty Serial (actual %q)", z.Serial) |
| 193 |
} |
| 194 |
|
| 195 |
t.Log("OK") |
| 196 |
}) |
| 197 |
} |
| 198 |
|
| 199 |
func TestConvenientDeleteZone(t *testing.T) { |
| 200 |
testWithConvenientClientSession("fixtures/convenient_delete_zone", t, func(c *ConvenientClient) { |
| 201 |
subZone := fmt.Sprintf("zone-%s", testZone) |
| 202 |
|
| 203 |
if err := c.DeleteZone(subZone); err != nil { |
| 204 |
t.Fatal(err) |
| 205 |
} |
| 206 |
|
| 207 |
z := &Zone{Zone: subZone} |
| 208 |
|
| 209 |
if err := c.GetZone(z); err == nil { |
| 210 |
t.Fatalf("Zone %q not deleted", subZone) |
| 211 |
} |
| 212 |
|
| 213 |
t.Log("OK") |
| 214 |
}) |
| 215 |
} |
| 216 |
|
| 217 |
func TestConvenientDeleteSubZone(t *testing.T) { |
| 218 |
testWithConvenientClientSession("fixtures/convenient_delete_sub_zone", t, func(c *ConvenientClient) { |
| 219 |
subZone := fmt.Sprintf("subzone.%s", testZone) |
| 220 |
|
| 221 |
if err := c.DeleteZone(subZone); err != nil { |
| 222 |
t.Fatal(err) |
| 223 |
} |
| 224 |
|
| 225 |
if err := c.DeleteZoneNode(subZone); err != nil { |
| 226 |
t.Fatal(err) |
| 227 |
} |
| 228 |
|
| 229 |
if err := c.PublishZone(testZone); err != nil { |
| 230 |
t.Fatal(err) |
| 231 |
} |
| 232 |
|
| 233 |
z := &Zone{Zone: subZone} |
| 234 |
|
| 235 |
if err := c.GetZone(z); err == nil { |
| 236 |
t.Fatalf("Zone %q not deleted", subZone) |
| 237 |
} |
| 238 |
|
| 239 |
t.Log("OK") |
| 240 |
}) |
| 241 |
} |