-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Shiwei Zhang <[email protected]>
- Loading branch information
Showing
1 changed file
with
269 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,269 @@ | ||
/* | ||
Copyright The Ratify Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package ratify | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/opencontainers/image-spec/specs-go" | ||
ocispec "github.com/opencontainers/image-spec/specs-go/v1" | ||
) | ||
|
||
type testStore struct { | ||
t *testing.T | ||
name string | ||
resolve func(*testing.T, string) (ocispec.Descriptor, error) | ||
listReferrers func(*testing.T, string, []string, func([]ocispec.Descriptor) error) error | ||
fetchBlobContent func(*testing.T, string, ocispec.Descriptor) ([]byte, error) | ||
fetchImageManifest func(*testing.T, string, ocispec.Descriptor) (*ocispec.Manifest, error) | ||
} | ||
|
||
func (s *testStore) Name() string { | ||
return s.name | ||
} | ||
|
||
func (s *testStore) Resolve(ctx context.Context, ref string) (ocispec.Descriptor, error) { | ||
if s.resolve == nil { | ||
s.t.Fatal("unexpected call to Store.Resolve") | ||
} | ||
return s.resolve(s.t, ref) | ||
} | ||
|
||
func (s *testStore) ListReferrers(ctx context.Context, ref string, artifactTypes []string, fn func([]ocispec.Descriptor) error) error { | ||
if s.listReferrers == nil { | ||
s.t.Fatal("unexpected call to Store.ListReferrers") | ||
} | ||
return s.listReferrers(s.t, ref, artifactTypes, fn) | ||
} | ||
|
||
func (s *testStore) FetchBlobContent(ctx context.Context, repo string, desc ocispec.Descriptor) ([]byte, error) { | ||
if s.fetchBlobContent == nil { | ||
s.t.Fatal("unexpected call to Store.FetchBlobContent") | ||
} | ||
return s.fetchBlobContent(s.t, repo, desc) | ||
} | ||
|
||
func (s *testStore) FetchImageManifest(ctx context.Context, repo string, desc ocispec.Descriptor) (*ocispec.Manifest, error) { | ||
if s.fetchImageManifest == nil { | ||
s.t.Fatal("unexpected call to Store.FetchImageManifest") | ||
} | ||
return s.fetchImageManifest(s.t, repo, desc) | ||
} | ||
|
||
func TestStoreMux_Name(t *testing.T) { | ||
want := "test" | ||
s := NewStoreMux(want) | ||
if got := s.Name(); got != want { | ||
t.Errorf("StoreMux.Name() = %v, want %v", got, want) | ||
} | ||
} | ||
|
||
func TestStoreMux_Resolve(t *testing.T) { | ||
ctx := context.Background() | ||
const pattern = "registry.test" | ||
|
||
tests := []struct { | ||
name string | ||
ref string | ||
resolve func(*testing.T, string) (ocispec.Descriptor, error) | ||
want ocispec.Descriptor | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "match store", | ||
ref: "registry.test/foo:latest", | ||
resolve: func(t *testing.T, ref string) (ocispec.Descriptor, error) { | ||
return ocispec.DescriptorEmptyJSON, nil | ||
}, | ||
want: ocispec.DescriptorEmptyJSON, | ||
}, | ||
{ | ||
name: "no matching store found", | ||
ref: "null.test/foo:latest", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
store := NewStoreMux("hello") | ||
if err := store.Register(pattern, &testStore{ | ||
t: t, | ||
resolve: tt.resolve, | ||
}); err != nil { | ||
t.Fatalf("StoreMux.Register() error = %s, want nil", err) | ||
} | ||
got, err := store.Resolve(ctx, tt.ref) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("StoreMux.Resolve() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StoreMux.Resolve() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStoreMux_ListReferrers(t *testing.T) { | ||
ctx := context.Background() | ||
const pattern = "registry.test" | ||
|
||
tests := []struct { | ||
name string | ||
ref string | ||
listReferrers func(*testing.T, string, []string, func([]ocispec.Descriptor) error) error | ||
want []ocispec.Descriptor | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "match store", | ||
ref: "registry.test/foo:latest", | ||
listReferrers: func(t *testing.T, _ string, _ []string, fn func([]ocispec.Descriptor) error) error { | ||
return fn([]ocispec.Descriptor{ocispec.DescriptorEmptyJSON}) | ||
}, | ||
want: []ocispec.Descriptor{ocispec.DescriptorEmptyJSON}, | ||
}, | ||
{ | ||
name: "no matching store found", | ||
ref: "null.test/foo:latest", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
store := NewStoreMux("hello") | ||
if err := store.Register(pattern, &testStore{ | ||
t: t, | ||
listReferrers: tt.listReferrers, | ||
}); err != nil { | ||
t.Fatalf("StoreMux.Register() error = %s, want nil", err) | ||
} | ||
var got []ocispec.Descriptor | ||
fn := func(referrers []ocispec.Descriptor) error { | ||
got = append(got, referrers...) | ||
return nil | ||
} | ||
if err := store.ListReferrers(ctx, tt.ref, nil, fn); (err != nil) != tt.wantErr { | ||
t.Errorf("StoreMux.ListReferrers() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StoreMux.ListReferrers() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStoreMux_FetchBlobContent(t *testing.T) { | ||
ctx := context.Background() | ||
const pattern = "registry.test" | ||
|
||
tests := []struct { | ||
name string | ||
repo string | ||
fetchBlobContent func(*testing.T, string, ocispec.Descriptor) ([]byte, error) | ||
want []byte | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "match store", | ||
repo: "registry.test/foo", | ||
fetchBlobContent: func(t *testing.T, _ string, _ ocispec.Descriptor) ([]byte, error) { | ||
return []byte("foo"), nil | ||
}, | ||
want: []byte("foo"), | ||
}, | ||
{ | ||
name: "no matching store found", | ||
repo: "null.test/foo", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
store := NewStoreMux("hello") | ||
if err := store.Register(pattern, &testStore{ | ||
t: t, | ||
fetchBlobContent: tt.fetchBlobContent, | ||
}); err != nil { | ||
t.Fatalf("StoreMux.Register() error = %s, want nil", err) | ||
} | ||
got, err := store.FetchBlobContent(ctx, tt.repo, ocispec.Descriptor{}) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("StoreMux.FetchBlobContent() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StoreMux.FetchBlobContent() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestStoreMux_FetchImageManifest(t *testing.T) { | ||
ctx := context.Background() | ||
const pattern = "registry.test" | ||
manifest := &ocispec.Manifest{ | ||
Versioned: specs.Versioned{ | ||
SchemaVersion: 2, | ||
}, | ||
MediaType: "application/vnd.oci.image.manifest.v1+json", | ||
ArtifactType: "application/vnd.unknown.artifact.v1", | ||
Config: ocispec.DescriptorEmptyJSON, | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
repo string | ||
fetchImageManifest func(*testing.T, string, ocispec.Descriptor) (*ocispec.Manifest, error) | ||
want *ocispec.Manifest | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "match store", | ||
repo: "registry.test/foo", | ||
fetchImageManifest: func(t *testing.T, _ string, _ ocispec.Descriptor) (*ocispec.Manifest, error) { | ||
return manifest, nil | ||
}, | ||
want: manifest, | ||
}, | ||
{ | ||
name: "no matching store found", | ||
repo: "null.test/foo", | ||
wantErr: true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
store := NewStoreMux("hello") | ||
if err := store.Register(pattern, &testStore{ | ||
t: t, | ||
fetchImageManifest: tt.fetchImageManifest, | ||
}); err != nil { | ||
t.Fatalf("StoreMux.Register() error = %s, want nil", err) | ||
} | ||
got, err := store.FetchImageManifest(ctx, tt.repo, ocispec.Descriptor{}) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("StoreMux.FetchImageManifest() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("StoreMux.FetchImageManifest() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |