Skip to content

Commit

Permalink
plugin: add go so testing
Browse files Browse the repository at this point in the history
  • Loading branch information
jouyouyun committed Sep 3, 2020
1 parent 890ed08 commit a4a6edf
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
15 changes: 15 additions & 0 deletions plugin/golang/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
TARGET := test_plugin
PLUGIN_TARGET := dell7590.so

build: build-plugin
go build -o ${TARGET} main.go


build-plugin:
cd plugin && go build -buildmode=plugin -o ${PLUGIN_TARGET} && cd ..

clean:
rm -f plugin/${PLUGIN_TARGET}
rm -f ${TARGET}

rebuild: clean build
6 changes: 6 additions & 0 deletions plugin/golang/adapter/adapter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package adapter

type Adapter interface {
Apply() error
Check() error
}
58 changes: 58 additions & 0 deletions plugin/golang/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package main

import (
"fmt"
"plugin"

"reflect"

"./adapter"
)

func testAdapter(adp adapter.Adapter) {
fmt.Println("\tstart test adapter...")
if err := adp.Apply(); err != nil {
fmt.Println(err)
}
if err := adp.Check(); err != nil {
fmt.Println(err)
}
fmt.Println("\ttest done")
}

func main() {
fmt.Println("Start test...")

plg, err := plugin.Open("./plugin/dell7590.so")
if err != nil {
fmt.Println("Failed to open plugin:", err)
return
}

obj, err := plg.Lookup("Object")
if err != nil {
fmt.Println("Failed to lookup obj:", err)
return
}

dell7590, ok := obj.(*adapter.Adapter)
if !ok {
fmt.Printf("Failed to convert to adapter from Object, %#v\n", reflect.TypeOf(obj).String())
} else {
testAdapter(*dell7590)
}

fobj, err := plg.Lookup("NewObject")
if err != nil {
fmt.Println("Failed to lookup NewObject:", err)
return
}

ret := fobj.(func() adapter.Adapter)()
if ret == nil {
fmt.Println("Failed to convert to adapter from NewObject")
} else {
testAdapter(ret)
}
fmt.Println("Done")
}
29 changes: 29 additions & 0 deletions plugin/golang/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package main

import (
"fmt"

. "../adapter"
)

type Dell7590 struct{}

func (info *Dell7590) Apply() error {
fmt.Println("Apply dell 7590")
return nil
}

func (info *Dell7590) Check() error {
fmt.Println("Check dell 7590")
return nil
}

func NewObject() Adapter {
return &Dell7590{}
}

var Object Adapter

func init() {
Object = &Dell7590{}
}

0 comments on commit a4a6edf

Please sign in to comment.