github.com/gophercloud/gophercloud@v1.11.0/openstack/sharedfilesystems/v2/snapshots/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	th "github.com/gophercloud/gophercloud/testhelper"
     9  	fake "github.com/gophercloud/gophercloud/testhelper/client"
    10  )
    11  
    12  const (
    13  	snapshotEndpoint = "/snapshots"
    14  	snapshotID       = "bc082e99-3bdb-4400-b95e-b85c7a41622c"
    15  	shareID          = "19865c43-3b91-48c9-85a0-7ac4d6bb0efe"
    16  )
    17  
    18  var createRequest = `{
    19  	"snapshot": {
    20  		"share_id": "19865c43-3b91-48c9-85a0-7ac4d6bb0efe",
    21  		"name": "test snapshot",
    22  		"description": "test description"
    23  	}
    24  }`
    25  
    26  var createResponse = `{
    27  	"snapshot": {
    28  		"status": "creating",
    29  		"share_id": "19865c43-3b91-48c9-85a0-7ac4d6bb0efe",
    30  		"description": "test description",
    31  		"links": [
    32  			{
    33  				"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/9897f5ca-2559-4a4c-b761-d3439c0c9455",
    34  				"rel": "self"
    35  			},
    36  			{
    37  				"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/9897f5ca-2559-4a4c-b761-d3439c0c9455",
    38  				"rel": "bookmark"
    39  			}
    40  		],
    41  		"id": "bc082e99-3bdb-4400-b95e-b85c7a41622c",
    42  		"size": 1,
    43  		"user_id": "619e2ad074321cf246b03a89e95afee95fb26bb0b2d1fc7ba3bd30fcca25588a",
    44  		"name": "test snapshot",
    45  		"created_at": "2019-01-09T10:22:39.613550",
    46  		"share_proto": "NFS",
    47  		"project_id": "16e1ab15c35a457e9c2b2aa189f544e1",
    48  		"share_size": 1
    49  	}
    50  }`
    51  
    52  // MockCreateResponse creates a mock response
    53  func MockCreateResponse(t *testing.T) {
    54  	th.Mux.HandleFunc(snapshotEndpoint, func(w http.ResponseWriter, r *http.Request) {
    55  		th.TestMethod(t, r, "POST")
    56  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    57  		th.TestHeader(t, r, "Content-Type", "application/json")
    58  		th.TestHeader(t, r, "Accept", "application/json")
    59  		th.TestJSONRequest(t, r, createRequest)
    60  		w.Header().Add("Content-Type", "application/json")
    61  		w.WriteHeader(http.StatusAccepted)
    62  		fmt.Fprintf(w, createResponse)
    63  	})
    64  }
    65  
    66  // MockDeleteResponse creates a mock delete response
    67  func MockDeleteResponse(t *testing.T) {
    68  	th.Mux.HandleFunc(snapshotEndpoint+"/"+snapshotID, func(w http.ResponseWriter, r *http.Request) {
    69  		th.TestMethod(t, r, "DELETE")
    70  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    71  		w.WriteHeader(http.StatusAccepted)
    72  	})
    73  }
    74  
    75  var updateRequest = `{
    76  		"snapshot": {
    77  			"display_name": "my_new_test_snapshot",
    78  			"display_description": ""
    79  		}
    80  }`
    81  
    82  var updateResponse = `{
    83  	"snapshot": {
    84  		"status": "available",
    85  		"share_id": "19865c43-3b91-48c9-85a0-7ac4d6bb0efe",
    86  		"description": "",
    87  		"links": [
    88  			{
    89  				"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/9897f5ca-2559-4a4c-b761-d3439c0c9455",
    90  				"rel": "self"
    91  			},
    92  			{
    93  				"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/9897f5ca-2559-4a4c-b761-d3439c0c9455",
    94  				"rel": "bookmark"
    95  			}
    96  		],
    97  		"id": "9897f5ca-2559-4a4c-b761-d3439c0c9455",
    98  		"size": 1,
    99  		"user_id": "619e2ad074321cf246b03a89e95afee95fb26bb0b2d1fc7ba3bd30fcca25588a",
   100  		"name": "my_new_test_snapshot",
   101  		"created_at": "2019-01-09T10:22:39.613550",
   102  		"share_proto": "NFS",
   103  		"project_id": "16e1ab15c35a457e9c2b2aa189f544e1",
   104  		"share_size": 1
   105  	}
   106  }`
   107  
   108  func MockUpdateResponse(t *testing.T) {
   109  	th.Mux.HandleFunc(snapshotEndpoint+"/"+snapshotID, func(w http.ResponseWriter, r *http.Request) {
   110  		th.TestMethod(t, r, "PUT")
   111  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   112  		th.TestHeader(t, r, "Content-Type", "application/json")
   113  		th.TestHeader(t, r, "Accept", "application/json")
   114  		th.TestJSONRequest(t, r, updateRequest)
   115  		w.Header().Add("Content-Type", "application/json")
   116  		w.WriteHeader(http.StatusOK)
   117  		fmt.Fprintf(w, updateResponse)
   118  	})
   119  }
   120  
   121  var getResponse = `{
   122  	"snapshot": {
   123  		"status": "available",
   124  		"share_id": "19865c43-3b91-48c9-85a0-7ac4d6bb0efe",
   125  		"description": null,
   126  		"links": [
   127  			{
   128  				"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/bc082e99-3bdb-4400-b95e-b85c7a41622c",
   129  				"rel": "self"
   130  			},
   131  			{
   132  				"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/bc082e99-3bdb-4400-b95e-b85c7a41622c",
   133  				"rel": "bookmark"
   134  			}
   135  		],
   136  		"id": "bc082e99-3bdb-4400-b95e-b85c7a41622c",
   137  		"size": 1,
   138  		"user_id": "619e2ad074321cf246b03a89e95afee95fb26bb0b2d1fc7ba3bd30fcca25588a",
   139  		"name": "new_app_snapshot",
   140  		"created_at": "2019-01-06T11:11:02.000000",
   141  		"share_proto": "NFS",
   142  		"project_id": "16e1ab15c35a457e9c2b2aa189f544e1",
   143  		"share_size": 1
   144  	}
   145  }`
   146  
   147  // MockGetResponse creates a mock get response
   148  func MockGetResponse(t *testing.T) {
   149  	th.Mux.HandleFunc(snapshotEndpoint+"/"+snapshotID, func(w http.ResponseWriter, r *http.Request) {
   150  		th.TestMethod(t, r, "GET")
   151  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   152  		w.WriteHeader(http.StatusOK)
   153  		fmt.Fprintf(w, getResponse)
   154  	})
   155  }
   156  
   157  var listDetailResponse = `{
   158  	"snapshots": [
   159  		{
   160  			"status": "available",
   161  			"share_id": "19865c43-3b91-48c9-85a0-7ac4d6bb0efe",
   162  			"description": null,
   163  			"links": [
   164  				{
   165  					"href": "http://172.18.198.54:8786/v2/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/bc082e99-3bdb-4400-b95e-b85c7a41622c",
   166  					"rel": "self"
   167  				},
   168  				{
   169  					"href": "http://172.18.198.54:8786/16e1ab15c35a457e9c2b2aa189f544e1/snapshots/bc082e99-3bdb-4400-b95e-b85c7a41622c",
   170  					"rel": "bookmark"
   171  				}
   172  			],
   173  			"id": "bc082e99-3bdb-4400-b95e-b85c7a41622c",
   174  			"size": 1,
   175  			"user_id": "619e2ad074321cf246b03a89e95afee95fb26bb0b2d1fc7ba3bd30fcca25588a",
   176  			"name": "new_app_snapshot",
   177  			"created_at": "2019-01-06T11:11:02.000000",
   178  			"share_proto": "NFS",
   179  			"project_id": "16e1ab15c35a457e9c2b2aa189f544e1",
   180  			"share_size": 1
   181  		}
   182  	]
   183  }`
   184  
   185  var listDetailEmptyResponse = `{"snapshots": []}`
   186  
   187  // MockListDetailResponse creates a mock detailed-list response
   188  func MockListDetailResponse(t *testing.T) {
   189  	th.Mux.HandleFunc(snapshotEndpoint+"/detail", func(w http.ResponseWriter, r *http.Request) {
   190  		th.TestMethod(t, r, "GET")
   191  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   192  
   193  		w.Header().Add("Content-Type", "application/json")
   194  		w.WriteHeader(http.StatusOK)
   195  
   196  		r.ParseForm()
   197  		marker := r.Form.Get("offset")
   198  
   199  		switch marker {
   200  		case "":
   201  			fmt.Fprint(w, listDetailResponse)
   202  		default:
   203  			fmt.Fprint(w, listDetailEmptyResponse)
   204  		}
   205  	})
   206  }
   207  
   208  var resetStatusRequest = `{
   209                  "reset_status": {
   210                          "status": "error"
   211                  }
   212          }`
   213  
   214  // MockResetStatusResponse creates a mock reset status snapshot response
   215  func MockResetStatusResponse(t *testing.T) {
   216  	th.Mux.HandleFunc(snapshotEndpoint+"/"+snapshotID+"/action", func(w http.ResponseWriter, r *http.Request) {
   217  		th.TestMethod(t, r, "POST")
   218  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   219  		th.TestHeader(t, r, "Content-Type", "application/json")
   220  		th.TestHeader(t, r, "Accept", "application/json")
   221  		th.TestJSONRequest(t, r, resetStatusRequest)
   222  		w.Header().Add("Content-Type", "application/json")
   223  		w.WriteHeader(http.StatusAccepted)
   224  	})
   225  }
   226  
   227  var forceDeleteRequest = `{
   228                  "force_delete": null
   229          }`
   230  
   231  // MockForceDeleteResponse creates a mock force delete snapshot response
   232  func MockForceDeleteResponse(t *testing.T) {
   233  	th.Mux.HandleFunc(snapshotEndpoint+"/"+snapshotID+"/action", func(w http.ResponseWriter, r *http.Request) {
   234  		th.TestMethod(t, r, "POST")
   235  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   236  		th.TestHeader(t, r, "Content-Type", "application/json")
   237  		th.TestHeader(t, r, "Accept", "application/json")
   238  		th.TestJSONRequest(t, r, forceDeleteRequest)
   239  		w.Header().Add("Content-Type", "application/json")
   240  		w.WriteHeader(http.StatusAccepted)
   241  	})
   242  }