github.com/gophercloud/gophercloud@v1.11.0/openstack/baremetal/v1/allocations/testing/fixtures_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/gophercloud/gophercloud/openstack/baremetal/v1/allocations"
    10  	th "github.com/gophercloud/gophercloud/testhelper"
    11  	"github.com/gophercloud/gophercloud/testhelper/client"
    12  )
    13  
    14  const AllocationListBody = `
    15  {
    16    "allocations": [
    17      {
    18        "candidate_nodes": [],
    19        "created_at": "2019-02-20T09:43:58+00:00",
    20        "extra": {},
    21        "last_error": null,
    22        "links": [
    23          {
    24            "href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
    25            "rel": "self"
    26          },
    27          {
    28            "href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
    29            "rel": "bookmark"
    30          }
    31        ],
    32        "name": "allocation-1",
    33        "node_uuid": "6d85703a-565d-469a-96ce-30b6de53079d",
    34        "resource_class": "bm-large",
    35        "state": "active",
    36        "traits": [],
    37        "updated_at": "2019-02-20T09:43:58+00:00",
    38        "uuid": "5344a3e2-978a-444e-990a-cbf47c62ef88"
    39      },
    40      {
    41        "candidate_nodes": [],
    42        "created_at": "2019-02-20T09:43:58+00:00",
    43        "extra": {},
    44        "last_error": "Failed to process allocation eff80f47-75f0-4d41-b1aa-cf07c201adac: no available nodes match the resource class bm-large.",
    45        "links": [
    46          {
    47            "href": "http://127.0.0.1:6385/v1/allocations/eff80f47-75f0-4d41-b1aa-cf07c201adac",
    48            "rel": "self"
    49          },
    50          {
    51            "href": "http://127.0.0.1:6385/allocations/eff80f47-75f0-4d41-b1aa-cf07c201adac",
    52            "rel": "bookmark"
    53          }
    54        ],
    55        "name": "allocation-2",
    56        "node_uuid": null,
    57        "resource_class": "bm-large",
    58        "state": "error",
    59        "traits": [
    60          "CUSTOM_GOLD"
    61        ],
    62        "updated_at": "2019-02-20T09:43:58+00:00",
    63        "uuid": "eff80f47-75f0-4d41-b1aa-cf07c201adac"
    64      }
    65    ]
    66  }
    67  `
    68  
    69  const SingleAllocationBody = `
    70  {
    71    "candidate_nodes": ["344a3e2-978a-444e-990a-cbf47c62ef88"],
    72    "created_at": "2019-02-20T09:43:58+00:00",
    73    "extra": {},
    74    "last_error": null,
    75    "links": [
    76      {
    77        "href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
    78        "rel": "self"
    79      },
    80      {
    81        "href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88",
    82        "rel": "bookmark"
    83      }
    84    ],
    85    "name": "allocation-1",
    86    "node_uuid": null,
    87    "resource_class": "baremetal",
    88    "state": "allocating",
    89    "traits": ["foo"],
    90    "updated_at": null,
    91    "uuid": "5344a3e2-978a-444e-990a-cbf47c62ef88"
    92  }`
    93  
    94  var (
    95  	createdAt, _ = time.Parse(time.RFC3339, "2019-02-20T09:43:58+00:00")
    96  
    97  	Allocation1 = allocations.Allocation{
    98  		UUID:           "5344a3e2-978a-444e-990a-cbf47c62ef88",
    99  		CandidateNodes: []string{"344a3e2-978a-444e-990a-cbf47c62ef88"},
   100  		Name:           "allocation-1",
   101  		State:          "allocating",
   102  		ResourceClass:  "baremetal",
   103  		Traits:         []string{"foo"},
   104  		Extra:          map[string]string{},
   105  		CreatedAt:      createdAt,
   106  		Links:          []interface{}{map[string]interface{}{"href": "http://127.0.0.1:6385/v1/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88", "rel": "self"}, map[string]interface{}{"href": "http://127.0.0.1:6385/allocations/5344a3e2-978a-444e-990a-cbf47c62ef88", "rel": "bookmark"}},
   107  	}
   108  )
   109  
   110  // HandleAllocationListSuccessfully sets up the test server to respond to a allocation List request.
   111  func HandleAllocationListSuccessfully(t *testing.T) {
   112  	th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
   113  		th.TestMethod(t, r, "GET")
   114  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   115  		w.Header().Add("Content-Type", "application/json")
   116  		r.ParseForm()
   117  
   118  		marker := r.Form.Get("marker")
   119  		switch marker {
   120  		case "":
   121  			fmt.Fprintf(w, AllocationListBody)
   122  
   123  		case "eff80f47-75f0-4d41-b1aa-cf07c201adac":
   124  			fmt.Fprintf(w, `{ "allocations": [] }`)
   125  		default:
   126  			t.Fatalf("/allocations invoked with unexpected marker=[%s]", marker)
   127  		}
   128  	})
   129  }
   130  
   131  // HandleAllocationCreationSuccessfully sets up the test server to respond to a allocation creation request
   132  // with a given response.
   133  func HandleAllocationCreationSuccessfully(t *testing.T, response string) {
   134  	th.Mux.HandleFunc("/allocations", func(w http.ResponseWriter, r *http.Request) {
   135  		th.TestMethod(t, r, "POST")
   136  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   137  		th.TestJSONRequest(t, r, `{
   138      		"name": "allocation-1",
   139      		"resource_class": "baremetal",
   140  			"candidate_nodes": ["344a3e2-978a-444e-990a-cbf47c62ef88"],
   141  		 	"traits": ["foo"]
   142          }`)
   143  
   144  		w.WriteHeader(http.StatusAccepted)
   145  		w.Header().Add("Content-Type", "application/json")
   146  		fmt.Fprintf(w, response)
   147  	})
   148  }
   149  
   150  // HandleAllocationDeletionSuccessfully sets up the test server to respond to a allocation deletion request.
   151  func HandleAllocationDeletionSuccessfully(t *testing.T) {
   152  	th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
   153  		th.TestMethod(t, r, "DELETE")
   154  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   155  
   156  		w.WriteHeader(http.StatusNoContent)
   157  	})
   158  }
   159  
   160  func HandleAllocationGetSuccessfully(t *testing.T) {
   161  	th.Mux.HandleFunc("/allocations/344a3e2-978a-444e-990a-cbf47c62ef88", func(w http.ResponseWriter, r *http.Request) {
   162  		th.TestMethod(t, r, "GET")
   163  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   164  		th.TestHeader(t, r, "Accept", "application/json")
   165  
   166  		fmt.Fprintf(w, SingleAllocationBody)
   167  	})
   168  }