Exemple #1
0
func TestCrosscheck(t *testing.T) {

	r := reporter.GetRoot("Test")

	ports := []string{"55550", "55551", "55553"}

	allRemotes := make([]string, len(ports), len(ports))

	for i, port := range ports {
		allRemotes[i] = "http://localhost:" + port
	}

	// Should all succeed, with one elected host
	anyGotElected := false
	w := sync.WaitGroup{}
	for _, port := range ports {
		w.Add(1)
		go func(port string) {
			elected := Crosscheck(&CXConfig{
				Bind:    ":" + port,
				Remotes: allRemotes,
			}, "asdf", r.Spawn("Child %s", port))
			if elected {
				anyGotElected = true
			}
			w.Done()
		}(port)
	}
	w.Wait()
	if !anyGotElected {
		t.Log("No host was elected")
		t.Fail()
	}
	r.DumpReport()

	allRemotes = append(allRemotes, "http://10.10.10.10:5000")
	w = sync.WaitGroup{}
	for _, port := range ports {
		w.Add(1)
		go func(port string) {
			elected := Crosscheck(&CXConfig{
				Bind:     ":" + port,
				Remotes:  allRemotes,
				MaxTries: 1,
			}, "asdf", r.Spawn("Child %s", port))
			if !elected {
				t.Log("Host not elected on fail")
				t.Fail()
			}
			w.Done()
		}(port)
	}
	w.Wait()
	r.DumpReport()
}
Exemple #2
0
func main() {
	flag.Parse()

	core, err := loadConfig(configDir)
	if err != nil {
		fmt.Fprintln(os.Stderr, err.Error())
		os.Exit(1)
		return
	}

	if !solo && core.Crosscheck != nil {

		configHash, err := crosscheck.XmlHash(core)
		if err != nil {
			log.Printf("Couldn't encode config: %s\n", err.Error())
		}
		log.Printf("Config %s\n", configHash)
		r := reporter.GetRoot("Crosscheck")
		elected := crosscheck.Crosscheck(core.Crosscheck, configHash, r)
		r.DumpReport()
		if !dryRun {
			core.DoWarnings(r, &core.Admins)
		}
		if !elected {
			return
		}
	}

	list := map[string][]objects.Group{}

	for _, group := range core.Groups {
		// Matches "", which is 'unspecified'
		if runGroup == "all" || runGroup == group.RunGroup {
			_, ok := list[group.SyncGroup]
			if !ok {
				list[group.SyncGroup] = []objects.Group{}
			}
			list[group.SyncGroup] = append(list[group.SyncGroup], group)
		}
	}

	times := map[string]int64{}
	wg := sync.WaitGroup{}
	for name, sg := range list {
		//fmt.Printf("Run sync %s - %d groups\n", name, len(sg))
		wg.Add(1)
		go func(name string, sg []objects.Group) {
			defer wg.Done()
			start := time.Now().Unix()
			defer func() { times[name] = time.Now().Unix() - start }()
			for _, group := range sg {
				r := reporter.GetRoot(group.Name)
				r.ID = group.GetHash()
				for _, check := range group.Checks {
					err := check.RunCheck(r)
					if err != nil {
						r.AddError(err)
					}
				}
				r.DumpReport()
				if !dryRun {
					core.DoWarnings(r, &group.Informants)
				}
			}

		}(name, sg)
	}
	wg.Wait()

	for name, seconds := range times {
		fmt.Printf("%s took %d seconds\n", name, seconds)
	}

}