github.com/Azure/draft-classic@v0.16.0/cmd/draft/plugin_install_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "strings" 9 "testing" 10 11 "github.com/Azure/draft/pkg/draft/draftpath" 12 ) 13 14 type pluginTest struct { 15 name string 16 plugin string 17 path string 18 output string 19 fail bool 20 flags []string 21 } 22 23 type testPluginEnv struct { 24 pluginEnvVar string 25 draftHome string 26 } 27 28 func setupTestPluginEnv(target *testPluginEnv) (*testPluginEnv, error) { 29 // save old 30 old := draftHome 31 oldenv := os.Getenv(pluginEnvVar) 32 33 // set new 34 draftHome = target.draftHome 35 err := os.Setenv(pluginEnvVar, target.pluginEnvVar) 36 37 return &testPluginEnv{ 38 draftHome: old, 39 pluginEnvVar: oldenv, 40 }, err 41 } 42 43 func teardownTestPluginEnv(current, original *testPluginEnv) { 44 draftHome = original.draftHome 45 os.Setenv(pluginEnvVar, original.pluginEnvVar) 46 os.RemoveAll(current.draftHome) 47 } 48 49 func newTestPluginEnv(home, pluginEnvVarValue string) (*testPluginEnv, error) { 50 target := &testPluginEnv{} 51 52 if home == "" { 53 tempHome, err := ioutil.TempDir("", "draft_home-") 54 if err != nil { 55 return target, err 56 } 57 58 if err := os.Mkdir(filepath.Join(tempHome, "plugins"), 0755); err != nil { 59 return target, err 60 } 61 62 target.draftHome = tempHome 63 } else { 64 target.draftHome = home 65 } 66 67 target.pluginEnvVar = pluginEnvVarValue 68 69 return target, nil 70 } 71 72 func TestPluginInstallCmd(t *testing.T) { 73 target, err := newTestPluginEnv("", "") 74 if err != nil { 75 t.Fatal(err) 76 } 77 78 old, err := setupTestPluginEnv(target) 79 if err != nil { 80 t.Fatal(err) 81 } 82 defer teardownTestPluginEnv(target, old) 83 84 tests := []pluginTest{ 85 { 86 name: "install plugin", 87 plugin: "echo", 88 path: filepath.Join("testdata", "plugins", "echo"), 89 output: "Installed plugin: echo\n", 90 fail: false, 91 }, 92 { 93 name: "error installing nonexistent plugin", 94 plugin: "dummy", 95 path: filepath.Join("testdata", "plugins", "dummy"), 96 output: "", 97 fail: true, 98 }, 99 } 100 101 home := draftpath.Home(draftHome) 102 buf := bytes.NewBuffer(nil) 103 for _, tt := range tests { 104 cmd := newPluginInstallCmd(buf) 105 106 if err := cmd.PreRunE(cmd, []string{tt.path}); err != nil { 107 t.Errorf("%q reported error: %s", tt.name, err) 108 } 109 110 if err := cmd.RunE(cmd, []string{tt.path}); err != nil && !tt.fail { 111 t.Errorf("%q reported error: %s", tt.name, err) 112 } 113 114 if !tt.fail { 115 result := buf.String() 116 if strings.Compare(result, tt.output) != 0 { 117 t.Errorf("Expected %v, got %v", tt.output, result) 118 } 119 120 if _, err = os.Stat(filepath.Join(home.Plugins(), tt.plugin)); err != nil && os.IsNotExist(err) { 121 t.Errorf("Installed plugin not found: %v", err) 122 } 123 124 } 125 126 buf.Reset() 127 } 128 129 cmd := newPluginInstallCmd(buf) 130 if err := cmd.PreRunE(cmd, []string{"arg1", "extra arg"}); err == nil { 131 t.Error("Expected failure due to incorrect number of arguments for plugin install command") 132 } 133 134 }