crawler_manager_go/crawler_manager_test.go

433 lines
7.1 KiB
Go
Raw Normal View History

2017-04-13 02:53:00 +00:00
package crawler_manager
import (
"testing"
//grpc1 "google.golang.org/grpc"
2017-04-14 02:29:55 +00:00
"os"
"os/exec"
"path/filepath"
"strings"
2017-04-14 07:23:42 +00:00
"io/ioutil"
2017-04-17 04:48:53 +00:00
2017-04-27 09:07:56 +00:00
"loafle.com/overflow/agent_api/config_manager"
"encoding/json"
"fmt"
2017-04-28 04:44:56 +00:00
"time"
"loafle.com/overflow/agent_api/observer"
"loafle.com/overflow/agent_api/observer/messages"
conMgr "loafle.com/overflow/config_manager_go"
"sync"
2017-04-13 02:53:00 +00:00
)
func TestCallInit(t *testing.T) {
2017-04-14 02:29:55 +00:00
//CallInit("")
2017-04-13 02:53:00 +00:00
}
2017-04-15 11:10:33 +00:00
2017-04-14 02:29:55 +00:00
func TestPid(t *testing.T) {
pp, err := os.FindProcess(12314)
if err != nil{
t.Log("err : ", err)
}
t.Log(pp.Pid)
}
func TestPr(t *testing.T) {
//ps -aux | awk '{print $2}' | grep 15538
bytes, _ := exec.Command("ps","-aux", "awk", "{print $2}").Output()
t.Log(string(bytes))
}
func TestExe(t *testing.T) {
ps := exec.Command("ps", "-aux")
awk := exec.Command("awk", "{print $2}")
grep := exec.Command("grep", "22373")
awk.Stdin, _ = ps.StdoutPipe()
grep.Stdin,_ = awk.StdoutPipe()
ps.Start()
awk.Start()
byt, _ := grep.Output()
t.Log(len(byt))
}
func TestExeState(t *testing.T) {
ps := exec.Command("ps", "-aux")
awk := exec.Command("awk", "{print $2, $7}")
grep := exec.Command("grep", "22373")
awk2 := exec.Command("awk", "{print $2}")
awk.Stdin, _ = ps.StdoutPipe()
grep.Stdin,_ = awk.StdoutPipe()
awk2.Stdin,_ = grep.StdoutPipe()
ps.Start()
awk.Start()
grep.Start()
byt, _ := awk2.Output()
t.Log(len(byt))
t.Log(string(byt))
t.Log(len(strings.TrimSpace(string(byt))))
}
func TestPipe(t *testing.T) {
c1 := exec.Command("ls")
c2 := exec.Command("wc", "-l")
c2.Stdin, _ = c1.StdoutPipe()
//c2.Stdout = os.Stdout
_ = c1.Start()
aa, _ := c2.Output()
t.Log(string(aa))
}
func TestCom(t *testing.T) {
str:= "Z"
if str == "Z" {
t.Log("aaa")
}
}
func TestReadConfig(t *testing.T) {
2017-04-15 11:10:33 +00:00
//c := ReadConfig("/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/example.json")
//t.Log(c)
2017-04-14 02:29:55 +00:00
}
func TestDir(t *testing.T) {
a := filepath.Dir("/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/example.json")
t.Log(a)
}
func TestMyPid(t *testing.T) {
t.Log(os.Getpid())
}
2017-04-14 07:23:42 +00:00
func TestDir22(t *testing.T) {
2017-04-15 11:10:33 +00:00
//cs := IsStartContainer()
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
//t.Log(cs)
2017-04-14 07:23:42 +00:00
}
func TestCrateDir(t *testing.T) {
2017-04-17 04:48:53 +00:00
cp := "/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_manager_go/"
2017-04-14 07:23:42 +00:00
var configPath []string;
2017-04-28 04:44:56 +00:00
rootFolder := "/home/cm2/"
ConfigFolder := rootFolder + "/config/container/"
BinaryFolder := rootFolder + "/container/"
PidFolder := rootFolder + "/pids/"
//runFile := "ttnc"
2017-04-14 07:23:42 +00:00
configPath = append(configPath, ConfigFolder + "/java/oracle/")
configPath = append(configPath, ConfigFolder + "/go/test/")
configPath = append(configPath, ConfigFolder + "/java/mysql/")
configPath = append(configPath, ConfigFolder + "/network/http/")
configPath = append(configPath, ConfigFolder + "/network/redis/")
2017-04-14 08:19:02 +00:00
configPath = append(configPath, ConfigFolder + "/network/ldap/")
2017-04-17 04:53:30 +00:00
configPath = append(configPath, PidFolder)
2017-04-14 07:23:42 +00:00
var containerPath []string;
containerPath = append(containerPath, BinaryFolder + "/java/")
containerPath = append(containerPath, BinaryFolder + "/go/")
containerPath = append(containerPath, BinaryFolder + "/network/")
2017-04-17 04:53:30 +00:00
2017-04-14 07:23:42 +00:00
2017-04-17 04:48:53 +00:00
b, _:=ioutil.ReadFile(cp + "config/example.json")
2017-04-14 07:23:42 +00:00
for i, p := range configPath {
pp := filepath.Join(p)
os.MkdirAll(pp, os.ModePerm)
if i % 2 == 0 {
ioutil.WriteFile(pp + "/id.json", b, os.ModePerm)
}
}
2017-04-17 04:48:53 +00:00
bbb, _:=ioutil.ReadFile(cp + "config/ttnc")
2017-04-14 07:23:42 +00:00
for _, p := range containerPath {
pp := filepath.Join(p)
os.MkdirAll(pp, os.ModePerm)
2017-04-17 04:48:53 +00:00
pp += "/"
pp += "/ttnc"
ioutil.WriteFile(pp, bbb, os.ModePerm)
2017-04-14 07:23:42 +00:00
}
}
func TestInit(t *testing.T) {
2017-04-15 11:10:33 +00:00
//InitContainer()
2017-04-14 07:23:42 +00:00
}
func TestDirs(t *testing.T) {
2017-04-15 11:10:33 +00:00
//cs := IsStartContainer()
//
//for _, c := range cs {
// var ccl []string
// ExistConfigFileDir(ConfigFolder, c, &ccl)
// t.Log(ccl)
//
//}
2017-04-14 07:23:42 +00:00
2017-04-14 08:19:02 +00:00
}
func TestSplitPath(t *testing.T) {
ff :="/home/snoop/develop/path/go/src/loafle.com/overflow/crawler_go/config/ffff"
a := filepath.Dir(ff)
t.Log(a)
a = filepath.Clean(a)
t.Log(a)
a = filepath.Base(a)
t.Log(a)
2017-04-14 07:23:42 +00:00
2017-04-15 11:10:33 +00:00
}
func TestInitCM(t *testing.T) {
GetInstance().init()
GetInstance().stopContainerAll()
}
2017-04-17 04:33:11 +00:00
func TestCallStatus(t *testing.T) {
2017-04-27 09:07:56 +00:00
//c := "java"
//GetInstance().runAndInitContainer(&c)
//
//callStatus(&c)
//
//GetInstance().stopContainer(&c)
2017-04-17 04:33:11 +00:00
}
2017-04-15 11:10:33 +00:00
func TestCallAdd(t *testing.T) {
2017-04-27 09:07:56 +00:00
////
//c := "java"
//cn := "HEALTH_"+"MYSQL"
//id := "id.json"
2017-04-15 11:10:33 +00:00
//
2017-04-27 09:07:56 +00:00
//GetInstance().runAndInitContainer(&c)
//
//callAdd(&c, &cn, &id)
//
//GetInstance().stopContainer(&c)
2017-04-15 11:10:33 +00:00
}
func TestCallRemove22(t *testing.T) {
2017-04-27 09:07:56 +00:00
//c := "java"
//cn := "HEALTH_"+"MYSQL"
//id := "id.json"
//
//GetInstance().runAndInitContainer(&c)
//
//callRemove(&c, &cn, &id)
//
//GetInstance().stopContainer(&c)
2017-04-15 11:10:33 +00:00
2017-04-15 11:16:14 +00:00
}
func TestRunRun(t *testing.T) {
c := "java"
GetInstance().runContainer(&c)
GetInstance().runContainer(&c)
GetInstance().runContainer(&c)
2017-04-15 11:27:40 +00:00
GetInstance().stopContainer(&c)
2017-04-15 11:16:14 +00:00
2017-04-15 11:36:15 +00:00
}
func TestActiveCrawler(t *testing.T) {
2017-04-27 09:07:56 +00:00
//c := "java"
//GetInstance().runContainer(&c)
//
//aa := GetInstance().activeCrawler(&c)
//
//t.Log(aa)
//
//GetInstance().stopContainer(&c)
2017-04-15 11:36:15 +00:00
}
func TestErer(t *testing.T) {
var asdf []string
asdf = nil
t.Log(len(asdf))
2017-04-17 04:48:53 +00:00
}
2017-04-28 04:44:56 +00:00
//
//func TestStartContainer(t *testing.T) {
//
// cs := GetStartContainer()
//
// t.Log(cs)
//
//}
//
//func GetStartContainer() []string {
//
// files, _ := ioutil.ReadDir(ConfigFolder)
//
// var cs []string
//
// for _,file := range files {
//
// if file.IsDir() {
// b := existConfigFile(ConfigFolder, file.Name())
// if b {
// cs = append(cs, file.Name())
// }
// }
//
// }
// return cs
//}
2017-04-17 04:48:53 +00:00
2017-04-28 04:44:56 +00:00
func TestJson(t *testing.T) {
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
c := config_manager.Config{}
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
c.Crawler.Name = "wmi_crawler"
c.Crawler.Container = "java_proxy";
c.Id = "WMI_TEST_ID_001"
b, _ := json.Marshal(c)
fmt.Println(string(b))
2017-04-27 09:07:56 +00:00
}
2017-04-28 04:44:56 +00:00
func TestJsonMa(t *testing.T) {
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
b, _ := ioutil.ReadFile("./test/example.json")
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
c := config_manager.Config{}
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
json.Unmarshal(b, &c)
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
t.Log(c)
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
}
func TestMock(t *testing.T) {
GetInstance().ConfigMgr = &ConfigManagerTest{}
GetInstance().init()
2017-04-27 09:07:56 +00:00
}
2017-04-28 04:44:56 +00:00
func TestRPC(t *testing.T) {
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
//c := conMgr.NewConfigManager()
//c.LoadGlobalConfigTTT("/home/snoop/develop/path/go/src/loafle.com/overflow/config_manager_go/test_agent/global.yaml")
//c.LoadCrawlerConfigAllTTT()
//
//observer.Notify(messages.CONFIGMANAGER_LOADED,c)
//
//
//wg := sync.WaitGroup{}
//
//wg.Add(1)
//
//go func() {
// time.Sleep(20 * time.Second)
// wg.Done()
//}()
//
//
//wg.Wait()
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
}
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
func TestGRPC(t *testing.T) {
b := callStatusAddress("localhost:50052")
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
t.Log(b)
}
2017-04-27 09:07:56 +00:00
2017-04-28 04:44:56 +00:00
type ConfigManagerTest struct {
2017-04-27 09:07:56 +00:00
}
2017-04-17 04:48:53 +00:00
2017-04-28 04:44:56 +00:00
func (c *ConfigManagerTest)GetGlobalConfig() *config_manager.GlobalConfig {
g := config_manager.GlobalConfig{}
rootFolder := "/home/cm2/"
g.Paths.ConfigFolder = rootFolder + "/config/container/"
g.Paths.BinaryFolder = rootFolder + "/container/"
g.Paths.PidFolder = rootFolder + "/pids/"
return &g
}
func (c *ConfigManagerTest)GetCrawlerById(id string) *config_manager.Config {
return nil
}
func (c *ConfigManagerTest)GetCrawlers() map[string] *config_manager.Config {
var m map[string] *config_manager.Config
b, _ := ioutil.ReadFile("./test/example.json")
ccc := config_manager.Config{}
json.Unmarshal(b, &ccc)
m["wmi_crawler"] = &ccc
return m
}