github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/compute/v2/extensions/servergroups/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  
     8  	"github.com/huaweicloud/golangsdk/openstack/compute/v2/extensions/servergroups"
     9  	th "github.com/huaweicloud/golangsdk/testhelper"
    10  	"github.com/huaweicloud/golangsdk/testhelper/client"
    11  )
    12  
    13  // ListOutput is a sample response to a List call.
    14  const ListOutput = `
    15  {
    16      "server_groups": [
    17          {
    18              "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    19              "name": "test",
    20              "policies": [
    21                  "anti-affinity"
    22              ],
    23              "members": [],
    24              "metadata": {}
    25          },
    26          {
    27              "id": "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    28              "name": "test2",
    29              "policies": [
    30                  "affinity"
    31              ],
    32              "members": [],
    33              "metadata": {}
    34          }
    35      ]
    36  }
    37  `
    38  
    39  // GetOutput is a sample response to a Get call.
    40  const GetOutput = `
    41  {
    42      "server_group": {
    43          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    44          "name": "test",
    45          "policies": [
    46              "anti-affinity"
    47          ],
    48          "members": [],
    49          "metadata": {}
    50      }
    51  }
    52  `
    53  
    54  // CreateOutput is a sample response to a Post call
    55  const CreateOutput = `
    56  {
    57      "server_group": {
    58          "id": "616fb98f-46ca-475e-917e-2563e5a8cd19",
    59          "name": "test",
    60          "policies": [
    61              "anti-affinity"
    62          ],
    63          "members": [],
    64          "metadata": {}
    65      }
    66  }
    67  `
    68  
    69  // FirstServerGroup is the first result in ListOutput.
    70  var FirstServerGroup = servergroups.ServerGroup{
    71  	ID:   "616fb98f-46ca-475e-917e-2563e5a8cd19",
    72  	Name: "test",
    73  	Policies: []string{
    74  		"anti-affinity",
    75  	},
    76  	Members:  []string{},
    77  	Metadata: map[string]interface{}{},
    78  }
    79  
    80  // SecondServerGroup is the second result in ListOutput.
    81  var SecondServerGroup = servergroups.ServerGroup{
    82  	ID:   "4d8c3732-a248-40ed-bebc-539a6ffd25c0",
    83  	Name: "test2",
    84  	Policies: []string{
    85  		"affinity",
    86  	},
    87  	Members:  []string{},
    88  	Metadata: map[string]interface{}{},
    89  }
    90  
    91  // ExpectedServerGroupSlice is the slice of results that should be parsed
    92  // from ListOutput, in the expected order.
    93  var ExpectedServerGroupSlice = []servergroups.ServerGroup{FirstServerGroup, SecondServerGroup}
    94  
    95  // CreatedServerGroup is the parsed result from CreateOutput.
    96  var CreatedServerGroup = servergroups.ServerGroup{
    97  	ID:   "616fb98f-46ca-475e-917e-2563e5a8cd19",
    98  	Name: "test",
    99  	Policies: []string{
   100  		"anti-affinity",
   101  	},
   102  	Members:  []string{},
   103  	Metadata: map[string]interface{}{},
   104  }
   105  
   106  // HandleListSuccessfully configures the test server to respond to a List request.
   107  func HandleListSuccessfully(t *testing.T) {
   108  	th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
   109  		th.TestMethod(t, r, "GET")
   110  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   111  
   112  		w.Header().Add("Content-Type", "application/json")
   113  		fmt.Fprintf(w, ListOutput)
   114  	})
   115  }
   116  
   117  // HandleGetSuccessfully configures the test server to respond to a Get request
   118  // for an existing server group
   119  func HandleGetSuccessfully(t *testing.T) {
   120  	th.Mux.HandleFunc("/os-server-groups/4d8c3732-a248-40ed-bebc-539a6ffd25c0", func(w http.ResponseWriter, r *http.Request) {
   121  		th.TestMethod(t, r, "GET")
   122  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   123  
   124  		w.Header().Add("Content-Type", "application/json")
   125  		fmt.Fprintf(w, GetOutput)
   126  	})
   127  }
   128  
   129  // HandleCreateSuccessfully configures the test server to respond to a Create request
   130  // for a new server group
   131  func HandleCreateSuccessfully(t *testing.T) {
   132  	th.Mux.HandleFunc("/os-server-groups", func(w http.ResponseWriter, r *http.Request) {
   133  		th.TestMethod(t, r, "POST")
   134  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   135  		th.TestJSONRequest(t, r, `
   136  {
   137      "server_group": {
   138          "name": "test",
   139          "policies": [
   140              "anti-affinity"
   141          ]
   142      }
   143  }
   144  `)
   145  
   146  		w.Header().Add("Content-Type", "application/json")
   147  		fmt.Fprintf(w, CreateOutput)
   148  	})
   149  }
   150  
   151  // HandleDeleteSuccessfully configures the test server to respond to a Delete request for a
   152  // an existing server group
   153  func HandleDeleteSuccessfully(t *testing.T) {
   154  	th.Mux.HandleFunc("/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19", func(w http.ResponseWriter, r *http.Request) {
   155  		th.TestMethod(t, r, "DELETE")
   156  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   157  
   158  		w.WriteHeader(http.StatusAccepted)
   159  	})
   160  }
   161  
   162  func HandleAddMemberSuccessfully(t *testing.T) {
   163  	th.Mux.HandleFunc("/cloudservers/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19/action", func(w http.ResponseWriter, r *http.Request) {
   164  		th.TestMethod(t, r, "POST")
   165  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   166  		th.TestJSONRequest(t, r, `
   167  {
   168      "add_member": {
   169          "instance_uuid": "d194d539-07b0-446e-b52c-e639e618e49d"
   170      }
   171  }
   172  `)
   173  
   174  		w.Header().Add("Content-Type", "application/json")
   175  		w.WriteHeader(http.StatusAccepted)
   176  	})
   177  }
   178  
   179  func HandleRemoveMemberSuccessfully(t *testing.T) {
   180  	th.Mux.HandleFunc("/cloudservers/os-server-groups/616fb98f-46ca-475e-917e-2563e5a8cd19/action", func(w http.ResponseWriter, r *http.Request) {
   181  		th.TestMethod(t, r, "POST")
   182  		th.TestHeader(t, r, "X-Auth-Token", client.TokenID)
   183  		th.TestJSONRequest(t, r, `
   184  {
   185      "remove_member": {
   186          "instance_uuid": "d194d539-07b0-446e-b52c-e639e618e49d"
   187      }
   188  }
   189  `)
   190  
   191  		w.Header().Add("Content-Type", "application/json")
   192  		w.WriteHeader(http.StatusAccepted)
   193  	})
   194  }