Skip to content

Commit

Permalink
Merge pull request #132 from ChaseRun/testing-framework
Browse files Browse the repository at this point in the history
Implementation of Testing Framework
  • Loading branch information
Chase Austin authored Apr 12, 2022
2 parents 9ad5ccd + a8efc46 commit 6c5fcda
Show file tree
Hide file tree
Showing 20 changed files with 1,522 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
/.idea/vcs.xml
/.idea/
/vendor/
.vscode/
/.vscode/
.DS_Store
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,4 @@ This section contains some steps required by the dev for contributing changes to

1. Install the latest [go-swagger](https://github.com/go-swagger/go-swagger/releases) version on your machine.
1. Download the last released version of the service broker API definitions (`swagger.yaml`).
1. Run `./bin/update-swagger.sh` from the project root.
1. Run `./bin/update-swagger.sh` from the project root.
34 changes: 34 additions & 0 deletions clients/instance/Development.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
## Integration Tests
Runnning integration tests will create real IBM Power Cloud resources.

- `internal/testutils/integration_utils.go`: Defines commonly used test functions
- `internal/testutils/integration_variables.go`: Lists default values for all test variables
- `internal/testutils/example.env`: An example .env which is used when overriding default test variables
- `internal/testutils/launch.json`: An example test configuration
- `client/instance/*_integration_test.go`: Defines tests for an individual resource type

### Setup
To setup the integration tests, you will neet to create a `.vscode` directory in the project root. Copy `.env` and `launch.json` into `.vscode`. Default integration test values are provided, however, you can define custom variables in `.env`. You can also create additional `.env` files (`staging.env`, `production.env`, `dal12.env`, ...) for different environments, regions, resources, etc. If you create a different `.env` file, modify `launch.json` to include a testing configuration for that file.

Every Integration Test requires these variables:
- `API_KEY` or `BEARER_TOKEN` (deprecated but currently working)
- `CRN`
A default `CRN` is provided, but you will need to define your own `API_KEY` or `BEARER_TOKEN`. It is recomended that you do this in `.env`.
### Test Steps
Each integration test runs through these steps:
- `init()` in `integration_utils.go` is called. This first checks to see any environment variables are defined in `.env`. If an environment variable isn't defined, the default value in `integration_variables.go` is used.
- A precheck function (ex. `ImagePreCheck()`) is called. This verifies that all required variables for the preCheck's test are defined and loaded properly.
- The test runs

### Run a test
To run a test:
- Set `DisableTesting = False` in `integration_variables.go`.
- Double click the test function name to select the text.
- Click `Run and Debug` on the VScode sidebar, and then click `Run a test`. Output will be visible in the VScode Debug Console.
- `launch.json` runs tests by using this selected text. Feel free to create your own `launch.json` configuration.

### Updating
When updating or creating new integration tests:
- Add test variable definitions to `integration_variables.go`.
- Update `init()` and relevant `PreCheck()` functions in `integration_utils.go`
- Make sure to add a way for newly provisioned resources to be deleted at the end of the test
63 changes: 63 additions & 0 deletions clients/instance/ibm-pi-cloud_connection_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package instance_test

import (
"context"
"testing"

utl "internal/testutils"

client "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/stretchr/testify/require"
)

func TestCloudConnection(t *testing.T) {
if utl.DisableTesting {
return
}

// create session and clients
utl.CloudConnectionPreCheck(t)
session := utl.TestSession(t)
cloudConnectionClient := client.NewIBMPICloudConnectionClient(context.Background(), session, utl.CloudInstanceID)
jobClient := client.NewIBMPIJobClient(context.Background(), session, utl.CloudInstanceID)

// CREATE
body := &models.CloudConnectionCreate{
Name: &utl.CloudConnectionName,
Speed: &utl.CloudConnectionSpeed,
}
createRespOk, createRespAccepted, err := cloudConnectionClient.Create(body)
require.Nil(t, err)

// get cloud connection id from recently created cloud connection
var CloudConnectionID string
if createRespOk != nil {
CloudConnectionID = *createRespOk.CloudConnectionID
} else {
CloudConnectionID = *createRespAccepted.CloudConnectionID
utl.WaitForJobState(t, jobClient, *createRespAccepted.JobRef.ID)
}
utl.TestMessage("CREATE Cloud Connection", CloudConnectionID, *createRespAccepted)

// DELETE
defer func() {
delResp, err := cloudConnectionClient.Delete(CloudConnectionID)
require.Nil(t, err)
utl.WaitForJobState(t, jobClient, *delResp.ID)
utl.TestMessage("DELETE Cloud Connection", CloudConnectionID, *delResp)
}()

// GET
getResp, err := cloudConnectionClient.Get(CloudConnectionID)
require.Nil(t, err)
utl.TestMessage("GET Cloud Connection", CloudConnectionID, *getResp)
// verify variables match
require.Equal(t, *getResp.Name, utl.CloudConnectionName)
require.Equal(t, *getResp.Speed, utl.CloudConnectionSpeed)

// GET ALL
getAllResp, err := cloudConnectionClient.GetAll()
require.Nil(t, err)
utl.TestMessage("GET All Cloud Connections", "", *getAllResp)
}
66 changes: 66 additions & 0 deletions clients/instance/ibm-pi-dhcp_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package instance_test

import (
"context"
"fmt"
"testing"
"time"

utl "internal/testutils"

client "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/stretchr/testify/require"
)

func TestDHCP(t *testing.T) {
if utl.DisableTesting {
return
}

// create session and client
utl.DHCPPreCheck(t)
session := utl.TestSession(t)
dhcpClient := client.NewIBMPIDhcpClient(context.Background(), session, utl.CloudInstanceID)

// CREATE
createResp, err := dhcpClient.Create(&models.DHCPServerCreate{})
require.Nil(t, err)
dhcpID := *createResp.ID

// DELETE
defer func() {
err = dhcpClient.Delete(dhcpID)
require.Nil(t, err)
utl.TestMessage("DELETE DHCP", dhcpID, nil)
}()

// check that DHCP is successfully created
createErr := fmt.Sprintf("Create DHCP %s has failed. Still in Build state after %d minutes", dhcpID, ((utl.TimeoutAttempts * utl.JobTimeout) / 60000000000))
checkAttempts := 0
getResp := &models.DHCPServerDetail{}
for checkAttempts < utl.TimeoutAttempts {
getResp, err = dhcpClient.Get(dhcpID)
require.Nil(t, err)
if *getResp.Status == "ERROR" {
t.Fatalf("Newly Created DHCP: %s has Status: ERROR", dhcpID)
}
if *getResp.Status != "BUILD" {
break
}
time.Sleep(time.Duration(utl.JobTimeout))
checkAttempts += 1
}
require.Less(t, checkAttempts, utl.TimeoutAttempts, createErr)
utl.TestMessage("CREATE DHCP", dhcpID, *createResp)

// GET
getResp, err = dhcpClient.Get(dhcpID)
require.Nil(t, err)
utl.TestMessage("GET DHCP", dhcpID, *getResp)

// GET ALL
getAllResp, err := dhcpClient.GetAll()
require.Nil(t, err)
utl.TestMessage("GET ALL DHCPs", "", getAllResp)
}
64 changes: 64 additions & 0 deletions clients/instance/ibm-pi-image_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package instance_test

import (
"context"
"testing"

utl "internal/testutils"

client "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/stretchr/testify/require"
)

func TestImage(t *testing.T) {
if utl.DisableTesting {
return
}

// create session and client
utl.ImagePreCheck(t)
session := utl.TestSession(t)
imageClient := client.NewIBMPIImageClient(context.Background(), session, utl.CloudInstanceID)

// CREATE
// imageBody := &models.CreateImage{
// ImageID: utl.ImageID,
// Source: &utl.ImageSource,
// }
// createResp, err := imageClient.Create(imageBody)
// require.Nil(t, err)
// createdImageID := *createResp.ImageID
// utl.TestMessage("CREATE Image", createdImageID, *createResp)

// // DELETE
// defer func() {
// err = imageClient.Delete(createdImageID)
// require.Nil(t, err)
// utl.TestMessage("DELETE Image", createdImageID, nil)
// }()

// GET
// getResp, err := imageClient.Get(createdImageID)
// require.Nil(t, err)
// utl.TestMessage("GET Image", createdImageID, *getResp)

// GET ALL
getAllResp, err := imageClient.GetAll()
require.Nil(t, err)
utl.TestMessage("GET All Images", "", *getAllResp)

// GET All SAP Images
getSapResp, err := imageClient.GetAllStockImages(true, false)
require.Nil(t, err)
utl.TestMessage("GET All SAP Images", "", *getSapResp)

// GET ALL Stock Images
getStockResp, err := imageClient.GetAllStockImages(true, true)
require.Nil(t, err)
utl.TestMessage("GET All Stock Images", "", *getStockResp)

// GET ALL VTL Images
getVtlImages, err := imageClient.GetAllStockImages(false, true)
require.Nil(t, err)
utl.TestMessage("GET All VTL Images", "", *getVtlImages)
}
88 changes: 88 additions & 0 deletions clients/instance/ibm-pi-instance_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package instance_test

import (
"context"
"fmt"
"testing"
"time"

utl "internal/testutils"

client "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/stretchr/testify/require"
)

func TestInstance(t *testing.T) {
if utl.DisableTesting {
return
}

// create session and client
utl.InstancePreCheck(t)
session := utl.TestSession(t)
instanceClient := client.NewIBMPIInstanceClient(context.Background(), session, utl.CloudInstanceID)

// CREATE
body := &models.PVMInstanceCreate{
ImageID: &utl.InstanceImageID,
KeyPairName: utl.InstanceSSHKey,
NetworkIDs: []string{utl.InstanceNetworkID},
ServerName: &utl.InstanceName,
//VolumeIds: []string{InstanceVolumeID},
Memory: &utl.InstanceMemory,
Processors: &utl.InstanceProcessors,
ProcType: &utl.InstanceProcType,
SysType: utl.InstanceSysType,
StorageType: utl.InstanceStorageType,
}
createResp, err := instanceClient.Create(body)
require.Nil(t, err)
InstanceID := *(*createResp)[0].PvmInstanceID

// DELETE
defer func() {
err = instanceClient.Delete(InstanceID)
require.Nil(t, err)
utl.TestMessage("DELETE Instance", InstanceID, nil)
}()

// check that instance is successfully created
createErr := fmt.Sprintf("Create Instance %s has failed. Still in Build state after %d minutes", InstanceID, ((utl.TimeoutAttempts * utl.JobTimeout) / 60000000000))
checkAttempts := 0
getResp := &models.PVMInstance{}
for checkAttempts < utl.TimeoutAttempts {
getResp, err = instanceClient.Get(InstanceID)
require.Nil(t, err)
if *getResp.Status == "ERROR" {
t.Fatalf("Newly Created Instance: %s has Status: ERROR", InstanceID)
}
if *getResp.Status != "BUILD" {
break
}
time.Sleep(time.Duration(utl.JobTimeout))
checkAttempts += 1
}
require.Less(t, checkAttempts, utl.TimeoutAttempts, createErr)
utl.TestMessage("CREATE Instance", InstanceID, *createResp)

// GET
getResp, err = instanceClient.Get(InstanceID)
require.Nil(t, err)
utl.TestMessage("GET Instance", InstanceID, *getResp)
// verify variables match
//require.Equal(t, *getResp.ImageID, utl.InstanceImageID)
require.Equal(t, getResp.NetworkIDs, []string{utl.InstanceNetworkID})
require.Equal(t, *getResp.ServerName, utl.InstanceName)
require.Equal(t, getResp.SysType, utl.InstanceSysType)

// these are not "set" when calling get immediately after reation for some reason
//require.Equal(t, *getResp.Memory, InstanceMemory)
//require.Equal(t, *getResp.Processors, InstanceProcessors)
//require.Equal(t, *getResp.ProcType, InstanceProcType)

// GET ALL
getAllResp, err := instanceClient.GetAll()
require.Nil(t, err)
utl.TestMessage("GET All Instances", "", *getAllResp)
}
52 changes: 52 additions & 0 deletions clients/instance/ibm-pi-key_integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package instance_test

import (
"context"
"testing"

utl "internal/testutils"

client "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/stretchr/testify/require"
)

func TestKey(t *testing.T) {
if utl.DisableTesting {
return
}

// create session and client
utl.SSHKeyPreCheck(t)
session := utl.TestSession(t)
sshKeyClient := client.NewIBMPIKeyClient(context.Background(), session, utl.CloudInstanceID)

// CREATE
body := &models.SSHKey{
Name: &utl.SSHKeyName,
SSHKey: &utl.SSHKeyRSA,
}
createResp, err := sshKeyClient.Create(body)
require.Nil(t, err)
utl.TestMessage("CREATE SSH Key", utl.SSHKeyName, *createResp)

// DELETE
defer func() {
err := sshKeyClient.Delete(utl.SSHKeyName)
require.Nil(t, err)
utl.TestMessage("DELETE SSH Key", utl.SSHKeyName, nil)
}()

// GET
getResp, err := sshKeyClient.Get(utl.SSHKeyName)
require.Nil(t, err)
utl.TestMessage("GET SSH Key", utl.SSHKeyName, *getResp)
// verify variables match
require.Equal(t, *getResp.Name, utl.SSHKeyName)
require.Equal(t, *getResp.SSHKey, utl.SSHKeyRSA)

// GET ALL
getAllResp, err := sshKeyClient.GetAll()
require.Nil(t, err)
utl.TestMessage("GET All SSH Keys", "", *getAllResp)
}
Loading

0 comments on commit 6c5fcda

Please sign in to comment.