github.com/huaweicloud/golangsdk@v0.0.0-20210831081626-d823fe11ceba/openstack/db/v1/instances/testing/requests_test.go (about)

     1  package testing
     2  
     3  import (
     4  	"testing"
     5  
     6  	db "github.com/huaweicloud/golangsdk/openstack/db/v1/databases"
     7  	"github.com/huaweicloud/golangsdk/openstack/db/v1/instances"
     8  	"github.com/huaweicloud/golangsdk/openstack/db/v1/users"
     9  	"github.com/huaweicloud/golangsdk/pagination"
    10  	th "github.com/huaweicloud/golangsdk/testhelper"
    11  	fake "github.com/huaweicloud/golangsdk/testhelper/client"
    12  )
    13  
    14  func TestCreate(t *testing.T) {
    15  	th.SetupHTTP()
    16  	defer th.TeardownHTTP()
    17  	HandleCreate(t)
    18  
    19  	opts := instances.CreateOpts{
    20  		Name:      "json_rack_instance",
    21  		FlavorRef: "1",
    22  		Databases: db.BatchCreateOpts{
    23  			{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
    24  			{Name: "nextround"},
    25  		},
    26  		Users: users.BatchCreateOpts{
    27  			{
    28  				Name:     "demouser",
    29  				Password: "demopassword",
    30  				Databases: db.BatchCreateOpts{
    31  					{Name: "sampledb"},
    32  				},
    33  			},
    34  		},
    35  		Size: 2,
    36  	}
    37  
    38  	instance, err := instances.Create(fake.ServiceClient(), opts).Extract()
    39  
    40  	th.AssertNoErr(t, err)
    41  	th.AssertDeepEquals(t, &expectedInstance, instance)
    42  }
    43  
    44  func TestCreateWithFault(t *testing.T) {
    45  	th.SetupHTTP()
    46  	defer th.TeardownHTTP()
    47  	HandleCreateWithFault(t)
    48  
    49  	opts := instances.CreateOpts{
    50  		Name:      "json_rack_instance",
    51  		FlavorRef: "1",
    52  		Databases: db.BatchCreateOpts{
    53  			{CharSet: "utf8", Collate: "utf8_general_ci", Name: "sampledb"},
    54  			{Name: "nextround"},
    55  		},
    56  		Users: users.BatchCreateOpts{
    57  			{
    58  				Name:     "demouser",
    59  				Password: "demopassword",
    60  				Databases: db.BatchCreateOpts{
    61  					{Name: "sampledb"},
    62  				},
    63  			},
    64  		},
    65  		Size: 2,
    66  	}
    67  
    68  	instance, err := instances.Create(fake.ServiceClient(), opts).Extract()
    69  
    70  	th.AssertNoErr(t, err)
    71  	th.AssertDeepEquals(t, &expectedInstanceWithFault, instance)
    72  }
    73  
    74  func TestInstanceList(t *testing.T) {
    75  	th.SetupHTTP()
    76  	defer th.TeardownHTTP()
    77  	HandleList(t)
    78  
    79  	pages := 0
    80  	err := instances.List(fake.ServiceClient()).EachPage(func(page pagination.Page) (bool, error) {
    81  		pages++
    82  
    83  		actual, err := instances.ExtractInstances(page)
    84  		if err != nil {
    85  			return false, err
    86  		}
    87  
    88  		th.CheckDeepEquals(t, []instances.Instance{expectedInstance}, actual)
    89  		return true, nil
    90  	})
    91  
    92  	th.AssertNoErr(t, err)
    93  	th.AssertEquals(t, 1, pages)
    94  }
    95  
    96  func TestGetInstance(t *testing.T) {
    97  	th.SetupHTTP()
    98  	defer th.TeardownHTTP()
    99  	HandleGet(t)
   100  
   101  	instance, err := instances.Get(fake.ServiceClient(), instanceID).Extract()
   102  
   103  	th.AssertNoErr(t, err)
   104  	th.AssertDeepEquals(t, &expectedInstance, instance)
   105  }
   106  
   107  func TestDeleteInstance(t *testing.T) {
   108  	th.SetupHTTP()
   109  	defer th.TeardownHTTP()
   110  	HandleDelete(t)
   111  
   112  	res := instances.Delete(fake.ServiceClient(), instanceID)
   113  	th.AssertNoErr(t, res.Err)
   114  }
   115  
   116  func TestEnableRootUser(t *testing.T) {
   117  	th.SetupHTTP()
   118  	defer th.TeardownHTTP()
   119  	HandleEnableRoot(t)
   120  
   121  	expected := &users.User{Name: "root", Password: "secretsecret"}
   122  	user, err := instances.EnableRootUser(fake.ServiceClient(), instanceID).Extract()
   123  
   124  	th.AssertNoErr(t, err)
   125  	th.AssertDeepEquals(t, expected, user)
   126  }
   127  
   128  func TestIsRootEnabled(t *testing.T) {
   129  	th.SetupHTTP()
   130  	defer th.TeardownHTTP()
   131  	HandleIsRootEnabled(t)
   132  
   133  	isEnabled, err := instances.IsRootEnabled(fake.ServiceClient(), instanceID).Extract()
   134  
   135  	th.AssertNoErr(t, err)
   136  	th.AssertEquals(t, true, isEnabled)
   137  }
   138  
   139  func TestRestart(t *testing.T) {
   140  	th.SetupHTTP()
   141  	defer th.TeardownHTTP()
   142  	HandleRestart(t)
   143  
   144  	res := instances.Restart(fake.ServiceClient(), instanceID)
   145  	th.AssertNoErr(t, res.Err)
   146  }
   147  
   148  func TestResize(t *testing.T) {
   149  	th.SetupHTTP()
   150  	defer th.TeardownHTTP()
   151  	HandleResize(t)
   152  
   153  	res := instances.Resize(fake.ServiceClient(), instanceID, "2")
   154  	th.AssertNoErr(t, res.Err)
   155  }
   156  
   157  func TestResizeVolume(t *testing.T) {
   158  	th.SetupHTTP()
   159  	defer th.TeardownHTTP()
   160  	HandleResizeVol(t)
   161  
   162  	res := instances.ResizeVolume(fake.ServiceClient(), instanceID, 4)
   163  	th.AssertNoErr(t, res.Err)
   164  }
   165  
   166  func TestAttachConfigurationGroup(t *testing.T) {
   167  	th.SetupHTTP()
   168  	defer th.TeardownHTTP()
   169  	HandleAttachConfigurationGroup(t)
   170  
   171  	res := instances.AttachConfigurationGroup(fake.ServiceClient(), instanceID, configGroupID)
   172  	th.AssertNoErr(t, res.Err)
   173  }
   174  
   175  func TestDetachConfigurationGroup(t *testing.T) {
   176  	th.SetupHTTP()
   177  	defer th.TeardownHTTP()
   178  	HandleDetachConfigurationGroup(t)
   179  
   180  	res := instances.DetachConfigurationGroup(fake.ServiceClient(), instanceID)
   181  	th.AssertNoErr(t, res.Err)
   182  }