func TestMachineGroupFreshStart(t *testing.T) { st, stop, err := testBoltStorage() if err != nil { t.Fatalf("want err = nil; got %v", err) } defer stop() builder := testutil.NewBuilder(nil) g, err := New(testOptionsStorage(builder, st)) if err != nil { t.Fatalf("want err = nil; got %v", err) } defer g.Close() // Nothing should be added to addresses storage. address, err := addresses.NewCached(st) if err != nil { t.Fatalf("want err = nil; got %v", err) } if len(address.Registered()) != 0 { t.Errorf("want no registered machines; got %v", address.Registered()) } // Nothing should be added to aliases storage. alias, err := aliases.NewCached(st) if err != nil { t.Fatalf("want err = nil; got %v", err) } if len(alias.Registered()) != 0 { t.Errorf("want no registered machines; got %v", alias.Registered()) } }
// 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 }
func TestMachineGroupNoAliases(t *testing.T) { st, stop, err := testBoltStorage() if err != nil { t.Fatalf("want err = nil; got %v", err) } defer stop() // Add initial address. id := machine.ID("servA") address, err := addresses.NewCached(st) if err != nil { t.Fatalf("want err = nil; got %v", err) } if err := address.Add(id, testutil.TurnOnAddr()); err != nil { t.Fatalf("want err = nil; got %v", err) } if len(address.Registered()) != 1 { t.Errorf("want one registered machine; got %v", address.Registered()) } builder := testutil.NewBuilder(nil) g, err := New(testOptionsStorage(builder, st)) if err != nil { t.Fatalf("want err = nil; got %v", err) } defer g.Close() // Machine group should add alias for missing ID. alias, err := aliases.NewCached(st) if err != nil { t.Fatalf("want err = nil; got %v", err) } if len(alias.Registered()) != 1 { t.Errorf("want one registered machine; got %v", address.Registered()) } // Dynamic client should be started. if err := builder.WaitForBuild(time.Second); err != nil { t.Fatalf("want err = nil; got %v", err) } if builder.BuildsCount() != 1 { t.Errorf("want dynamic builds number = 1; got %d", builder.BuildsCount()) } }