Skip to content

Commit 0f51a89

Browse files
authored
Merge pull request #15 from basenana/fix_test
fix schema init
2 parents 8ee45e6 + 0b6121f commit 0f51a89

File tree

5 files changed

+63
-24
lines changed

5 files changed

+63
-24
lines changed

cmd/apps/root.go

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/basenana/nanafs/pkg/controller"
99
"github.com/basenana/nanafs/pkg/files"
1010
"github.com/basenana/nanafs/pkg/storage"
11+
"github.com/basenana/nanafs/pkg/workflow"
1112
"github.com/basenana/nanafs/utils"
1213
"github.com/basenana/nanafs/utils/logger"
1314
"github.com/spf13/cobra"
@@ -61,12 +62,9 @@ var daemonCmd = &cobra.Command{
6162
}
6263

6364
ctrl := controller.New(loader, meta, sto)
64-
//wfMgr, err := workflow.NewWorkflowManager(ctrl)
65-
//if err != nil {
66-
// panic(err)
67-
//}
68-
//go wfMgr.Run()
69-
65+
if err := controller.InitSchemas(ctrl); err != nil {
66+
panic(err)
67+
}
7068
stop := utils.HandleTerminalSignal()
7169
files.InitFileIoChain(cfg, sto, stop)
7270
run(ctrl, cfg, stop)
@@ -102,6 +100,13 @@ func run(ctrl controller.Controller, cfg config.Config, stopCh chan struct{}) {
102100
if err != nil {
103101
panic(err)
104102
}
103+
wfManager, err := workflow.NewWorkflowManager(ctrl)
104+
if err != nil {
105+
panic(err)
106+
}
107+
if err = wfManager.Run(); err != nil {
108+
panic(err)
109+
}
105110
}
106111

107112
log.Info("started")

pkg/controller/controller.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,20 +44,30 @@ func New(loader config.Loader, meta storage.MetaStore, storage storage.Storage)
4444
}
4545
return ctl
4646
}
47-
48-
func (c *controller) setup(ctx context.Context) error {
49-
for _, s := range c.registry.GetSchemas() {
50-
root, _ := types.InitNewObject(nil, types.ObjectAttr{Name: fmt.Sprintf(".%s", string(s.CType)), Kind: types.GroupKind})
51-
root.ParentID = dentry.RootObjectID
52-
root.Labels = types.Labels{Labels: []types.Label{{
47+
func InitSchemas(ctrl Controller) error {
48+
schemas := dentry.Registry.GetSchemas()
49+
root, err := ctrl.LoadRootObject(context.TODO())
50+
if err != nil {
51+
return err
52+
}
53+
for _, s := range schemas {
54+
obj, _ := types.InitNewObject(nil, types.ObjectAttr{Name: fmt.Sprintf(".%s", string(s.CType)), Kind: types.GroupKind})
55+
_, err = ctrl.FindObject(context.TODO(), root, obj.Name)
56+
if err != nil && err != types.ErrNotFound {
57+
return err
58+
}
59+
if err == nil {
60+
continue
61+
}
62+
obj.ParentID = root.ID
63+
obj.Labels = types.Labels{Labels: []types.Label{{
5364
Key: types.VersionKey,
5465
Value: s.Version,
5566
}, {
5667
Key: types.KindKey,
5768
Value: string(s.CType),
5869
}}}
59-
if err := c.SaveObject(ctx, root); err != nil {
60-
c.logger.Infow("save object error", "err", err)
70+
if err = ctrl.SaveObject(context.TODO(), obj); err != nil {
6171
return err
6272
}
6373
}

pkg/controller/object.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,7 @@ func (c *controller) LoadRootObject(ctx context.Context) (*types.Object, error)
2929
if err != nil {
3030
if err == types.ErrNotFound {
3131
root = dentry.InitRootObject()
32-
if err := c.SaveObject(ctx, root); err != nil {
33-
return nil, err
34-
}
35-
return root, c.setup(ctx)
32+
return root, c.SaveObject(ctx, root)
3633
}
3734
c.logger.Errorw("load root object error", "err", err.Error())
3835
return nil, err

pkg/workflow/spec_test.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,33 @@ import (
44
"github.com/basenana/go-flow/controller"
55
"github.com/basenana/go-flow/flow"
66
"github.com/basenana/go-flow/fsm"
7+
"github.com/basenana/nanafs/config"
8+
ctrl "github.com/basenana/nanafs/pkg/controller"
9+
"github.com/basenana/nanafs/pkg/files"
710
"github.com/basenana/nanafs/pkg/plugin"
11+
"github.com/basenana/nanafs/pkg/storage"
812
"github.com/basenana/nanafs/pkg/types"
913
. "github.com/onsi/ginkgo"
1014
. "github.com/onsi/gomega"
1115
"time"
1216
)
1317

18+
type mockConfig struct{}
19+
20+
func (m mockConfig) GetConfig() (config.Config, error) {
21+
return config.Config{ApiConfig: config.Api{Enable: true}}, nil
22+
}
23+
24+
var _ config.Loader = mockConfig{}
25+
26+
func NewControllerForTest() ctrl.Controller {
27+
m, _ := storage.NewMetaStorage("memory", config.Meta{})
28+
s, _ := storage.NewStorage("memory", config.Storage{})
29+
30+
files.InitFileIoChain(config.Config{}, s, make(chan struct{}))
31+
return ctrl.New(mockConfig{}, m, s)
32+
}
33+
1434
type fakePlugin struct {
1535
}
1636

@@ -24,19 +44,21 @@ func (f fakePlugin) Run(object *types.Object) error {
2444

2545
var _ = Describe("TestWorkflow", func() {
2646
var (
47+
ctl ctrl.Controller
2748
testCtl *controller.FlowController
2849
fakePlugin fakePlugin
2950
)
3051
BeforeEach(func() {
52+
ctl = NewControllerForTest()
3153
opt := controller.Option{
3254
Storage: FlowStorage,
3355
}
34-
ctl, err := controller.NewFlowController(opt)
56+
flowCtl, err := controller.NewFlowController(opt)
3557
if err != nil {
3658
panic(err)
3759
}
38-
testCtl = ctl
39-
if err := testCtl.Register(&NanaFlow{}); err != nil {
60+
testCtl = flowCtl
61+
if err := testCtl.Register(&NanaJob{}); err != nil {
4062
panic(err)
4163
}
4264
})
@@ -47,11 +69,13 @@ var _ = Describe("TestWorkflow", func() {
4769
rule := types.Rule{Logic: "", Rules: nil, Operation: nil}
4870
f := types.Object{}
4971
w := NewWorkflow("test", rule, []plugin.Plugin{fakePlugin})
50-
j := NewJob(w, &f)
51-
err := j.Run()
72+
jobObj := types.Object{}
73+
job, _, err := NewNanaJob(ctl, w, &jobObj, &f)
74+
Expect(err).Should(BeNil())
75+
err = job.Run()
5276
Expect(err).Should(BeNil())
5377
Eventually(func() []byte {
54-
return status2Bytes(j.Flow.GetStatus())
78+
return status2Bytes(job.GetStatus())
5579
}, time.Minute*3, time.Second).Should(Equal(status2Bytes(flow.SucceedStatus)))
5680
})
5781
})

pkg/workflow/suite_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package workflow
22

33
import (
4+
"github.com/basenana/nanafs/utils/logger"
45
"testing"
56

67
. "github.com/onsi/ginkgo"
78
. "github.com/onsi/gomega"
89
)
910

1011
func TestWorkflow(t *testing.T) {
12+
logger.InitLogger()
13+
defer logger.Sync()
1114
RegisterFailHandler(Fail)
1215
RunSpecs(t, "Workflow Suite")
1316
}

0 commit comments

Comments
 (0)