From 028402c65fa354f80753797aca168660647d9866 Mon Sep 17 00:00:00 2001 From: zwwhdls Date: Fri, 6 May 2022 00:26:57 +0800 Subject: [PATCH] fix test --- cmd/apps/root.go | 12 ++++++++---- pkg/controller/controller.go | 26 ++++++++++++++++++-------- pkg/controller/object.go | 5 +---- pkg/workflow/spec_test.go | 36 ++++++++++++++++++++++++++++++------ pkg/workflow/suite_test.go | 3 +++ 5 files changed, 60 insertions(+), 22 deletions(-) diff --git a/cmd/apps/root.go b/cmd/apps/root.go index e100408f..22688c82 100644 --- a/cmd/apps/root.go +++ b/cmd/apps/root.go @@ -62,12 +62,9 @@ var daemonCmd = &cobra.Command{ } ctrl := controller.New(loader, meta, sto) - wfMgr, err := workflow.NewWorkflowManager(ctrl) - if err != nil { + if err := controller.InitSchemas(ctrl); err != nil { panic(err) } - go wfMgr.Run() - stop := utils.HandleTerminalSignal() files.InitFileIoChain(cfg, sto, stop) run(ctrl, cfg, stop) @@ -103,6 +100,13 @@ func run(ctrl controller.Controller, cfg config.Config, stopCh chan struct{}) { if err != nil { panic(err) } + wfManager, err := workflow.NewWorkflowManager(ctrl) + if err != nil { + panic(err) + } + if err = wfManager.Run(); err != nil { + panic(err) + } } log.Info("started") diff --git a/pkg/controller/controller.go b/pkg/controller/controller.go index 78706ede..9ac917b8 100644 --- a/pkg/controller/controller.go +++ b/pkg/controller/controller.go @@ -44,20 +44,30 @@ func New(loader config.Loader, meta storage.MetaStore, storage storage.Storage) } return ctl } - -func (c *controller) setup(ctx context.Context) error { - for _, s := range c.registry.GetSchemas() { - root, _ := types.InitNewObject(nil, types.ObjectAttr{Name: fmt.Sprintf(".%s", string(s.CType)), Kind: types.GroupKind}) - root.ParentID = dentry.RootObjectID - root.Labels = types.Labels{Labels: []types.Label{{ +func InitSchemas(ctrl Controller) error { + schemas := dentry.Registry.GetSchemas() + root, err := ctrl.LoadRootObject(context.TODO()) + if err != nil { + return err + } + for _, s := range schemas { + obj, _ := types.InitNewObject(nil, types.ObjectAttr{Name: fmt.Sprintf(".%s", string(s.CType)), Kind: types.GroupKind}) + _, err = ctrl.FindObject(context.TODO(), root, obj.Name) + if err != nil && err != types.ErrNotFound { + return err + } + if err == nil { + continue + } + obj.ParentID = root.ID + obj.Labels = types.Labels{Labels: []types.Label{{ Key: types.VersionKey, Value: s.Version, }, { Key: types.KindKey, Value: string(s.CType), }}} - if err := c.SaveObject(ctx, root); err != nil { - c.logger.Infow("save object error", "err", err) + if err = ctrl.SaveObject(context.TODO(), obj); err != nil { return err } } diff --git a/pkg/controller/object.go b/pkg/controller/object.go index c29acae4..d5b89286 100644 --- a/pkg/controller/object.go +++ b/pkg/controller/object.go @@ -29,10 +29,7 @@ func (c *controller) LoadRootObject(ctx context.Context) (*types.Object, error) if err != nil { if err == types.ErrNotFound { root = dentry.InitRootObject() - if err := c.SaveObject(ctx, root); err != nil { - return nil, err - } - return root, c.setup(ctx) + return root, c.SaveObject(ctx, root) } c.logger.Errorw("load root object error", "err", err.Error()) return nil, err diff --git a/pkg/workflow/spec_test.go b/pkg/workflow/spec_test.go index 40236773..d1fc4234 100644 --- a/pkg/workflow/spec_test.go +++ b/pkg/workflow/spec_test.go @@ -4,13 +4,33 @@ import ( "github.com/basenana/go-flow/controller" "github.com/basenana/go-flow/flow" "github.com/basenana/go-flow/fsm" + "github.com/basenana/nanafs/config" + ctrl "github.com/basenana/nanafs/pkg/controller" + "github.com/basenana/nanafs/pkg/files" "github.com/basenana/nanafs/pkg/plugin" + "github.com/basenana/nanafs/pkg/storage" "github.com/basenana/nanafs/pkg/types" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "time" ) +type mockConfig struct{} + +func (m mockConfig) GetConfig() (config.Config, error) { + return config.Config{ApiConfig: config.Api{Enable: true}}, nil +} + +var _ config.Loader = mockConfig{} + +func NewControllerForTest() ctrl.Controller { + m, _ := storage.NewMetaStorage("memory", config.Meta{}) + s, _ := storage.NewStorage("memory", config.Storage{}) + + files.InitFileIoChain(config.Config{}, s, make(chan struct{})) + return ctrl.New(mockConfig{}, m, s) +} + type fakePlugin struct { } @@ -24,19 +44,21 @@ func (f fakePlugin) Run(object *types.Object) error { var _ = Describe("TestWorkflow", func() { var ( + ctl ctrl.Controller testCtl *controller.FlowController fakePlugin fakePlugin ) BeforeEach(func() { + ctl = NewControllerForTest() opt := controller.Option{ Storage: FlowStorage, } - ctl, err := controller.NewFlowController(opt) + flowCtl, err := controller.NewFlowController(opt) if err != nil { panic(err) } - testCtl = ctl - if err := testCtl.Register(&NanaFlow{}); err != nil { + testCtl = flowCtl + if err := testCtl.Register(&NanaJob{}); err != nil { panic(err) } }) @@ -47,11 +69,13 @@ var _ = Describe("TestWorkflow", func() { rule := types.Rule{Logic: "", Rules: nil, Operation: nil} f := types.Object{} w := NewWorkflow("test", rule, []plugin.Plugin{fakePlugin}) - j := NewJob(w, &f) - err := j.Run() + jobObj := types.Object{} + job, _, err := NewNanaJob(ctl, w, &jobObj, &f) + Expect(err).Should(BeNil()) + err = job.Run() Expect(err).Should(BeNil()) Eventually(func() []byte { - return status2Bytes(j.Flow.GetStatus()) + return status2Bytes(job.GetStatus()) }, time.Minute*3, time.Second).Should(Equal(status2Bytes(flow.SucceedStatus))) }) }) diff --git a/pkg/workflow/suite_test.go b/pkg/workflow/suite_test.go index e62acb59..cae2afe3 100644 --- a/pkg/workflow/suite_test.go +++ b/pkg/workflow/suite_test.go @@ -1,6 +1,7 @@ package workflow import ( + "github.com/basenana/nanafs/utils/logger" "testing" . "github.com/onsi/ginkgo" @@ -8,6 +9,8 @@ import ( ) func TestWorkflow(t *testing.T) { + logger.InitLogger() + defer logger.Sync() RegisterFailHandler(Fail) RunSpecs(t, "Workflow Suite") }