github.com/dahs81/otto@v0.2.1-0.20160126165905-6400716cf085/command/plugin_manager_test.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "reflect" 9 "testing" 10 11 "github.com/hashicorp/otto/app" 12 "github.com/hashicorp/otto/plugin" 13 ) 14 15 const TestPluginProcessMagicCookie = "abcd" 16 17 func TestPluginLoad(t *testing.T) { 18 plugin := testPlugin(t, "mock") 19 if err := plugin.Load(); err != nil { 20 t.Fatalf("err: %s", err) 21 } 22 23 if !reflect.DeepEqual(plugin.AppMeta, testPluginMockMeta) { 24 t.Fatalf("bad: %#v", plugin.AppMeta) 25 } 26 } 27 28 func TestPluginLoad_used(t *testing.T) { 29 plugin := testPlugin(t, "mock") 30 if err := plugin.Load(); err != nil { 31 t.Fatalf("err: %s", err) 32 } 33 34 if plugin.Used() { 35 t.Fatal("should not be used") 36 } 37 38 if _, err := plugin.App(); err != nil { 39 t.Fatalf("err: %s", err) 40 } 41 42 if !plugin.Used() { 43 t.Fatal("should be used") 44 } 45 } 46 47 func TestPluginManager_saveLoad(t *testing.T) { 48 mock := testPlugin(t, "mock") 49 mgr := &PluginManager{ 50 plugins: []*Plugin{ 51 mock, 52 testPlugin(t, "empty"), 53 testPlugin(t, "empty"), 54 testPlugin(t, "empty"), 55 }, 56 } 57 58 if err := mgr.LoadAll(); err != nil { 59 t.Fatalf("err: %s", err) 60 } 61 62 // Use only the mock 63 if _, err := mock.App(); err != nil { 64 t.Fatalf("err: %s", err) 65 } 66 if !mock.Used() { 67 t.Fatal("should be used") 68 } 69 70 // Create the temporary file to save the data 71 td, err := ioutil.TempDir("", "otto") 72 if err != nil { 73 t.Fatalf("err: %s", err) 74 } 75 defer os.RemoveAll(td) 76 path := filepath.Join(td, "used") 77 78 if err := mgr.StoreUsed(path); err != nil { 79 t.Fatalf("err: %s", err) 80 } 81 82 // Create a new manager and load only the used 83 mgr = &PluginManager{} 84 if err := mgr.LoadUsed(path); err != nil { 85 t.Fatalf("err: %s", err) 86 } 87 if v := mgr.Plugins(); len(v) != 1 { 88 t.Fatalf("bad: %#v", v) 89 } 90 } 91 92 // testPlugin returns a test plugin of the given name. This should correspond 93 // to one of the availabile plugins. 94 func testPlugin(t *testing.T, n string) *Plugin { 95 var result Plugin 96 result.Path = os.Args[0] 97 result.Args = []string{ 98 "-test.run=TestPluginProcess", 99 "--", 100 TestPluginProcessMagicCookie, 101 n, 102 } 103 104 return &result 105 } 106 107 func TestPluginProcess(*testing.T) { 108 // Find where our arguments start based on "--" 109 args := os.Args 110 for len(args) > 0 { 111 if args[0] == "--" { 112 args = args[1:] 113 break 114 } 115 116 args = args[1:] 117 } 118 if len(args) < 1 { 119 return 120 } 121 if args[0] != TestPluginProcessMagicCookie { 122 return 123 } 124 125 // We know we're in a requested plugin process, we want to completely 126 // exit at the end of this so let's defer that. 127 defer os.Exit(0) 128 129 // Determine what plugin we're supposed to be serving 130 var opts plugin.ServeOpts 131 switch args[1] { 132 case "empty": 133 appImpl := &app.Mock{} 134 opts.AppFunc = func() app.App { 135 return appImpl 136 } 137 case "mock": 138 appImpl := &app.Mock{ 139 MetaResult: testPluginMockMeta, 140 } 141 opts.AppFunc = func() app.App { 142 return appImpl 143 } 144 default: 145 fmt.Fprintf(os.Stderr, "Invalid plugin: %s\n", args[1]) 146 os.Exit(2) 147 } 148 149 // Serve the plugin! 150 plugin.Serve(&opts) 151 } 152 153 var testPluginMockMeta = &app.Meta{ 154 Tuples: []app.Tuple{ 155 app.Tuple{"test", "test", "test"}, 156 }, 157 }