github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/dns/v2/recordsets/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/vnpaycloud-console/gophercloud/v2"
    10  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/dns/v2/recordsets"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  	"github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    13  )
    14  
    15  // ListByZoneOutput is a sample response to a ListByZone call.
    16  const ListByZoneOutput = `
    17  {
    18      "recordsets": [
    19          {
    20              "description": "This is an example record set.",
    21              "links": {
    22                  "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648"
    23              },
    24              "updated_at": null,
    25              "records": [
    26                  "10.1.0.2"
    27              ],
    28              "ttl": 3600,
    29              "id": "f7b10e9b-0cae-4a91-b162-562bc6096648",
    30              "name": "example.org.",
    31              "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    32              "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
    33              "zone_name": "example.com.",
    34              "created_at": "2014-10-24T19:59:44.000000",
    35              "version": 1,
    36              "type": "A",
    37              "status": "PENDING",
    38              "action": "CREATE"
    39          },
    40          {
    41              "description": "This is another example record set.",
    42              "links": {
    43                  "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/7423aeaf-b354-4bd7-8aba-2e831567b478"
    44              },
    45              "updated_at": "2017-03-04T14:29:07.000000",
    46              "records": [
    47                  "10.1.0.3",
    48                  "10.1.0.4"
    49              ],
    50              "ttl": 3600,
    51              "id": "7423aeaf-b354-4bd7-8aba-2e831567b478",
    52              "name": "foo.example.org.",
    53              "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    54              "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
    55              "zone_name": "example.com.",
    56              "created_at": "2014-10-24T19:59:44.000000",
    57              "version": 1,
    58              "type": "A",
    59              "status": "PENDING",
    60              "action": "CREATE"
    61          }
    62      ],
    63      "links": {
    64          "self": "http://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets"
    65      },
    66      "metadata": {
    67          "total_count": 2
    68      }
    69  }
    70  `
    71  
    72  // ListByZoneOutputLimited is a sample response to a ListByZone call with a requested limit.
    73  const ListByZoneOutputLimited = `
    74  {
    75      "recordsets": [
    76          {
    77              "description": "This is another example record set.",
    78              "links": {
    79                  "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/7423aeaf-b354-4bd7-8aba-2e831567b478"
    80              },
    81              "updated_at": "2017-03-04T14:29:07.000000",
    82              "records": [
    83                  "10.1.0.3",
    84                  "10.1.0.4"
    85              ],
    86              "ttl": 3600,
    87              "id": "7423aeaf-b354-4bd7-8aba-2e831567b478",
    88              "name": "foo.example.org.",
    89              "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
    90              "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
    91              "zone_name": "example.com.",
    92              "created_at": "2014-10-24T19:59:44.000000",
    93              "version": 1,
    94              "type": "A",
    95              "status": "PENDING",
    96              "action": "CREATE"
    97          }
    98      ],
    99      "links": {
   100          "self": "http://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets?limit=1"
   101      },
   102      "metadata": {
   103          "total_count": 1
   104      }
   105  }
   106  `
   107  
   108  // GetOutput is a sample response to a Get call.
   109  const GetOutput = `
   110  {
   111  		"description": "This is an example record set.",
   112  		"links": {
   113  				"self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648"
   114  		},
   115  		"updated_at": null,
   116  		"records": [
   117  				"10.1.0.2"
   118  		],
   119  		"ttl": 3600,
   120  		"id": "f7b10e9b-0cae-4a91-b162-562bc6096648",
   121  		"name": "example.org.",
   122  		"project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   123  		"zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   124  		"zone_name": "example.com.",
   125  		"created_at": "2014-10-24T19:59:44.000000",
   126  		"version": 1,
   127  		"type": "A",
   128  		"status": "PENDING",
   129  		"action": "CREATE"
   130  }
   131  `
   132  
   133  // NextPageRequest is a sample request to test pagination.
   134  const NextPageRequest = `
   135  {
   136    "links": {
   137      "self": "http://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets?limit=1",
   138      "next": "http://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets?limit=1&marker=f7b10e9b-0cae-4a91-b162-562bc6096648"
   139    }
   140  }
   141  `
   142  
   143  // FirstRecordSet is the first result in ListByZoneOutput
   144  var FirstRecordSetCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2014-10-24T19:59:44.000000")
   145  var FirstRecordSet = recordsets.RecordSet{
   146  	ID:          "f7b10e9b-0cae-4a91-b162-562bc6096648",
   147  	Description: "This is an example record set.",
   148  	UpdatedAt:   time.Time{},
   149  	Records:     []string{"10.1.0.2"},
   150  	TTL:         3600,
   151  	Name:        "example.org.",
   152  	ProjectID:   "4335d1f0-f793-11e2-b778-0800200c9a66",
   153  	ZoneID:      "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   154  	ZoneName:    "example.com.",
   155  	CreatedAt:   FirstRecordSetCreatedAt,
   156  	Version:     1,
   157  	Type:        "A",
   158  	Status:      "PENDING",
   159  	Action:      "CREATE",
   160  	Links: []gophercloud.Link{
   161  		{
   162  			Rel:  "self",
   163  			Href: "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648",
   164  		},
   165  	},
   166  }
   167  
   168  // SecondRecordSet is the first result in ListByZoneOutput
   169  var SecondRecordSetCreatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2014-10-24T19:59:44.000000")
   170  var SecondRecordSetUpdatedAt, _ = time.Parse(gophercloud.RFC3339MilliNoZ, "2017-03-04T14:29:07.000000")
   171  var SecondRecordSet = recordsets.RecordSet{
   172  	ID:          "7423aeaf-b354-4bd7-8aba-2e831567b478",
   173  	Description: "This is another example record set.",
   174  	UpdatedAt:   SecondRecordSetUpdatedAt,
   175  	Records:     []string{"10.1.0.3", "10.1.0.4"},
   176  	TTL:         3600,
   177  	Name:        "foo.example.org.",
   178  	ProjectID:   "4335d1f0-f793-11e2-b778-0800200c9a66",
   179  	ZoneID:      "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   180  	ZoneName:    "example.com.",
   181  	CreatedAt:   SecondRecordSetCreatedAt,
   182  	Version:     1,
   183  	Type:        "A",
   184  	Status:      "PENDING",
   185  	Action:      "CREATE",
   186  	Links: []gophercloud.Link{
   187  		{
   188  			Rel:  "self",
   189  			Href: "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/7423aeaf-b354-4bd7-8aba-2e831567b478",
   190  		},
   191  	},
   192  }
   193  
   194  // ExpectedRecordSetSlice is the slice of results that should be parsed
   195  // from ListByZoneOutput, in the expected order.
   196  var ExpectedRecordSetSlice = []recordsets.RecordSet{FirstRecordSet, SecondRecordSet}
   197  
   198  // ExpectedRecordSetSliceLimited is the slice of limited results that should be parsed
   199  // from ListByZoneOutput.
   200  var ExpectedRecordSetSliceLimited = []recordsets.RecordSet{SecondRecordSet}
   201  
   202  // HandleListByZoneSuccessfully configures the test server to respond to a ListByZone request.
   203  func HandleListByZoneSuccessfully(t *testing.T) {
   204  	th.Mux.HandleFunc("/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets", func(w http.ResponseWriter, r *http.Request) {
   205  		th.TestMethod(t, r, "GET")
   206  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   207  
   208  		w.Header().Add("Content-Type", "application/json")
   209  		if err := r.ParseForm(); err != nil {
   210  			t.Errorf("Failed to parse request form %v", err)
   211  		}
   212  		marker := r.Form.Get("marker")
   213  		switch marker {
   214  		case "f7b10e9b-0cae-4a91-b162-562bc6096648":
   215  			fmt.Fprint(w, ListByZoneOutputLimited)
   216  		case "":
   217  			fmt.Fprint(w, ListByZoneOutput)
   218  		}
   219  	})
   220  }
   221  
   222  // HandleGetSuccessfully configures the test server to respond to a Get request.
   223  func HandleGetSuccessfully(t *testing.T) {
   224  	th.Mux.HandleFunc("/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648",
   225  		func(w http.ResponseWriter, r *http.Request) {
   226  			th.TestMethod(t, r, "GET")
   227  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   228  
   229  			w.Header().Add("Content-Type", "application/json")
   230  			fmt.Fprint(w, GetOutput)
   231  		})
   232  }
   233  
   234  // CreateRecordSetRequest is a sample request to create a resource record.
   235  const CreateRecordSetRequest = `
   236  {
   237    "name" : "example.org.",
   238    "description" : "This is an example record set.",
   239    "type" : "A",
   240    "ttl" : 3600,
   241    "records" : [
   242        "10.1.0.2"
   243    ]
   244  }
   245  `
   246  
   247  // CreateRecordSetResponse is a sample response to a create request.
   248  const CreateRecordSetResponse = `
   249  {
   250      "description": "This is an example record set.",
   251      "links": {
   252          "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648"
   253      },
   254      "updated_at": null,
   255      "records": [
   256          "10.1.0.2"
   257      ],
   258      "ttl": 3600,
   259      "id": "f7b10e9b-0cae-4a91-b162-562bc6096648",
   260      "name": "example.org.",
   261      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   262      "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   263      "zone_name": "example.com.",
   264      "created_at": "2014-10-24T19:59:44.000000",
   265      "version": 1,
   266      "type": "A",
   267      "status": "PENDING",
   268      "action": "CREATE"
   269  }
   270  `
   271  
   272  // CreatedRecordSet is the expected created resource record.
   273  var CreatedRecordSet = FirstRecordSet
   274  
   275  // HandleZoneCreationSuccessfully configures the test server to respond to a Create request.
   276  func HandleCreateSuccessfully(t *testing.T) {
   277  	th.Mux.HandleFunc("/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets",
   278  		func(w http.ResponseWriter, r *http.Request) {
   279  			th.TestMethod(t, r, "POST")
   280  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   281  			th.TestJSONRequest(t, r, CreateRecordSetRequest)
   282  
   283  			w.WriteHeader(http.StatusCreated)
   284  			w.Header().Add("Content-Type", "application/json")
   285  			fmt.Fprint(w, CreateRecordSetResponse)
   286  		})
   287  }
   288  
   289  // UpdateRecordSetRequest is a sample request to update a record set.
   290  const UpdateRecordSetRequest = `
   291  {
   292    "description" : "Updated description",
   293    "ttl" : null,
   294    "records" : [
   295        "10.1.0.2",
   296        "10.1.0.3"
   297    ]
   298  }
   299  `
   300  
   301  // UpdateRecordSetResponse is a sample response to an update request.
   302  const UpdateRecordSetResponse = `
   303  {
   304      "description": "Updated description",
   305      "links": {
   306          "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648"
   307      },
   308      "updated_at": null,
   309      "records": [
   310          "10.1.0.2",
   311          "10.1.0.3"
   312      ],
   313      "ttl": 3600,
   314      "id": "f7b10e9b-0cae-4a91-b162-562bc6096648",
   315      "name": "example.org.",
   316      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   317      "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   318      "zone_name": "example.com.",
   319      "created_at": "2014-10-24T19:59:44.000000",
   320      "version": 2,
   321      "type": "A",
   322      "status": "PENDING",
   323      "action": "UPDATE"
   324  }
   325  `
   326  
   327  // HandleUpdateSuccessfully configures the test server to respond to an Update request.
   328  func HandleUpdateSuccessfully(t *testing.T) {
   329  	th.Mux.HandleFunc("/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648",
   330  		func(w http.ResponseWriter, r *http.Request) {
   331  			th.TestMethod(t, r, "PUT")
   332  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   333  			th.TestJSONRequest(t, r, UpdateRecordSetRequest)
   334  
   335  			w.WriteHeader(http.StatusOK)
   336  			w.Header().Add("Content-Type", "application/json")
   337  			fmt.Fprint(w, UpdateRecordSetResponse)
   338  		})
   339  }
   340  
   341  // DeleteRecordSetResponse is a sample response to a delete request.
   342  const DeleteRecordSetResponse = `
   343  {
   344      "description": "Updated description",
   345      "links": {
   346          "self": "https://127.0.0.1:9001/v2/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648"
   347      },
   348      "updated_at": null,
   349      "records": [
   350          "10.1.0.2",
   351          "10.1.0.3",
   352      ],
   353      "ttl": null,
   354      "id": "f7b10e9b-0cae-4a91-b162-562bc6096648",
   355      "name": "example.org.",
   356      "project_id": "4335d1f0-f793-11e2-b778-0800200c9a66",
   357      "zone_id": "2150b1bf-dee2-4221-9d85-11f7886fb15f",
   358      "zone_name": "example.com.",
   359      "created_at": "2014-10-24T19:59:44.000000",
   360      "version": 2,
   361      "type": "A",
   362      "status": "PENDING",
   363      "action": "UPDATE"
   364  }
   365  `
   366  
   367  // HandleDeleteSuccessfully configures the test server to respond to an Delete request.
   368  func HandleDeleteSuccessfully(t *testing.T) {
   369  	th.Mux.HandleFunc("/zones/2150b1bf-dee2-4221-9d85-11f7886fb15f/recordsets/f7b10e9b-0cae-4a91-b162-562bc6096648",
   370  		func(w http.ResponseWriter, r *http.Request) {
   371  			th.TestMethod(t, r, "DELETE")
   372  			th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   373  
   374  			w.WriteHeader(http.StatusAccepted)
   375  			//w.Header().Add("Content-Type", "application/json")
   376  			//fmt.Fprint(w, DeleteZoneResponse)
   377  		})
   378  }