Revert "test: remove broken tests for client"

This reverts commit 17a5f1529a.
This commit is contained in:
decentral1se 2021-11-09 17:58:31 +01:00
parent 6f26b51f3e
commit 59031595ea
No known key found for this signature in database
GPG Key ID: 5E2EF5A63E3718CC
2 changed files with 127 additions and 0 deletions

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), "", "")
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

@ -0,0 +1,81 @@
package client_test
import (
"testing"
"coopcloud.tech/abra/pkg/client"
contextPkg "coopcloud.tech/abra/pkg/context"
dContext "github.com/docker/cli/cli/context"
dCliContextStore "github.com/docker/cli/cli/context/store"
)
type TestContext struct {
context dCliContextStore.Metadata
expected_endpoint string
}
func dockerContext(host, key string) TestContext {
dockerContext := dCliContextStore.Metadata{
Name: "foo",
Metadata: nil,
Endpoints: map[string]interface{}{
key: dContext.EndpointMetaBase{
Host: host,
SkipTLSVerify: false,
},
},
}
return TestContext{
context: dockerContext,
expected_endpoint: host,
}
}
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"),
dockerContext("ssh://foobar", "k8"),
}
for _, context := range testDockerContexts {
endpoint, err := contextPkg.GetContextEndpoint(context.context)
if err != nil {
if err.Error() != "context lacks Docker endpoint" {
t.Error(err)
}
} else {
if endpoint != context.expected_endpoint {
t.Errorf("did not get correct context endpoint. Expected: %s, received: %s", context.expected_endpoint, endpoint)
}
}
}
}