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

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