Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Add wip crud test #7

Merged
merged 3 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
663 changes: 532 additions & 131 deletions Gopkg.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,8 @@

[[constraint]]
name = "k8s.io/kube-openapi"

[[constraint]]
name = "github.com/kubernetes-sig-testing/frameworks"
source = "github.com/marun/frameworks"
branch = "fnord"
65 changes: 65 additions & 0 deletions test/integration/crud_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package integration

import (
"testing"

fedapiv1alpha1 "github.com/marun/fnord/pkg/apis/federation/v1alpha1"
"github.com/marun/fnord/test/integration/framework"
apiv1 "k8s.io/api/core/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// TestCrud validates create/read/update/delete operations for federated types.
func TestCrud(t *testing.T) {
namespace := "foo"

kubeApiFixture := framework.SetUpKubernetesApiFixture(t)
defer kubeApiFixture.TearDown(t)

kubeClient := kubeApiFixture.NewClient(t, "crud-test")
_, err := kubeClient.CoreV1().Secrets(namespace).Create(&apiv1.Secret{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "test-secret-",
Namespace: namespace,
},
Data: map[string][]byte{
"A": []byte("ala ma kota"),
},
Type: apiv1.SecretTypeOpaque,
})
if err != nil {
t.Fatal(err)
}

fedApiFixture := framework.SetUpFederationApiFixture(t)
defer fedApiFixture.TearDown(t)

fedClient := fedApiFixture.NewClient(t, "crud-test")
_, err = fedClient.FederationV1alpha1().FederatedSecrets(namespace).Create(&fedapiv1alpha1.FederatedSecret{
ObjectMeta: metav1.ObjectMeta{Name: "my-fed-secret"},
Spec: fedapiv1alpha1.FederatedSecretSpec{
Template: corev1.Secret{},
},
})
if err != nil {
t.Fatal(err)
}

}
54 changes: 54 additions & 0 deletions test/integration/framework/etcd.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"testing"

"github.com/kubernetes-sig-testing/frameworks/integration"
)

var (
etcd *integration.Etcd
refCount int
)

func SetUpEtcd(t *testing.T) string {
if etcd == nil {
etcd = &integration.Etcd{}
err := etcd.Start()
if err != nil {
etcd = nil
t.Fatalf("Error starting etcd: %v", err)
}
}
refCount += 1
return etcd.URL.String()
}

func TearDownEtcd(t *testing.T) {
if etcd != nil {
refCount -= 1
if refCount <= 0 {
err := etcd.Stop()
if err != nil {
t.Errorf("Error stopping etcd: %v", err)
}
etcd = nil
}
}
}
102 changes: 102 additions & 0 deletions test/integration/framework/fedapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"fmt"
"os"
"strconv"
"testing"

"github.com/pborman/uuid"

"github.com/kubernetes-incubator/apiserver-builder/pkg/cmd/server"
"github.com/marun/fnord/pkg/apis"
"github.com/marun/fnord/pkg/client/clientset_generated/clientset"
"github.com/marun/fnord/pkg/openapi"
)

// FederationApiFixture manages a federation api server
type FederationApiFixture struct {
stopCh chan struct{}
EtcdUrl string
Host string
SecureConfigFixture *SecureConfigFixture
}

func SetUpFederationApiFixture(t *testing.T) *FederationApiFixture {
f := &FederationApiFixture{}
f.setUp(t)
return f
}

func (f *FederationApiFixture) setUp(t *testing.T) {
defer TearDownOnPanic(t, f)

f.EtcdUrl = SetUpEtcd(t)
f.SecureConfigFixture = SetUpSecureConfigFixture(t)

// TODO(marun) ensure resiliency in the face of another process
// taking the port.

port, err := FindFreeLocalPort()
if err != nil {
t.Fatal(err)
}

f.stopCh = make(chan struct{})

server.GetOpenApiDefinition = openapi.GetOpenAPIDefinitions
cmd, _ := server.NewCommandStartServer(uuid.New(), os.Stdout, os.Stderr, apis.GetAllApiBuilders(), f.stopCh, "Api", "v0")
bindAddress := "127.0.0.1"
cmd.SetArgs([]string{
"--etcd-servers", f.EtcdUrl,
"--client-ca-file", f.SecureConfigFixture.CACertFile,
"--cert-dir", f.SecureConfigFixture.CertDir,
"--bind-address", bindAddress,
"--secure-port", strconv.Itoa(port),
"--delegated-auth=false",
})

f.Host = fmt.Sprintf("https://%s:%d", bindAddress, port)

go func() {
if err := cmd.Execute(); err != nil {
t.Fatal(err)
}
}()
}

func (f *FederationApiFixture) TearDown(t *testing.T) {
if f.stopCh != nil {
close(f.stopCh)
f.stopCh = nil
}
if len(f.EtcdUrl) > 0 {
TearDownEtcd(t)
f.EtcdUrl = ""
}
if f.SecureConfigFixture != nil {
f.SecureConfigFixture.TearDown(t)
f.SecureConfigFixture = nil
}
}

func (f *FederationApiFixture) NewClient(t *testing.T, userAgent string) clientset.Interface {
config := f.SecureConfigFixture.NewClientConfig(t, f.Host, userAgent)
return clientset.NewForConfigOrDie(config)
}
108 changes: 108 additions & 0 deletions test/integration/framework/kubeapi.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
Copyright 2018 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package framework

import (
"fmt"
"net/url"
"os"
"strconv"
"testing"

"github.com/pborman/uuid"

"github.com/kubernetes-sig-testing/frameworks/integration"
clientset "k8s.io/client-go/kubernetes"
)

// KubernetesApiFixture manages a kubernetes api server
type KubernetesApiFixture struct {
EtcdUrl string
Host string
SecureConfigFixture *SecureConfigFixture
ApiServer *integration.APIServer
}

func SetUpKubernetesApiFixture(t *testing.T) *KubernetesApiFixture {
f := &KubernetesApiFixture{}
f.setUp(t)
return f
}

func (f *KubernetesApiFixture) setUp(t *testing.T) {
defer TearDownOnPanic(t, f)

f.EtcdUrl = SetUpEtcd(t)
f.SecureConfigFixture = SetUpSecureConfigFixture(t)

// TODO(marun) ensure resiliency in the face of another process
// taking the port

port, err := FindFreeLocalPort()
if err != nil {
t.Fatal(err)
}

bindAddress := "127.0.0.1"
f.Host = fmt.Sprintf("https://%s:%d", bindAddress, port)
url, err := url.Parse(f.Host)
if err != nil {
t.Fatalf("Error parsing url: %v", err)
}

args := []string{
"--etcd-servers", f.EtcdUrl,
"--client-ca-file", f.SecureConfigFixture.CACertFile,
"--cert-dir", f.SecureConfigFixture.CertDir,
"--bind-address", bindAddress,
"--secure-port", strconv.Itoa(port),
"--insecure-port", "0",
"--etcd-prefix", uuid.New(),
}

apiServer := &integration.APIServer{
URL: url,
Args: args,
Out: os.Stdout,
Err: os.Stderr,
}
err = apiServer.Start()
if err != nil {
t.Fatalf("Error starting kubernetes apiserver: %v", err)
}
f.ApiServer = apiServer
}

func (f *KubernetesApiFixture) TearDown(t *testing.T) {
if f.ApiServer != nil {
f.ApiServer.Stop()
f.ApiServer = nil
}
if len(f.EtcdUrl) > 0 {
TearDownEtcd(t)
f.EtcdUrl = ""
}
if f.SecureConfigFixture != nil {
f.SecureConfigFixture.TearDown(t)
f.SecureConfigFixture = nil
}
}

func (f *KubernetesApiFixture) NewClient(t *testing.T, userAgent string) clientset.Interface {
config := f.SecureConfigFixture.NewClientConfig(t, f.Host, userAgent)
return clientset.NewForConfigOrDie(config)
}
Loading