2020-01-30 14:42:51 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-01-31 15:03:26 +01:00
|
|
|
"bytes"
|
2020-01-31 12:29:28 +01:00
|
|
|
"encoding/json"
|
2020-01-30 14:42:51 +01:00
|
|
|
"errors"
|
|
|
|
"flag"
|
2020-01-31 12:29:28 +01:00
|
|
|
"fmt"
|
2020-01-30 14:42:51 +01:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2020-01-31 12:29:28 +01:00
|
|
|
"net/http"
|
2020-01-31 15:03:26 +01:00
|
|
|
"net/url"
|
2020-01-30 14:42:51 +01:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-01-31 08:18:38 +01:00
|
|
|
"path/filepath"
|
2020-01-30 14:42:51 +01:00
|
|
|
"strings"
|
|
|
|
"text/template"
|
|
|
|
|
2020-01-31 08:18:38 +01:00
|
|
|
"git.andreafazzi.eu/andrea/oef/client"
|
2020-01-30 14:42:51 +01:00
|
|
|
"git.andreafazzi.eu/andrea/oef/orm"
|
|
|
|
"github.com/gocarina/gocsv"
|
2020-01-31 12:29:28 +01:00
|
|
|
vegeta "github.com/tsenart/vegeta/lib"
|
2020-01-30 14:42:51 +01:00
|
|
|
)
|
|
|
|
|
2020-01-31 10:50:28 +01:00
|
|
|
type config struct {
|
2020-02-14 10:06:07 +01:00
|
|
|
Url string
|
|
|
|
SubscribePassword string
|
2020-01-31 10:50:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
type genFunc func(string, *config) error
|
2020-01-30 14:42:51 +01:00
|
|
|
|
|
|
|
var (
|
|
|
|
generators map[string]genFunc
|
|
|
|
templateDir string = "./templates"
|
|
|
|
)
|
|
|
|
|
|
|
|
func incr(value int) int {
|
|
|
|
return value + 1
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:06:07 +01:00
|
|
|
func genGETschoolSubscription(targetName string, conf *config) error {
|
|
|
|
token, err := client.GetToken(conf.Url, "", conf.SubscribePassword)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir("targets", 0777)
|
|
|
|
if errors.Is(err, &os.PathError{}) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
tmplPath := path.Join(templateDir, targetName+".tpl")
|
|
|
|
|
|
|
|
log.Printf("Parse %s template", tmplPath)
|
|
|
|
tmpl, err := template.New(filepath.Base(tmplPath)).Funcs(template.FuncMap{"incr": incr}).ParseFiles(tmplPath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output, err := os.Create("./targets/school_subscription.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
log.Printf("Execute %s template and generate targets...", targetName)
|
|
|
|
data := struct {
|
|
|
|
Token string
|
|
|
|
Config *config
|
|
|
|
}{token, conf}
|
|
|
|
|
|
|
|
err = tmpl.Execute(output, data)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-31 12:29:28 +01:00
|
|
|
func genPOSTResponseTargets(targetName string, conf *config) error {
|
|
|
|
var (
|
|
|
|
participants []*orm.Participant
|
|
|
|
tokens map[string]string
|
|
|
|
)
|
|
|
|
|
|
|
|
log.Println("Read participants.csv...")
|
|
|
|
|
|
|
|
input, err := ioutil.ReadFile("./testdata/participants.csv")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gocsv.Unmarshal(strings.NewReader(string(input)), &participants); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Obtaining tokens for each participants...")
|
|
|
|
tokens = make(map[string]string, 0)
|
|
|
|
for _, participant := range participants {
|
|
|
|
token, err := client.GetToken(conf.Url, participant.FiscalCode, participant.Password)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
tokens[participant.FiscalCode] = token
|
|
|
|
}
|
|
|
|
|
|
|
|
targets := make([]*vegeta.Target, 0)
|
2020-01-31 15:03:26 +01:00
|
|
|
form := url.Values{}
|
|
|
|
form.Set("Singleresponses.0", "32")
|
2020-01-31 12:49:33 +01:00
|
|
|
|
|
|
|
for i, participant := range participants {
|
2020-01-31 12:29:28 +01:00
|
|
|
targets = append(targets, &vegeta.Target{
|
|
|
|
Method: "POST",
|
2020-01-31 12:49:33 +01:00
|
|
|
URL: fmt.Sprintf("%s/responses/%d/update?format=html&tpl_content=responses_add_update&tpl_layout=base&update=true&login_session=%s", conf.Url, i+1, tokens[participant.FiscalCode]),
|
2020-01-31 15:03:26 +01:00
|
|
|
Body: []byte(form.Encode()),
|
2020-01-31 12:29:28 +01:00
|
|
|
Header: http.Header{"Content-Type": []string{"application/x-www-form-urlencoded"}},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
err = os.Mkdir("targets", 0777)
|
|
|
|
if errors.Is(err, &os.PathError{}) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
output, err := os.Create("./targets/post_response_targets.txt")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
for _, t := range targets {
|
2020-01-31 15:03:26 +01:00
|
|
|
var line string
|
|
|
|
buf := bytes.NewBufferString(line)
|
|
|
|
encoder := json.NewEncoder(buf)
|
|
|
|
encoder.SetEscapeHTML(false)
|
|
|
|
err := encoder.Encode(t)
|
|
|
|
// jsonData, err := json.Marshal(t)
|
2020-01-31 12:29:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-31 15:03:26 +01:00
|
|
|
_, err = output.WriteString(buf.String() + "\n\n")
|
2020-01-31 12:29:28 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func genGETResponseTargets(targetName string, conf *config) error {
|
2020-01-30 14:42:51 +01:00
|
|
|
var data struct {
|
|
|
|
Participants []*orm.Participant
|
|
|
|
Tokens map[string]string
|
2020-01-31 10:50:28 +01:00
|
|
|
Config *config
|
2020-01-30 14:42:51 +01:00
|
|
|
}
|
|
|
|
|
2020-01-31 10:50:28 +01:00
|
|
|
data.Config = conf
|
|
|
|
|
2020-01-30 14:42:51 +01:00
|
|
|
log.Println("Read participants.csv...")
|
|
|
|
|
2020-01-30 14:51:35 +01:00
|
|
|
input, err := ioutil.ReadFile("./testdata/participants.csv")
|
2020-01-30 14:42:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := gocsv.Unmarshal(strings.NewReader(string(input)), &data.Participants); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Println("Obtaining tokens for each participants...")
|
|
|
|
data.Tokens = make(map[string]string, 0)
|
2020-01-31 08:18:38 +01:00
|
|
|
for _, participant := range data.Participants {
|
2020-01-31 10:50:28 +01:00
|
|
|
token, err := client.GetToken(data.Config.Url, participant.FiscalCode, participant.Password)
|
2020-01-31 08:18:38 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
data.Tokens[participant.FiscalCode] = token
|
|
|
|
}
|
2020-01-30 14:42:51 +01:00
|
|
|
|
|
|
|
tmplPath := path.Join(templateDir, targetName+".tpl")
|
2020-01-30 14:51:35 +01:00
|
|
|
|
2020-01-30 14:42:51 +01:00
|
|
|
log.Printf("Parse %s template", tmplPath)
|
2020-01-31 08:18:38 +01:00
|
|
|
tmpl, err := template.New(filepath.Base(tmplPath)).Funcs(template.FuncMap{"incr": incr}).ParseFiles(tmplPath)
|
2020-01-30 14:42:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-31 08:18:38 +01:00
|
|
|
err = os.Mkdir("targets", 0777)
|
|
|
|
if errors.Is(err, &os.PathError{}) {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-30 14:51:35 +01:00
|
|
|
output, err := os.Create("./targets/response_targets.txt")
|
2020-01-30 14:42:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer output.Close()
|
|
|
|
|
|
|
|
log.Printf("Execute %s template and generate targets...", targetName)
|
2020-01-30 14:51:35 +01:00
|
|
|
err = tmpl.Execute(output, data)
|
2020-01-30 14:42:51 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
generators = map[string]genFunc{
|
2020-02-14 10:06:07 +01:00
|
|
|
"responses": genGETResponseTargets,
|
|
|
|
"post_responses": genPOSTResponseTargets,
|
|
|
|
"school_subscription": genGETschoolSubscription,
|
2020-01-30 14:42:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2020-01-31 12:49:33 +01:00
|
|
|
target := flag.String("targets", "responses", "Generate targets for participants responses.")
|
2020-01-31 10:50:28 +01:00
|
|
|
url := flag.String("url", "http://localhost:3000", "The URL of the host.")
|
2020-01-30 14:42:51 +01:00
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
fn, ok := generators[*target]
|
|
|
|
if !ok {
|
|
|
|
log.Fatal(errors.New("Unknown target"))
|
|
|
|
}
|
|
|
|
|
2020-02-14 10:06:07 +01:00
|
|
|
err := fn(*target, &config{Url: *url, SubscribePassword: "subscribe"})
|
2020-01-30 14:42:51 +01:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|