Rumors of a South African-backed coup swept the small black-ruled country of Lesotho today as troops surrounded the offices of Prime Minister Leabua Jonathan for five hours. But a statement by the Jonathan government later described the operation as an exercise to test security alertness.

The wave of alarm it caused, however, reflected the tensions that have been generated in the little country by a border-crossing slowdown that South Africa has imposed on it for the past two weeks.

As fear of a South African incursion gripped the independent nation, which is totally surrounded by South Africa, many people in Maseru, the capital, closed their shops and offices and barricaded themselves in their homes, sources there said in telephone interviews tonight.

In the past two years there have been two commando raids on Maseru from across the South African border, in which 51 people have been killed.

The most recent raid was on Dec. 24. People in Maseru say that particularly since the border slowdown started on Jan. 1, residents feel they are living in the shadow of an ever-present threat from South Africa.

South Africa has been slowing traffic at all border crossings to a few vehicles a day, choking vital supplies to a point where Lesotho is running out of fresh food and is so short of fuel that yesterday it banned gasoline sales to private motorists.

The purpose of the border delays appears to be to pressure Lesotho into agreeing not to give sanctuary to black activists who flee from South Africa.

Lesotho appealed to the world community yesterday to organize an emergency airlift.