Skip to content

Commit

Permalink
Add missing tests for GetMetadata method (artifacthub#666)
Browse files Browse the repository at this point in the history
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
  • Loading branch information
tegioz authored Sep 23, 2020
1 parent 65c003d commit 42846f7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 0 deletions.
73 changes: 73 additions & 0 deletions internal/repo/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -889,6 +889,79 @@ func TestGetByName(t *testing.T) {
})
}

func TestGetMetadata(t *testing.T) {
t.Run("local file: error reading repository metadata file", func(t *testing.T) {
m := NewManager(nil, nil, nil)
_, err := m.GetMetadata("testdata/not-exists.yaml")
assert.Error(t, err)
assert.Contains(t, err.Error(), "error reading repository metadata file")
})

t.Run("remote file: error downloading repository metadata file", func(t *testing.T) {
hg := &tests.HTTPGetterMock{}
hg.On("Get", "http://url.test/not-found").Return(nil, tests.ErrFake)
m := NewManager(nil, nil, nil, withHTTPGetter(hg))
_, err := m.GetMetadata("http://url.test/not-found")
assert.Error(t, err)
assert.Contains(t, err.Error(), "error downloading repository metadata file")
})

t.Run("remote file: unexpected status code received", func(t *testing.T) {
hg := &tests.HTTPGetterMock{}
hg.On("Get", "http://url.test/not-found").Return(&http.Response{
Body: ioutil.NopCloser(strings.NewReader("")),
StatusCode: http.StatusNotFound,
}, nil)
m := NewManager(nil, nil, nil, withHTTPGetter(hg))
_, err := m.GetMetadata("http://url.test/not-found")
assert.Error(t, err)
assert.Contains(t, err.Error(), "unexpected status code received")
})

t.Run("remote file: error reading repository metadata file", func(t *testing.T) {
hg := &tests.HTTPGetterMock{}
hg.On("Get", "http://url.test/not-found").Return(&http.Response{
Body: ioutil.NopCloser(tests.ErrReader(0)),
StatusCode: http.StatusOK,
}, nil)
m := NewManager(nil, nil, nil, withHTTPGetter(hg))
_, err := m.GetMetadata("http://url.test/not-found")
assert.Error(t, err)
assert.Contains(t, err.Error(), "error reading repository metadata file")
})

t.Run("error unmarshaling repository metadata file", func(t *testing.T) {
m := NewManager(nil, nil, nil)
_, err := m.GetMetadata("testdata/invalid.yml")
assert.Error(t, err)
assert.Contains(t, err.Error(), "error unmarshaling repository metadata file")
})

t.Run("invalid repository id", func(t *testing.T) {
m := NewManager(nil, nil, nil)
_, err := m.GetMetadata("testdata/invalid-repo-id.yml")
assert.Error(t, err)
assert.Contains(t, err.Error(), "invalid repository id")
})

t.Run("local file: success", func(t *testing.T) {
m := NewManager(nil, nil, nil)
_, err := m.GetMetadata("testdata/artifacthub-repo.yml")
assert.NoError(t, err)
})

t.Run("remote file: success", func(t *testing.T) {
hg := &tests.HTTPGetterMock{}
hg.On("Get", "http://url.test/ok").Return(&http.Response{
Body: ioutil.NopCloser(strings.NewReader("repositoryID: 00000000-0000-0000-0000-000000000001")),
StatusCode: http.StatusOK,
}, nil)
m := NewManager(nil, nil, nil, withHTTPGetter(hg))
_, err := m.GetMetadata("http://url.test/ok")
assert.NoError(t, err)
})
}

func TestGetPackagesDigest(t *testing.T) {
ctx := context.Background()

Expand Down
1 change: 1 addition & 0 deletions internal/repo/testdata/invalid-repo-id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
repositoryID: invalid
1 change: 1 addition & 0 deletions internal/repo/testdata/invalid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"
9 changes: 9 additions & 0 deletions internal/tests/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ func (m *HTTPGetterMock) Get(url string) (*http.Response, error) {
resp, _ := args.Get(0).(*http.Response)
return resp, args.Error(1)
}

// ErrReader represents a faulty reader implementation. It can be handy to
// simulate faulty requests bodies (ioutil.NopCloser(tests.ErrReader(0))).
type ErrReader int

// Read implements the io.Reader interface.
func (ErrReader) Read(p []byte) (n int, err error) {
return 0, ErrFake
}

0 comments on commit 42846f7

Please sign in to comment.