Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
fix: Rename from inmemory store to memory store
Browse files Browse the repository at this point in the history
Signed-off-by: Uanid <uanid@outlook.com>
  • Loading branch information
uanid committed Sep 8, 2023
1 parent 488f9f4 commit 8628b66
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 43 deletions.
16 changes: 8 additions & 8 deletions inmemory_store.go → memory_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import (
"oras.land/oras-go/v2/registry/remote/auth"
)

// InMemoryStore is a store that keeps credentials in memory.
type InMemoryStore struct {
// MemoryStore is a store that keeps credentials in memory.
type MemoryStore struct {
store sync.Map
}

// NewInMemoryStore creates a new in-memory credentials store.
func NewInMemoryStore() *InMemoryStore {
return &InMemoryStore{}
// NewMemoryStore creates a new in-memory credentials store.
func NewMemoryStore() *MemoryStore {
return &MemoryStore{}
}

// Get retrieves credentials from the store for the given server address.
func (is *InMemoryStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) {
func (is *MemoryStore) Get(_ context.Context, serverAddress string) (auth.Credential, error) {
cred, found := is.store.Load(serverAddress)
if !found {
return auth.EmptyCredential, nil
Expand All @@ -42,13 +42,13 @@ func (is *InMemoryStore) Get(_ context.Context, serverAddress string) (auth.Cred
}

// Put saves credentials into the store for the given server address.
func (is *InMemoryStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error {
func (is *MemoryStore) Put(_ context.Context, serverAddress string, cred auth.Credential) error {
is.store.Store(serverAddress, cred)
return nil
}

// Delete removes credentials from the store for the given server address.
func (is *InMemoryStore) Delete(_ context.Context, serverAddress string) error {
func (is *MemoryStore) Delete(_ context.Context, serverAddress string) error {
is.store.Delete(serverAddress)
return nil
}
70 changes: 35 additions & 35 deletions inmemory_store_test.go → memory_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,24 +23,24 @@ import (
"oras.land/oras-go/v2/registry/remote/auth"
)

func TestInMemoryStore_Get_notExistRecord(t *testing.T) {
func TestMemoryStore_Get_notExistRecord(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

serverAddress := "registry.example.com"
got, err := is.Get(ctx, serverAddress)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got, auth.EmptyCredential) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got, auth.EmptyCredential)
t.Errorf("MemoryStore.Get() = %v, want %v", got, auth.EmptyCredential)
}
}

func TestInMemoryStore_Get_validRecord(t *testing.T) {
func TestMemoryStore_Get_validRecord(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

serverAddress := "registry.example.com"
want := auth.Credential{
Expand All @@ -53,17 +53,17 @@ func TestInMemoryStore_Get_validRecord(t *testing.T) {

got, err := is.Get(ctx, serverAddress)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got, want) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got, want)
t.Errorf("MemoryStore.Get() = %v, want %v", got, want)
}
}

func TestInMemoryStore_Put_addNew(t *testing.T) {
func TestMemoryStore_Put_addNew(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

// Test Put
server1 := "registry.example.com"
Expand All @@ -74,7 +74,7 @@ func TestInMemoryStore_Put_addNew(t *testing.T) {
AccessToken: "registry_token",
}
if err := is.Put(ctx, server1, cred1); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

Expand All @@ -86,35 +86,35 @@ func TestInMemoryStore_Put_addNew(t *testing.T) {
AccessToken: "registry_token2",
}
if err := is.Put(ctx, server2, cred2); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

// Verify Content
got1, err := is.Get(ctx, server1)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got1, cred1) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got1, cred1)
t.Errorf("MemoryStore.Get() = %v, want %v", got1, cred1)
return
}

got2, err := is.Get(ctx, server2)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got2, cred2) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got2, cred2)
t.Errorf("MemoryStore.Get() = %v, want %v", got2, cred2)
return
}
}

func TestInMemoryStore_Put_update(t *testing.T) {
func TestMemoryStore_Put_update(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

// Test Put
serverAddress := "registry.example.com"
Expand All @@ -125,7 +125,7 @@ func TestInMemoryStore_Put_update(t *testing.T) {
AccessToken: "registry_token",
}
if err := is.Put(ctx, serverAddress, cred1); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

Expand All @@ -136,24 +136,24 @@ func TestInMemoryStore_Put_update(t *testing.T) {
AccessToken: "registry_token2",
}
if err := is.Put(ctx, serverAddress, cred2); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

got, err := is.Get(ctx, serverAddress)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got, cred2) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got, cred2)
t.Errorf("MemoryStore.Get() = %v, want %v", got, cred2)
return
}
}

func TestInMemoryStore_Delete_existRecord(t *testing.T) {
func TestMemoryStore_Delete_existRecord(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

// Test Put
serverAddress := "registry.example.com"
Expand All @@ -164,42 +164,42 @@ func TestInMemoryStore_Delete_existRecord(t *testing.T) {
AccessToken: "registry_token",
}
if err := is.Put(ctx, serverAddress, cred); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

// Test Get
got, err := is.Get(ctx, serverAddress)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got, cred) {
t.Errorf("InMemoryStore.Get(%s) = %v, want %v", serverAddress, got, cred)
t.Errorf("MemoryStore.Get(%s) = %v, want %v", serverAddress, got, cred)
return
}

// Test Delete
if err := is.Delete(ctx, serverAddress); err != nil {
t.Errorf("InMemoryStore.Delete() error = %v", err)
t.Errorf("MemoryStore.Delete() error = %v", err)
return
}

// Test Get again
got, err = is.Get(ctx, serverAddress)
if err != nil {
t.Errorf("InMemoryStore.Get() error = %v", err)
t.Errorf("MemoryStore.Get() error = %v", err)
return
}
if !reflect.DeepEqual(got, auth.EmptyCredential) {
t.Errorf("InMemoryStore.Get() = %v, want %v", got, auth.EmptyCredential)
t.Errorf("MemoryStore.Get() = %v, want %v", got, auth.EmptyCredential)
return
}
}

func TestInMemoryStore_Delete_notExistRecord(t *testing.T) {
func TestMemoryStore_Delete_notExistRecord(t *testing.T) {
ctx := context.Background()
is := NewInMemoryStore()
is := NewMemoryStore()

// Test Put
serverAddress := "registry.example.com"
Expand All @@ -210,20 +210,20 @@ func TestInMemoryStore_Delete_notExistRecord(t *testing.T) {
AccessToken: "registry_token",
}
if err := is.Put(ctx, serverAddress, cred); err != nil {
t.Errorf("InMemoryStore.Put() error = %v", err)
t.Errorf("MemoryStore.Put() error = %v", err)
return
}

// Test Delete
if err := is.Delete(ctx, serverAddress); err != nil {
t.Errorf("InMemoryStore.Delete() error = %v", err)
t.Errorf("MemoryStore.Delete() error = %v", err)
return
}

// Test Delete again
// Expect no error if target record does not exist
if err := is.Delete(ctx, serverAddress); err != nil {
t.Errorf("InMemoryStore.Delete() error = %v", err)
t.Errorf("MemoryStore.Delete() error = %v", err)
return
}
}

0 comments on commit 8628b66

Please sign in to comment.