Vajrayana Buddhism relies on a teacher-pupil relationship. What in tradition or the literature explains how to find the appropriate teacher? Vajrayana Buddhism is esoteric, in the sense that the transmission of certain teachings only occurs directly from teacher to student during an initiation or empowerment and cannot be simply learned from a book. Many techniques are also commonly said to be secret, but some Vajrayana teachers have responded that secrecy itself is not important and only a side-effect of the reality that the techniques have no validity outside the teacher-student lineage.
  (Source: http://en.wikipedia.org/wiki/Vajrayana#Esoteric_transmission)


But in my travels through the Buddhist landscape, I find that teachers are human: some good, some mediocre.  How do I find the right teacher?  Are there tests that the student can apply to the teacher?
"The Words of My Perfect Teacher" by Patrul Rinpoche has a chapter (Ch. 6) on how to choose, test and follow a "spiritual friend" (teacher):


pure, never having contravened any of the commitments or prohibitions (Pratimoksha, Bodhisattva's Vow, Secret Vows of Secret Mantrayana)
unstained by negative emotions
learned, not lacking in knowledge of sutras, tantras and shastras
heart suffused with compassion, loves each being like his only child
well-versed in vast array of rituals and ceremonies
not merely verbal but by putting the meaning of teaching into practice has actualized in himself the achievements of riddance and realization
generous, completely free of attachments
language should be pleasant
teaches each according to person's needs
acts in accordance with what he teaches


Teachers to avoid:


Like a millstone made of wool (high lamas by birth but have not studied and practiced seriously enough for the above qualities to arise)
Like the frog that lived in a well (with very limited exposure to true depth and breadth of Practice, having never seen a genuine spiritual master)
Mad guides (loose in discipline, strong in emotions, overemphasizing the esoteric and transcendental)
Blind guides (don't know what they are talking about)


"Treasury of Precious Qualities" by Jigme Lingpa has several pages (in Ch.5) on fully qualified masters vs. false teachers:

(repeats most of the above and adds the following)


Good teacher must have few activities, exclusively preoccupied with Dharma
His presence has transforming effect, inspiring all who meet him to seek for liberation
Must have an unconfined view, having realized the equality of nirvana and samsara
Having direct experience of the above, he is the source of his own freedom
Has been ripened by empowerments that came down to him in an uninterrupted lineage.