From 5a48d8a82d2e2d7ff34d7cee1e40893ebedd4add Mon Sep 17 00:00:00 2001 From: "m.nabokikh" Date: Mon, 19 Apr 2021 16:45:12 +0400 Subject: [PATCH] chore: test Kubernetes storage with KinD Signed-off-by: m.nabokikh --- .github/workflows/ci.yaml | 7 ++++ Makefile | 17 ++-------- go.mod | 1 - storage/kubernetes/storage_test.go | 53 +++++++++--------------------- 4 files changed, 24 insertions(+), 54 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index d4140108..2738364f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -57,6 +57,12 @@ jobs: - name: Start services run: docker-compose -f docker-compose.test.yaml up -d + - name: Create kind cluster + uses: helm/kind-action@v1.0.0 + with: + version: v0.10.0 + node_image: kindest/node:v1.19.7@sha256:a70639454e97a4b733f9d9b67e12c01f6b0297449d5b9cbbef87473458e26dca + - name: Test run: make testall env: @@ -78,6 +84,7 @@ jobs: DEX_KEYSTONE_ADMIN_URL: http://localhost:${{ job.services.keystone.ports[35357] }} DEX_KEYSTONE_ADMIN_USER: demo DEX_KEYSTONE_ADMIN_PASS: DEMO_PASS + DEX_KUBERNETES_CONFIG_PATH: ~/.kube/config - name: Lint run: make lint diff --git a/Makefile b/Makefile index 81923af4..a5f2d7c9 100644 --- a/Makefile +++ b/Makefile @@ -61,25 +61,12 @@ up: docker-compose.override.yaml ## Launch the development environment down: clear ## Destroy the development environment docker-compose down --volumes --remove-orphans --rmi local -test: bin/test/kube-apiserver bin/test/etcd +test: @go test -v ./... -testrace: bin/test/kube-apiserver bin/test/etcd +testrace: @go test -v --race ./... -export TEST_ASSET_KUBE_APISERVER=$(abspath bin/test/kube-apiserver) -export TEST_ASSET_ETCD=$(abspath bin/test/etcd) - -bin/test/kube-apiserver: - @mkdir -p bin/test - curl -L https://storage.googleapis.com/k8s-c10s-test-binaries/kube-apiserver-$(shell uname)-x86_64 > bin/test/kube-apiserver - chmod +x bin/test/kube-apiserver - -bin/test/etcd: - @mkdir -p bin/test - curl -L https://storage.googleapis.com/k8s-c10s-test-binaries/etcd-$(shell uname)-x86_64 > bin/test/etcd - chmod +x bin/test/etcd - bin/golangci-lint: bin/golangci-lint-${GOLANGCI_VERSION} @ln -sf golangci-lint-${GOLANGCI_VERSION} bin/golangci-lint bin/golangci-lint-${GOLANGCI_VERSION}: diff --git a/go.mod b/go.mod index 62507059..ed0dd182 100644 --- a/go.mod +++ b/go.mod @@ -35,7 +35,6 @@ require ( google.golang.org/grpc v1.38.0 google.golang.org/protobuf v1.26.0 gopkg.in/square/go-jose.v2 v2.5.1 - sigs.k8s.io/testing_frameworks v0.1.2 ) replace github.com/dexidp/dex/api/v2 => ./api/v2 diff --git a/storage/kubernetes/storage_test.go b/storage/kubernetes/storage_test.go index 42ba19a4..08a25d9f 100644 --- a/storage/kubernetes/storage_test.go +++ b/storage/kubernetes/storage_test.go @@ -5,40 +5,26 @@ import ( "crypto/tls" "errors" "fmt" - "io/ioutil" "net/http" "net/http/httptest" "os" + "path/filepath" "strings" "testing" "github.com/sirupsen/logrus" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" - "sigs.k8s.io/testing_frameworks/integration" "github.com/dexidp/dex/storage" "github.com/dexidp/dex/storage/conformance" ) -const kubeconfigTemplate = `apiVersion: v1 -kind: Config -clusters: -- name: local - cluster: - server: SERVERURL -users: -- name: local - user: -contexts: -- context: - cluster: local - user: local -` +const kubeconfigPathVariableName = "DEX_KUBERNETES_CONFIG_PATH" func TestStorage(t *testing.T) { - if os.Getenv("TEST_ASSET_KUBE_APISERVER") == "" || os.Getenv("TEST_ASSET_ETCD") == "" { - t.Skip("control plane binaries are missing") + if os.Getenv(kubeconfigPathVariableName) == "" { + t.Skip(fmt.Sprintf("variable %q not set, skipping kubernetes storage tests\n", kubeconfigPathVariableName)) } suite.Run(t, new(StorageTestSuite)) @@ -46,33 +32,24 @@ func TestStorage(t *testing.T) { type StorageTestSuite struct { suite.Suite - - controlPlane *integration.ControlPlane - client *client } -func (s *StorageTestSuite) SetupSuite() { - s.controlPlane = &integration.ControlPlane{} +func (s *StorageTestSuite) expandDir(dir string) string { + if strings.HasPrefix(dir, "~/") { + homedir, err := os.UserHomeDir() + s.Require().NoError(err) - err := s.controlPlane.Start() - s.Require().NoError(err) -} - -func (s *StorageTestSuite) TearDownSuite() { - s.controlPlane.Stop() + dir = filepath.Join(homedir, strings.TrimPrefix(dir, "~/")) + } + return dir } 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) + kubeconfigPath := s.expandDir(os.Getenv(kubeconfigPathVariableName)) config := Config{ - KubeConfigFile: f.Name(), + KubeConfigFile: kubeconfigPath, } logger := &logrus.Logger{ @@ -81,10 +58,10 @@ func (s *StorageTestSuite) SetupTest() { Level: logrus.DebugLevel, } - client, err := config.open(logger, true) + kubeClient, err := config.open(logger, true) s.Require().NoError(err) - s.client = client + s.client = kubeClient } func (s *StorageTestSuite) TestStorage() {