Exemple #1
0
// New creates a new Group object.
func New(opts *GroupOpts) (*Group, error) {
	if err := opts.Valid(); err != nil {
		return nil, err
	}

	g := &Group{}

	// Add logger to group.
	if opts.Log != nil {
		g.log = opts.Log.New("machines")
	} else {
		g.log = machine.DefaultLogger.New("machines")
	}

	// Use default discover client when not set.
	if opts.Discover != nil {
		g.discover = opts.Discover
	} else {
		g.discover = discover.NewClient()
	}

	// Create dynamic clients.
	var err error
	g.client, err = clients.New(&clients.ClientsOpts{
		Builder:         opts.Builder,
		DynAddrInterval: opts.DynAddrInterval,
		PingInterval:    opts.PingInterval,
		Log:             g.log,
	})
	if err != nil {
		g.log.Critical("Cannot create machine monitor: %s", err)
		return nil, err
	}

	// Add default components.
	g.address = addresses.New()
	g.alias = aliases.New()
	if opts.Storage == nil {
		return g, nil
	}

	// Try to add storage for Addresses.
	if address, err := addresses.NewCached(opts.Storage); err != nil {
		g.log.Warning("Cannot load addresses cache: %s", err)
	} else {
		g.address = address
	}

	// Try to add storage for Aliases.
	if alias, err := aliases.NewCached(opts.Storage); err != nil {
		g.log.Warning("Cannot load aliases cache: %s", err)
	} else {
		g.alias = alias
	}

	// Start memory workers.
	g.bootstrap()

	return g, nil
}
Exemple #2
0
func TestAliasesMachineID(t *testing.T) {
	m := map[machine.ID]string{
		"ID_1": "blue_banana",
		"ID_2": "red_orange1",
		"ID_3": "white_mango",
		"ID_4": "black_tomato2",
		"ID_5": "silver_kiwi",
	}

	as := aliases.New()
	for id, alias := range m {
		if err := as.Add(id, alias); err != nil {
			t.Fatalf("want err = nil; got %v", err)
		}
	}

	tests := map[string]struct {
		Alias    string
		Expected machine.ID
		NotFound bool
	}{
		"full alias": {
			Alias:    "white_mango",
			Expected: machine.ID("ID_3"),
			NotFound: false,
		},
		"machine id": {
			Alias:    "ID_5",
			Expected: machine.ID("ID_5"),
			NotFound: false,
		},
		"unknown": {
			Alias:    "green_kiwi",
			Expected: machine.ID(""),
			NotFound: true,
		},
	}

	for name, test := range tests {
		t.Run(name, func(t *testing.T) {
			id, err := as.MachineID(test.Alias)
			if (err == machine.ErrMachineNotFound) != test.NotFound {
				t.Fatalf("want err machine not found = %t; got %v", test.NotFound, err)
			}

			if id != test.Expected {
				t.Fatalf("want id = %v; got %v", test.Expected, id)
			}
		})
	}
}
Exemple #3
0
func TestAliasesCreate(t *testing.T) {
	const wantAliases = 500
	m := make(map[string]struct{})

	as := aliases.New()
	for i := 0; i < wantAliases; i++ {
		alias, err := as.Create(machine.ID(i))
		if err != nil {
			t.Fatalf("want err = nil; got %v", err)
		}
		m[alias] = struct{}{}

		// Second creation for the same ID should be no-op.
		alias, err = as.Create(machine.ID(i))
		if err != nil {
			t.Fatalf("want err = nil; got %v", err)
		}
		m[alias] = struct{}{}
	}

	if len(m) != wantAliases {
		t.Fatalf("want %d aliases; got %d", wantAliases, len(m))
	}
}