func (s *AgentTestSuite) TestStartServiceSlow(t *C) { // This test is like TestStartService but simulates a slow starting service. qanConfig := &qan.Config{ Interval: 60, // seconds MaxSlowLogSize: 1073741824, // 1 GiB RemoveOldSlowLogs: true, ExampleQueries: true, MaxWorkers: 2, WorkerRunTime: 120, // seconds } qanConfigData, _ := json.Marshal(qanConfig) serviceCmd := &proto.ServiceData{ Name: "qan", Config: qanConfigData, } serviceData, _ := json.Marshal(serviceCmd) now := time.Now() cmd := &proto.Cmd{ Ts: now, User: "******", Service: "agent", Cmd: "StartService", Data: serviceData, } // Send the cmd to the client, tell the agent to stop, then wait for it. s.sendChan <- cmd // No replies yet. gotReplies := test.WaitReply(s.recvChan) if len(gotReplies) != 0 { t.Fatal("No reply before StartService") } // Agent should be able to reply on status chan, indicating that it's // still starting the service. gotStatus := test.GetStatus(s.sendChan, s.recvChan) if !t.Check(gotStatus["agent"], Equals, "Idle") { test.Dump(gotStatus) } // Make it seem like service has started now. s.readyChan <- true // Agent sends reply: no error. gotReplies = test.WaitReply(s.recvChan) if len(gotReplies) == 0 { t.Fatal("Get reply") } if len(gotReplies) > 1 { t.Errorf("One reply, got %+v", gotReplies) } reply := &proto.Reply{} _ = json.Unmarshal(gotReplies[0].Data, reply) t.Check(reply.Error, Equals, "") }
func (s *AgentTestSuite) TestStartStopService(t *C) { // To start a service, first we make a config for the service: qanConfig := &qan.Config{ Interval: 60, // seconds MaxSlowLogSize: 1073741824, // 1 GiB RemoveOldSlowLogs: true, ExampleQueries: true, MaxWorkers: 2, WorkerRunTime: 120, // seconds } // Second, the service config is encoded and encapsulated in a ServiceData: qanConfigData, _ := json.Marshal(qanConfig) serviceCmd := &proto.ServiceData{ Name: "qan", Config: qanConfigData, } // Third and final, the service data is encoded and encapsulated in a Cmd: serviceData, _ := json.Marshal(serviceCmd) cmd := &proto.Cmd{ Ts: time.Now(), User: "******", Service: "agent", Cmd: "StartService", Data: serviceData, } // The readyChan is used by mock.MockServiceManager.Start() and Stop() // to simulate slow starts and stops. We're not testing that here, so // this lets the service start immediately. s.readyChan <- true // Send the StartService cmd to the client, then wait for the reply // which should not have an error, indicating success. s.sendChan <- cmd gotReplies := test.WaitReply(s.recvChan) if len(gotReplies) != 1 { t.Fatal("Got Reply to Cmd:StartService") } reply := &proto.Reply{} _ = json.Unmarshal(gotReplies[0].Data, reply) if reply.Error != "" { t.Error("No Reply.Error to Cmd:StartService; got ", reply.Error) } // To double-check that the agent started without error, get its status // which should show everything is "Ready" or "Idle". status := test.GetStatus(s.sendChan, s.recvChan) expectStatus := map[string]string{ "agent": "Idle", "qan": "Ready", "mm": "", } if same, diff := test.IsDeeply(status, expectStatus); !same { t.Error(diff) } // Finally, since we're using mock objects, let's double check the // execution trace, i.e. what calls the agent made based on all // the previous ^. got := test.WaitTrace(s.traceChan) expect := []string{ `Start qan`, `Status qan`, `Status mm`, } t.Check(got, DeepEquals, expect) /** * Stop the service. */ serviceCmd = &proto.ServiceData{ Name: "qan", } serviceData, _ = json.Marshal(serviceCmd) cmd = &proto.Cmd{ Ts: time.Now(), User: "******", Service: "agent", Cmd: "StopService", Data: serviceData, } // Let fake qan service stop immediately. s.readyChan <- true s.sendChan <- cmd gotReplies = test.WaitReply(s.recvChan) if len(gotReplies) != 1 { t.Fatal("Got Reply to Cmd:StopService") } reply = &proto.Reply{} _ = json.Unmarshal(gotReplies[0].Data, reply) if reply.Error != "" { t.Error("No Reply.Error to Cmd:StopService; got ", reply.Error) } status = test.GetStatus(s.sendChan, s.recvChan) t.Check(status["agent"], Equals, "Idle") t.Check(status["qan"], Equals, "Stopped") t.Check(status["mm"], Equals, "") }