forked from mystiq/dex
bc16de0b58
Using the default KUBECONFIG environment variable to indicate that the Kubernetes tests should be run lead to cases where developers accidentally ran the tests. This has now been changed to "DEX_KUBECONFIG" and documentation hsa been added detailing how to run these tests. Additionally, no other storage reads environment variables for its normal configuration (outside of tests) so the Kubernetes storage no longer does. Overall, be less surprising.
96 lines
2.1 KiB
Go
96 lines
2.1 KiB
Go
package kubernetes
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
|
|
"github.com/coreos/dex/storage"
|
|
"github.com/coreos/dex/storage/conformance"
|
|
)
|
|
|
|
const testKubeConfigEnv = "DEX_KUBECONFIG"
|
|
|
|
func TestLoadClient(t *testing.T) {
|
|
loadClient(t)
|
|
}
|
|
|
|
func loadClient(t *testing.T) *client {
|
|
config := Config{
|
|
KubeConfigFile: os.Getenv(testKubeConfigEnv),
|
|
}
|
|
if config.KubeConfigFile == "" {
|
|
t.Skipf("test environment variable %q not set, skipping", testKubeConfigEnv)
|
|
}
|
|
s, err := config.open()
|
|
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 {
|
|
c := &client{baseURL: test.baseURL}
|
|
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)
|
|
conformance.RunTests(t, func() storage.Storage {
|
|
for _, resource := range []string{
|
|
resourceAuthCode,
|
|
resourceAuthRequest,
|
|
resourceClient,
|
|
resourceRefreshToken,
|
|
resourceKeys,
|
|
resourcePassword,
|
|
} {
|
|
if err := client.deleteAll(resource); err != nil {
|
|
t.Fatalf("delete all %q failed: %v", resource, err)
|
|
}
|
|
}
|
|
return client
|
|
})
|
|
}
|