func BenchmarkDonburi(b *testing.B) { beforeEach() defer afterEach() donburi, _ := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) extension, _ := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - vars: message: "hello" - vars: template_test: "message: {{.message}}" `, "path": ".*", "code_type": "donburi", }) context := map[string]interface{}{} extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}) err := env.LoadExtensionsForPath(extensions, "test_path") if err != nil { b.Error(err) } b.ResetTimer() for i := 0; i < b.N; i++ { env.HandleEvent("post_create", context) } }
func TestDonburiResources(t *testing.T) { RegisterTestingT(t) beforeEach() defer afterEach() donburi, err := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) if err != nil { t.Error(err) } extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - vars: failed: false - resources: - vars: dependent: true - eval: "if(dependent){failed = true}" when: event_type == "pre_delete" - eval: "if(!dependent){failed = true}" when: event_type == "post_create" - vars: dependent: false `, "path": ".*", "code_type": "donburi", }) if err != nil { t.Error(err) } context := map[string]interface{}{} extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}) err = env.LoadExtensionsForPath(extensions, "test_path") if err != nil { t.Error(err) } err = env.HandleEvent("post_create", context) if err != nil { t.Error(err) } Expect(context["failed"]).To(Equal(false)) err = env.HandleEvent("pre_delete", context) if err != nil { t.Error(err) } Expect(context["failed"]).To(Equal(false)) }
func TestDonburiInjectionAttack(t *testing.T) { RegisterTestingT(t) beforeEach() defer afterEach() donburi, err := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) if err != nil { t.Error(err) } extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - vars: executed: true when: code - eval: "{{.code}}" - block: - eval: "throw error" rescue: - eval "{{.code}}" always: - eval "{{.code}}" when: code `, "path": ".*", "code_type": "donburi", }) if err != nil { t.Error(err) } context := map[string]interface{}{ "attacked": false, "code": "context['attacked'] = true", } extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}) err = env.LoadExtensionsForPath(extensions, "test_path") if err != nil { t.Error(err) } err = env.HandleEvent("post_create", context) if err != nil { t.Error(err) } Expect(context["attacked"]).To(Equal(false)) }
func TestDonburiExec(t *testing.T) { RegisterTestingT(t) beforeEach() defer afterEach() donburi, err := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) if err != nil { t.Error(err) } extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - command: name: echo args: - test register: result - command: name: no_command args: [] register: result2 `, "path": ".*", "code_type": "donburi", }) if err != nil { t.Error(err) } context := map[string]interface{}{} extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}) err = env.LoadExtensionsForPath(extensions, "test_path") if err != nil { t.Error(err) } err = env.HandleEvent("post_create", context) if err != nil { t.Error(err) } output := context["result"].(map[string]string) Expect(output["status"]).To(Equal("success")) Expect(output["output"]).To(Equal("test\n")) output2 := context["result2"].(map[string]string) Expect(output2["status"]).To(Equal("error")) }
func TestDefine(t *testing.T) { RegisterTestingT(t) beforeEach() defer afterEach() donburi, err := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) if err != nil { t.Error(err) } extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - define: name: "add" tasks: - eval: "left+right" - add: left: 1 right: 2 register: answer `, "path": ".*", "code_type": "donburi", }) if err != nil { t.Error(err) } context := map[string]interface{}{} extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}, timelimit) err = env.LoadExtensionsForPath(extensions, "test_path") if err != nil { t.Error(err) } err = env.HandleEvent("post_create", context) if err != nil { t.Error(err) } Expect(context["answer"]).To(Equal(float64(3))) }
err = clearTable(tx, schema) Expect(err).ToNot(HaveOccurred(), "Failed to clear table.") } err = tx.Commit() Expect(err).ToNot(HaveOccurred(), "Failed to commite transaction.") extension.ClearManager() }) Describe("Loading an extension", func() { Context("When extension uses code property", func() { It("should run the extension code", func() { extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_extension", "code": `tasks: - vars: person: John `, "path": ".*", }) Expect(err).ToNot(HaveOccurred()) extensions := []*schema.Extension{extension} env := gohanscript.NewEnvironment(timelimit) Expect(env.LoadExtensionsForPath(extensions, "test_path")).To(Succeed()) context := map[string]interface{}{ "id": "test", } Expect(env.HandleEvent("test_event", context)).To(Succeed()) Expect(context["person"]).ToNot(BeNil()) }) })
Expect(err).ToNot(HaveOccurred()) r1, err = schema.NewResource(s, fakeResources[1]) Expect(err).ToNot(HaveOccurred()) }) Describe("gohan_db_list", func() { Context("When valid minimum parameters are given", func() { It("returns the list ordered by id", func() { extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_extension", "code": ` gohan_register_handler("test_event", function(context){ var tx = context.transaction; context.resp = gohan_db_list( tx, "test", {"tenant_id": "tenant0"} ); });`, "path": ".*", }) Expect(err).ToNot(HaveOccurred()) env := newEnvironmentWithExtension(extension) var pagenator *pagination.Paginator var fakeTx = new(mocks.Transaction) fakeTx.On( "List", s, transaction.Filter{"tenant_id": "tenant0"}, pagenator, ).Return( []*schema.Resource{r0, r1},
} err = clearTable(tx, schema) Expect(err).ToNot(HaveOccurred(), "Failed to clear table.") } err = tx.Commit() Expect(err).ToNot(HaveOccurred(), "Failed to commite transaction.") extension.ClearManager() }) Describe("Loading an extension", func() { Context("When extension is not a valid JavaScript", func() { It("returns a meaningful compilation error", func() { goodExtension, err := schema.NewExtension(map[string]interface{}{ "id": "good_extension", "code": `gohan_register_handler("test_event", function(context) {});`, "path": ".*", }) Expect(err).ToNot(HaveOccurred()) goodExtension.URL = "good_extension.js" badExtension, err := schema.NewExtension(map[string]interface{}{ "id": "bad_extension", "code": `gohan_register_handler("test_event", function(context {});`, "path": ".*", }) Expect(err).ToNot(HaveOccurred()) badExtension.URL = "bad_extension.js" extensions := []*schema.Extension{goodExtension, badExtension} env := otto.NewEnvironment(testDB, &middleware.FakeIdentity{})
func TestDonburiFlows(t *testing.T) { RegisterTestingT(t) beforeEach() defer afterEach() donburi, err := schema.NewExtension(map[string]interface{}{ "id": "donburi", "url": "file://../../etc/extensions/donburi.js", "path": ".*", }) if err != nil { t.Error(err) } extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_donburi", "code": `tasks: - eval: "1 + 1" register: result - eval: "true" register: when_is_working when: "result == 2" - block: - vars: list2 : [4, 5, 6] - eval: "result += item" with_items: - 1 - 2 - 3 when: when_is_working - vars: message: "hello" - vars: retry_count: 0 rescue_executed: false always_executed: false template_test: "message: {{.message}}" - eval: "result += item" with_items: "list2" - eval: "context[item.key] = item.value" with_dict: alice: 18 bob: 21 - block: - sleep: 10 - eval: retry_count += 1 - eval: throw 'error' rescue: - eval: "rescue_executed = true" always: - eval: "always_executed = true" retry: 3 `, "path": ".*", "code_type": "donburi", }) if err != nil { t.Error(err) } context := map[string]interface{}{} extensions := []*schema.Extension{donburi, extension} env := NewEnvironment(dataStore, &middleware.FakeIdentity{}) err = env.LoadExtensionsForPath(extensions, "test_path") if err != nil { t.Error(err) } err = env.HandleEvent("post_create", context) if err != nil { t.Error(err) } Expect(context["result"]).To(Equal(float64(23))) Expect(context["when_is_working"]).To(Equal(true)) Expect(context["rescue_executed"]).To(Equal(true)) Expect(context["always_executed"]).To(Equal(true)) Expect(context["template_test"]).To(Equal("message: hello")) Expect(context["alice"]).To(Equal(int(18))) Expect(context["retry_count"]).To(Equal(float64(3))) }
"github.com/cloudwan/gohan/extension" "github.com/cloudwan/gohan/schema" ) var _ = Describe("Gohanscript extension manager", func() { AfterEach(func() { extension.ClearManager() }) Describe("Loading an extension", func() { Context("When extension uses code property", func() { It("should run the extension code", func() { extension, err := schema.NewExtension(map[string]interface{}{ "id": "test_extension", "code": "test_callback", "code_type": "go", "path": ".*", }) Expect(err).ToNot(HaveOccurred()) extensions := []*schema.Extension{extension} env := golang.NewEnvironment() Expect(env.LoadExtensionsForPath(extensions, "test_path")).To(Succeed()) context := map[string]interface{}{ "id": "test", } Expect(env.HandleEvent("test_event", context)).To(Succeed()) Expect(context["person"]).ToNot(BeNil()) }) }) })