2016-07-25 20:00:28 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2016-10-26 20:08:03 +00:00
|
|
|
"fmt"
|
2016-07-25 20:00:28 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-07-25 20:45:17 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2018-09-03 06:44:44 +00:00
|
|
|
|
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/conformance"
|
2016-07-25 20:00:28 +00:00
|
|
|
)
|
|
|
|
|
2016-10-23 14:58:28 +00:00
|
|
|
const testKubeConfigEnv = "DEX_KUBECONFIG"
|
|
|
|
|
2016-07-25 20:00:28 +00:00
|
|
|
func TestLoadClient(t *testing.T) {
|
|
|
|
loadClient(t)
|
|
|
|
}
|
|
|
|
|
2016-08-02 05:53:12 +00:00
|
|
|
func loadClient(t *testing.T) *client {
|
2016-10-23 14:58:28 +00:00
|
|
|
config := Config{
|
|
|
|
KubeConfigFile: os.Getenv(testKubeConfigEnv),
|
|
|
|
}
|
|
|
|
if config.KubeConfigFile == "" {
|
|
|
|
t.Skipf("test environment variable %q not set, skipping", testKubeConfigEnv)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|
2016-11-22 23:35:46 +00:00
|
|
|
logger := &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{DisableColors: true},
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
}
|
2017-02-27 17:34:59 +00:00
|
|
|
s, err := config.open(logger, true)
|
2016-07-25 20:00:28 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestURLFor(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
apiVersion, namespace, resource, name string
|
|
|
|
|
|
|
|
baseURL string
|
|
|
|
want string
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
"v1", "default", "pods", "a",
|
|
|
|
"https://k8s.example.com",
|
|
|
|
"https://k8s.example.com/api/v1/namespaces/default/pods/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo/v1", "default", "bar", "a",
|
|
|
|
"https://k8s.example.com",
|
|
|
|
"https://k8s.example.com/apis/foo/v1/namespaces/default/bar/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo/v1", "default", "bar", "a",
|
|
|
|
"https://k8s.example.com/",
|
|
|
|
"https://k8s.example.com/apis/foo/v1/namespaces/default/bar/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"foo/v1", "default", "bar", "a",
|
|
|
|
"https://k8s.example.com/",
|
|
|
|
"https://k8s.example.com/apis/foo/v1/namespaces/default/bar/a",
|
|
|
|
},
|
|
|
|
{
|
|
|
|
// no namespace
|
|
|
|
"foo/v1", "", "bar", "a",
|
|
|
|
"https://k8s.example.com",
|
|
|
|
"https://k8s.example.com/apis/foo/v1/bar/a",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
2016-10-13 23:50:20 +00:00
|
|
|
c := &client{baseURL: test.baseURL}
|
2016-07-25 20:00:28 +00:00
|
|
|
got := c.urlFor(test.apiVersion, test.namespace, test.resource, test.name)
|
|
|
|
if got != test.want {
|
|
|
|
t.Errorf("(&client{baseURL:%q}).urlFor(%q, %q, %q, %q): expected %q got %q",
|
|
|
|
test.baseURL,
|
|
|
|
test.apiVersion, test.namespace, test.resource, test.name,
|
|
|
|
test.want, got,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorage(t *testing.T) {
|
|
|
|
client := loadClient(t)
|
2016-10-26 20:08:03 +00:00
|
|
|
newStorage := func() storage.Storage {
|
2016-10-05 23:04:48 +00:00
|
|
|
for _, resource := range []string{
|
|
|
|
resourceAuthCode,
|
|
|
|
resourceAuthRequest,
|
|
|
|
resourceClient,
|
|
|
|
resourceRefreshToken,
|
|
|
|
resourceKeys,
|
|
|
|
resourcePassword,
|
|
|
|
} {
|
|
|
|
if err := client.deleteAll(resource); err != nil {
|
2016-10-26 20:08:03 +00:00
|
|
|
// Fatalf sometimes doesn't print the error message.
|
|
|
|
fmt.Fprintf(os.Stderr, "delete all %q failed: %v\n", resource, err)
|
2016-10-05 23:04:48 +00:00
|
|
|
t.Fatalf("delete all %q failed: %v", resource, err)
|
|
|
|
}
|
|
|
|
}
|
2016-09-18 01:01:15 +00:00
|
|
|
return client
|
2016-10-26 20:08:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
conformance.RunTests(t, newStorage)
|
|
|
|
conformance.RunTransactionTests(t, newStorage)
|
2016-07-25 20:00:28 +00:00
|
|
|
}
|