From b9252c047874eededeb452a3de7835cc1f8f6e1b Mon Sep 17 00:00:00 2001 From: Aleksander Mistewicz Date: Thu, 16 Nov 2017 17:57:47 +0100 Subject: [PATCH] Use mockups of generated interfaces in tests Change-Id: I77760fa2028e3c096929e82683c8024e01ee3c06 Signed-off-by: Aleksander Mistewicz --- manager/dryad_job_runner.go | 7 +-- manager/dryad_job_runner_test.go | 106 +++++++++++++++++++++++++++++++++++++++ manager/dryad_job_test.go | 44 ++++++++++++++-- 3 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 manager/dryad_job_runner_test.go diff --git a/manager/dryad_job_runner.go b/manager/dryad_job_runner.go index 611d665..5b279c2 100644 --- a/manager/dryad_job_runner.go +++ b/manager/dryad_job_runner.go @@ -45,17 +45,18 @@ func newDryadJobRunner(ctx context.Context, rusalka dryad.SessionProvider, devic // Deploy is part of DryadJobRunner interface. func (d *dryadJobRunner) Deploy() error { // TODO(amistewicz): implement. - return nil + return d.rusalka.TS() } // Boot is part of DryadJobRunner interface. func (d *dryadJobRunner) Boot() error { // TODO(amistewicz): implement. - return nil + return d.rusalka.DUT() } // Test is part of DryadJobRunner interface. func (d *dryadJobRunner) Test() error { // TODO(amistewicz): implement. - return nil + _, _, err := d.rusalka.Exec([]string{"echo", "healthcheck"}) + return err } diff --git a/manager/dryad_job_runner_test.go b/manager/dryad_job_runner_test.go new file mode 100644 index 0000000..e91ae57 --- /dev/null +++ b/manager/dryad_job_runner_test.go @@ -0,0 +1,106 @@ +/* + * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved + * + * 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 manager + +import ( + "context" + "errors" + + "github.com/golang/mock/gomock" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) + +var _ = Describe("DryadJobRunner", func() { + var ( + mockSession *MockSessionProvider + mockDevice *MockDeviceCommunicationProvider + ctrl *gomock.Controller + djr DryadJobRunner + ) + + BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + mockSession = NewMockSessionProvider(ctrl) + mockDevice = NewMockDeviceCommunicationProvider(ctrl) + djr = newDryadJobRunner(context.Background(), mockSession, mockDevice) + }) + + AfterEach(func() { + ctrl.Finish() + }) + + Describe("Deploy", func() { + var tsCall *gomock.Call + + BeforeEach(func() { + tsCall = mockSession.EXPECT().TS().Times(1) + }) + + It("should switch to TS", func() { + err := djr.Deploy() + Expect(err).ToNot(HaveOccurred()) + }) + + It("should fail if TS fails", func() { + tsCall.Return(errors.New("TS failed")) + + err := djr.Deploy() + Expect(err).To(HaveOccurred()) + }) + }) + + Describe("Boot", func() { + var dutCall *gomock.Call + + BeforeEach(func() { + dutCall = mockSession.EXPECT().DUT().Times(1) + }) + + It("should switch to DUT", func() { + err := djr.Boot() + Expect(err).ToNot(HaveOccurred()) + }) + + It("should fail if DUT fails", func() { + dutCall.Return(errors.New("DUT failed")) + + err := djr.Boot() + Expect(err).To(HaveOccurred()) + }) + }) + + Describe("Test", func() { + var execCall *gomock.Call + + BeforeEach(func() { + execCall = mockSession.EXPECT().Exec([]string{"echo", "healthcheck"}) + }) + + It("should exec echo healthcheck", func() { + err := djr.Test() + Expect(err).ToNot(HaveOccurred()) + }) + + It("should fail if Exec fails", func() { + execCall.Return(nil, nil, errors.New("exec failed")) + + err := djr.Test() + Expect(err).To(HaveOccurred()) + }) + }) +}) diff --git a/manager/dryad_job_test.go b/manager/dryad_job_test.go index 76f2f3a..450dbed 100644 --- a/manager/dryad_job_test.go +++ b/manager/dryad_job_test.go @@ -17,21 +17,57 @@ package manager import ( + "context" + . "git.tizen.org/tools/weles" + "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" ) var _ = Describe("dryadJob", func() { - var changes chan DryadJobStatusChange - var jobID JobID - var dj *dryadJob + var ( + changes chan DryadJobStatusChange + jobID JobID + dj *dryadJob + djSync chan struct{} + ctrl *gomock.Controller + mockDryadJobRunner DryadJobRunner + cancel context.CancelFunc + ) + + newMockDryadJob := func(job JobID) (*dryadJob, chan struct{}) { + dJobSync := make(chan struct{}) + var ctx context.Context + ctx, cancel = context.WithCancel(context.Background()) + dJob := newDryadJobWithCancel(job, changes, mockDryadJobRunner, cancel) + go func() { + defer close(dJobSync) + defer GinkgoRecover() + dJob.run(ctx) + }() + return dJob, dJobSync + } BeforeEach(func() { + ctrl = gomock.NewController(GinkgoT()) + mockDryadJobRunner = NewMockDryadJobRunner(ctrl) + mockOfDryadJobRunner := mockDryadJobRunner.(*MockDryadJobRunner) + gomock.InOrder( + mockOfDryadJobRunner.EXPECT().Deploy(), + mockOfDryadJobRunner.EXPECT().Boot(), + mockOfDryadJobRunner.EXPECT().Test(), + ) + jobID = 666 changes = make(chan DryadJobStatusChange, 6) - dj = newDryadJob(jobID, Dryad{}, changes) + dj, djSync = newMockDryadJob(jobID) + }) + + AfterEach(func() { + Eventually(djSync).Should(BeClosed()) + ctrl.Finish() }) It("should go through proper states", func() { -- 2.7.4