Skip to content

Commit

Permalink
Avoid pkg global err
Browse files Browse the repository at this point in the history
Signed-off-by: Marek Aufart <[email protected]>
  • Loading branch information
aufi committed Mar 21, 2023
1 parent 152c1eb commit a68ce37
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 22 deletions.
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -120,5 +120,4 @@ test: test-api

# Run Hub REST API tests.
test-api:
echo "Using Hub API from ${HUB_BASE_URL}"
go test -v ./test/api/...
HUB_BASE_URL=${HUB_BASE_URL} go test -v ./test/api/...
6 changes: 3 additions & 3 deletions test/api/application/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ func TestApplicationCreate(t *testing.T) {
for _, application := range samples {
t.Run(fmt.Sprintf("Create application %s", application.Name), func(t *testing.T) {

err = Client.Post(api.ApplicationsRoot, &application)
err := Client.Post(api.ApplicationsRoot, &application)
if err != nil {
t.Errorf("Create error: %v", err.Error()) // Error for standard test failure or failed assertion
}
Expand All @@ -30,7 +30,7 @@ func TestApplicationNotCreateDuplicates(t *testing.T) {
application := Samples()[0]

// Create sample Application.
err = Client.Post(api.ApplicationsRoot, &application)
err := Client.Post(api.ApplicationsRoot, &application)
if err != nil {
t.Errorf("Create error: %v", err.Error())
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func TestApplicationNotCreateWithoutName(t *testing.T) {
}

// Try create the duplicate Application.
err = Client.Post(api.ApplicationsRoot, &emptyApplication)
err := Client.Post(api.ApplicationsRoot, &emptyApplication)
if err == nil {
t.Errorf("Created empty application: %v", emptyApplication)

Expand Down
2 changes: 1 addition & 1 deletion test/api/application/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestApplicationDelete(t *testing.T) {
Create(t, application)

// Delete the application.
err = Client.Delete(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID))
err := Client.Delete(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID))
if err != nil {
t.Errorf("Delete error: %v", err.Error())
}
Expand Down
6 changes: 3 additions & 3 deletions test/api/application/facts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestApplicationFactCRUD(t *testing.T) {

//fmt.Printf("#### fresh fact: %v", fact)
// Create fact.
err = Client.Post(factPath, &fact)
err := Client.Post(factPath, &fact)
if err != nil {
t.Errorf("Create error: %v", err.Error())
}
Expand Down Expand Up @@ -95,7 +95,7 @@ func TestApplicationFactsList(t *testing.T) {

// Create facts.
for _, fact := range SampleFacts {
err = Client.Post(fmt.Sprintf("%s/%d/facts/%s", api.ApplicationsRoot, application.ID, fact.Key), &fact)
err := Client.Post(fmt.Sprintf("%s/%d/facts/%s", api.ApplicationsRoot, application.ID, fact.Key), &fact)
if err != nil {
t.Fatalf("Create error: %v", err.Error())
}
Expand All @@ -106,7 +106,7 @@ func TestApplicationFactsList(t *testing.T) {
for _, pathSuffix := range factsPathSuffix {
t.Run(fmt.Sprintf("Fact list application %s with %s", application.Name, pathSuffix), func(t *testing.T) {
gotFacts := []api.Fact{}
err = Client.Get(fmt.Sprintf("%s/%d/%s", api.ApplicationsRoot, application.ID, pathSuffix), &gotFacts)
err := Client.Get(fmt.Sprintf("%s/%d/%s", api.ApplicationsRoot, application.ID, pathSuffix), &gotFacts)
if err != nil {
t.Errorf("Get list error: %v", err.Error())
}
Expand Down
11 changes: 6 additions & 5 deletions test/api/application/fixtures.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@ import (
"github.com/konveyor/tackle2-hub/test/api/client"
)

// Hub REST API/addon Client
// Setup Hub API client
var Client, err = client.NewHubClient()
var (
// Setup Hub API client
Client = client.Client
)

//if err != nil {
// t.Fatalf("Unable connect to Hub API: %v", err.Error())
Expand Down Expand Up @@ -54,7 +55,7 @@ func Samples() (applications []*api.Application) {
//
// Create an Application (and stop tests on failure).
func Create(t *testing.T, application *api.Application) {
err = Client.Post(api.ApplicationsRoot, &application)
err := Client.Post(api.ApplicationsRoot, &application)
if err != nil {
t.Fatalf("Create fatal error: %v", err.Error()) // Fatal here, Error for standard test failure or failed assertion.
}
Expand All @@ -63,7 +64,7 @@ func Create(t *testing.T, application *api.Application) {
//
// Delete the Application (and stop tests on failure).
func Delete(t *testing.T, application *api.Application) {
err = Client.Delete(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID))
err := Client.Delete(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID))
if err != nil {
t.Fatalf("Delete fatal error: %v", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion test/api/application/get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestApplicationGet(t *testing.T) {

// Try get the application.
gotApplication := api.Application{}
err = Client.Get(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID), &gotApplication)
err := Client.Get(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID), &gotApplication)
if err != nil {
t.Errorf("Get error: %v", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion test/api/application/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestApplicationList(t *testing.T) {

// Try list applications.
gotApplications := []*api.Application{}
err = Client.Get(api.ApplicationsRoot, &gotApplications)
err := Client.Get(api.ApplicationsRoot, &gotApplications)
if err != nil {
t.Errorf("List error: %v", err.Error())
}
Expand Down
2 changes: 1 addition & 1 deletion test/api/application/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestApplicationUpdateName(t *testing.T) {
updateApplication := api.Application{
Name: updatedName,
}
err = Client.Put(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID), &updateApplication)
err := Client.Put(fmt.Sprintf("%s/%d", api.ApplicationsRoot, application.ID), &updateApplication)
if err != nil {
t.Errorf("Update error: %v", err.Error())
}
Expand Down
18 changes: 13 additions & 5 deletions test/api/client/client.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,28 @@
package client

import (
"fmt"
"os"

"github.com/konveyor/tackle2-hub/addon"
"github.com/konveyor/tackle2-hub/api"
)

type Client struct {
client *addon.Client
var Client *addon.Client

func init() {
var err error
baseUrl := os.Getenv("HUB_BASE_URL")
Client, err = NewHubClient(baseUrl, "admin", "")
if err != nil {
panic(fmt.Sprintf("Error: Cannot setup API client for URL '%s': %v.", baseUrl, err.Error()))
}
}

// Add with and without login/token creation
func NewHubClient() (client *addon.Client, err error) {
client = addon.NewClient(os.Getenv("HUB_BASE_URL"), "")
token, err := login(client, "admin", "foobar")
func NewHubClient(baseUrl, username, password string) (client *addon.Client, err error) {
client = addon.NewClient(baseUrl, "")
token, err := login(client, username, password)
if err != nil {
return
}
Expand Down

0 comments on commit a68ce37

Please sign in to comment.