func NewAgent(config *Config, logger *pct.Logger, api pct.APIConnector, client pct.WebsocketClient, services map[string]pct.ServiceManager) *Agent { agent := &Agent{ config: config, api: api, configMux: &sync.RWMutex{}, logger: logger, client: client, services: services, updater: pct.NewUpdater(logger, api, pct.PublicKey, os.Args[0], VERSION), // -- status: pct.NewStatus([]string{"agent", "agent-cmd-handler"}), cmdChan: make(chan *proto.Cmd, CMD_QUEUE_SIZE), statusChan: make(chan *proto.Cmd, STATUS_QUEUE_SIZE), } return agent }
func (s *UpdateTestSuite) TestCheck(t *C) { // First make a very fake percona-agent "binary": just a file containing "A". // Later we'll check that the update process overwrites this with the updated binary. curBin := filepath.Join(s.tmpDir + "/percona-agent") if err := ioutil.WriteFile(curBin, []byte{0x41}, os.FileMode(0655)); err != nil { t.Fatal(err) } // Create an updater. Normally we'd pass in hard-coded pct.PublicKey and os.Args[0]. u := pct.NewUpdater(s.logger, s.api, s.pubKey, curBin, "1.0.0") t.Assert(u, NotNil) // Updater.Update() makes 2 API calls: first to get percona-agent-VERSION-ARCH.gz, // second to get the corresponding .sig file. The bin and sig here are "real" in // the sense that in SetUpSuite() we compiled a fake percona-agent bin and signed // it with the test private key (test/pct/key.pem). s.api.GetCode = []int{200, 200} s.api.GetData = [][]byte{s.bin, s.sig} s.api.GetError = []error{nil, nil} // Run the update process. It thinks it's getting percona-agent 1.0.1 from the real API. err := u.Update("1.0.1") t.Assert(err, IsNil) // The update process should have fetched and verified the test bin and sig, then // write the bin to disk and copied it over the very fake percona-agent "binary" // we created first. So the whole process looks like: // 1. Test writes percona-agent (very fake binary) // 2. Test compiles fake-percona-agent-1.0.1.go to fake-percona-agent-1.0.1 (bin) // 3. Test queues bytes of fake-percona-agent-go-1.0.1 (bin) and sig in fake API // 4. Updater gets those ^ bytes, verifies bin with sig // 5. Updater writes bin bytes as percona-agent-1.0.1-ARCH // 6. Updater runs "percona-agent-1.0.1-ARCH -version", expects output to be "1.0.1" // 7. Update does "mv percona-agent-1.0.1-ARCH percona-agent" // Therefore, fake-percona-agent-1.0.1 (bin) == percona-agent at the byte level. newBin, err := ioutil.ReadFile(curBin) t.Assert(err, IsNil) if bytes.Compare(s.bin, newBin) != 0 { t.Error("percona-agent binary != fake-percona-agent-1.0.1 binary") } // And if percona-agent is *really* fake-percona-agent-1.0.1, then we too should // get "1.0.1" if we run it with -version: out, err := exec.Command(curBin, "-version").Output() t.Assert(err, IsNil) t.Check(strings.TrimSpace(string(out)), Equals, "percona-agent 1.0.1 rev 19b6b2ede12bfd2a012d40ac572a660be7aff1e7") }