2016-07-26 01:30:28 +05:30
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2019-12-18 21:30:57 +05:30
|
|
|
"io/ioutil"
|
2016-07-26 01:30:28 +05:30
|
|
|
"os"
|
2019-12-18 21:30:57 +05:30
|
|
|
"strings"
|
2016-07-26 01:30:28 +05:30
|
|
|
"testing"
|
|
|
|
|
2017-07-26 02:15:17 +05:30
|
|
|
"github.com/sirupsen/logrus"
|
2019-12-18 21:30:57 +05:30
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"sigs.k8s.io/testing_frameworks/integration"
|
2018-09-03 12:14:44 +05:30
|
|
|
|
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/conformance"
|
2016-07-26 01:30:28 +05:30
|
|
|
)
|
|
|
|
|
2019-12-18 21:30:57 +05:30
|
|
|
const kubeconfigTemplate = `apiVersion: v1
|
|
|
|
kind: Config
|
|
|
|
clusters:
|
|
|
|
- name: local
|
|
|
|
cluster:
|
|
|
|
server: SERVERURL
|
|
|
|
users:
|
|
|
|
- name: local
|
|
|
|
user:
|
|
|
|
contexts:
|
|
|
|
- context:
|
|
|
|
cluster: local
|
|
|
|
user: local
|
|
|
|
`
|
2016-10-23 20:28:28 +05:30
|
|
|
|
2019-12-18 21:30:57 +05:30
|
|
|
func TestStorage(t *testing.T) {
|
|
|
|
if os.Getenv("TEST_ASSET_KUBE_APISERVER") == "" || os.Getenv("TEST_ASSET_ETCD") == "" {
|
|
|
|
t.Skip("control plane binaries are missing")
|
|
|
|
}
|
|
|
|
|
|
|
|
suite.Run(t, new(StorageTestSuite))
|
|
|
|
}
|
|
|
|
|
|
|
|
type StorageTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
controlPlane *integration.ControlPlane
|
|
|
|
|
|
|
|
client *client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageTestSuite) SetupSuite() {
|
|
|
|
s.controlPlane = &integration.ControlPlane{}
|
|
|
|
|
|
|
|
err := s.controlPlane.Start()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageTestSuite) TearDownSuite() {
|
|
|
|
s.controlPlane.Stop()
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
2019-12-18 21:30:57 +05:30
|
|
|
func (s *StorageTestSuite) SetupTest() {
|
|
|
|
f, err := ioutil.TempFile("", "dex-kubeconfig-*")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = f.WriteString(strings.ReplaceAll(kubeconfigTemplate, "SERVERURL", s.controlPlane.APIURL().String()))
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2016-10-23 20:28:28 +05:30
|
|
|
config := Config{
|
2019-12-18 21:30:57 +05:30
|
|
|
KubeConfigFile: f.Name(),
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2019-12-18 21:30:57 +05:30
|
|
|
|
2016-11-23 05:05:46 +05:30
|
|
|
logger := &logrus.Logger{
|
|
|
|
Out: os.Stderr,
|
|
|
|
Formatter: &logrus.TextFormatter{DisableColors: true},
|
|
|
|
Level: logrus.DebugLevel,
|
|
|
|
}
|
2019-12-18 21:30:57 +05:30
|
|
|
|
|
|
|
client, err := config.open(logger, true)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.client = client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *StorageTestSuite) TestStorage() {
|
|
|
|
newStorage := func() storage.Storage {
|
|
|
|
for _, resource := range []string{
|
|
|
|
resourceAuthCode,
|
|
|
|
resourceAuthRequest,
|
|
|
|
resourceClient,
|
|
|
|
resourceRefreshToken,
|
|
|
|
resourceKeys,
|
|
|
|
resourcePassword,
|
|
|
|
} {
|
|
|
|
if err := s.client.deleteAll(resource); err != nil {
|
|
|
|
s.T().Fatalf("delete all %q failed: %v", resource, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return s.client
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
2019-12-18 21:30:57 +05:30
|
|
|
|
|
|
|
conformance.RunTests(s.T(), newStorage)
|
|
|
|
conformance.RunTransactionTests(s.T(), newStorage)
|
2016-07-26 01:30:28 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
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-14 05:20:20 +05:30
|
|
|
c := &client{baseURL: test.baseURL}
|
2016-07-26 01:30:28 +05:30
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|