Пример #1
0
func TestMinionLastseen(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-lastseen")
	defer tc.recorder.Stop()

	cfg := &minion.EtcdMinionConfig{
		Name:       "Kevin",
		EtcdConfig: tc.config,
	}
	m, err := minion.NewEtcdMinion(cfg)
	if err != nil {
		t.Fatal(err)
	}

	id := m.ID()
	var want int64 = 1450357761

	err = m.SetLastseen(want)
	if err != nil {
		t.Fatal(err)
	}

	got, err := tc.client.MinionLastseen(id)
	if err != nil {
		t.Fatal(err)
	}

	if want != got {
		t.Errorf("want %d lastseen, got %d lastseen", want, got)
	}
}
Пример #2
0
func TestMinionName(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-name")
	defer tc.recorder.Stop()

	wantName := "Kevin"
	cfg := &minion.EtcdMinionConfig{
		Name:       wantName,
		EtcdConfig: tc.config,
	}
	m, err := minion.NewEtcdMinion(cfg)
	if err != nil {
		t.Fatal(err)
	}

	minionID := m.ID()
	err = m.SetName(wantName)
	if err != nil {
		t.Fatal(err)
	}

	gotName, err := tc.client.MinionName(minionID)
	if err != nil {
		t.Fatal(err)
	}

	if wantName != gotName {
		t.Errorf("want %q, got %q", wantName, gotName)
	}
}
Пример #3
0
// Executes the "serve" command
func execServeCommand(c *cli.Context) {
	name, err := os.Hostname()
	if err != nil {
		displayError(err, 1)
	}

	nameFlag := c.String("name")
	if nameFlag != "" {
		name = nameFlag
	}

	cfg := etcdConfigFromFlags(c)
	m := minion.NewEtcdMinion(name, cfg)

	// Channel on which the shutdown signal is sent
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	// Start minion
	err = m.Serve()
	if err != nil {
		displayError(err, 1)
	}

	// Block until a shutdown signal is received
	<-quit
	m.Stop()
}
Пример #4
0
func TestMinionList(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-list")
	defer tc.recorder.Stop()

	minionNames := []string{
		"Bob", "Kevin", "Stuart",
	}

	wantMinions := []uuid.UUID{
		uuid.Parse("f827bffd-bd9e-5441-be36-a92a51d0b79e"), // Bob
		uuid.Parse("46ce0385-0e2b-5ede-8279-9cd98c268170"), // Kevin
		uuid.Parse("f87cf58e-1e19-57e1-bed3-9dff5064b86a"), // Stuart
	}

	// Convert minion uuids as strings for
	// sorting and equality testing
	var wantMinionsAsString []string
	for _, m := range wantMinions {
		wantMinionsAsString = append(wantMinionsAsString, m.String())
	}
	sort.Strings(wantMinionsAsString)

	// Register our minions in etcd
	for _, name := range minionNames {
		cfg := &minion.EtcdMinionConfig{
			Name:       name,
			EtcdConfig: tc.config,
		}
		m, err := minion.NewEtcdMinion(cfg)
		if err != nil {
			t.Fatal(err)
		}

		err = m.SetName(name)
		if err != nil {
			t.Error(err)
		}
	}

	// Get minions from etcd
	gotMinions, err := tc.client.MinionList()
	if err != nil {
		t.Fatal(err)
	}

	// Convert retrieved minion uuids as string for
	// sorting and equality testing
	var gotMinionsAsString []string
	for _, m := range gotMinions {
		gotMinionsAsString = append(gotMinionsAsString, m.String())
	}
	sort.Strings(gotMinionsAsString)

	if !reflect.DeepEqual(wantMinionsAsString, gotMinionsAsString) {
		t.Errorf("want %q minions, got %q minions", wantMinions, gotMinions)
	}
}
Пример #5
0
// Executes the "serve" command
func execServeCommand(c *cli.Context) error {
	name, err := os.Hostname()
	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	concurrency := c.Int("concurrency")
	if concurrency < 0 {
		concurrency = runtime.NumCPU()
	}

	if c.String("siterepo") == "" {
		return cli.NewExitError(errNoSiteRepo.Error(), 64)
	}

	nameFlag := c.String("name")
	if nameFlag != "" {
		name = nameFlag
	}

	etcdCfg := etcdConfigFromFlags(c)
	minionCfg := &minion.EtcdMinionConfig{
		Concurrency: concurrency,
		Name:        name,
		SiteRepo:    c.String("siterepo"),
		EtcdConfig:  etcdCfg,
	}

	m, err := minion.NewEtcdMinion(minionCfg)
	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	// Channel on which the shutdown signal is sent
	quit := make(chan os.Signal, 1)
	signal.Notify(quit, syscall.SIGINT, syscall.SIGTERM)

	// Start minion
	err = m.Serve()
	if err != nil {
		return cli.NewExitError(err.Error(), 1)
	}

	// Block until a shutdown signal is received
	<-quit
	m.Stop()

	return nil
}
Пример #6
0
func TestMinionTaskBacklog(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-task-backlog")
	defer tc.recorder.Stop()

	// Setup our minion
	minionName := "Kevin"
	cfg := &minion.EtcdMinionConfig{
		Name:       minionName,
		EtcdConfig: tc.config,
	}

	testMinion, err := minion.NewEtcdMinion(cfg)
	if err != nil {
		t.Fatal(err)
	}

	minionID := testMinion.ID()

	err = testMinion.SetName(minionName)
	if err != nil {
		t.Fatal(err)
	}

	// Create a test task and submit it
	wantTask := task.New("foo", "bar")
	wantTask.ID = uuid.Parse("e6d2bebd-2219-4a8c-9d30-a861097c147e")

	err = tc.client.MinionSubmitTask(minionID, wantTask)
	if err != nil {
		t.Fatal(err)
	}

	// Get pending tasks and verify the task we sent is the task we get
	backlog, err := tc.client.MinionTaskQueue(minionID)
	if err != nil {
		t.Fatal(err)
	}

	if len(backlog) != 1 {
		t.Errorf("want 1 backlog task, got %d backlog tasks", len(backlog))
	}

	gotTask := backlog[0]
	if !reflect.DeepEqual(wantTask, gotTask) {
		t.Errorf("want %q task, got %q task", wantTask.ID, gotTask.ID)
	}
}
Пример #7
0
func TestMinionName(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-name")
	defer tc.recorder.Stop()

	wantName := "Kevin"
	m := minion.NewEtcdMinion(wantName, tc.config)
	minionId := m.ID()
	err := m.SetName(wantName)
	if err != nil {
		t.Fatal(err)
	}

	gotName, err := tc.client.MinionName(minionId)
	if err != nil {
		t.Fatal(err)
	}

	if wantName != gotName {
		t.Errorf("want %q, got %q", wantName, gotName)
	}
}
Пример #8
0
func TestMinionClassifiers(t *testing.T) {
	tc := mustNewTestClient("fixtures/minion-classifier")
	defer tc.recorder.Stop()

	// Classifiers to test
	wantClassifierKeys := make([]string, 0)
	testClassifiers := []*classifier.Classifier{
		&classifier.Classifier{
			Key:   "foo",
			Value: "bar",
		},
		&classifier.Classifier{
			Key:   "baz",
			Value: "qux",
		},
	}

	m := minion.NewEtcdMinion("Kevin", tc.config)
	minionId := m.ID()

	// Set minion classifiers
	for _, c := range testClassifiers {
		err := m.SetClassifier(c)
		if err != nil {
			t.Error(err)
		}
		wantClassifierKeys = append(wantClassifierKeys, c.Key)
	}
	sort.Strings(wantClassifierKeys)

	// Get classifiers keys from etcd
	gotClassifierKeys, err := tc.client.MinionClassifierKeys(minionId)

	if err != nil {
		t.Fatal(err)
	}

	sort.Strings(gotClassifierKeys)
	if !reflect.DeepEqual(wantClassifierKeys, gotClassifierKeys) {
		t.Errorf("want %q classifier keys, got %q classifier keys", wantClassifierKeys, gotClassifierKeys)
	}

	// Get classifier values
	for _, c := range testClassifiers {
		klassifier, err := tc.client.MinionClassifier(minionId, c.Key)
		if err != nil {
			t.Fatal(err)
		}

		if c.Value != klassifier.Value {
			t.Errorf("want %q classifier value, got %q classifier value", c.Value, klassifier.Value)
		}
	}

	// Get minions which contain given classifier key
	for _, c := range testClassifiers {
		minions, err := tc.client.MinionWithClassifierKey(c.Key)
		if err != nil {
			t.Fatal(err)
		}

		// We expect a single minion with the test classifier keys
		if len(minions) != 1 {
			t.Errorf("want 1 minion, got %d minion(s)", len(minions))
		}
	}
}