Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Ce Gao <gaoce@caicloud.io>
  • Loading branch information
gaocegege committed May 17, 2019
1 parent 10c7759 commit 26a2212
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
4 changes: 4 additions & 0 deletions pkg/controller/v1alpha2/trial/trial_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,10 @@ func (r *ReconcileTrial) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, nil

}
logger.Info("DEBUG")

if !instance.IsCreated() {
logger.Info("DEBUG-created")
//Trial not created in DB
if instance.Status.StartTime == nil {
now := metav1.Now()
Expand All @@ -166,6 +169,7 @@ func (r *ReconcileTrial) Reconcile(request reconcile.Request) (reconcile.Result,
return reconcile.Result{}, err
}
} else {
logger.Info("DEBUG-update")
// Trial already created in DB
err := r.reconcileTrial(instance)
if err != nil {
Expand Down
62 changes: 59 additions & 3 deletions pkg/controller/v1alpha2/trial/trial_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const (
trialName = "foo"
namespace = "default"

timeout = time.Second * 5
timeout = time.Second * 20
)

var expectedRequest = reconcile.Request{NamespacedName: types.NamespacedName{Name: trialName, Namespace: namespace}}
Expand Down Expand Up @@ -57,6 +57,9 @@ func TestCreateTFJobTrial(t *testing.T) {
scheme: mgr.GetScheme(),
ManagerClient: mc,
updateStatusHandler: func(instance *trialsv1alpha2.Trial) error {
if !instance.IsCreated() {
t.Errorf("Expected got condition created")
}
return nil
},
})
Expand All @@ -80,6 +83,59 @@ func TestCreateTFJobTrial(t *testing.T) {
g.Expect(err).NotTo(gomega.HaveOccurred())
defer c.Delete(context.TODO(), instance)
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
}

func TestReconcileTFJobTrial(t *testing.T) {
g := gomega.NewGomegaWithT(t)
instance := newFakeTrialWithTFJob()

mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()

mc := managerclientmock.NewMockClient(mockCtrl)
mc.EXPECT().CreateTrialInDB(gomock.Any()).Return(nil).AnyTimes()
mc.EXPECT().UpdateTrialStatusInDB(gomock.Any()).Return(nil).AnyTimes()

// Setup the Manager and Controller. Wrap the Controller Reconcile function so it writes each request to a
// channel when it is finished.
mgr, err := manager.New(cfg, manager.Options{})
g.Expect(err).NotTo(gomega.HaveOccurred())
c := mgr.GetClient()

r := &ReconcileTrial{
Client: mgr.GetClient(),
scheme: mgr.GetScheme(),
ManagerClient: mc,
}

r.updateStatusHandler = func(instance *trialsv1alpha2.Trial) error {
if !instance.IsCreated() {
t.Errorf("Expected got condition created")
}
return r.updateStatus(instance)
}

recFn, requests := SetupTestReconcile(r)
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)

defer func() {
close(stopMgr)
mgrStopped.Wait()
}()

// Create the Trial object and expect the Reconcile and Deployment to be created
err = c.Create(context.TODO(), instance)
// The instance object may not be a valid object because it might be missing some required fields.
// Please modify the instance object by adding required fields and then remove the following if statement.
if apierrors.IsInvalid(err) {
t.Logf("failed to create object, got an invalid object error: %v", err)
return
}
g.Expect(err).NotTo(gomega.HaveOccurred())
defer c.Delete(context.TODO(), instance)
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))

tfJob := &unstructured.Unstructured{}
bufSize := 1024
Expand All @@ -90,13 +146,13 @@ func TestCreateTFJobTrial(t *testing.T) {
g.Eventually(func() error { return c.Get(context.TODO(), tfJobKey, tfJob) }, timeout).
Should(gomega.Succeed())

// Delete the Deployment and expect Reconcile to be called for Deployment deletion
// Delete the TFJob and expect Reconcile to be called for TFJob deletion
g.Expect(c.Delete(context.TODO(), tfJob)).NotTo(gomega.HaveOccurred())
g.Eventually(requests, timeout).Should(gomega.Receive(gomega.Equal(expectedRequest)))
g.Eventually(func() error { return c.Get(context.TODO(), tfJobKey, tfJob) }, timeout).
Should(gomega.Succeed())

// Manually delete Deployment since GC isn't enabled in the test control plane
// Manually delete TFJob since GC isn't enabled in the test control plane
g.Eventually(func() error { return c.Delete(context.TODO(), tfJob) }, timeout).
Should(gomega.MatchError("tfjobs.kubeflow.org \"test\" not found"))
}
Expand Down

0 comments on commit 26a2212

Please sign in to comment.