// TestInitEngines tests whether multiple engines specified as a // single comma-separated list are parsed correctly. func TestInitEngines(t *testing.T) { tmp := createTempDirs(2, t) defer resetTestData(tmp) stores := fmt.Sprintf("mem=1000,mem:ddr3=1000,ssd=%s,hdd:7200rpm=%s", tmp[0], tmp[1]) expEngines := []struct { attrs storage.Attributes isMem bool }{ {storage.Attributes([]string{"mem"}), true}, {storage.Attributes([]string{"mem", "ddr3"}), true}, {storage.Attributes([]string{"ssd"}), false}, {storage.Attributes([]string{"hdd", "7200rpm"}), false}, } engines, err := initEngines(stores) if err != nil { t.Fatal(err) } if len(engines) != len(expEngines) { t.Errorf("number of engines parsed %d != expected %d", len(engines), len(expEngines)) } for i, engine := range engines { if engine.Attrs().SortedString() != expEngines[i].attrs.SortedString() { t.Errorf("wrong engine attributes, expected %v but got %v: %+v", expEngines[i].attrs, engine.Attrs(), expEngines[i]) } _, ok := engine.(*storage.InMem) if expEngines[i].isMem != ok { t.Errorf("expected in memory? %b, got %b: %+v", expEngines[i].isMem, ok, expEngines[i]) } } }
// parseAttributes parses a colon-separated list of strings, // filtering empty strings (i.e. ",," will yield no attributes. // Returns the list of strings as Attributes. func parseAttributes(attrsStr string) storage.Attributes { var filtered []string for _, attr := range strings.Split(attrsStr, ":") { if len(attr) != 0 { filtered = append(filtered, attr) } } sort.Strings(filtered) return storage.Attributes(filtered) }
// BootstrapConfigs sets default configurations for accounting, // permissions, and zones. All configs are specified for the empty key // prefix, meaning they apply to the entire database. Permissions are // granted to all users and the zone requires three replicas with no // other specifications. func BootstrapConfigs(db DB) error { // Accounting config. acctConfig := &storage.AcctConfig{} if err := PutI(db, storage.MakeKey(storage.KeyConfigAccountingPrefix, storage.KeyMin), acctConfig); err != nil { return err } // Permission config. permConfig := &storage.PermConfig{ Perms: []storage.Permission{ { Users: []string{""}, // all users Read: true, Write: true, Priority: 1.0, }, }, } if err := PutI(db, storage.MakeKey(storage.KeyConfigPermissionPrefix, storage.KeyMin), permConfig); err != nil { return err } // Zone config. // TODO(spencer): change this when zone specifications change to elect for three // replicas with no specific features set. zoneConfig := &storage.ZoneConfig{ Replicas: []storage.Attributes{ storage.Attributes([]string{"hdd"}), storage.Attributes([]string{"hdd"}), storage.Attributes([]string{"hdd"}), }, RangeMinBytes: 1048576, RangeMaxBytes: 67108864, } if err := PutI(db, storage.MakeKey(storage.KeyConfigZonePrefix, storage.KeyMin), zoneConfig); err != nil { return err } return nil }
// TestInitEngine tests whether the data directory string is parsed correctly. func TestInitEngine(t *testing.T) { tmp := createTempDirs(5, t) defer resetTestData(tmp) testCases := []struct { key string // data directory expAttrs storage.Attributes // attributes for engine wantError bool // do we expect an error from this key? isMem bool // is the engine in-memory? }{ {"mem=1000", storage.Attributes([]string{"mem"}), false, true}, {"ssd=1000", storage.Attributes([]string{"ssd"}), false, true}, {fmt.Sprintf("ssd=%s", tmp[0]), storage.Attributes([]string{"ssd"}), false, false}, {fmt.Sprintf("hdd=%s", tmp[1]), storage.Attributes([]string{"hdd"}), false, false}, {fmt.Sprintf("mem=%s", tmp[2]), storage.Attributes([]string{"mem"}), false, false}, {fmt.Sprintf("abc=%s", tmp[3]), storage.Attributes([]string{"abc"}), false, false}, {fmt.Sprintf("hdd:7200rpm=%s", tmp[4]), storage.Attributes([]string{"hdd", "7200rpm"}), false, false}, {"hdd=/dev/null", storage.Attributes{}, true, false}, {"", storage.Attributes{}, true, false}, {" ", storage.Attributes{}, true, false}, {"arbitrarystring", storage.Attributes{}, true, false}, {"mem=", storage.Attributes{}, true, false}, {"ssd=", storage.Attributes{}, true, false}, {"hdd=", storage.Attributes{}, true, false}, } for _, spec := range testCases { engines, err := initEngines(spec.key) if err == nil { if spec.wantError { t.Fatalf("invalid engine spec '%v' erroneously accepted: %+v", spec.key, spec) } if len(engines) != 1 { t.Fatalf("unexpected number of engines: %d: %+v", len(engines), spec) } engine := engines[0] if engine.Attrs().SortedString() != spec.expAttrs.SortedString() { t.Errorf("wrong engine attributes, expected %v but got %v: %+v", spec.expAttrs, engine.Attrs(), spec) } _, ok := engine.(*storage.InMem) if spec.isMem != ok { t.Errorf("expected in memory? %b, got %b: %+v", spec.isMem, ok, spec) } } else if !spec.wantError { t.Errorf("expected no error, got %v: %+v", err, spec) } } }