Skip to content

Commit

Permalink
Merge pull request #15 from basenana/fix_test
Browse files Browse the repository at this point in the history
fix schema init
  • Loading branch information
hyponet authored May 5, 2022
2 parents 8ee45e6 + 0b6121f commit 0f51a89
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 24 deletions.
17 changes: 11 additions & 6 deletions cmd/apps/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/basenana/nanafs/pkg/controller"
"github.com/basenana/nanafs/pkg/files"
"github.com/basenana/nanafs/pkg/storage"
"github.com/basenana/nanafs/pkg/workflow"
"github.com/basenana/nanafs/utils"
"github.com/basenana/nanafs/utils/logger"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -61,12 +62,9 @@ var daemonCmd = &cobra.Command{
}

ctrl := controller.New(loader, meta, sto)
//wfMgr, err := workflow.NewWorkflowManager(ctrl)
//if err != nil {
// panic(err)
//}
//go wfMgr.Run()

if err := controller.InitSchemas(ctrl); err != nil {
panic(err)
}
stop := utils.HandleTerminalSignal()
files.InitFileIoChain(cfg, sto, stop)
run(ctrl, cfg, stop)
Expand Down Expand Up @@ -102,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")
Expand Down
26 changes: 18 additions & 8 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand Down
5 changes: 1 addition & 4 deletions pkg/controller/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
36 changes: 30 additions & 6 deletions pkg/workflow/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
}

Expand All @@ -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)
}
})
Expand All @@ -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)))
})
})
Expand Down
3 changes: 3 additions & 0 deletions pkg/workflow/suite_test.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package workflow

import (
"github.com/basenana/nanafs/utils/logger"
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestWorkflow(t *testing.T) {
logger.InitLogger()
defer logger.Sync()
RegisterFailHandler(Fail)
RunSpecs(t, "Workflow Suite")
}

0 comments on commit 0f51a89

Please sign in to comment.