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