diff --git a/main_test.go b/main_test.go index 07da112..662d87d 100644 --- a/main_test.go +++ b/main_test.go @@ -119,6 +119,31 @@ func TestIndex(t *testing.T) { if err != nil { t.Log(err) } + resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample", + url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"1"}, "addsamplename": {""}, "tubes[aurora_test]": {"1"}}) + if err != nil { + t.Log(err) + } + resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample", + url.Values{"tube": {"default"}, "addsamplejobid": {"1"}, "addsamplename": {"sample_1"}, "tubes[aurora_test]": {"1"}}) + if err != nil { + t.Log(err) + } + resp, err = http.PostForm(server+"/tube?server=not_exist_server_addr&action=addSample", + url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"1"}, "addsamplename": {"sample_2"}, "tubes[default]": {"1"}}) + if err != nil { + t.Log(err) + } + resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample", + url.Values{"tube": {"aurora_test"}, "addsamplejobid": {""}, "addsamplename": {"sample_2"}, "tubes[aurora_test]": {"1"}}) + if err != nil { + t.Log(err) + } + resp, err = http.PostForm(server+"/tube?server="+bstk+"&action=addSample", + url.Values{"tube": {"aurora_test"}, "addsamplejobid": {"not_int"}, "addsamplename": {"sample_2"}, "tubes[aurora_test]": {"1"}}) + if err != nil { + t.Log(err) + } for _, v := range urls { req, err := http.NewRequest("GET", server+v, nil) if err != nil { @@ -305,7 +330,7 @@ func TestPrettyJSON(t *testing.T) { func TestBase64Decode(t *testing.T) { once.Do(testSetup) base64Decode(`dGVzdA==`) - base64Decode(`test`) + base64Decode(`test-%?s`) } func TestRemoveServerInConfig(t *testing.T) { @@ -327,10 +352,40 @@ func TestAddSampleTube(t *testing.T) { once.Do(testSetup) addSampleTube(`aurora_test_2`, `test`) getSampleJobList() + getSampleJobNameByKey(`97ec882fd75855dfa1b4bd00d4a367d4`) + loadSample(``, `default`, `97ec882fd75855dfa1b4bd00d4a367d4`) loadSample(bstk, `default`, `97ec882fd75855dfa1b4bd00d4a367d4`) deleteSamples(`97ec882fd75855dfa1b4bd00d4a367d4`) } +func TestBasicAuth(t *testing.T) { + once.Do(testSetup) + var err error + var req *http.Request + var client = &http.Client{} + pubConf.Auth.Enabled = true + http.HandleFunc("/test", basicAuth(handlerMain)) + req, err = http.NewRequest("GET", server+"/test", nil) + if err != nil { + t.Log(err) + } + client = &http.Client{} + _, err = client.Do(req) + if err != nil { + t.Log(err) + } + req, err = http.NewRequest("GET", server+"/test", nil) + if err != nil { + t.Log(err) + } + req.SetBasicAuth(`admin`, `password`) + client = &http.Client{} + _, err = client.Do(req) + if err != nil { + t.Log(err) + } +} + func TestDeleteSamples(t *testing.T) { once.Do(testSetup) deleteSamples(``) diff --git a/statisticsUtils.go b/statisticsUtils.go index 666deee..94ffced 100644 --- a/statisticsUtils.go +++ b/statisticsUtils.go @@ -100,7 +100,6 @@ func statistic() { case <-notify: break NOTIFY case <-tick: - statisticsData.Lock() for k, v := range statisticsData.Server { for t := range v { if selfConf.StatisticsCollection == 0 { @@ -112,7 +111,6 @@ func statistic() { } } } - statisticsData.Unlock() } } } @@ -141,6 +139,7 @@ func statisticAgent(server string, tube string) error { if err != nil { continue } + statisticsData.Lock() _, ok := statisticsData.Server[server][tube][k] if !ok { statisticsData.Server[server][tube][k] = list.New() @@ -150,6 +149,7 @@ func statisticAgent(server string, tube string) error { statisticsData.Server[server][tube][k].Remove(front) } statisticsData.Server[server][tube][k].PushFront([]int{t.Year(), int(t.Month()), t.Day(), t.Hour(), t.Minute(), t.Second(), stats}) + statisticsData.Unlock() } } bstkConn.Close() @@ -160,7 +160,6 @@ func statisticAgent(server string, tube string) error { func statisticWaitress(server string, tube string) string { var buf, b, s, l bytes.Buffer b.WriteString(`{`) - statisticsData.Lock() for _, field := range statisticsFields { for k := range field { b.WriteString(`"`) @@ -186,7 +185,6 @@ func statisticWaitress(server string, tube string) string { b.WriteString(`],`) } } - statisticsData.Unlock() buf.WriteString(strings.TrimSuffix(b.String(), `,`)) buf.WriteString(`}`) return buf.String() diff --git a/structs.go b/structs.go index 25acc3a..a048fe4 100644 --- a/structs.go +++ b/structs.go @@ -15,74 +15,25 @@ const ( DefaultPriority = 1024 // most urgent: 0, least urgent: 4294967295. DefaultTTR = 60 // 1 minute DefaultTubePauseSeconds = 3600 - TplHead = ` - - - - - - - - - - - - - - - - - - - - - Beanstalk Console - - - - - - - ` - TplLinks = `` - TplNoScript = `` - UpdateURL = `https://api.github.com/repos/Luxurioust/aurora/tags` - Version = 1.5 + TplHead = `Beanstalk Console` + TplLinks = `` + TplNoScript = `` + UpdateURL = `https://api.github.com/repos/Luxurioust/aurora/tags` + Version = 1.5 ) // Define server and tube stats fields. var ( - ConfigFile = `.` + string(os.PathSeparator) + `aurora.toml` - // Stdout is the io.Writer to which executed commands write standard output. - Stdout io.Writer = os.Stdout - // Stderr is the io.Writer to which executed commands write standard error. - Stderr io.Writer = os.Stderr pubConf PubConfig - selfConf SelfConf sampleJobs SampleJobs - statisticsDataServer = make(map[string]map[string]map[string]*list.List) - statisticsData = StatisticsData{ - new(sync.RWMutex), - statisticsDataServer, - } - notify = make(chan bool, 1) - updateInfo = "uncheck" + selfConf SelfConf + Stderr io.Writer = os.Stderr // Stderr is the io.Writer to which executed commands write standard error. + Stdout io.Writer = os.Stdout // Stdout is the io.Writer to which executed commands write standard output. + ConfigFile = `.` + string(os.PathSeparator) + `aurora.toml` + statisticsData = StatisticsData{new(sync.RWMutex), statisticsDataServer} + statisticsDataServer = make(map[string]map[string]map[string]*list.List) + notify = make(chan bool, 1) + updateInfo = "uncheck" // Server filter columns. binlogStatsGroups = []map[string]string{ map[string]string{"binlog-current-index": "the index of the current binlog file being written to. If binlog is not active this value will be 0"},