-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Integration test skeleton #800
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_test") | ||
|
||
go_test( | ||
name = "go_default_test", | ||
srcs = ["cluster_test.go"], | ||
deps = [ | ||
"//pkg/apis/cluster/v1alpha1:go_default_library", | ||
"//pkg/client/clientset_generated/clientset:go_default_library", | ||
"//pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1:go_default_library", | ||
"//vendor/github.com/onsi/ginkgo:go_default_library", | ||
"//vendor/github.com/onsi/gomega:go_default_library", | ||
"//vendor/k8s.io/api/core/v1:go_default_library", | ||
"//vendor/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library", | ||
"//vendor/k8s.io/client-go/informers:go_default_library", | ||
"//vendor/k8s.io/client-go/kubernetes:go_default_library", | ||
"//vendor/k8s.io/client-go/tools/cache:go_default_library", | ||
"//vendor/k8s.io/client-go/tools/clientcmd:go_default_library", | ||
], | ||
) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
/* | ||
Copyright 2019 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 ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
informers "k8s.io/client-go/informers" | ||
"k8s.io/client-go/kubernetes" | ||
"k8s.io/client-go/tools/cache" | ||
"k8s.io/client-go/tools/clientcmd" | ||
clusterv1alpha1 "sigs.k8s.io/cluster-api/pkg/apis/cluster/v1alpha1" | ||
clientset "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset" | ||
client "sigs.k8s.io/cluster-api/pkg/client/clientset_generated/clientset/typed/cluster/v1alpha1" | ||
) | ||
|
||
var clusterSpec = &clusterv1alpha1.ClusterSpec{ | ||
ClusterNetwork: clusterv1alpha1.ClusterNetworkingConfig{ | ||
ServiceDomain: "mydomain.com", | ||
Services: clusterv1alpha1.NetworkRanges{ | ||
CIDRBlocks: []string{"10.96.0.0/12"}, | ||
}, | ||
Pods: clusterv1alpha1.NetworkRanges{ | ||
CIDRBlocks: []string{"192.168.0.0/16"}, | ||
}, | ||
}, | ||
} | ||
|
||
// Timeout for waiting events in seconds | ||
const TIMEOUT = 60 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this a test flag with default to 60s? |
||
|
||
func TestCluster(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Cluster-Controller") | ||
} | ||
|
||
var _ = Describe("Cluster-Controller", func() { | ||
var clusterapi client.ClusterInterface | ||
var client *kubernetes.Clientset | ||
var stopper chan struct{} | ||
var informer cache.SharedIndexInformer | ||
var testNamespace string | ||
|
||
BeforeEach(func() { | ||
// Load configuration | ||
kubeconfig := os.Getenv("KUBECONFIG") | ||
config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
// Create kubernetes client | ||
client, err = kubernetes.NewForConfig(config) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
// Create namespace for test | ||
ns := &corev1.Namespace{ObjectMeta: metav1.ObjectMeta{GenerateName: "clusterapi-test-"}} | ||
ns, err = client.Core().Namespaces().Create(ns) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
testNamespace = ns.ObjectMeta.Name | ||
|
||
// Create informer for events in the namespace | ||
factory := informers.NewSharedInformerFactoryWithOptions(client, 0, informers.WithNamespace(testNamespace)) | ||
informer = factory.Core().V1().Events().Informer() | ||
stopper = make(chan struct{}) | ||
|
||
// Create clusterapi client | ||
cs, err := clientset.NewForConfig(config) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
clusterapi = cs.ClusterV1alpha1().Clusters(testNamespace) | ||
}) | ||
|
||
AfterEach(func() { | ||
close(stopper) | ||
client.Core().Namespaces().Delete(testNamespace, &metav1.DeleteOptions{}) | ||
}) | ||
|
||
Describe("Create Cluster", func() { | ||
It("Should trigger an event", func(done Done) { | ||
// Register handler for cluster events | ||
events := make(chan *corev1.Event, 0) | ||
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{ | ||
AddFunc: func(obj interface{}) { | ||
// Guard against miscofigured informer not listening to Events | ||
Expect(obj).Should(BeAssignableToTypeOf(&corev1.Event{})) | ||
|
||
e := obj.(*corev1.Event) | ||
pablochacin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if e.InvolvedObject.Kind == "Cluster" { | ||
events <- e | ||
} | ||
}, | ||
}) | ||
go informer.Run(stopper) | ||
pablochacin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Expect(cache.WaitForCacheSync(stopper, informer.HasSynced)).To(BeTrue()) | ||
|
||
// Create Cluster | ||
cluster := &clusterv1alpha1.Cluster{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
GenerateName: "cluster-", | ||
Namespace: testNamespace, | ||
}, | ||
Spec: *clusterSpec.DeepCopy(), | ||
} | ||
_, err := clusterapi.Create(cluster) | ||
Expect(err).ShouldNot(HaveOccurred()) | ||
|
||
Expect(<-events).ShouldNot(BeNil()) | ||
|
||
close(done) | ||
}, TIMEOUT) | ||
}) | ||
}) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.