github.com/gophercloud/gophercloud@v1.11.0/openstack/orchestration/v1/stacks/testing/requests_test.go (about)

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