increase code coverage for pkg/client #115

Merged
knoflook merged 4 commits from knoflook/abra:unit-tests into main 2021-10-16 16:18:56 +00:00
4 changed files with 79 additions and 0 deletions

1
.gitignore vendored
View File

@ -3,3 +3,4 @@ abra
vendor/
.envrc
dist/
*fmtcoverage.html

4
pkg/client/README.md Normal file
View File

@ -0,0 +1,4 @@
IMPORTANT POINT ABOUT CONTEXTS
Please use context names starting with `testContext` for testing purposes to ensure that no data is lost. such as `testContext`, `testContext2`, `testContextFail` etc

46
pkg/client/client_test.go Normal file
View File

@ -0,0 +1,46 @@
package client_test
import (
"fmt"
"testing"
"coopcloud.tech/abra/pkg/client"
)
// use at the start to ensure testContext[0, 1, ..., amnt-1] exist and
// testContextFail[0, 1, ..., failAmnt-1] don't exist
func ensureTestState(amnt, failAmnt int) error {
for i := 0; i < amnt; i++ {
err := client.CreateContext(fmt.Sprintf("testContext%d", i), "", "")
knoflook marked this conversation as resolved
Review

nitpick: in a lot of places with just an error check statement we do if err := client.CreateContext(fmt.Sprintf("testContext%d", i), "", ""); err != nil { to inline the thing. Don't need to do this if you don't want tho, just pointing it out.

nitpick: in a lot of places with just an error check statement we do `if err := client.CreateContext(fmt.Sprintf("testContext%d", i), "", ""); err != nil {` to inline the thing. Don't need to do this if you don't want tho, just pointing it out.
Review

yeah I think I'll start doing that from now!

yeah I think I'll start doing that from now!
if err != nil {
return err
}
}
for i := 0; i < failAmnt; i++ {
if _, er := client.GetContext(fmt.Sprintf("testContextFail%d", i)); er == nil {
err := client.DeleteContext(fmt.Sprintf("testContextFail%d", i))
if err != nil {
return err
}
}
}
return nil
}
func TestNew(t *testing.T) {
err := ensureTestState(1, 1)
if err != nil {
t.Errorf("Couldn't ensure existence/nonexistence of contexts: %s", err)
}
contextName := "testContext0"
_, err = client.New(contextName)
if err != nil {
t.Errorf("couldn't initialise a new client with context %s: %s", contextName, err)
}
contextName = "testContextFail0"
_, err = client.New(contextName)
if err == nil {
t.Errorf("client.New(\"testContextFail0\") should have failed but didn't return an error")
}
}

View File

@ -30,6 +30,34 @@ func dockerContext(host, key string) TestContext {
}
}
func TestCreateContext(t *testing.T) {
err := client.CreateContext("testContext0", "wronguser", "wrongport")
if err == nil {
t.Error("client.CreateContext(\"testContextCreate\", \"wronguser\", \"wrongport\") should have failed but didn't return an error")
}
err = client.CreateContext("testContext0", "", "")
if err != nil {
t.Errorf("Couldn't create context: %s", err)
}
}
func TestDeleteContext(t *testing.T) {
ensureTestState(1, 1)
err := client.DeleteContext("default")
if err == nil {
t.Errorf("client.DeleteContext(\"default\") should have failed but didn't return an error")
}
err = client.DeleteContext("testContext0")
if err != nil {
t.Errorf("client.DeleteContext(\"testContext0\") failed: %s", err)
}
err = client.DeleteContext("testContextFail0")
if err == nil {
t.Errorf("client.DeleteContext(\"testContextFail0\") should have failed (attempt to delete non-existent context) but didn't return an error")
}
}
func TestGetContextEndpoint(t *testing.T) {
var testDockerContexts = []TestContext{
dockerContext("ssh://foobar", "docker"),