Merge pull request #94 from huyujie/master
1 fix-go-test-err. 2 refactor exiting testing code.
This commit is contained in:
commit
aa89edef9b
|
@ -0,0 +1,153 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/DATA-DOG/godog"
|
||||
"github.com/DATA-DOG/godog/gherkin"
|
||||
qcErrors "github.com/yunify/qingcloud-sdk-go/request/errors"
|
||||
qc "github.com/yunify/qingcloud-sdk-go/service"
|
||||
"strconv"
|
||||
"time"
|
||||
)
|
||||
|
||||
var instanceService *qc.InstanceService
|
||||
var runInstanceInput *qc.RunInstancesInput
|
||||
var runInstanceOutput *qc.RunInstancesOutput
|
||||
|
||||
func InstanceFeatureContext(s *godog.Suite) {
|
||||
s.Step(`^initialize instance service$`, initializeInstanceService)
|
||||
s.Step(`^the instance service is initialized$`, theInstanceServiceIsInitialized)
|
||||
|
||||
s.Step(`^instance configuration:$`, instanceConfiguration)
|
||||
s.Step(`^run instances$`, runInstances)
|
||||
|
||||
s.Step(`^run instances should get a job ID$`, runInstancesShouldGetAJobID)
|
||||
s.Step(`^run instances will be finished$`, runInstancesWillBeFinished)
|
||||
|
||||
s.Step(`^terminate instances$`, terminateInstances)
|
||||
s.Step(`^terminate instances should get a job ID$`, terminateInstancesShouldGetAJobID)
|
||||
s.Step(`^terminate instances will be finished$`, terminateInstancesWillBeFinished)
|
||||
}
|
||||
|
||||
func initializeInstanceService() error {
|
||||
instanceService, err = qcService.Instance(tc.Zone)
|
||||
return err
|
||||
}
|
||||
|
||||
func theInstanceServiceIsInitialized() error {
|
||||
if instanceService == nil {
|
||||
return errors.New("Instance sub service is not initialized")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
func instanceConfiguration(configuration *gherkin.DataTable) error {
|
||||
count, err := strconv.Atoi(configuration.Rows[1].Cells[2].Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
runInstanceInput = &qc.RunInstancesInput{
|
||||
ImageID: qc.String(configuration.Rows[1].Cells[0].Value),
|
||||
InstanceType: qc.String(configuration.Rows[1].Cells[1].Value),
|
||||
Count: qc.Int(count),
|
||||
LoginMode: qc.String(configuration.Rows[1].Cells[3].Value),
|
||||
LoginPasswd: qc.String(configuration.Rows[1].Cells[4].Value),
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runInstances() error {
|
||||
runInstanceOutput, err = instanceService.RunInstances(runInstanceInput)
|
||||
return err
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
func runInstancesShouldGetAJobID() error {
|
||||
if runInstanceOutput.JobID != nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New("RunInstances don't get a job ID")
|
||||
}
|
||||
|
||||
func runInstancesWillBeFinished() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
describeJobOutput, err := jobService.DescribeJobs(
|
||||
&qc.DescribeJobsInput{
|
||||
Jobs: []*string{runInstanceOutput.JobID},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if qc.StringValue(describeJobOutput.JobSet[0].Status) == "successful" {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var terminateInstanceOutput *qc.TerminateInstancesOutput
|
||||
|
||||
func terminateInstances() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
terminateInstanceOutput, err = instanceService.TerminateInstances(
|
||||
&qc.TerminateInstancesInput{
|
||||
Instances: runInstanceOutput.Instances,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *qcErrors.QingCloudError:
|
||||
fmt.Println(e)
|
||||
if e.RetCode != 1400 {
|
||||
return e
|
||||
}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func terminateInstancesShouldGetAJobID() error {
|
||||
if terminateInstanceOutput.JobID != nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New("TerminateInstances doesn't get a job ID")
|
||||
}
|
||||
|
||||
func terminateInstancesWillBeFinished() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
describeJobOutput, err := jobService.DescribeJobs(
|
||||
&qc.DescribeJobsInput{
|
||||
Jobs: []*string{terminateInstanceOutput.JobID},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if qc.StringValue(describeJobOutput.JobSet[0].Status) == "successful" {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -0,0 +1,65 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/DATA-DOG/godog"
|
||||
qc "github.com/yunify/qingcloud-sdk-go/service"
|
||||
)
|
||||
|
||||
func JobFeatureContext(s *godog.Suite) {
|
||||
s.Step(`^initialize job service$`, initializeJobService)
|
||||
s.Step(`^the job service is initialized$`, theJobServiceIsInitialized)
|
||||
|
||||
s.Step(`^describe jobs$`, describeJobs)
|
||||
s.Step(`^describe jobs should get (\d+) job at least$`, describeJobsShouldGetJobAtLeast)
|
||||
s.Step(`^describe jobs should have the jobs I just created$`, describeJobsShouldHaveTheJobsIJustCreated)
|
||||
}
|
||||
|
||||
var jobService *qc.JobService
|
||||
var describeJobOutput *qc.DescribeJobsOutput
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
func initializeJobService() error {
|
||||
jobService, err = qcService.Job(tc.Zone)
|
||||
return err
|
||||
}
|
||||
|
||||
func theJobServiceIsInitialized() error {
|
||||
if jobService == nil {
|
||||
return errors.New("Job sub service is not initialized")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
func describeJobs() error {
|
||||
describeJobOutput, err = jobService.DescribeJobs(nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func describeJobsShouldGetJobAtLeast(count int) error {
|
||||
if len(describeJobOutput.JobSet) >= count {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("DescribeJobs doesn't get \"%d\" job(s)", count)
|
||||
}
|
||||
|
||||
func describeJobsShouldHaveTheJobsIJustCreated() error {
|
||||
okCount := 0
|
||||
for _, job := range describeJobOutput.JobSet {
|
||||
if qc.StringValue(job.JobID) == qc.StringValue(runInstanceOutput.JobID) {
|
||||
okCount++
|
||||
}
|
||||
if qc.StringValue(job.JobID) == qc.StringValue(terminateInstanceOutput.JobID) {
|
||||
okCount++
|
||||
}
|
||||
}
|
||||
|
||||
if okCount == 2 {
|
||||
return nil
|
||||
}
|
||||
return errors.New("DescribeJobs doesn't get the jobs I just created")
|
||||
}
|
|
@ -14,7 +14,7 @@
|
|||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package test
|
||||
package main
|
||||
|
||||
import (
|
||||
"io/ioutil"
|
||||
|
@ -34,6 +34,8 @@ func TestMain(m *testing.M) {
|
|||
|
||||
context := func(s *godog.Suite) {
|
||||
QingCloudServiceFeatureContext(s)
|
||||
InstanceFeatureContext(s)
|
||||
JobFeatureContext(s)
|
||||
}
|
||||
options := godog.Options{
|
||||
Format: "pretty",
|
||||
|
@ -50,7 +52,7 @@ func TestMain(m *testing.M) {
|
|||
func setUp() {
|
||||
loadTestConfig()
|
||||
loadConfig()
|
||||
initQingStorService()
|
||||
initQingCloudService()
|
||||
}
|
||||
|
||||
var err error
|
||||
|
@ -86,7 +88,7 @@ func loadConfig() {
|
|||
}
|
||||
}
|
||||
|
||||
func initQingStorService() {
|
||||
func initQingCloudService() {
|
||||
if qcService == nil {
|
||||
qcService, err = qc.Init(c)
|
||||
checkErrorForExit(err)
|
||||
|
|
|
@ -14,17 +14,12 @@
|
|||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package test
|
||||
package main
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"github.com/DATA-DOG/godog"
|
||||
"github.com/DATA-DOG/godog/gherkin"
|
||||
qcErrors "github.com/yunify/qingcloud-sdk-go/request/errors"
|
||||
qc "github.com/yunify/qingcloud-sdk-go/service"
|
||||
)
|
||||
|
||||
|
@ -33,35 +28,13 @@ func QingCloudServiceFeatureContext(s *godog.Suite) {
|
|||
s.Step(`^initialize QingCloud service$`, initializeQingCloudService)
|
||||
s.Step(`^the QingCloud service is initialized$`, theQingCloudServiceIsInitialized)
|
||||
|
||||
s.Step(`^initialize instance service$`, initializeInstanceService)
|
||||
s.Step(`^the instance service is initialized$`, theInstanceServiceIsInitialized)
|
||||
|
||||
s.Step(`^initialize job service$`, initializeJobService)
|
||||
s.Step(`^the job service is initialized$`, theJobServiceIsInitialized)
|
||||
|
||||
s.Step(`^describe zones$`, describeZones)
|
||||
s.Step(`^describe zones should get (\d+) zone at least$`, describeZonesShouldGetZoneAtLeast)
|
||||
s.Step(`^describe zones should have the zone I\'m using$`, describeZonesShouldHaveTheZoneIamUsing)
|
||||
|
||||
s.Step(`^instance configuration:$`, instanceConfiguration)
|
||||
s.Step(`^run instances$`, runInstances)
|
||||
s.Step(`^run instances should get a job ID$`, runInstancesShouldGetAJobID)
|
||||
s.Step(`^run instances will be finished$`, runInstancesWillBeFinished)
|
||||
|
||||
s.Step(`^terminate instances$`, terminateInstances)
|
||||
s.Step(`^terminate instances should get a job ID$`, terminateInstancesShouldGetAJobID)
|
||||
s.Step(`^terminate instances will be finished$`, terminateInstancesWillBeFinished)
|
||||
|
||||
s.Step(`^describe jobs$`, describeJobs)
|
||||
s.Step(`^describe jobs should get (\d+) job at least$`, describeJobsShouldGetJobAtLeast)
|
||||
s.Step(`^describe jobs should have the jobs I just created$`, describeJobsShouldHaveTheJobsIJustCreated)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var instanceService *qc.InstanceService
|
||||
var jobService *qc.JobService
|
||||
|
||||
func initializeQingCloudService() error {
|
||||
return nil
|
||||
}
|
||||
|
@ -73,30 +46,6 @@ func theQingCloudServiceIsInitialized() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func initializeInstanceService() error {
|
||||
instanceService, err = qcService.Instance(tc.Zone)
|
||||
return err
|
||||
}
|
||||
|
||||
func theInstanceServiceIsInitialized() error {
|
||||
if instanceService == nil {
|
||||
return errors.New("Instance sub service is not initialized")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func initializeJobService() error {
|
||||
jobService, err = qcService.Job(tc.Zone)
|
||||
return err
|
||||
}
|
||||
|
||||
func theJobServiceIsInitialized() error {
|
||||
if jobService == nil {
|
||||
return errors.New("Job sub service is not initialized")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var describeZonesOutput *qc.DescribeZonesOutput
|
||||
|
@ -107,7 +56,7 @@ func describeZones() error {
|
|||
}
|
||||
|
||||
func describeZonesShouldGetZoneAtLeast(count int) error {
|
||||
if len(describeZonesOutput.ZoneSet) > count {
|
||||
if len(describeZonesOutput.ZoneSet) >= count {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("DescribeZones only got \"%d\" zone(s)", count)
|
||||
|
@ -122,147 +71,3 @@ func describeZonesShouldHaveTheZoneIamUsing() error {
|
|||
|
||||
return fmt.Errorf("DescribeZones dosen't have zone \"%s\"", tc.Zone)
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var runInstanceInput *qc.RunInstancesInput
|
||||
var runInstanceOutput *qc.RunInstancesOutput
|
||||
|
||||
func instanceConfiguration(configuration *gherkin.DataTable) error {
|
||||
count, err := strconv.Atoi(configuration.Rows[1].Cells[2].Value)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
runInstanceInput = &qc.RunInstancesInput{
|
||||
ImageID: qc.String(configuration.Rows[1].Cells[0].Value),
|
||||
InstanceType: qc.String(configuration.Rows[1].Cells[1].Value),
|
||||
Count: qc.Int(count),
|
||||
LoginMode: qc.String(configuration.Rows[1].Cells[3].Value),
|
||||
LoginPasswd: qc.String(configuration.Rows[1].Cells[4].Value),
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func runInstances() error {
|
||||
runInstanceOutput, err = instanceService.RunInstances(runInstanceInput)
|
||||
return err
|
||||
}
|
||||
|
||||
func runInstancesShouldGetAJobID() error {
|
||||
if runInstanceOutput.JobID != nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New("RunInstances don't get a job ID")
|
||||
}
|
||||
|
||||
func runInstancesWillBeFinished() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
describeJobOutput, err := jobService.DescribeJobs(
|
||||
&qc.DescribeJobsInput{
|
||||
Jobs: []*string{runInstanceOutput.JobID},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if qc.StringValue(describeJobOutput.JobSet[0].Status) == "successful" {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var terminateInstanceOutput *qc.TerminateInstancesOutput
|
||||
|
||||
func terminateInstances() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
terminateInstanceOutput, err = instanceService.TerminateInstances(
|
||||
&qc.TerminateInstancesInput{
|
||||
Instances: runInstanceOutput.Instances,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
switch e := err.(type) {
|
||||
case *qcErrors.QingCloudError:
|
||||
fmt.Println(e)
|
||||
if e.RetCode != 1400 {
|
||||
return e
|
||||
}
|
||||
default:
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func terminateInstancesShouldGetAJobID() error {
|
||||
if terminateInstanceOutput.JobID != nil {
|
||||
return nil
|
||||
}
|
||||
return errors.New("TerminateInstances doesn't get a job ID")
|
||||
}
|
||||
|
||||
func terminateInstancesWillBeFinished() error {
|
||||
retries := 0
|
||||
for retries < tc.MaxRetries {
|
||||
describeJobOutput, err := jobService.DescribeJobs(
|
||||
&qc.DescribeJobsInput{
|
||||
Jobs: []*string{terminateInstanceOutput.JobID},
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if qc.StringValue(describeJobOutput.JobSet[0].Status) == "successful" {
|
||||
return nil
|
||||
}
|
||||
retries++
|
||||
time.Sleep(time.Second * time.Duration(tc.RetryWaitTime))
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// --------------------------------------------------------------------------
|
||||
|
||||
var describeJobOutput *qc.DescribeJobsOutput
|
||||
|
||||
func describeJobs() error {
|
||||
describeJobOutput, err = jobService.DescribeJobs(nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func describeJobsShouldGetJobAtLeast(count int) error {
|
||||
if len(describeJobOutput.JobSet) > count {
|
||||
return nil
|
||||
}
|
||||
return fmt.Errorf("DescribeJobs doesn't get \"%d\" job(s)", count)
|
||||
}
|
||||
|
||||
func describeJobsShouldHaveTheJobsIJustCreated() error {
|
||||
okCount := 0
|
||||
for _, job := range describeJobOutput.JobSet {
|
||||
if qc.StringValue(job.JobID) == qc.StringValue(runInstanceOutput.JobID) {
|
||||
okCount++
|
||||
}
|
||||
if qc.StringValue(job.JobID) == qc.StringValue(terminateInstanceOutput.JobID) {
|
||||
okCount++
|
||||
}
|
||||
}
|
||||
|
||||
if okCount == 2 {
|
||||
return nil
|
||||
}
|
||||
return errors.New("DescribeJobs doesn't get the jobs I just created")
|
||||
}
|
||||
|
|
|
@ -14,7 +14,7 @@
|
|||
// | limitations under the License.
|
||||
// +-------------------------------------------------------------------------
|
||||
|
||||
package test
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
Loading…
Reference in New Issue