forked from chaos-mesh/chaos-mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Unit Tests for podchaos (chaos-mesh#337)
- Loading branch information
Showing
16 changed files
with
474 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 97 additions & 0 deletions
97
controllers/podchaos/containerkill/containerkill_suite_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package containerkill_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
v1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
"github.com/pingcap/chaos-mesh/api/v1alpha1" | ||
controllerkill "github.com/pingcap/chaos-mesh/controllers/podchaos/containerkill" | ||
. "github.com/pingcap/chaos-mesh/controllers/test" | ||
"github.com/pingcap/chaos-mesh/pkg/mock" | ||
) | ||
|
||
func TestContainerKill(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecsWithDefaultAndCustomReporters(t, | ||
"ContainerKill Suite", | ||
[]Reporter{envtest.NewlineReporter{}}) | ||
} | ||
|
||
var _ = BeforeSuite(func(done Done) { | ||
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) | ||
|
||
Expect(v1.AddToScheme(scheme.Scheme)).To(Succeed()) | ||
Expect(v1alpha1.AddToScheme(scheme.Scheme)).To(Succeed()) | ||
|
||
close(done) | ||
}, 60) | ||
|
||
var _ = AfterSuite(func() { | ||
}) | ||
|
||
var _ = Describe("PodChaos", func() { | ||
Context("ContainerKill", func() { | ||
objs, _ := GenerateNPods("p", 1, v1.PodRunning, metav1.NamespaceDefault, nil, nil, v1.ContainerStatus{ | ||
ContainerID: "fake-container-id", | ||
Name: "container-name", | ||
}) | ||
|
||
podChaos := v1alpha1.PodChaos{ | ||
TypeMeta: metav1.TypeMeta{ | ||
Kind: "PodChaos", | ||
APIVersion: "v1", | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: metav1.NamespaceDefault, | ||
Name: "podchaos-name", | ||
}, | ||
Spec: v1alpha1.PodChaosSpec{ | ||
Selector: v1alpha1.SelectorSpec{}, | ||
Mode: v1alpha1.OnePodMode, | ||
ContainerName: "container-name", | ||
Scheduler: &v1alpha1.SchedulerSpec{Cron: "@hourly"}, | ||
}, | ||
} | ||
|
||
r := controllerkill.Reconciler{ | ||
Client: fake.NewFakeClientWithScheme(scheme.Scheme, objs...), | ||
EventRecorder: &record.FakeRecorder{}, | ||
Log: ctrl.Log.WithName("controllers").WithName("PodChaos"), | ||
} | ||
|
||
It("ContainerKill Apply", func() { | ||
defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})() | ||
|
||
err := r.Apply(context.TODO(), ctrl.Request{}, &podChaos) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
err = r.Recover(context.TODO(), ctrl.Request{}, &podChaos) | ||
Expect(err).ToNot(HaveOccurred()) | ||
}) | ||
|
||
It("ContainerKill Apply Error", func() { | ||
defer mock.With("MockChaosDaemonClient", &MockChaosDaemonClient{})() | ||
defer mock.With("MockContainerKillError", errors.New("ContainerKillError"))() | ||
|
||
err := r.Apply(context.TODO(), ctrl.Request{}, &podChaos) | ||
|
||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("ContainerKillError")) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package podchaos_test | ||
|
||
import ( | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/record" | ||
ctrl "sigs.k8s.io/controller-runtime" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"sigs.k8s.io/controller-runtime/pkg/envtest" | ||
logf "sigs.k8s.io/controller-runtime/pkg/log" | ||
"sigs.k8s.io/controller-runtime/pkg/log/zap" | ||
|
||
"github.com/pingcap/chaos-mesh/api/v1alpha1" | ||
"github.com/pingcap/chaos-mesh/controllers/podchaos" | ||
) | ||
|
||
func TestPodChaos(t *testing.T) { | ||
RegisterFailHandler(Fail) | ||
|
||
RunSpecsWithDefaultAndCustomReporters(t, | ||
"PodChaos Suite", | ||
[]Reporter{envtest.NewlineReporter{}}) | ||
} | ||
|
||
var _ = BeforeSuite(func(done Done) { | ||
logf.SetLogger(zap.LoggerTo(GinkgoWriter, true)) | ||
close(done) | ||
}, 60) | ||
|
||
var _ = AfterSuite(func() { | ||
}) | ||
|
||
var _ = Describe("PodChaos", func() { | ||
Context("PodChaos", func() { | ||
invalidDuration := "invalid duration" | ||
meta := metav1.TypeMeta{ | ||
Kind: "PodChaos", | ||
APIVersion: "v1", | ||
} | ||
|
||
r := podchaos.Reconciler{ | ||
Client: fake.NewFakeClientWithScheme(scheme.Scheme), | ||
EventRecorder: &record.FakeRecorder{}, | ||
Log: ctrl.Log.WithName("controllers").WithName("PodChaos"), | ||
} | ||
|
||
It("PodChaos Reconcile", func() { | ||
var err error | ||
|
||
_, err = r.Reconcile(ctrl.Request{}, &v1alpha1.PodChaos{ | ||
TypeMeta: meta, | ||
Spec: v1alpha1.PodChaosSpec{Scheduler: nil, Duration: &invalidDuration}, | ||
}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("invalid duration")) | ||
|
||
_, err = r.Reconcile(ctrl.Request{}, &v1alpha1.PodChaos{ | ||
TypeMeta: meta, | ||
Spec: v1alpha1.PodChaosSpec{Scheduler: nil, Duration: nil, Action: v1alpha1.PodKillAction}, | ||
}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("unsupported chaos action")) | ||
_, err = r.Reconcile(ctrl.Request{}, &v1alpha1.PodChaos{ | ||
TypeMeta: meta, | ||
Spec: v1alpha1.PodChaosSpec{Scheduler: nil, Duration: nil, Action: v1alpha1.ContainerKillAction}, | ||
}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("unsupported chaos action")) | ||
_, err = r.Reconcile(ctrl.Request{}, &v1alpha1.PodChaos{ | ||
TypeMeta: meta, | ||
Spec: v1alpha1.PodChaosSpec{Scheduler: nil, Duration: nil, Action: ""}, | ||
}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("invalid chaos action")) | ||
|
||
_, err = r.Reconcile(ctrl.Request{}, &v1alpha1.PodChaos{ | ||
TypeMeta: meta, | ||
Spec: v1alpha1.PodChaosSpec{Scheduler: &v1alpha1.SchedulerSpec{}, Duration: nil, Action: ""}, | ||
}) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("invalid chaos action")) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.