Skip to content

Commit

Permalink
Remove mock test fallback (#193)
Browse files Browse the repository at this point in the history
* ovirt password is mandatory for live test helper
* made tests runnable with either mock or live client
* added nolint to pass new golangci-lint v1.46.0
  • Loading branch information
engelmi authored May 10, 2022
1 parent e223831 commit 9949879
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 103 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
run: |
set -euo pipefail
go generate
go test -json -v ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
go test -json -v -client=mock ./... 2>&1 | tee /tmp/gotest.log | gotestfmt
- name: Upload test log
uses: actions/upload-artifact@v2
if: always()
Expand Down
9 changes: 5 additions & 4 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ You can run the tests against the mock backend simply by running:

```
go generate
go test -v ./...
go test -v -client=mock ./...
```

If you don't specify `-client`, it will default to `all` and run each test against the mock backend as well as the live oVirt engine.

Before you submit, we would like to ask you to run your tests against the live oVirt engine as we do not have one integrated with the CI at the moment. You can do so by running tests as follows:

```
Expand All @@ -73,11 +75,10 @@ OVIRT_SYSTEM=1
# OVIRT_INSECURE=1
# Run the tests
go test -v ./...
go test -v -client=live ./...
```

If you want to connect to a live oVirt engine you need to define these environment variables.
If the variables are not defined, the test will run against the internal mock backend.
To get a PR merged please run your tests against both the mock and the live backend.

In the test code you can then obtain the test helper using the `getHelper(t)` function:
Expand All @@ -86,7 +87,7 @@ In the test code you can then obtain the test helper using the `getHelper(t)` fu
helper := getHelper(t)
```

The client is then available using the `helper.GetClient()` function.
The client is then available using the `helper.GetClient()` function.

## Submitting a PR

Expand Down
14 changes: 10 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,17 @@ The test helper can work in two ways:
Either it sets up test fixtures in the mock client, or it sets up a live connection and identifies a usable storage
domain, cluster, etc. for testing purposes.

The easiest way to set up the test helper is using environment variables. To do that, you can use the
`ovirtclient.NewTestHelperFromEnv()` function:
The ovirtclient.NewMockTestHelper() function can be used to create a test helper with a mock client in the backend:

```go
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper := ovirtclient.NewMockTestHelper(ovirtclientlog.NewNOOPLogger())
```

The easiest way to set up the test helper for a live connection is by using environment variables. To do that, you
can use the `ovirtclient.NewLiveTestHelperFromEnv()` function:

```go
helper := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
```

This function will inspect environment variables to determine if a connection to a live oVirt engine can be estabilshed.
Expand Down Expand Up @@ -144,7 +150,7 @@ func TestSomething(t *testing.T) {
"https://localhost/ovirt-engine/api",
"admin@internal",
"super-secret",
// Leave these empty for auto-detection /fixture setup
// Leave these empty for auto-detection / fixture setup
params,
ovirtclient.TLS().CACertsFromSystem(),
isMock,
Expand Down
5 changes: 4 additions & 1 deletion disk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ func ExampleDiskClient_CreateDisk() {
// Create a logger. This can be adapter to use your own logger.
logger := ovirtclientlog.NewNOOPLogger()
// Create the test helper. This will give us our test storage domain.
helper := ovirtclient.NewTestHelperFromEnv(logger)
helper, err := ovirtclient.NewMockTestHelper(logger)
if err != nil {
panic(fmt.Errorf("failed to create mock test helper (%w)", err))
}
// Create the client. Replace with ovirtclient.New() for production use.
client := helper.GetClient()

Expand Down
38 changes: 23 additions & 15 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,25 +52,29 @@ The test helper can work in two ways:
Either it sets up test fixtures in the mock client, or it sets up a live connection and identifies a usable storage
domain, cluster, etc. for testing purposes.
The easiest way to set up the test helper is using environment variables. To do that, you can use the
ovirtclient.NewTestHelperFromEnv() function:
The ovirtclient.NewMockTestHelper() function can be used to create a test helper with a mock client in the backend:
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper := ovirtclient.NewMockTestHelper(ovirtclientlog.NewNOOPLogger())
The easiest way to set up the test helper for a live connection is by using environment variables. To do that, you
can use the ovirtclient.NewLiveTestHelperFromEnv() function:
helper := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
This function will inspect environment variables to determine if a connection to a live oVirt engine can be
established. The following environment variables are supported:
OVIRT_URL
URL of the oVirt engine API.
URL of the oVirt engine API. Mandatory.
OVIRT_USERNAME
The username for the oVirt engine.
The username for the oVirt engine. Mandatory.
OVIRT_PASSWORD
The password for the oVirt engine
The password for the oVirt engine. Mandatory.
OVIRT_CAFILE
Expand Down Expand Up @@ -114,22 +118,26 @@ You can also create the test helper manually:
// Create a logger that logs to the standard Go log here
logger := ovirtclientlog.NewTestLogger(t)
// Set to true to use in-memory mock, otherwise this will use a live connection.
mock := false
// Set to true to use in-memory mock, otherwise this will use a live connection
isMock := true
// The following parameters define which infrastructure parts to use for testing
params := ovirtclient.TestHelperParams().
WithClusterID(ovirtclient.ClusterID(os.Getenv("OVIRT_CLUSTER_ID"))).
WithBlankTemplateID(ovirtclient.TemplateID(os.Getenv("OVIRT_BLANK_TEMPLATE_ID"))).
WithStorageDomainID(ovirtclient.StorageDomainID(os.Getenv("OVIRT_STORAGE_DOMAIN_ID"))).
WithSecondaryStorageDomainID(ovirtclient.StorageDomainID(os.Getenv("OVIRT_SECONDARY_STORAGE_DOMAIN_ID"))).
WithVNICProfileID(ovirtclient.VNICProfileID(os.Getenv("OVIRT_VNIC_PROFILE_ID")))
// Create the test helper
helper, err := ovirtclient.NewTestHelper(
"https://localhost/ovirt-engine/api",
"admin@internal",
"super-secret",
// Leave these empty for auto-detection / fixture setup
params,
ovirtclient.TLS().CACertsFromSystem(),
// The following parameters define which infrastructure parts to use for testing.
// Leave these empty for auto-detection / fixture setup.
os.Getenv("OVIRT_CLUSTER_ID"),
os.Getenv("OVIRT_BLANK_TEMPLATE_ID"),
os.Getenv("OVIRT_STORAGE_DOMAIN_ID"),
os.Getenv("OVIRT_VNIC_PROFILE_ID"),
mock,
isMock,
logger,
)
if err != nil {
Expand Down
5 changes: 4 additions & 1 deletion example_vm_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ import (
// using the test helper, but can be easily modified to use the client directly.
func ExampleVMClient_create() {
// Create the helper for testing. Alternatively, you could create a production client with ovirtclient.New()
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
// Get the oVirt client
client := helper.GetClient()

Expand Down
5 changes: 4 additions & 1 deletion example_vm_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import (
// The following example demonstrates how to list virtual machines.
func ExampleVMClient_list() {
// Create the helper for testing. Alternatively, you could create a production client with ovirtclient.New()
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
// Get the oVirt client
client := helper.GetClient()

Expand Down
10 changes: 8 additions & 2 deletions example_vm_uploadimage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ func ExampleDiskClient_uploadImage() {
}

// Obtain oVirt client. Alternatively, you can call ovirtclient.New() to do this directly.
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
client := helper.GetClient()

imageName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
Expand Down Expand Up @@ -70,7 +73,10 @@ func ExampleDiskClient_uploadImageWithCancel() {
}

// Obtain oVirt client. Alternatively, you can call ovirtclient.New() to do this directly.
helper := ovirtclient.NewTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewNOOPLogger())
if err != nil {
panic(fmt.Errorf("failed to create live test helper (%w)", err))
}
client := helper.GetClient()

imageName := fmt.Sprintf("client_test_%s", helper.GenerateRandomID(5))
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ go 1.16
require (
github.com/google/uuid v1.3.0
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 // indirect
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0
github.com/stretchr/testify v1.7.0 // indirect
)
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c h1:jXRFpl7+W0YZj/fghoYuE4vJWW/KeQGvdrhnRwRGtAY=
github.com/ovirt/go-ovirt v0.0.0-20220427092237-114c47f2835c/go.mod h1:Zkdj9/rW6eyuw0uOeEns6O3pP5G2ak+bI/tgkQ/tEZI=
github.com/ovirt/go-ovirt-client-log/v2 v2.2.0 h1:7iZQs+8moX7aopeAdNU1b12mF/yWWBVA/Pey55F9PTQ=
github.com/ovirt/go-ovirt-client-log/v2 v2.2.0/go.mod h1:mDoU3KIwftpsgZGzXGk5d2UEJYTY0bYMfg/GwPapXL0=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0 h1:uvACVHYhYPMkNJrrgWiABcfELB6qoFfsDDUTbpb4Jv4=
github.com/ovirt/go-ovirt-client-log/v3 v3.0.0/go.mod h1:chKKxCv4lRjxezrTG+EIhkWXGhDAWByglPVXh/iYdnQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
67 changes: 67 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ovirtclient_test

import (
"flag"
"fmt"
"os"
"testing"

ovirtclient "github.com/ovirt/go-ovirt-client"
ovirtclientlog "github.com/ovirt/go-ovirt-client-log/v3"
)

var getHelper func(t *testing.T) ovirtclient.TestHelper

func getHelperLive(t *testing.T) ovirtclient.TestHelper {
helper, err := ovirtclient.NewLiveTestHelperFromEnv(ovirtclientlog.NewTestLogger(t))
if err != nil {
t.Fatal(fmt.Errorf("failed to create live test helper (%w)", err))
}
return helper
}

func getHelperMock(t *testing.T) ovirtclient.TestHelper {
helper, err := ovirtclient.NewMockTestHelper(ovirtclientlog.NewTestLogger(t))
if err != nil {
t.Fatal(fmt.Errorf("failed to create mock test helper (%w)", err))
}
return helper
}

func TestMain(m *testing.M) {

flagValueClientMock := "mock"
flagValueClientLive := "live"
flagValueClientAll := "all"

clientFlag := flag.String("client", flagValueClientAll,
"Client to use for running the tests. \n"+
"Supported values: \n"+
fmt.Sprintf("\t%s\t: Run tests with mock client \n", flagValueClientMock)+
fmt.Sprintf("\t%s\t: Run tests with live client \n", flagValueClientLive)+
fmt.Sprintf("\t%s\t: Run tests with mock and live client \n", flagValueClientAll),
)
flag.Parse()

switch *clientFlag {
case flagValueClientLive:
getHelper = getHelperLive
exitVal := m.Run()
os.Exit(exitVal)
case flagValueClientMock:
getHelper = getHelperMock
exitVal := m.Run()
os.Exit(exitVal)
case flagValueClientAll:
getHelper = getHelperMock
exitVal := m.Run()
if exitVal != 0 {
os.Exit(exitVal)
}
getHelper = getHelperLive
exitVal = m.Run()
os.Exit(exitVal)
default:
panic(fmt.Errorf("Unsupported client '%s'", *clientFlag))
}
}
12 changes: 0 additions & 12 deletions util_get_helper_test.go

This file was deleted.

Loading

0 comments on commit 9949879

Please sign in to comment.