github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/blockstorage/v3/attachments/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/blockstorage/v3/attachments"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  	fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    13  )
    14  
    15  var (
    16  	attachedAt, _      = time.Parse(gophercloud.RFC3339MilliNoZ, "2015-09-16T09:28:52.000000")
    17  	detachedAt, _      = time.Parse(gophercloud.RFC3339MilliNoZ, "2015-09-16T09:28:52.000000")
    18  	expectedAttachment = &attachments.Attachment{
    19  		ID:             "05551600-a936-4d4a-ba42-79a037c1-c91a",
    20  		VolumeID:       "289da7f8-6440-407c-9fb4-7db01ec49164",
    21  		Instance:       "83ec2e3b-4321-422b-8706-a84185f52a0a",
    22  		AttachMode:     "rw",
    23  		Status:         "attaching",
    24  		AttachedAt:     attachedAt,
    25  		DetachedAt:     detachedAt,
    26  		ConnectionInfo: map[string]any{},
    27  	}
    28  )
    29  
    30  func MockListResponse(t *testing.T) {
    31  	th.Mux.HandleFunc("/attachments/detail", func(w http.ResponseWriter, r *http.Request) {
    32  		th.TestMethod(t, r, "GET")
    33  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    34  
    35  		w.Header().Add("Content-Type", "application/json")
    36  		w.WriteHeader(http.StatusOK)
    37  
    38  		if err := r.ParseForm(); err != nil {
    39  			t.Errorf("Failed to parse request form %v", err)
    40  		}
    41  		marker := r.Form.Get("marker")
    42  		switch marker {
    43  		case "":
    44  			fmt.Fprintf(w, `
    45    {
    46      "attachments": [
    47          {
    48              "status": "attaching",
    49              "detached_at": "2015-09-16T09:28:52.000000",
    50              "connection_info": {},
    51              "attached_at": "2015-09-16T09:28:52.000000",
    52              "attach_mode": "rw",
    53              "instance": "83ec2e3b-4321-422b-8706-a84185f52a0a",
    54              "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    55              "id": "05551600-a936-4d4a-ba42-79a037c1-c91a"
    56          }
    57      ],
    58  	"attachments_links": [
    59  	{
    60  		"href": "%s/attachments/detail?marker=1",
    61  		"rel": "next"
    62  	}
    63      ]
    64  }
    65    `, th.Server.URL)
    66  		case "1":
    67  			fmt.Fprint(w, `{"volumes": []}`)
    68  		default:
    69  			t.Fatalf("Unexpected marker: [%s]", marker)
    70  		}
    71  	})
    72  }
    73  
    74  func MockGetResponse(t *testing.T) {
    75  	th.Mux.HandleFunc("/attachments/05551600-a936-4d4a-ba42-79a037c1-c91a", func(w http.ResponseWriter, r *http.Request) {
    76  		th.TestMethod(t, r, "GET")
    77  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    78  
    79  		w.Header().Add("Content-Type", "application/json")
    80  		w.WriteHeader(http.StatusOK)
    81  		fmt.Fprint(w, `
    82  {
    83      "attachment": {
    84          "status": "attaching",
    85          "detached_at": "2015-09-16T09:28:52.000000",
    86          "connection_info": {},
    87          "attached_at": "2015-09-16T09:28:52.000000",
    88          "attach_mode": "rw",
    89          "instance": "83ec2e3b-4321-422b-8706-a84185f52a0a",
    90          "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
    91          "id": "05551600-a936-4d4a-ba42-79a037c1-c91a"
    92      }
    93  }
    94        `)
    95  	})
    96  }
    97  
    98  func MockCreateResponse(t *testing.T) {
    99  	th.Mux.HandleFunc("/attachments", func(w http.ResponseWriter, r *http.Request) {
   100  		th.TestMethod(t, r, "POST")
   101  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   102  		th.TestHeader(t, r, "Content-Type", "application/json")
   103  		th.TestHeader(t, r, "Accept", "application/json")
   104  		th.TestJSONRequest(t, r, `
   105  {
   106      "attachment": {
   107          "instance_uuid": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   108          "connector": {
   109              "initiator": "iqn.1993-08.org.debian: 01: cad181614cec",
   110              "ip": "192.168.1.20",
   111              "platform": "x86_64",
   112              "host": "tempest-1",
   113              "os_type": "linux2",
   114              "multipath": false,
   115              "mountpoint": "/dev/vdb",
   116              "mode": "rw"
   117          },
   118          "volume_uuid": "289da7f8-6440-407c-9fb4-7db01ec49164"
   119      }
   120  }
   121        `)
   122  
   123  		w.Header().Add("Content-Type", "application/json")
   124  		w.WriteHeader(http.StatusOK)
   125  
   126  		fmt.Fprint(w, `
   127  {
   128      "attachment": {
   129          "status": "attaching",
   130          "detached_at": "2015-09-16T09:28:52.000000",
   131          "connection_info": {},
   132          "attached_at": "2015-09-16T09:28:52.000000",
   133          "attach_mode": "rw",
   134          "instance": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   135          "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
   136          "id": "05551600-a936-4d4a-ba42-79a037c1-c91a"
   137      }
   138  }
   139      `)
   140  	})
   141  }
   142  
   143  func MockDeleteResponse(t *testing.T) {
   144  	th.Mux.HandleFunc("/attachments/05551600-a936-4d4a-ba42-79a037c1-c91a", func(w http.ResponseWriter, r *http.Request) {
   145  		th.TestMethod(t, r, "DELETE")
   146  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   147  		w.WriteHeader(http.StatusOK)
   148  	})
   149  }
   150  
   151  func MockUpdateResponse(t *testing.T) {
   152  	th.Mux.HandleFunc("/attachments/05551600-a936-4d4a-ba42-79a037c1-c91a", func(w http.ResponseWriter, r *http.Request) {
   153  		th.TestMethod(t, r, "PUT")
   154  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   155  
   156  		th.TestJSONRequest(t, r, `
   157  {
   158      "attachment": {
   159          "connector": {
   160              "initiator": "iqn.1993-08.org.debian: 01: cad181614cec",
   161              "ip": "192.168.1.20",
   162              "platform": "x86_64",
   163              "host": "tempest-1",
   164              "os_type": "linux2",
   165              "multipath": false,
   166              "mountpoint": "/dev/vdb",
   167              "mode": "rw"
   168          }
   169      }
   170  }
   171        `)
   172  
   173  		w.WriteHeader(http.StatusOK)
   174  		fmt.Fprint(w, `
   175  {
   176      "attachment": {
   177          "status": "attaching",
   178          "detached_at": "2015-09-16T09:28:52.000000",
   179          "connection_info": {},
   180          "attached_at": "2015-09-16T09:28:52.000000",
   181          "attach_mode": "rw",
   182          "instance": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   183          "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
   184          "id": "05551600-a936-4d4a-ba42-79a037c1-c91a"
   185      }
   186  }
   187          `)
   188  	})
   189  }
   190  
   191  func MockUpdateEmptyResponse(t *testing.T) {
   192  	th.Mux.HandleFunc("/attachments/05551600-a936-4d4a-ba42-79a037c1-c91a", func(w http.ResponseWriter, r *http.Request) {
   193  		th.TestMethod(t, r, "PUT")
   194  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   195  
   196  		th.TestJSONRequest(t, r, `
   197  {
   198      "attachment": {
   199          "connector": null
   200      }
   201  }
   202        `)
   203  
   204  		w.WriteHeader(http.StatusOK)
   205  		fmt.Fprint(w, `
   206  {
   207      "attachment": {
   208          "status": "attaching",
   209          "detached_at": "2015-09-16T09:28:52.000000",
   210          "connection_info": {},
   211          "attached_at": "2015-09-16T09:28:52.000000",
   212          "attach_mode": "rw",
   213          "instance": "83ec2e3b-4321-422b-8706-a84185f52a0a",
   214          "volume_id": "289da7f8-6440-407c-9fb4-7db01ec49164",
   215          "id": "05551600-a936-4d4a-ba42-79a037c1-c91a"
   216      }
   217  }
   218          `)
   219  	})
   220  }
   221  
   222  var completeRequest = `
   223  {
   224      "os-complete": null
   225  }
   226  `
   227  
   228  func MockCompleteResponse(t *testing.T) {
   229  	th.Mux.HandleFunc("/attachments/05551600-a936-4d4a-ba42-79a037c1-c91a/action", func(w http.ResponseWriter, r *http.Request) {
   230  		th.TestMethod(t, r, "POST")
   231  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   232  		th.TestJSONRequest(t, r, completeRequest)
   233  		w.WriteHeader(http.StatusNoContent)
   234  	})
   235  }