github.com/vnpaycloud-console/gophercloud/v2@v2.0.5/openstack/orchestration/v1/stacks/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/vnpaycloud-console/gophercloud/v2"
     8  	"github.com/vnpaycloud-console/gophercloud/v2/openstack/orchestration/v1/stacks"
     9  	"github.com/vnpaycloud-console/gophercloud/v2/pagination"
    10  	th "github.com/vnpaycloud-console/gophercloud/v2/testhelper"
    11  	fake "github.com/vnpaycloud-console/gophercloud/v2/testhelper/client"
    12  )
    13  
    14  func TestCreateStack(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	HandleCreateSuccessfully(t, CreateOutput)
    18  	template := new(stacks.Template)
    19  	template.Bin = []byte(`
    20  		{
    21  			"heat_template_version": "2013-05-23",
    22  			"description": "Simple template to test heat commands",
    23  			"parameters": {
    24  				"flavor": {
    25  					"default": "m1.tiny",
    26  					"type": "string"
    27  				}
    28  			}
    29  		}`)
    30  	createOpts := stacks.CreateOpts{
    31  		Name:            "stackcreated",
    32  		Timeout:         60,
    33  		TemplateOpts:    template,
    34  		DisableRollback: gophercloud.Disabled,
    35  	}
    36  	actual, err := stacks.Create(context.TODO(), fake.ServiceClient(), createOpts).Extract()
    37  	th.AssertNoErr(t, err)
    38  
    39  	expected := CreateExpected
    40  	th.AssertDeepEquals(t, expected, actual)
    41  }
    42  
    43  func TestCreateStackMissingRequiredInOpts(t *testing.T) {
    44  	th.SetupHTTP()
    45  	defer th.TeardownHTTP()
    46  	HandleCreateSuccessfully(t, CreateOutput)
    47  	template := new(stacks.Template)
    48  	template.Bin = []byte(`
    49  		{
    50  			"heat_template_version": "2013-05-23",
    51  			"description": "Simple template to test heat commands",
    52  			"parameters": {
    53  				"flavor": {
    54  					"default": "m1.tiny",
    55  					"type": "string"
    56  				}
    57  			}
    58  		}`)
    59  	createOpts := stacks.CreateOpts{
    60  		DisableRollback: gophercloud.Disabled,
    61  	}
    62  	r := stacks.Create(context.TODO(), fake.ServiceClient(), createOpts)
    63  	th.AssertEquals(t, "error creating the options map: Missing input for argument [Name]", r.Err.Error())
    64  }
    65  
    66  func TestAdoptStack(t *testing.T) {
    67  	th.SetupHTTP()
    68  	defer th.TeardownHTTP()
    69  	HandleCreateSuccessfully(t, CreateOutput)
    70  	template := new(stacks.Template)
    71  	template.Bin = []byte(`
    72  {
    73    "stack_name": "postman_stack",
    74    "template": {
    75  	"heat_template_version": "2013-05-23",
    76  	"description": "Simple template to test heat commands",
    77  	"parameters": {
    78  	  "flavor": {
    79  		"default": "m1.tiny",
    80  		"type": "string"
    81  	  }
    82  	},
    83  	"resources": {
    84  	  "hello_world": {
    85  		"type":"OS::Nova::Server",
    86  		"properties": {
    87  		  "key_name": "heat_key",
    88  		  "flavor": {
    89  			"get_param": "flavor"
    90  		  },
    91  		  "image": "ad091b52-742f-469e-8f3c-fd81cadf0743",
    92  		  "user_data": "#!/bin/bash -xv\necho \"hello world\" > /root/hello-world.txt\n"
    93  		}
    94  	  }
    95  	}
    96    }
    97  }`)
    98  	adoptOpts := stacks.AdoptOpts{
    99  		AdoptStackData:  `{environment{parameters{}}}`,
   100  		Name:            "stackcreated",
   101  		Timeout:         60,
   102  		TemplateOpts:    template,
   103  		DisableRollback: gophercloud.Disabled,
   104  	}
   105  	actual, err := stacks.Adopt(context.TODO(), fake.ServiceClient(), adoptOpts).Extract()
   106  	th.AssertNoErr(t, err)
   107  
   108  	expected := CreateExpected
   109  	th.AssertDeepEquals(t, expected, actual)
   110  }
   111  
   112  func TestListStack(t *testing.T) {
   113  	th.SetupHTTP()
   114  	defer th.TeardownHTTP()
   115  	HandleListSuccessfully(t, FullListOutput)
   116  
   117  	count := 0
   118  	err := stacks.List(fake.ServiceClient(), nil).EachPage(context.TODO(), func(_ context.Context, page pagination.Page) (bool, error) {
   119  		count++
   120  		actual, err := stacks.ExtractStacks(page)
   121  		th.AssertNoErr(t, err)
   122  
   123  		th.CheckDeepEquals(t, ListExpected, actual)
   124  
   125  		return true, nil
   126  	})
   127  	th.AssertNoErr(t, err)
   128  	th.CheckEquals(t, count, 1)
   129  }
   130  
   131  func TestGetStack(t *testing.T) {
   132  	th.SetupHTTP()
   133  	defer th.TeardownHTTP()
   134  	HandleGetSuccessfully(t, GetOutput)
   135  
   136  	actual, err := stacks.Get(context.TODO(), fake.ServiceClient(), "postman_stack", "16ef0584-4458-41eb-87c8-0dc8d5f66c87").Extract()
   137  	th.AssertNoErr(t, err)
   138  
   139  	expected := GetExpected
   140  	th.AssertDeepEquals(t, expected, actual)
   141  }
   142  
   143  func TestFindStack(t *testing.T) {
   144  	th.SetupHTTP()
   145  	defer th.TeardownHTTP()
   146  	HandleFindSuccessfully(t, GetOutput)
   147  
   148  	actual, err := stacks.Find(context.TODO(), fake.ServiceClient(), "16ef0584-4458-41eb-87c8-0dc8d5f66c87").Extract()
   149  	th.AssertNoErr(t, err)
   150  
   151  	expected := GetExpected
   152  	th.AssertDeepEquals(t, expected, actual)
   153  }
   154  
   155  func TestUpdateStack(t *testing.T) {
   156  	th.SetupHTTP()
   157  	defer th.TeardownHTTP()
   158  	HandleUpdateSuccessfully(t)
   159  
   160  	template := new(stacks.Template)
   161  	template.Bin = []byte(`
   162  		{
   163  			"heat_template_version": "2013-05-23",
   164  			"description": "Simple template to test heat commands",
   165  			"parameters": {
   166  				"flavor": {
   167  					"default": "m1.tiny",
   168  					"type": "string"
   169  				}
   170  			}
   171  		}`)
   172  	updateOpts := &stacks.UpdateOpts{
   173  		TemplateOpts: template,
   174  	}
   175  	err := stacks.Update(context.TODO(), fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada", updateOpts).ExtractErr()
   176  	th.AssertNoErr(t, err)
   177  }
   178  
   179  func TestUpdateStackNoTemplate(t *testing.T) {
   180  	th.SetupHTTP()
   181  	defer th.TeardownHTTP()
   182  	HandleUpdateSuccessfully(t)
   183  
   184  	parameters := make(map[string]any)
   185  	parameters["flavor"] = "m1.tiny"
   186  
   187  	updateOpts := &stacks.UpdateOpts{
   188  		Parameters: parameters,
   189  	}
   190  	expected := stacks.ErrTemplateRequired{}
   191  
   192  	err := stacks.Update(context.TODO(), fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada", updateOpts).ExtractErr()
   193  	th.AssertEquals(t, expected, err)
   194  }
   195  
   196  func TestUpdatePatchStack(t *testing.T) {
   197  	th.SetupHTTP()
   198  	defer th.TeardownHTTP()
   199  	HandleUpdatePatchSuccessfully(t)
   200  
   201  	parameters := make(map[string]any)
   202  	parameters["flavor"] = "m1.tiny"
   203  
   204  	updateOpts := &stacks.UpdateOpts{
   205  		Parameters: parameters,
   206  	}
   207  	err := stacks.UpdatePatch(context.TODO(), fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada", updateOpts).ExtractErr()
   208  	th.AssertNoErr(t, err)
   209  }
   210  
   211  func TestDeleteStack(t *testing.T) {
   212  	th.SetupHTTP()
   213  	defer th.TeardownHTTP()
   214  	HandleDeleteSuccessfully(t)
   215  
   216  	err := stacks.Delete(context.TODO(), fake.ServiceClient(), "gophercloud-test-stack-2", "db6977b2-27aa-4775-9ae7-6213212d4ada").ExtractErr()
   217  	th.AssertNoErr(t, err)
   218  }
   219  
   220  func TestPreviewStack(t *testing.T) {
   221  	th.SetupHTTP()
   222  	defer th.TeardownHTTP()
   223  	HandlePreviewSuccessfully(t, GetOutput)
   224  
   225  	template := new(stacks.Template)
   226  	template.Bin = []byte(`
   227  		{
   228  			"heat_template_version": "2013-05-23",
   229  			"description": "Simple template to test heat commands",
   230  			"parameters": {
   231  				"flavor": {
   232  					"default": "m1.tiny",
   233  					"type": "string"
   234  				}
   235  			}
   236  		}`)
   237  	previewOpts := stacks.PreviewOpts{
   238  		Name:            "stackcreated",
   239  		Timeout:         60,
   240  		TemplateOpts:    template,
   241  		DisableRollback: gophercloud.Disabled,
   242  	}
   243  	actual, err := stacks.Preview(context.TODO(), fake.ServiceClient(), previewOpts).Extract()
   244  	th.AssertNoErr(t, err)
   245  
   246  	expected := PreviewExpected
   247  	th.AssertDeepEquals(t, expected, actual)
   248  }
   249  
   250  func TestAbandonStack(t *testing.T) {
   251  	th.SetupHTTP()
   252  	defer th.TeardownHTTP()
   253  	HandleAbandonSuccessfully(t, AbandonOutput)
   254  
   255  	actual, err := stacks.Abandon(context.TODO(), fake.ServiceClient(), "postman_stack", "16ef0584-4458-41eb-87c8-0dc8d5f66c8").Extract()
   256  	th.AssertNoErr(t, err)
   257  
   258  	expected := AbandonExpected
   259  	th.AssertDeepEquals(t, expected, actual)
   260  }