本文整理汇总了Golang中github.com/cloudwan/gohan/schema.NewExtension函数的典型用法代码示例。如果您正苦于以下问题:Golang NewExtension函数的具体用法?Golang NewExtension怎么用?Golang NewExtension使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewExtension函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: BenchmarkDonburi
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)
}
}
示例2: TestDonburiResources
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))
}
示例3: TestDonburiInjectionAttack
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))
}
示例4: TestDonburiExec
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"))
}
示例5: TestDefine
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)))
}
示例6:
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())
})
})
示例7:
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},
示例8:
}
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{})
示例9: TestDonburiFlows
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)))
}