github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/rts/v1/stacks/testing/fixtures.go (about)

     1  package testing
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/huaweicloud/golangsdk"
    10  	"github.com/huaweicloud/golangsdk/openstack/rts/v1/stacks"
    11  	th "github.com/huaweicloud/golangsdk/testhelper"
    12  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    13  )
    14  
    15  // CreateExpected represents the expected object from a Create request.
    16  var CreateExpected = &stacks.CreatedStack{
    17  	ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    18  	Links: []golangsdk.Link{
    19  		{
    20  			Href: "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    21  			Rel:  "self",
    22  		},
    23  	},
    24  }
    25  
    26  // CreateOutput represents the response body from a Create request.
    27  const CreateOutput = `
    28  {
    29    "stack": {
    30      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    31      "links": [
    32      {
    33        "href": "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    34        "rel": "self"
    35      }
    36      ]
    37    }
    38  }`
    39  
    40  // HandleCreateSuccessfully creates an HTTP handler at `/stacks` on the test handler mux
    41  // that responds with a `Create` response.
    42  func HandleCreateSuccessfully(t *testing.T, output string) {
    43  	th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) {
    44  		th.TestMethod(t, r, "POST")
    45  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    46  		th.TestHeader(t, r, "Accept", "application/json")
    47  		w.WriteHeader(http.StatusCreated)
    48  		fmt.Fprintf(w, output)
    49  	})
    50  }
    51  
    52  // ListExpected represents the expected object from a List request.
    53  var ListExpected = []stacks.ListedStack{
    54  	{
    55  		Description: "Simple template to test heat commands",
    56  		Links: []golangsdk.Link{
    57  			{
    58  				Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    59  				Rel:  "self",
    60  			},
    61  		},
    62  		StatusReason: "Stack CREATE completed successfully",
    63  		Name:         "postman_stack",
    64  		CreationTime: time.Date(2015, 2, 3, 20, 7, 39, 0, time.UTC),
    65  		Status:       "CREATE_COMPLETE",
    66  		ID:           "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    67  	},
    68  	{
    69  		Description: "Simple template to test heat commands",
    70  		Links: []golangsdk.Link{
    71  			{
    72  				Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/golangsdk-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada",
    73  				Rel:  "self",
    74  			},
    75  		},
    76  		StatusReason: "Stack successfully updated",
    77  		Name:         "golangsdk-test-stack-2",
    78  		CreationTime: time.Date(2014, 12, 11, 17, 39, 16, 0, time.UTC),
    79  		UpdatedTime:  time.Date(2014, 12, 11, 17, 40, 37, 0, time.UTC),
    80  		Status:       "UPDATE_COMPLETE",
    81  		ID:           "db6977b2-27aa-4775-9ae7-6213212d4ada",
    82  	},
    83  }
    84  
    85  // FullListOutput represents the response body from a List request without a marker.
    86  const FullListOutput = `
    87  {
    88    "stacks": [
    89    {
    90      "description": "Simple template to test heat commands",
    91      "links": [
    92      {
    93        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    94        "rel": "self"
    95      }
    96      ],
    97      "stack_status_reason": "Stack CREATE completed successfully",
    98      "stack_name": "postman_stack",
    99      "creation_time": "2015-02-03T20:07:39",
   100      "updated_time": null,
   101      "stack_status": "CREATE_COMPLETE",
   102      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   103  	"tags": ["rackspace", "atx"]
   104    },
   105    {
   106      "description": "Simple template to test heat commands",
   107      "links": [
   108      {
   109        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/golangsdk-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada",
   110        "rel": "self"
   111      }
   112      ],
   113      "stack_status_reason": "Stack successfully updated",
   114      "stack_name": "golangsdk-test-stack-2",
   115      "creation_time": "2014-12-11T17:39:16",
   116      "updated_time": "2014-12-11T17:40:37",
   117      "stack_status": "UPDATE_COMPLETE",
   118      "id": "db6977b2-27aa-4775-9ae7-6213212d4ada",
   119  	"tags": ["sfo", "satx"]
   120    }
   121    ]
   122  }
   123  `
   124  
   125  // HandleListSuccessfully creates an HTTP handler at `/stacks` on the test handler mux
   126  // that responds with a `List` response.
   127  func HandleListSuccessfully(t *testing.T, output string) {
   128  	th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) {
   129  		th.TestMethod(t, r, "GET")
   130  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   131  		th.TestHeader(t, r, "Accept", "application/json")
   132  
   133  		w.Header().Set("Content-Type", "application/json")
   134  		r.ParseForm()
   135  		marker := r.Form.Get("marker")
   136  		switch marker {
   137  		case "":
   138  			fmt.Fprintf(w, output)
   139  		case "db6977b2-27aa-4775-9ae7-6213212d4ada":
   140  			fmt.Fprintf(w, `[]`)
   141  		default:
   142  			t.Fatalf("Unexpected marker: [%s]", marker)
   143  		}
   144  	})
   145  }
   146  
   147  // GetExpected represents the expected object from a Get request.
   148  var GetExpected = &stacks.RetrievedStack{
   149  	DisableRollback: true,
   150  	Description:     "Simple template to test heat commands",
   151  	Parameters: map[string]string{
   152  		"flavor":         "m1.tiny",
   153  		"OS::stack_name": "postman_stack",
   154  		"OS::stack_id":   "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   155  	},
   156  	StatusReason: "Stack CREATE completed successfully",
   157  	Name:         "postman_stack",
   158  	Outputs:      []*stacks.Output{},
   159  	CreationTime: time.Date(2015, 2, 3, 20, 7, 39, 0, time.UTC),
   160  	Links: []golangsdk.Link{
   161  		{
   162  			Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   163  			Rel:  "self",
   164  		},
   165  	},
   166  	Capabilities:        []interface{}{},
   167  	NotificationTopics:  []interface{}{},
   168  	Status:              "CREATE_COMPLETE",
   169  	ID:                  "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   170  	TemplateDescription: "Simple template to test heat commands",
   171  	Tags:                []string{"rackspace", "atx"},
   172  }
   173  
   174  // GetOutput represents the response body from a Get request.
   175  const GetOutput = `
   176  {
   177    "stack": {
   178      "disable_rollback": true,
   179      "description": "Simple template to test heat commands",
   180      "parameters": {
   181        "flavor": "m1.tiny",
   182        "OS::stack_name": "postman_stack",
   183        "OS::stack_id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87"
   184      },
   185      "stack_status_reason": "Stack CREATE completed successfully",
   186      "stack_name": "postman_stack",
   187      "outputs": [],
   188      "creation_time": "2015-02-03T20:07:39",
   189      "links": [
   190      {
   191        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   192        "rel": "self"
   193      }
   194      ],
   195      "capabilities": [],
   196      "notification_topics": [],
   197      "timeout_mins": null,
   198      "stack_status": "CREATE_COMPLETE",
   199      "updated_time": null,
   200      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   201      "template_description": "Simple template to test heat commands",
   202  	"tags": ["rackspace", "atx"]
   203    }
   204  }
   205  `
   206  
   207  // HandleGetSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   208  // on the test handler mux that responds with a `Get` response.
   209  func HandleGetSuccessfully(t *testing.T, output string) {
   210  	th.Mux.HandleFunc("/stacks/postman_stack", func(w http.ResponseWriter, r *http.Request) {
   211  		th.TestMethod(t, r, "GET")
   212  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   213  		th.TestHeader(t, r, "Accept", "application/json")
   214  
   215  		w.Header().Set("Content-Type", "application/json")
   216  		w.WriteHeader(http.StatusOK)
   217  		fmt.Fprintf(w, output)
   218  	})
   219  }
   220  
   221  // HandleUpdateSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   222  // on the test handler mux that responds with an `Update` response.
   223  func HandleUpdateSuccessfully(t *testing.T) {
   224  	th.Mux.HandleFunc("/stacks/golangsdk-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) {
   225  		th.TestMethod(t, r, "PUT")
   226  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   227  		th.TestHeader(t, r, "Accept", "application/json")
   228  
   229  		w.Header().Set("Content-Type", "application/json")
   230  		w.WriteHeader(http.StatusAccepted)
   231  	})
   232  }
   233  
   234  // HandleDeleteSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   235  // on the test handler mux that responds with a `Delete` response.
   236  func HandleDeleteSuccessfully(t *testing.T) {
   237  	th.Mux.HandleFunc("/stacks/golangsdk-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) {
   238  		th.TestMethod(t, r, "DELETE")
   239  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   240  		th.TestHeader(t, r, "Accept", "application/json")
   241  
   242  		w.Header().Set("Content-Type", "application/json")
   243  		w.WriteHeader(http.StatusNoContent)
   244  	})
   245  }