github.com/mehmetalisavas/terraform@v0.7.10/builtin/providers/cloudflare/resource_cloudflare_record_migrate_test.go (about)

     1  package cloudflare
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"net/http"
     7  	"net/http/httptest"
     8  	"net/url"
     9  	"testing"
    10  
    11  	"github.com/cloudflare/cloudflare-go"
    12  	"github.com/hashicorp/terraform/terraform"
    13  )
    14  
    15  func TestCloudFlareRecordMigrateState(t *testing.T) {
    16  	// create the test server for mocking the API calls
    17  	ts := mockCloudFlareEnv()
    18  	defer ts.Close()
    19  
    20  	// Create a CloudFlare client, overriding the BaseURL
    21  	cfMeta, err := cloudflare.New(
    22  		"sometoken",
    23  		"someemail",
    24  		mockHTTPClient(ts.URL),
    25  	)
    26  
    27  	if err != nil {
    28  		t.Fatalf("Error building CloudFlare API: %s", err)
    29  	}
    30  
    31  	cases := map[string]struct {
    32  		StateVersion int
    33  		ID           string
    34  		Attributes   map[string]string
    35  		Expected     string
    36  		ShouldFail   bool
    37  	}{
    38  		"ttl_120": {
    39  			StateVersion: 0,
    40  			ID:           "123456",
    41  			Attributes: map[string]string{
    42  				"id":       "123456",
    43  				"name":     "notthesub",
    44  				"hostname": "notthesub.hashicorptest.com",
    45  				"type":     "A",
    46  				"content":  "10.0.2.5",
    47  				"ttl":      "120",
    48  				"zone_id":  "1234567890",
    49  				"domain":   "hashicorptest.com",
    50  			},
    51  			Expected: "7778f8766e583af8de0abfcd76c5dAAA",
    52  		},
    53  		"ttl_121": {
    54  			StateVersion: 0,
    55  			ID:           "123456",
    56  			Attributes: map[string]string{
    57  				"id":       "123456",
    58  				"name":     "notthesub",
    59  				"hostname": "notthesub.hashicorptest.com",
    60  				"type":     "A",
    61  				"content":  "10.0.2.5",
    62  				"ttl":      "121",
    63  				"zone_id":  "1234567890",
    64  				"domain":   "hashicorptest.com",
    65  			},
    66  			Expected: "5558f8766e583af8de0abfcd76c5dBBB",
    67  		},
    68  		"mx_priority": {
    69  			StateVersion: 0,
    70  			ID:           "123456",
    71  			Attributes: map[string]string{
    72  				"id":       "123456",
    73  				"name":     "hashicorptest.com",
    74  				"type":     "MX",
    75  				"content":  "some.registrar-servers.com",
    76  				"ttl":      "1",
    77  				"priority": "20",
    78  				"zone_id":  "1234567890",
    79  				"domain":   "hashicorptest.com",
    80  			},
    81  			Expected: "12342092cbc4c391be33ce548713bba3",
    82  		},
    83  		"mx_priority_mismatch": {
    84  			StateVersion: 0,
    85  			ID:           "123456",
    86  			Attributes: map[string]string{
    87  				"id":       "123456",
    88  				"type":     "MX",
    89  				"name":     "hashicorptest.com",
    90  				"content":  "some.registrar-servers.com",
    91  				"ttl":      "1",
    92  				"priority": "10",
    93  				"zone_id":  "1234567890",
    94  				"domain":   "hashicorptest.com",
    95  			},
    96  			Expected:   "12342092cbc4c391be33ce548713bba3",
    97  			ShouldFail: true,
    98  		},
    99  		"proxied": {
   100  			StateVersion: 0,
   101  			ID:           "123456",
   102  			Attributes: map[string]string{
   103  				"id":       "123456",
   104  				"name":     "tftestingsubv616",
   105  				"hostname": "tftestingsubv616.hashicorptest.com",
   106  				"type":     "A",
   107  				"content":  "52.39.212.111",
   108  				"proxied":  "true",
   109  				"ttl":      "1",
   110  				"zone_id":  "1234567890",
   111  				"domain":   "hashicorptest.com",
   112  			},
   113  			Expected: "888ffe3f93a31231ad6b0c6d09185eee",
   114  		},
   115  		"not_proxied": {
   116  			StateVersion: 0,
   117  			ID:           "123456",
   118  			Attributes: map[string]string{
   119  				"id":       "123456",
   120  				"name":     "tftestingsubv616",
   121  				"hostname": "tftestingsubv616.hashicorptest.com",
   122  				"type":     "A",
   123  				"content":  "52.39.212.111",
   124  				"proxied":  "false",
   125  				"ttl":      "1",
   126  				"zone_id":  "1234567890",
   127  				"domain":   "hashicorptest.com",
   128  			},
   129  			Expected: "222ffe3f93a31231ad6b0c6d09185jjj",
   130  		},
   131  	}
   132  
   133  	for tn, tc := range cases {
   134  		is := &terraform.InstanceState{
   135  			ID:         tc.ID,
   136  			Attributes: tc.Attributes,
   137  		}
   138  		is, err := resourceCloudFlareRecordMigrateState(
   139  			tc.StateVersion, is, cfMeta)
   140  
   141  		if err != nil {
   142  			if tc.ShouldFail {
   143  				// expected error
   144  				continue
   145  			}
   146  			t.Fatalf("bad: %s, err: %#v", tn, err)
   147  		}
   148  
   149  		if is.ID != tc.Expected {
   150  			t.Fatalf("bad sg rule id: %s\n\n expected: %s", is.ID, tc.Expected)
   151  		}
   152  	}
   153  }
   154  
   155  // cloudflareEnv establishes a httptest server to mock out the CloudFlare API
   156  // endpoints that we'll be calling.
   157  func mockCloudFlareEnv() *httptest.Server {
   158  	endpoints := mockEndpoints()
   159  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
   160  		w.Header().Set("Content-Type", "text/plain")
   161  		log.Printf("[DEBUG] Mocker server received request to %q", r.RequestURI)
   162  		rBase, err := url.ParseRequestURI(r.RequestURI)
   163  		if err != nil {
   164  			log.Fatalf("Failed to find the base path: %s", err)
   165  		}
   166  		for _, e := range endpoints {
   167  			if rBase.Path == e.BasePath {
   168  				fmt.Fprintln(w, e.Body)
   169  				w.WriteHeader(200)
   170  				return
   171  			}
   172  		}
   173  		w.WriteHeader(400)
   174  	}))
   175  
   176  	return ts
   177  }
   178  
   179  // Stub out the two CloudFlare API routes that will be called
   180  func mockEndpoints() []*endpoint {
   181  	return []*endpoint{
   182  		&endpoint{
   183  			BasePath: "/zones",
   184  			Body:     zoneResponse,
   185  		},
   186  		&endpoint{
   187  			BasePath: "/zones/1234567890/dns_records",
   188  			Body:     dnsResponse,
   189  		},
   190  	}
   191  }
   192  
   193  type routes struct {
   194  	Endpoints []*endpoint
   195  }
   196  type endpoint struct {
   197  	BasePath string
   198  	Body     string
   199  }
   200  
   201  // HTTPClient accepts a custom *http.Client for making API calls.
   202  // This function is used as a callback of sorts to override any of the client
   203  // options that you can't directly set on the struct
   204  func mockHTTPClient(testURL string) cloudflare.Option {
   205  	return func(api *cloudflare.API) error {
   206  		api.BaseURL = testURL
   207  		return nil
   208  	}
   209  }
   210  
   211  const zoneResponse = `
   212  {
   213    "result": [
   214      {
   215        "id": "1234567890",
   216        "name": "hashicorptest.com",
   217        "status": "active",
   218        "paused": false,
   219        "type": "full",
   220        "development_mode": 0
   221      }
   222    ],
   223    "result_info": {
   224      "page": 1,
   225      "per_page": 20,
   226      "total_pages": 1,
   227      "count": 1,
   228      "total_count": 1
   229    },
   230    "success": true,
   231    "errors": [],
   232    "messages": []
   233  }
   234  `
   235  
   236  const dnsResponse = `
   237  {
   238    "result": [
   239      {
   240        "id": "7778f8766e583af8de0abfcd76c5dAAA",
   241        "type": "A",
   242        "name": "notthesub.hashicorptest.com",
   243        "content": "10.0.2.5",
   244        "proxiable": false,
   245        "proxied": false,
   246        "ttl": 120,
   247        "locked": false,
   248        "zone_id": "1234567890",
   249        "zone_name": "hashicorptest.com"
   250      },
   251      {
   252        "id": "5558f8766e583af8de0abfcd76c5dBBB",
   253        "type": "A",
   254        "name": "notthesub.hashicorptest.com",
   255        "content": "10.0.2.5",
   256        "proxiable": false,
   257        "proxied": false,
   258        "ttl": 121,
   259        "locked": false,
   260        "zone_id": "1234567890",
   261        "zone_name": "hashicorptest.com"
   262      },
   263      {
   264        "id": "2220a9593ab869199b65c89bddf72ddd",
   265        "type": "A",
   266        "name": "maybethesub.hashicorptest.com",
   267        "content": "10.0.3.5",
   268        "proxiable": false,
   269        "proxied": false,
   270        "ttl": 120,
   271        "locked": false,
   272        "zone_id": "1234567890",
   273        "zone_name": "hashicorptest.com"
   274      },
   275      {
   276        "id": "222ffe3f93a31231ad6b0c6d09185jjj",
   277        "type": "A",
   278        "name": "tftestingsubv616.hashicorptest.com",
   279        "content": "52.39.212.111",
   280        "proxiable": true,
   281        "proxied": false,
   282        "ttl": 1,
   283        "locked": false,
   284        "zone_id": "1234567890",
   285        "zone_name": "hashicorptest.com"
   286      },
   287      {
   288        "id": "888ffe3f93a31231ad6b0c6d09185eee",
   289        "type": "A",
   290        "name": "tftestingsubv616.hashicorptest.com",
   291        "content": "52.39.212.111",
   292        "proxiable": true,
   293        "proxied": true,
   294        "ttl": 1,
   295        "locked": false,
   296        "zone_id": "1234567890",
   297        "zone_name": "hashicorptest.com"
   298      },
   299      {
   300        "id": "98y6t9ba87e6ee3e6aeba8f3dc52c81b",
   301        "type": "CNAME",
   302        "name": "somecname.hashicorptest.com",
   303        "content": "some.us-west-2.elb.amazonaws.com",
   304        "proxiable": true,
   305        "proxied": false,
   306        "ttl": 120,
   307        "locked": false,
   308        "zone_id": "1234567890",
   309        "zone_name": "hashicorptest.com"
   310      },
   311      {
   312        "id": "12342092cbc4c391be33ce548713bba3",
   313        "type": "MX",
   314        "name": "hashicorptest.com",
   315        "content": "some.registrar-servers.com",
   316        "proxiable": false,
   317        "proxied": false,
   318        "ttl": 1,
   319        "priority": 20,
   320        "locked": false,
   321        "zone_id": "1234567890",
   322        "zone_name": "hashicorptest.com"
   323      }
   324    ],
   325    "result_info": {
   326      "page": 1,
   327      "per_page": 20,
   328      "total_pages": 2,
   329      "count": 20,
   330      "total_count": 4
   331    },
   332    "success": true,
   333    "errors": [],
   334    "messages": []
   335  }`