github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/stackresources/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/stackresources"
    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:57:17Z")
    16  var Updated_time, _ = time.Parse(time.RFC3339, "2018-06-26T07:58:17Z")
    17  
    18  // FindExpected represents the expected object from a Find request.
    19  var FindExpected = []stackresources.Resource{
    20  	{
    21  		Name: "hello_world",
    22  		Links: []gophercloud.Link{
    23  			{
    24  				Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world",
    25  				Rel:  "self",
    26  			},
    27  			{
    28  				Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b",
    29  				Rel:  "stack",
    30  			},
    31  		},
    32  		LogicalID:    "hello_world",
    33  		StatusReason: "state changed",
    34  		UpdatedTime:  Updated_time,
    35  		CreationTime: Create_time,
    36  		RequiredBy:   []any{},
    37  		Status:       "CREATE_IN_PROGRESS",
    38  		PhysicalID:   "49181cd6-169a-4130-9455-31185bbfc5bf",
    39  		Type:         "OS::Nova::Server",
    40  		Attributes:   map[string]any{"SXSW": "atx"},
    41  		Description:  "Some resource",
    42  	},
    43  }
    44  
    45  // FindOutput represents the response body from a Find request.
    46  const FindOutput = `
    47  {
    48    "resources": [
    49    {
    50    	"description": "Some resource",
    51    	"attributes": {"SXSW": "atx"},
    52      "resource_name": "hello_world",
    53      "links": [
    54        {
    55        "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world",
    56        "rel": "self"
    57        },
    58        {
    59          "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b",
    60          "rel": "stack"
    61        }
    62      ],
    63      "logical_resource_id": "hello_world",
    64      "resource_status_reason": "state changed",
    65      "updated_time": "2018-06-26T07:58:17Z",
    66  	"creation_time": "2018-06-26T07:57:17Z",
    67      "required_by": [],
    68      "resource_status": "CREATE_IN_PROGRESS",
    69      "physical_resource_id": "49181cd6-169a-4130-9455-31185bbfc5bf",
    70      "resource_type": "OS::Nova::Server"
    71    }
    72    ]
    73  }`
    74  
    75  // HandleFindSuccessfully creates an HTTP handler at `/stacks/hello_world/resources`
    76  // on the test handler mux that responds with a `Find` response.
    77  func HandleFindSuccessfully(t *testing.T, output string) {
    78  	th.Mux.HandleFunc("/stacks/hello_world/resources", func(w http.ResponseWriter, r *http.Request) {
    79  		th.TestMethod(t, r, "GET")
    80  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
    81  		th.TestHeader(t, r, "Accept", "application/json")
    82  
    83  		w.Header().Set("Content-Type", "application/json")
    84  		w.WriteHeader(http.StatusOK)
    85  		fmt.Fprint(w, output)
    86  	})
    87  }
    88  
    89  // ListExpected represents the expected object from a List request.
    90  var ListExpected = []stackresources.Resource{
    91  	{
    92  		Name: "hello_world",
    93  		Links: []gophercloud.Link{
    94  			{
    95  				Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world",
    96  				Rel:  "self",
    97  			},
    98  			{
    99  				Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b",
   100  				Rel:  "stack",
   101  			},
   102  		},
   103  		LogicalID:    "hello_world",
   104  		StatusReason: "state changed",
   105  		UpdatedTime:  Updated_time,
   106  		CreationTime: Create_time,
   107  		RequiredBy:   []any{},
   108  		Status:       "CREATE_IN_PROGRESS",
   109  		PhysicalID:   "49181cd6-169a-4130-9455-31185bbfc5bf",
   110  		Type:         "OS::Nova::Server",
   111  		Attributes:   map[string]any{"SXSW": "atx"},
   112  		Description:  "Some resource",
   113  	},
   114  }
   115  
   116  // ListOutput represents the response body from a List request.
   117  const ListOutput = `{
   118    "resources": [
   119    {
   120      "resource_name": "hello_world",
   121      "links": [
   122      {
   123        "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b/resources/hello_world",
   124        "rel": "self"
   125      },
   126      {
   127        "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/postman_stack/5f57cff9-93fc-424e-9f78-df0515e7f48b",
   128        "rel": "stack"
   129      }
   130      ],
   131      "logical_resource_id": "hello_world",
   132      "resource_status_reason": "state changed",
   133      "updated_time": "2018-06-26T07:58:17Z",
   134      "creation_time": "2018-06-26T07:57:17Z",
   135      "required_by": [],
   136      "resource_status": "CREATE_IN_PROGRESS",
   137      "physical_resource_id": "49181cd6-169a-4130-9455-31185bbfc5bf",
   138      "resource_type": "OS::Nova::Server",
   139  	"attributes": {"SXSW": "atx"},
   140  	"description": "Some resource"
   141    }
   142  ]
   143  }`
   144  
   145  // HandleListSuccessfully creates an HTTP handler at `/stacks/hello_world/49181cd6-169a-4130-9455-31185bbfc5bf/resources`
   146  // on the test handler mux that responds with a `List` response.
   147  func HandleListSuccessfully(t *testing.T, output string) {
   148  	th.Mux.HandleFunc("/stacks/hello_world/49181cd6-169a-4130-9455-31185bbfc5bf/resources", func(w http.ResponseWriter, r *http.Request) {
   149  		th.TestMethod(t, r, "GET")
   150  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   151  		th.TestHeader(t, r, "Accept", "application/json")
   152  
   153  		w.Header().Set("Content-Type", "application/json")
   154  		if err := r.ParseForm(); err != nil {
   155  			t.Errorf("Failed to parse request form %v", err)
   156  		}
   157  		marker := r.Form.Get("marker")
   158  		switch marker {
   159  		case "":
   160  			fmt.Fprint(w, output)
   161  		case "49181cd6-169a-4130-9455-31185bbfc5bf":
   162  			fmt.Fprint(w, `{"resources":[]}`)
   163  		default:
   164  			t.Fatalf("Unexpected marker: [%s]", marker)
   165  		}
   166  	})
   167  }
   168  
   169  // GetExpected represents the expected object from a Get request.
   170  var GetExpected = &stackresources.Resource{
   171  	Name: "wordpress_instance",
   172  	Links: []gophercloud.Link{
   173  		{
   174  			Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance",
   175  			Rel:  "self",
   176  		},
   177  		{
   178  			Href: "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e",
   179  			Rel:  "stack",
   180  		},
   181  	},
   182  	LogicalID:    "wordpress_instance",
   183  	Attributes:   map[string]any{"SXSW": "atx"},
   184  	StatusReason: "state changed",
   185  	UpdatedTime:  Updated_time,
   186  	RequiredBy:   []any{},
   187  	Status:       "CREATE_COMPLETE",
   188  	PhysicalID:   "00e3a2fe-c65d-403c-9483-4db9930dd194",
   189  	Type:         "OS::Nova::Server",
   190  }
   191  
   192  // GetOutput represents the response body from a Get request.
   193  const GetOutput = `
   194  {
   195    "resource": {
   196      "description": "Some resource",
   197      "attributes": {"SXSW": "atx"},
   198      "resource_name": "wordpress_instance",
   199      "description": "",
   200      "links": [
   201      {
   202        "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance",
   203        "rel": "self"
   204      },
   205      {
   206        "href": "http://166.78.160.107:8004/v1/98606384f58d4ad0b3db7d0d779549ac/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e",
   207        "rel": "stack"
   208      }
   209      ],
   210      "logical_resource_id": "wordpress_instance",
   211      "resource_status": "CREATE_COMPLETE",
   212      "updated_time": "2018-06-26T07:58:17Z",
   213      "required_by": [],
   214      "resource_status_reason": "state changed",
   215      "physical_resource_id": "00e3a2fe-c65d-403c-9483-4db9930dd194",
   216      "resource_type": "OS::Nova::Server"
   217    }
   218  }`
   219  
   220  // HandleGetSuccessfully creates an HTTP handler at `/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance`
   221  // on the test handler mux that responds with a `Get` response.
   222  func HandleGetSuccessfully(t *testing.T, output string) {
   223  	th.Mux.HandleFunc("/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance", func(w http.ResponseWriter, r *http.Request) {
   224  		th.TestMethod(t, r, "GET")
   225  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   226  		th.TestHeader(t, r, "Accept", "application/json")
   227  
   228  		w.Header().Set("Content-Type", "application/json")
   229  		w.WriteHeader(http.StatusOK)
   230  		fmt.Fprint(w, output)
   231  	})
   232  }
   233  
   234  // MetadataExpected represents the expected object from a Metadata request.
   235  var MetadataExpected = map[string]string{
   236  	"number": "7",
   237  	"animal": "auk",
   238  }
   239  
   240  // MetadataOutput represents the response body from a Metadata request.
   241  const MetadataOutput = `
   242  {
   243      "metadata": {
   244        "number": "7",
   245        "animal": "auk"
   246      }
   247  }`
   248  
   249  // HandleMetadataSuccessfully creates an HTTP handler at `/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance/metadata`
   250  // on the test handler mux that responds with a `Metadata` response.
   251  func HandleMetadataSuccessfully(t *testing.T, output string) {
   252  	th.Mux.HandleFunc("/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance/metadata", func(w http.ResponseWriter, r *http.Request) {
   253  		th.TestMethod(t, r, "GET")
   254  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   255  		th.TestHeader(t, r, "Accept", "application/json")
   256  
   257  		w.Header().Set("Content-Type", "application/json")
   258  		w.WriteHeader(http.StatusOK)
   259  		fmt.Fprint(w, output)
   260  	})
   261  }
   262  
   263  // ListTypesExpected represents the expected object from a ListTypes request.
   264  var ListTypesExpected = stackresources.ResourceTypes{
   265  	"OS::Nova::Server",
   266  	"OS::Heat::RandomString",
   267  	"OS::Swift::Container",
   268  	"OS::Trove::Instance",
   269  	"OS::Nova::FloatingIPAssociation",
   270  	"OS::Cinder::VolumeAttachment",
   271  	"OS::Nova::FloatingIP",
   272  	"OS::Nova::KeyPair",
   273  }
   274  
   275  // same as above, but sorted
   276  var SortedListTypesExpected = stackresources.ResourceTypes{
   277  	"OS::Cinder::VolumeAttachment",
   278  	"OS::Heat::RandomString",
   279  	"OS::Nova::FloatingIP",
   280  	"OS::Nova::FloatingIPAssociation",
   281  	"OS::Nova::KeyPair",
   282  	"OS::Nova::Server",
   283  	"OS::Swift::Container",
   284  	"OS::Trove::Instance",
   285  }
   286  
   287  // ListTypesOutput represents the response body from a ListTypes request.
   288  const ListTypesOutput = `
   289  {
   290    "resource_types": [
   291      "OS::Nova::Server",
   292      "OS::Heat::RandomString",
   293      "OS::Swift::Container",
   294      "OS::Trove::Instance",
   295      "OS::Nova::FloatingIPAssociation",
   296      "OS::Cinder::VolumeAttachment",
   297      "OS::Nova::FloatingIP",
   298      "OS::Nova::KeyPair"
   299    ]
   300  }`
   301  
   302  // HandleListTypesSuccessfully creates an HTTP handler at `/resource_types`
   303  // on the test handler mux that responds with a `ListTypes` response.
   304  func HandleListTypesSuccessfully(t *testing.T, output string) {
   305  	th.Mux.HandleFunc("/resource_types", func(w http.ResponseWriter, r *http.Request) {
   306  		th.TestMethod(t, r, "GET")
   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  // GetSchemaExpected represents the expected object from a Schema request.
   317  var GetSchemaExpected = &stackresources.TypeSchema{
   318  	Attributes: map[string]any{
   319  		"an_attribute": map[string]any{
   320  			"description": "An attribute description .",
   321  		},
   322  	},
   323  	Properties: map[string]any{
   324  		"a_property": map[string]any{
   325  			"update_allowed": false,
   326  			"required":       true,
   327  			"type":           "string",
   328  			"description":    "A resource description.",
   329  		},
   330  	},
   331  	ResourceType: "OS::Heat::AResourceName",
   332  	SupportStatus: map[string]any{
   333  		"message": "A status message",
   334  		"status":  "SUPPORTED",
   335  		"version": "2014.1",
   336  	},
   337  }
   338  
   339  // GetSchemaOutput represents the response body from a Schema request.
   340  const GetSchemaOutput = `
   341  {
   342    "attributes": {
   343      "an_attribute": {
   344        "description": "An attribute description ."
   345      }
   346    },
   347    "properties": {
   348      "a_property": {
   349        "update_allowed": false,
   350        "required": true,
   351        "type": "string",
   352        "description": "A resource description."
   353      }
   354    },
   355    "resource_type": "OS::Heat::AResourceName",
   356    "support_status": {
   357  	"message": "A status message",
   358  	"status": "SUPPORTED",
   359  	"version": "2014.1"
   360    }
   361  }`
   362  
   363  // HandleGetSchemaSuccessfully creates an HTTP handler at `/resource_types/OS::Heat::AResourceName`
   364  // on the test handler mux that responds with a `Schema` response.
   365  func HandleGetSchemaSuccessfully(t *testing.T, output string) {
   366  	th.Mux.HandleFunc("/resource_types/OS::Heat::AResourceName", func(w http.ResponseWriter, r *http.Request) {
   367  		th.TestMethod(t, r, "GET")
   368  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   369  		th.TestHeader(t, r, "Accept", "application/json")
   370  
   371  		w.Header().Set("Content-Type", "application/json")
   372  		w.WriteHeader(http.StatusOK)
   373  		fmt.Fprint(w, output)
   374  	})
   375  }
   376  
   377  // GetTemplateExpected represents the expected object from a Template request.
   378  var GetTemplateExpected = "{\n  \"HeatTemplateFormatVersion\": \"2012-12-12\",\n  \"Outputs\": {\n    \"private_key\": {\n      \"Description\": \"The private key if it has been saved.\",\n      \"Value\": \"{\\\"Fn::GetAtt\\\": [\\\"KeyPair\\\", \\\"private_key\\\"]}\"\n    },\n    \"public_key\": {\n      \"Description\": \"The public key.\",\n      \"Value\": \"{\\\"Fn::GetAtt\\\": [\\\"KeyPair\\\", \\\"public_key\\\"]}\"\n    }\n  },\n  \"Parameters\": {\n    \"name\": {\n      \"Description\": \"The name of the key pair.\",\n      \"Type\": \"String\"\n    },\n    \"public_key\": {\n      \"Description\": \"The optional public key. This allows users to supply the public key from a pre-existing key pair. If not supplied, a new key pair will be generated.\",\n      \"Type\": \"String\"\n    },\n    \"save_private_key\": {\n      \"AllowedValues\": [\n        \"True\",\n        \"true\",\n        \"False\",\n        \"false\"\n      ],\n      \"Default\": false,\n      \"Description\": \"True if the system should remember a generated private key; False otherwise.\",\n      \"Type\": \"String\"\n    }\n  },\n  \"Resources\": {\n    \"KeyPair\": {\n      \"Properties\": {\n        \"name\": {\n          \"Ref\": \"name\"\n        },\n        \"public_key\": {\n          \"Ref\": \"public_key\"\n        },\n        \"save_private_key\": {\n          \"Ref\": \"save_private_key\"\n        }\n      },\n      \"Type\": \"OS::Nova::KeyPair\"\n    }\n  }\n}"
   379  
   380  // GetTemplateOutput represents the response body from a Template request.
   381  const GetTemplateOutput = `
   382  {
   383    "HeatTemplateFormatVersion": "2012-12-12",
   384    "Outputs": {
   385      "private_key": {
   386        "Description": "The private key if it has been saved.",
   387        "Value": "{\"Fn::GetAtt\": [\"KeyPair\", \"private_key\"]}"
   388      },
   389      "public_key": {
   390        "Description": "The public key.",
   391        "Value": "{\"Fn::GetAtt\": [\"KeyPair\", \"public_key\"]}"
   392      }
   393    },
   394    "Parameters": {
   395      "name": {
   396        "Description": "The name of the key pair.",
   397        "Type": "String"
   398      },
   399      "public_key": {
   400        "Description": "The optional public key. This allows users to supply the public key from a pre-existing key pair. If not supplied, a new key pair will be generated.",
   401        "Type": "String"
   402      },
   403      "save_private_key": {
   404        "AllowedValues": [
   405        "True",
   406        "true",
   407        "False",
   408        "false"
   409        ],
   410        "Default": false,
   411        "Description": "True if the system should remember a generated private key; False otherwise.",
   412        "Type": "String"
   413      }
   414    },
   415    "Resources": {
   416      "KeyPair": {
   417        "Properties": {
   418          "name": {
   419            "Ref": "name"
   420          },
   421          "public_key": {
   422            "Ref": "public_key"
   423          },
   424          "save_private_key": {
   425            "Ref": "save_private_key"
   426          }
   427        },
   428        "Type": "OS::Nova::KeyPair"
   429      }
   430    }
   431  }`
   432  
   433  // HandleGetTemplateSuccessfully creates an HTTP handler at `/resource_types/OS::Heat::AResourceName/template`
   434  // on the test handler mux that responds with a `Template` response.
   435  func HandleGetTemplateSuccessfully(t *testing.T, output string) {
   436  	th.Mux.HandleFunc("/resource_types/OS::Heat::AResourceName/template", func(w http.ResponseWriter, r *http.Request) {
   437  		th.TestMethod(t, r, "GET")
   438  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   439  		th.TestHeader(t, r, "Accept", "application/json")
   440  
   441  		w.Header().Set("Content-Type", "application/json")
   442  		w.WriteHeader(http.StatusOK)
   443  		fmt.Fprint(w, output)
   444  	})
   445  }
   446  
   447  // HandleMarkUnhealthySuccessfully creates an HTTP handler at `/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance`
   448  // on the test handler mux that responds with a `MarkUnhealthy` response.
   449  func HandleMarkUnhealthySuccessfully(t *testing.T) {
   450  	th.Mux.HandleFunc("/stacks/teststack/0b1771bd-9336-4f2b-ae86-a80f971faf1e/resources/wordpress_instance", func(w http.ResponseWriter, r *http.Request) {
   451  		th.TestMethod(t, r, "PATCH")
   452  		th.TestHeader(t, r, "X-Auth-Token", fake.TokenID)
   453  		th.TestHeader(t, r, "Accept", "application/json")
   454  
   455  		w.Header().Set("Content-Type", "application/json")
   456  		w.WriteHeader(http.StatusOK)
   457  	})
   458  }