Selaa lähdekoodia

feat(plugin): yaml is not required except source and add more test cases

ngjaying 5 vuotta sitten
vanhempi
commit
27ec2878d1
2 muutettua tiedostoa jossa 87 lisäystä ja 56 poistoa
  1. 41 49
      plugins/manager.go
  2. 46 7
      plugins/manager_test.go

+ 41 - 49
plugins/manager.go

@@ -127,7 +127,7 @@ func (m *Manager) Register(t PluginType, name string, uri string, callback OnReg
 	if name == "" {
 		return fmt.Errorf("invalid name %s: should not be empty", name)
 	}
-	if !isValidUrl(uri) && strings.HasSuffix(uri, ".zip") {
+	if !isValidUrl(uri) || !strings.HasSuffix(uri, ".zip") {
 		return fmt.Errorf("invalid uri %s", uri)
 	}
 
@@ -141,9 +141,9 @@ func (m *Manager) Register(t PluginType, name string, uri string, callback OnReg
 	//clean up: delete zip file and unzip files in error
 	defer func() {
 		os.Remove(zipPath)
-		if len(unzipFiles) == 1 {
+		if t == SOURCE && len(unzipFiles) == 1 { //source that only copy so file
 			os.Remove(unzipFiles[0])
-		} else if len(unzipFiles) == 2 {
+		} else if (t == SOURCE && len(unzipFiles) == 2) || (t != SOURCE && len(unzipFiles) == 1) { //no error
 			m.registry.Store(t, name)
 			callback()
 		}
@@ -176,26 +176,24 @@ func (m *Manager) Delete(t PluginType, name string, callback OnRegistered) (resu
 		return fmt.Errorf("invalid name %s: not exist", name)
 	}
 	var results []string
-	soPath := path.Join(m.pluginDir, pluginFolders[t], ucFirst(name)+".so")
-	_, err := os.Stat(soPath)
-	if err == nil {
-		err = os.Remove(soPath)
-		if err != nil {
-			results = append(results, err.Error())
-		}
-	} else {
-		results = append(results, fmt.Sprintf("can't find %s", soPath))
+	paths := []string{
+		path.Join(m.pluginDir, pluginFolders[t], ucFirst(name)+".so"),
 	}
-	etcPath := path.Join(m.etcDir, pluginFolders[t], name+".yaml")
-	_, err = os.Stat(etcPath)
-	if err == nil {
-		err = os.Remove(etcPath)
-		if err != nil {
-			results = append(results, err.Error())
+	if t == SOURCE {
+		paths = append(paths, path.Join(m.etcDir, pluginFolders[t], name+".yaml"))
+	}
+	for _, p := range paths {
+		_, err := os.Stat(p)
+		if err == nil {
+			err = os.Remove(p)
+			if err != nil {
+				results = append(results, err.Error())
+			}
+		} else {
+			results = append(results, fmt.Sprintf("can't find %s", p))
 		}
-	} else {
-		results = append(results, fmt.Sprintf("can't find %s", etcPath))
 	}
+
 	if len(results) > 0 {
 		return errors.New(strings.Join(results, "\n"))
 	} else {
@@ -211,40 +209,34 @@ func (m *Manager) unzipAndCopy(t PluginType, name string, src string) ([]string,
 	}
 	defer r.Close()
 
-	soFileName := ucFirst(name) + ".so"
-	confFileName := name + ".yaml"
-	var soFile, confFile *zip.File
-	found := 0
-	for _, file := range r.File {
-		fileName := file.Name
-		if fileName == soFileName {
-			soFile = file
-			found++
-		} else if fileName == confFileName {
-			confFile = file
-			found++
-		}
-		if found == 2 {
-			break
-		}
+	files := []string{
+		ucFirst(name) + ".so",
 	}
-	if found < 2 {
-		return filenames, fmt.Errorf("invalid zip file: so file or conf file is missing")
+	paths := []string{
+		path.Join(m.pluginDir, pluginFolders[t], files[0]),
 	}
-
-	soPath := path.Join(m.pluginDir, pluginFolders[t], soFileName)
-	err = unzipTo(soFile, soPath)
-	if err != nil {
-		return filenames, err
+	if t == SOURCE {
+		files = append(files, name+".yaml")
+		paths = append(paths, path.Join(m.etcDir, pluginFolders[t], files[1]))
 	}
-	filenames = append(filenames, soPath)
+	for i, d := range files {
+		var z *zip.File
+		for _, file := range r.File {
+			fileName := file.Name
+			if fileName == d {
+				z = file
+			}
+		}
+		if z == nil {
+			return filenames, fmt.Errorf("invalid zip file: so file or conf file is missing")
+		}
 
-	confPath := path.Join(m.etcDir, pluginFolders[t], confFileName)
-	err = unzipTo(confFile, confPath)
-	if err != nil {
-		return filenames, err
+		err = unzipTo(z, paths[i])
+		if err != nil {
+			return filenames, err
+		}
+		filenames = append(filenames, paths[i])
 	}
-	filenames = append(filenames, confPath)
 	return filenames, nil
 }
 

+ 46 - 7
plugins/manager_test.go

@@ -23,9 +23,42 @@ func TestManager_Register(t *testing.T) {
 			u:   "",
 			err: errors.New("invalid name : should not be empty"),
 		}, {
+			t:   SOURCE,
+			n:   "zipMissConf",
+			u:   endpoint + "/sources/zipMissConf.zip",
+			err: errors.New("fail to unzip file " + endpoint + "/sources/zipMissConf.zip: invalid zip file: so file or conf file is missing"),
+		}, {
+			t:   SINK,
+			n:   "urlerror",
+			u:   endpoint + "/sinks/nozip",
+			err: errors.New("invalid uri " + endpoint + "/sinks/nozip"),
+		}, {
+			t:   SINK,
+			n:   "zipWrongname",
+			u:   endpoint + "/sinks/zipWrongName.zip",
+			err: errors.New("fail to unzip file " + endpoint + "/sinks/zipWrongName.zip: invalid zip file: so file or conf file is missing"),
+		}, {
+			t:   FUNCTION,
+			n:   "zipMissSo",
+			u:   endpoint + "/functions/zipMissSo.zip",
+			err: errors.New("fail to unzip file " + endpoint + "/functions/zipMissSo.zip: invalid zip file: so file or conf file is missing"),
+		}, {
 			t: SOURCE,
 			n: "random2",
 			u: endpoint + "/sources/random2.zip",
+		}, {
+			t: SINK,
+			n: "file",
+			u: endpoint + "/sinks/file.zip",
+		}, {
+			t: FUNCTION,
+			n: "echo",
+			u: endpoint + "/functions/echo.zip",
+		}, {
+			t:   FUNCTION,
+			n:   "echo",
+			u:   endpoint + "/functions/echo.zip",
+			err: errors.New("invalid name echo: duplicate"),
 		},
 	}
 	manager, err := NewPluginManager()
@@ -57,6 +90,12 @@ func TestManager_Delete(t *testing.T) {
 		{
 			t: SOURCE,
 			n: "random2",
+		}, {
+			t: SINK,
+			n: "file",
+		}, {
+			t: FUNCTION,
+			n: "echo",
 		},
 	}
 	manager, err := NewPluginManager()
@@ -64,9 +103,7 @@ func TestManager_Delete(t *testing.T) {
 		t.Error(err)
 	}
 	fmt.Printf("The test bucket size is %d.\n\n", len(data))
-	if len(data) > 3 && manager.pluginDir == "" {
-		t.Errorf("Expect %s, actual %s; error is %s. \n", "test", "3", "3")
-	}
+
 	for i, p := range data {
 		err = manager.Delete(p.t, p.n, func() {})
 		if err != nil {
@@ -81,10 +118,12 @@ func checkFile(pluginDir string, etcDir string, t PluginType, name string) error
 	if err != nil {
 		return err
 	}
-	etcPath := path.Join(etcDir, pluginFolders[t], name+".yaml")
-	_, err = os.Stat(etcPath)
-	if err != nil {
-		return err
+	if t == SOURCE {
+		etcPath := path.Join(etcDir, pluginFolders[t], name+".yaml")
+		_, err = os.Stat(etcPath)
+		if err != nil {
+			return err
+		}
 	}
 	return nil
 }