forked from coop-cloud/abra
82 lines
2.2 KiB
Go
82 lines
2.2 KiB
Go
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)
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
}
|