Ejemplo n.º 1
0
// NewMachine initializes a new Machine struct with any internal vars created.
func NewMachine(meta MachineMeta, log logging.Logger, t Transport) (*Machine, error) {
	log = MachineLogger(meta, log)

	// Create our Pingers, to be used in the PingTrackers
	kitePinger := kitepinger.NewKitePinger(t)
	httpPinger, err := kitepinger.NewKiteHTTPPinger(meta.URL)
	if err != nil {
		log.Error(
			"Unable to create HTTPPinger from meta.URL. url:%s, err:%s", meta.URL, err,
		)
		return nil, err
	}

	m := &Machine{
		MachineMeta: meta,
		Log:         log,
		KiteTracker: kitepinger.NewPingTracker(kitePinger),
		HTTPTracker: kitepinger.NewPingTracker(httpPinger),
		Transport:   t,
		discover:    discover.NewClient(),
		mountLocker: util.NewMutexWithState(),
	}

	m.discover.Log = m.Log.New("discover")

	// Start our http pinger, to give online/offline statuses for all machines.
	m.HTTPTracker.Start()

	return m, nil
}
Ejemplo n.º 2
0
// InitHTTPTracker creates the HTTPTracker for this Machine, if it is
// currently nil.
//
// This allows a caller to simply call this method beforehand on any machine,
// valid or not, and access the online/offline status of the remote kite.
func (m *Machine) InitHTTPTracker() error {
	if m.HasHTTPTracker() {
		return nil
	}

	// If the URL is empty, don't bother - nothing we can do.
	if m.URL == "" {
		return errors.New("Unable to Init HTTPTracker, Machine.URL is empty.")
	}

	httpPinger, err := kitepinger.NewKiteHTTPPinger(m.URL)
	if err != nil {
		return err
	}

	m.HTTPTracker = kitepinger.NewPingTracker(httpPinger)

	return nil
}
Ejemplo n.º 3
0
func TestGetMachinesWithoutCache(t *testing.T) {
	Convey("Given a Remote", t, func() {
		kg := newMockKiteGetter()
		store := storage.NewMemoryStorage()
		r := &Remote{
			localKite: &kite.Kite{
				Id: "test id",
				Config: &config.Config{
					Username: "******",
				},
			},
			kitesGetter:       kg,
			log:               discardLogger,
			machines:          machine.NewMachines(discardLogger, store),
			machinesCacheMax:  1 * time.Second,
			machineNamesCache: map[string]string{},
			storage:           store,
		}
		// Sanity check our count.
		So(r.machines.Count(), ShouldEqual, 0)

		Convey("Given a new machine", func() {
			kg.AddByUrl("http://testhost1:56789")

			// Sanity check our config
			So(kg.Clients[0].Reconnect, ShouldBeFalse)
			Convey("It should configure the kite Client", func() {
				r.GetMachinesWithoutCache()
				So(kg.Clients[0].Reconnect, ShouldBeTrue)
			})

			Convey("It should add the new machine", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				So(machines, ShouldNotBeNil)
				So(machines.Count(), ShouldEqual, 1)
			})

			Convey("It should create the HTTPTracker", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				So(machines, ShouldNotBeNil)

				mach := machines.Machines()[0]
				So(mach, ShouldNotBeNil)
				So(mach.HTTPTracker, ShouldNotBeNil)

				Convey("It should start the HTTPTracker", func() {
					So(mach.HTTPTracker.IsPinging(), ShouldBeTrue)
				})
			})
		})

		Convey("Given a loaded but not yet valid machine", func() {
			kg.AddByUrl("http://testhost1:56789")
			// Add a machine, with just enough info to serve our needs
			r.machines.Add(&machine.Machine{
				MachineMeta: machine.MachineMeta{
					IP: "testhost1",
				},
			})

			// Sanity check our config
			So(kg.Clients[0].Reconnect, ShouldBeFalse)
			Convey("It should configure the kite Client", func() {
				r.GetMachinesWithoutCache()
				So(kg.Clients[0].Reconnect, ShouldBeTrue)
			})

			Convey("It should not add a new machine for the kite", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				So(machines, ShouldNotBeNil)
				So(machines.Count(), ShouldEqual, 1)
			})

			Convey("It should create the HTTPTracker", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				So(machines, ShouldNotBeNil)

				mach := machines.Machines()[0]
				So(mach, ShouldNotBeNil)
				So(mach.HTTPTracker, ShouldNotBeNil)

				Convey("It should start the HTTPTracker", func() {
					So(mach.HTTPTracker.IsPinging(), ShouldBeTrue)
				})
			})
		})

		Convey("Given an pre existing valid machine", func() {
			kg.AddByUrl("http://testhost1:56789")
			// Add a machine, with just enough info to serve our needs
			validMachine := &machine.Machine{
				MachineMeta: machine.MachineMeta{
					IP: "testhost1",
				},
				Log:         testutil.DiscardLogger,
				KiteTracker: kitepinger.NewPingTracker(nil), // Invalid ping trackers, but
				HTTPTracker: kitepinger.NewPingTracker(nil), // okay for this test currently
				// a bit of a trick, Machine implements Transport, so we're using an empty
				// machine here to implement transport, and thus make the validMachine instance
				// "valid". Both this and the pingtrackers above are not usable, but satisfy
				// the checkvalid requirements.
				Transport: &machine.Machine{},
			}
			So(r.machines.Add(validMachine), ShouldBeNil)
			// sanity check, to make sure it's valid
			So(validMachine.CheckValid(), ShouldBeNil)

			Convey("It should be the same machine instance", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				returnedMachine := machines.Machines()[0]
				So(returnedMachine, ShouldEqual, validMachine)
			})

			Convey("It should only return 1 machine", func() {
				machines, err := r.GetMachinesWithoutCache()
				So(err, ShouldBeNil)
				So(machines.Count(), ShouldEqual, 1)
			})

			Convey("With a label that does not match the local label", func() {
				kg.Clients[0].MachineLabel = "foobarbaz"
				// Sanity check
				So(kg.Clients[0].MachineLabel, ShouldNotEqual, validMachine.MachineLabel)

				Convey("It should update the machine label", func() {
					machines, err := r.GetMachinesWithoutCache()
					So(err, ShouldBeNil)
					returnedMachine := machines.Machines()[0]
					So(returnedMachine.MachineLabel, ShouldEqual, validMachine.MachineLabel)
				})
			})
		})
	})
}