github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/pkg/registrar/registrar_test.go (about) 1 package registrar 2 3 import ( 4 "reflect" 5 "testing" 6 ) 7 8 func TestReserve(t *testing.T) { 9 r := NewRegistrar() 10 11 obj := "test1" 12 if err := r.Reserve("test", obj); err != nil { 13 t.Fatal(err) 14 } 15 16 if err := r.Reserve("test", obj); err != nil { 17 t.Fatal(err) 18 } 19 20 obj2 := "test2" 21 err := r.Reserve("test", obj2) 22 if err == nil { 23 t.Fatalf("expected error when reserving an already reserved name to another object") 24 } 25 if err != ErrNameReserved { 26 t.Fatal("expected `ErrNameReserved` error when attempting to reserve an already reserved name") 27 } 28 } 29 30 func TestRelease(t *testing.T) { 31 r := NewRegistrar() 32 obj := "testing" 33 34 if err := r.Reserve("test", obj); err != nil { 35 t.Fatal(err) 36 } 37 r.Release("test") 38 r.Release("test") // Ensure there is no panic here 39 40 if err := r.Reserve("test", obj); err != nil { 41 t.Fatal(err) 42 } 43 } 44 45 func TestGetNames(t *testing.T) { 46 r := NewRegistrar() 47 obj := "testing" 48 names := []string{"test1", "test2"} 49 50 for _, name := range names { 51 if err := r.Reserve(name, obj); err != nil { 52 t.Fatal(err) 53 } 54 } 55 r.Reserve("test3", "other") 56 57 names2, err := r.GetNames(obj) 58 if err != nil { 59 t.Fatal(err) 60 } 61 62 if !reflect.DeepEqual(names, names2) { 63 t.Fatalf("Exepected: %v, Got: %v", names, names2) 64 } 65 } 66 67 func TestDelete(t *testing.T) { 68 r := NewRegistrar() 69 obj := "testing" 70 names := []string{"test1", "test2"} 71 for _, name := range names { 72 if err := r.Reserve(name, obj); err != nil { 73 t.Fatal(err) 74 } 75 } 76 77 r.Reserve("test3", "other") 78 r.Delete(obj) 79 80 _, err := r.GetNames(obj) 81 if err == nil { 82 t.Fatal("expected error getting names for deleted key") 83 } 84 85 if err != ErrNoSuchKey { 86 t.Fatal("expected `ErrNoSuchKey`") 87 } 88 } 89 90 func TestGet(t *testing.T) { 91 r := NewRegistrar() 92 obj := "testing" 93 name := "test" 94 95 _, err := r.Get(name) 96 if err == nil { 97 t.Fatal("expected error when key does not exist") 98 } 99 if err != ErrNameNotReserved { 100 t.Fatal(err) 101 } 102 103 if err := r.Reserve(name, obj); err != nil { 104 t.Fatal(err) 105 } 106 107 if _, err = r.Get(name); err != nil { 108 t.Fatal(err) 109 } 110 111 r.Delete(obj) 112 _, err = r.Get(name) 113 if err == nil { 114 t.Fatal("expected error when key does not exist") 115 } 116 if err != ErrNameNotReserved { 117 t.Fatal(err) 118 } 119 }