github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/stacks/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/orchestration/v1/stacks"
    11  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    12  	fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    13  )
    14  
    15  var Create_time, _ = time.Parse(time.RFC3339, "2018-06-26T07:58:17Z")
    16  var Updated_time, _ = time.Parse(time.RFC3339, "2018-06-26T07:59:17Z")
    17  
    18  // CreateExpected represents the expected object from a Create request.
    19  var CreateExpected = &stacks.CreatedStack{
    20  	ID: "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    21  	Links: []gophercloud.Link{
    22  		{
    23  			Href: "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    24  			Rel:  "self",
    25  		},
    26  	},
    27  }
    28  
    29  // CreateOutput represents the response body from a Create request.
    30  const CreateOutput = `
    31  {
    32    "stack": {
    33      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    34      "links": [
    35      {
    36        "href": "http://168.28.170.117:8004/v1/98606384f58drad0bhdb7d02779549ac/stacks/stackcreated/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    37        "rel": "self"
    38      }
    39      ]
    40    }
    41  }`
    42  
    43  // HandleCreateSuccessfully creates an HTTP handler at `/stacks` on the test handler mux
    44  // that responds with a `Create` response.
    45  func HandleCreateSuccessfully(t *testing.T, output string) {
    46  	th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) {
    47  		th.TestMethod(t, r, "POST")
    48  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    49  		th.TestHeader(t, r, "Accept", "application/json")
    50  		w.WriteHeader(http.StatusCreated)
    51  		fmt.Fprint(w, output)
    52  	})
    53  }
    54  
    55  // ListExpected represents the expected object from a List request.
    56  var ListExpected = []stacks.ListedStack{
    57  	{
    58  		Description: "Simple template to test heat commands",
    59  		Links: []gophercloud.Link{
    60  			{
    61  				Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    62  				Rel:  "self",
    63  			},
    64  		},
    65  		StatusReason: "Stack CREATE completed successfully",
    66  		Name:         "postman_stack",
    67  		CreationTime: Create_time,
    68  		Status:       "CREATE_COMPLETE",
    69  		ID:           "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    70  		Tags:         []string{"rackspace", "atx"},
    71  	},
    72  	{
    73  		Description: "Simple template to test heat commands",
    74  		Links: []gophercloud.Link{
    75  			{
    76  				Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada",
    77  				Rel:  "self",
    78  			},
    79  		},
    80  		StatusReason: "Stack successfully updated",
    81  		Name:         "gophercloud-test-stack-2",
    82  		CreationTime: Create_time,
    83  		UpdatedTime:  Updated_time,
    84  		Status:       "UPDATE_COMPLETE",
    85  		ID:           "db6977b2-27aa-4775-9ae7-6213212d4ada",
    86  		Tags:         []string{"sfo", "satx"},
    87  	},
    88  }
    89  
    90  // FullListOutput represents the response body from a List request without a marker.
    91  const FullListOutput = `
    92  {
    93    "stacks": [
    94    {
    95      "description": "Simple template to test heat commands",
    96      "links": [
    97      {
    98        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
    99        "rel": "self"
   100      }
   101      ],
   102      "stack_status_reason": "Stack CREATE completed successfully",
   103      "stack_name": "postman_stack",
   104      "creation_time": "2018-06-26T07:58:17Z",
   105      "updated_time": null,
   106      "stack_status": "CREATE_COMPLETE",
   107      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   108  	"tags": ["rackspace", "atx"]
   109    },
   110    {
   111      "description": "Simple template to test heat commands",
   112      "links": [
   113      {
   114        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada",
   115        "rel": "self"
   116      }
   117      ],
   118      "stack_status_reason": "Stack successfully updated",
   119      "stack_name": "gophercloud-test-stack-2",
   120      "creation_time": "2018-06-26T07:58:17Z",
   121      "updated_time": "2018-06-26T07:59:17Z",
   122      "stack_status": "UPDATE_COMPLETE",
   123      "id": "db6977b2-27aa-4775-9ae7-6213212d4ada",
   124  	"tags": ["sfo", "satx"]
   125    }
   126    ]
   127  }
   128  `
   129  
   130  // HandleListSuccessfully creates an HTTP handler at `/stacks` on the test handler mux
   131  // that responds with a `List` response.
   132  func HandleListSuccessfully(t *testing.T, output string) {
   133  	th.Mux.HandleFunc("/stacks", func(w http.ResponseWriter, r *http.Request) {
   134  		th.TestMethod(t, r, "GET")
   135  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   136  		th.TestHeader(t, r, "Accept", "application/json")
   137  
   138  		w.Header().Set("Content-Type", "application/json")
   139  		if err := r.ParseForm(); err != nil {
   140  			t.Errorf("Failed to parse request form %v", err)
   141  		}
   142  		marker := r.Form.Get("marker")
   143  		switch marker {
   144  		case "":
   145  			fmt.Fprint(w, output)
   146  		case "db6977b2-27aa-4775-9ae7-6213212d4ada":
   147  			fmt.Fprint(w, `[]`)
   148  		default:
   149  			t.Fatalf("Unexpected marker: [%s]", marker)
   150  		}
   151  	})
   152  }
   153  
   154  // GetExpected represents the expected object from a Get request.
   155  var GetExpected = &stacks.RetrievedStack{
   156  	DisableRollback: true,
   157  	Description:     "Simple template to test heat commands",
   158  	Parameters: map[string]string{
   159  		"flavor":         "m1.tiny",
   160  		"OS::stack_name": "postman_stack",
   161  		"OS::stack_id":   "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   162  	},
   163  	StatusReason: "Stack CREATE completed successfully",
   164  	Name:         "postman_stack",
   165  	Outputs:      []map[string]any{},
   166  	CreationTime: Create_time,
   167  	Links: []gophercloud.Link{
   168  		{
   169  			Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   170  			Rel:  "self",
   171  		},
   172  	},
   173  	Capabilities:        []any{},
   174  	NotificationTopics:  []any{},
   175  	Status:              "CREATE_COMPLETE",
   176  	ID:                  "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   177  	TemplateDescription: "Simple template to test heat commands",
   178  	Tags:                []string{"rackspace", "atx"},
   179  }
   180  
   181  // GetOutput represents the response body from a Get request.
   182  const GetOutput = `
   183  {
   184    "stack": {
   185      "disable_rollback": true,
   186      "description": "Simple template to test heat commands",
   187      "parameters": {
   188        "flavor": "m1.tiny",
   189        "OS::stack_name": "postman_stack",
   190        "OS::stack_id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87"
   191      },
   192      "stack_status_reason": "Stack CREATE completed successfully",
   193      "stack_name": "postman_stack",
   194      "outputs": [],
   195      "creation_time": "2018-06-26T07:58:17Z",
   196      "links": [
   197      {
   198        "href": "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   199        "rel": "self"
   200      }
   201      ],
   202      "capabilities": [],
   203      "notification_topics": [],
   204      "timeout_mins": null,
   205      "stack_status": "CREATE_COMPLETE",
   206      "updated_time": null,
   207      "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   208      "template_description": "Simple template to test heat commands",
   209  	"tags": ["rackspace", "atx"]
   210    }
   211  }
   212  `
   213  
   214  // HandleGetSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   215  // on the test handler mux that responds with a `Get` response.
   216  func HandleGetSuccessfully(t *testing.T, output string) {
   217  	th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87", func(w http.ResponseWriter, r *http.Request) {
   218  		th.TestMethod(t, r, "GET")
   219  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   220  		th.TestHeader(t, r, "Accept", "application/json")
   221  
   222  		w.Header().Set("Content-Type", "application/json")
   223  		w.WriteHeader(http.StatusOK)
   224  		fmt.Fprint(w, output)
   225  	})
   226  }
   227  
   228  func HandleFindSuccessfully(t *testing.T, output string) {
   229  	th.Mux.HandleFunc("/stacks/16ef0584-4458-41eb-87c8-0dc8d5f66c87", func(w http.ResponseWriter, r *http.Request) {
   230  		th.TestMethod(t, r, "GET")
   231  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   232  		th.TestHeader(t, r, "Accept", "application/json")
   233  
   234  		w.Header().Set("Content-Type", "application/json")
   235  		w.WriteHeader(http.StatusOK)
   236  		fmt.Fprint(w, output)
   237  	})
   238  }
   239  
   240  // HandleUpdateSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   241  // on the test handler mux that responds with an `Update` response.
   242  func HandleUpdateSuccessfully(t *testing.T) {
   243  	th.Mux.HandleFunc("/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) {
   244  		th.TestMethod(t, r, "PUT")
   245  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   246  		th.TestHeader(t, r, "Accept", "application/json")
   247  
   248  		w.Header().Set("Content-Type", "application/json")
   249  		w.WriteHeader(http.StatusAccepted)
   250  	})
   251  }
   252  
   253  // HandleUpdatePatchSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   254  // on the test handler mux that responds with an `Update` response.
   255  func HandleUpdatePatchSuccessfully(t *testing.T) {
   256  	th.Mux.HandleFunc("/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) {
   257  		th.TestMethod(t, r, "PATCH")
   258  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   259  		th.TestHeader(t, r, "Accept", "application/json")
   260  
   261  		w.Header().Set("Content-Type", "application/json")
   262  		w.WriteHeader(http.StatusAccepted)
   263  	})
   264  }
   265  
   266  // HandleDeleteSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87`
   267  // on the test handler mux that responds with a `Delete` response.
   268  func HandleDeleteSuccessfully(t *testing.T) {
   269  	th.Mux.HandleFunc("/stacks/gophercloud-test-stack-2/db6977b2-27aa-4775-9ae7-6213212d4ada", func(w http.ResponseWriter, r *http.Request) {
   270  		th.TestMethod(t, r, "DELETE")
   271  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   272  		th.TestHeader(t, r, "Accept", "application/json")
   273  
   274  		w.Header().Set("Content-Type", "application/json")
   275  		w.WriteHeader(http.StatusNoContent)
   276  	})
   277  }
   278  
   279  // GetExpected represents the expected object from a Get request.
   280  var PreviewExpected = &stacks.PreviewedStack{
   281  	DisableRollback: true,
   282  	Description:     "Simple template to test heat commands",
   283  	Parameters: map[string]string{
   284  		"flavor":         "m1.tiny",
   285  		"OS::stack_name": "postman_stack",
   286  		"OS::stack_id":   "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   287  	},
   288  	Name:         "postman_stack",
   289  	CreationTime: Create_time,
   290  	Links: []gophercloud.Link{
   291  		{
   292  			Href: "http://166.76.160.117:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   293  			Rel:  "self",
   294  		},
   295  	},
   296  	Capabilities:        []any{},
   297  	NotificationTopics:  []any{},
   298  	ID:                  "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   299  	TemplateDescription: "Simple template to test heat commands",
   300  }
   301  
   302  // HandlePreviewSuccessfully creates an HTTP handler at `/stacks/preview`
   303  // on the test handler mux that responds with a `Preview` response.
   304  func HandlePreviewSuccessfully(t *testing.T, output string) {
   305  	th.Mux.HandleFunc("/stacks/preview", func(w http.ResponseWriter, r *http.Request) {
   306  		th.TestMethod(t, r, "POST")
   307  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   308  		th.TestHeader(t, r, "Accept", "application/json")
   309  
   310  		w.Header().Set("Content-Type", "application/json")
   311  		w.WriteHeader(http.StatusOK)
   312  		fmt.Fprint(w, output)
   313  	})
   314  }
   315  
   316  // AbandonExpected represents the expected object from an Abandon request.
   317  var AbandonExpected = &stacks.AbandonedStack{
   318  	Status: "COMPLETE",
   319  	Name:   "postman_stack",
   320  	Template: map[string]any{
   321  		"heat_template_version": "2013-05-23",
   322  		"description":           "Simple template to test heat commands",
   323  		"parameters": map[string]any{
   324  			"flavor": map[string]any{
   325  				"default": "m1.tiny",
   326  				"type":    "string",
   327  			},
   328  		},
   329  		"resources": map[string]any{
   330  			"hello_world": map[string]any{
   331  				"type": "OS::Nova::Server",
   332  				"properties": map[string]any{
   333  					"key_name": "heat_key",
   334  					"flavor": map[string]any{
   335  						"get_param": "flavor",
   336  					},
   337  					"image":     "ad091b52-742f-469e-8f3c-fd81cadf0743",
   338  					"user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n",
   339  				},
   340  			},
   341  		},
   342  	},
   343  	Action: "CREATE",
   344  	ID:     "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   345  	Resources: map[string]any{
   346  		"hello_world": map[string]any{
   347  			"status":      "COMPLETE",
   348  			"name":        "hello_world",
   349  			"resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63",
   350  			"action":      "CREATE",
   351  			"type":        "OS::Nova::Server",
   352  		},
   353  	},
   354  	Files: map[string]string{
   355  		"file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "heat_template_version: 2014-10-16\nparameters:\n  flavor:\n    type: string\n    description: Flavor for the server to be created\n    default: 4353\n    hidden: true\nresources:\n  test_server:\n    type: \"OS::Nova::Server\"\n    properties:\n      name: test-server\n      flavor: 2 GB General Purpose v1\n image: Debian 7 (Wheezy) (PVHVM)\n",
   356  	},
   357  	StackUserProjectID: "897686",
   358  	ProjectID:          "897686",
   359  	Environment: map[string]any{
   360  		"encrypted_param_names": make([]map[string]any, 0),
   361  		"parameter_defaults":    make(map[string]any),
   362  		"parameters":            make(map[string]any),
   363  		"resource_registry": map[string]any{
   364  			"file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml",
   365  			"resources": make(map[string]any),
   366  		},
   367  	},
   368  }
   369  
   370  // AbandonOutput represents the response body from an Abandon request.
   371  const AbandonOutput = `
   372  {
   373    "status": "COMPLETE",
   374    "name": "postman_stack",
   375    "template": {
   376      "heat_template_version": "2013-05-23",
   377      "description": "Simple template to test heat commands",
   378      "parameters": {
   379        "flavor": {
   380          "default": "m1.tiny",
   381          "type": "string"
   382        }
   383      },
   384      "resources": {
   385        "hello_world": {
   386          "type": "OS::Nova::Server",
   387          "properties": {
   388            "key_name": "heat_key",
   389            "flavor": {
   390              "get_param": "flavor"
   391            },
   392            "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
   393            "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
   394          }
   395        }
   396      }
   397    },
   398    "action": "CREATE",
   399    "id": "16ef0584-4458-41eb-87c8-0dc8d5f66c87",
   400    "resources": {
   401      "hello_world": {
   402        "status": "COMPLETE",
   403        "name": "hello_world",
   404        "resource_id": "8a310d36-46fc-436f-8be4-37a696b8ac63",
   405        "action": "CREATE",
   406        "type": "OS::Nova::Server"
   407      }
   408    },
   409    "files": {
   410      "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "heat_template_version: 2014-10-16\nparameters:\n  flavor:\n    type: string\n    description: Flavor for the server to be created\n    default: 4353\n    hidden: true\nresources:\n  test_server:\n    type: \"OS::Nova::Server\"\n    properties:\n      name: test-server\n      flavor: 2 GB General Purpose v1\n image: Debian 7 (Wheezy) (PVHVM)\n"
   411  },
   412    "environment": {
   413  	"encrypted_param_names": [],
   414  	"parameter_defaults": {},
   415  	"parameters": {},
   416  	"resource_registry": {
   417  		"file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml": "file:///Users/prat8228/go/src/github.com/rackspace/rack/my_nova.yaml",
   418  		"resources": {}
   419  	}
   420    },
   421    "stack_user_project_id": "897686",
   422    "project_id": "897686"
   423  }`
   424  
   425  // HandleAbandonSuccessfully creates an HTTP handler at `/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c87/abandon`
   426  // on the test handler mux that responds with an `Abandon` response.
   427  func HandleAbandonSuccessfully(t *testing.T, output string) {
   428  	th.Mux.HandleFunc("/stacks/postman_stack/16ef0584-4458-41eb-87c8-0dc8d5f66c8/abandon", func(w http.ResponseWriter, r *http.Request) {
   429  		th.TestMethod(t, r, "DELETE")
   430  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   431  		th.TestHeader(t, r, "Accept", "application/json")
   432  
   433  		w.Header().Set("Content-Type", "application/json")
   434  		w.WriteHeader(http.StatusOK)
   435  		fmt.Fprint(w, output)
   436  	})
   437  }