Example #1
0
func TestInitForeman(t *testing.T) {
	conn := startTestWithRole(pb.MinionConfig_WORKER)
	conn.Transact(func(view db.Database) error {
		m := view.InsertMachine()
		m.PublicIP = "2.2.2.2"
		m.PrivateIP = "2.2.2.2"
		m.CloudID = "ID2"
		view.Commit(m)
		return nil
	})

	Init(conn)
	for _, m := range minions {
		assert.Equal(t, db.Role(db.Worker), m.machine.Role)
	}

	conn = startTestWithRole(pb.MinionConfig_Role(-7))
	Init(conn)
	for _, m := range minions {
		assert.Equal(t, db.None, m.machine.Role)
	}
}
Example #2
0
func TestConfigConsistency(t *testing.T) {
	masterRole := db.RoleToPB(db.Master)
	workerRole := db.RoleToPB(db.Worker)

	conn, clients := startTest()
	var master, worker db.Machine
	conn.Transact(func(view db.Database) error {
		master = view.InsertMachine()
		master.PublicIP = "1.1.1.1"
		master.PrivateIP = master.PublicIP
		master.CloudID = "ID1"
		view.Commit(master)
		worker = view.InsertMachine()
		worker.PublicIP = "2.2.2.2"
		worker.PrivateIP = worker.PublicIP
		worker.CloudID = "ID2"
		view.Commit(worker)
		return nil
	})

	Init(conn)
	conn.Transact(func(view db.Database) error {
		master.Role = db.Master
		worker.Role = db.Worker
		view.Commit(master)
		view.Commit(worker)
		return nil
	})

	RunOnce(conn)
	checkRoles := func() {
		r := minions["1.1.1.1"].client.(*fakeClient).mc.Role
		assert.Equal(t, masterRole, r)

		r = minions["2.2.2.2"].client.(*fakeClient).mc.Role
		assert.Equal(t, workerRole, r)
	}
	checkRoles()

	minions = map[string]*minion{}

	// Insert the clients into the client list to simulate fetching
	// from the remote cluster
	clients.clients["1.1.1.1"] = &fakeClient{clients, "1.1.1.1",
		pb.MinionConfig{Role: masterRole}}
	clients.clients["2.2.2.2"] = &fakeClient{clients, "2.2.2.2",
		pb.MinionConfig{Role: workerRole}}

	Init(conn)
	RunOnce(conn)
	checkRoles()

	// After many runs, the roles should never change
	for i := 0; i < 25; i++ {
		RunOnce(conn)
	}
	checkRoles()

	// Ensure that the DB machines have the correct roles as well.
	conn.Transact(func(view db.Database) error {
		machines := view.SelectFromMachine(nil)
		for _, m := range machines {
			if m.PublicIP == "1.1.1.1" {
				assert.Equal(t, db.Role(db.Master), m.Role)
			}
			if m.PublicIP == "2.2.2.2" {
				assert.Equal(t, db.Role(db.Worker), m.Role)
			}
		}
		return nil
	})
}