Skip to content

Commit

Permalink
Fix regression in v0.11.0 (#250)
Browse files Browse the repository at this point in the history
* Fix regression in v0.11.0

Change the default for writer opt 'IgnoreNoName' to be
true. This was set to false, and causing the example to
not run properly.

A new option 'WithErrorOnNoName' has been added to be
used in cases when this behaviour is expected. The existing
option 'WithIgnoreNoName' has now been deprecated.

A simple bash-based acceptance test has been added to
prevent this issue in the future. This starts a registry,
compiles the example into a binary, and makes sure it
runs properly. This test is now run in CI.

Signed-off-by: Josh Dolitsky <[email protected]>

* Allow hostname override for CI

Signed-off-by: Josh Dolitsky <[email protected]>

* use plain http for example..

Signed-off-by: Josh Dolitsky <[email protected]>

* Add logic to wait for registry to come up

Signed-off-by: Josh Dolitsky <[email protected]>
  • Loading branch information
jdolitsky authored Mar 18, 2021
1 parent 23f76e8 commit 2798290
Show file tree
Hide file tree
Showing 10 changed files with 86 additions and 8 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/build-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ jobs:
go-version: '1.16.2'
- name: run unit tests
run: make test
- name: run acceptance tests
run: |
export LOCAL_REGISTRY_HOSTNAME="$(hostname -I | awk '{print $1}')"
make acceptance
- name: upload coverage report
uses: actions/upload-artifact@master
with:
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ jobs:
uses: actions/setup-go@v1
with:
go-version: '1.16.2'
- name: run unit tests
run: make test
- name: run acceptance tests
run: |
export LOCAL_REGISTRY_HOSTNAME="$(hostname -I | awk '{print $1}')"
make acceptance
- name: upload coverage report
uses: actions/upload-artifact@master
with:
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ LDFLAGS += -X $(PROJECT_PKG)/internal/version.GitTreeState=${GIT_DIRTY}
test: vendor check-encoding
./scripts/test.sh

.PHONY: acceptance
acceptance:
./scripts/acceptance.sh

.PHONY: covhtml
covhtml:
open .cover/coverage.html
Expand Down
13 changes: 11 additions & 2 deletions examples/simple_push_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"os"

"github.com/deislabs/oras/pkg/content"
"github.com/deislabs/oras/pkg/oras"
Expand All @@ -17,14 +18,22 @@ func check(e error) {
}
}

func getLocalRegistryHostname() string {
hostname := "localhost"
if v := os.Getenv("LOCAL_REGISTRY_HOSTNAME"); v != "" {
hostname = v
}
return hostname
}

func main() {
ref := "localhost:5000/oras:test"
ref := fmt.Sprintf("%s:5000/oras:test", getLocalRegistryHostname())
fileName := "hello.txt"
fileContent := []byte("Hello World!\n")
customMediaType := "my.custom.media.type"

ctx := context.Background()
resolver := docker.NewResolver(docker.ResolverOptions{})
resolver := docker.NewResolver(docker.ResolverOptions{PlainHTTP: true})

// Push file(s) w custom mediatype to registry
memoryStore := content.NewMemoryStore()
Expand Down
2 changes: 1 addition & 1 deletion pkg/content/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ func (suite *ContentTestSuite) SetupSuite() {
os.Remove(testFileName)
err := ioutil.WriteFile(testFileName, testContent, 0644)
suite.Nil(err, "no error creating test file on disk")
testFileStore := NewFileStore(testDirRoot)
testFileStore := NewFileStore(testDirRoot, WithErrorOnNoName())
_, err = testFileStore.Add(testRef, "", testFileName)
suite.Nil(err, "no error adding item to file store")
suite.TestFileStore = testFileStore
Expand Down
2 changes: 1 addition & 1 deletion pkg/content/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type FileStore struct {
// NewFileStore creats a new file store
func NewFileStore(rootPath string, opts ...WriterOpt) *FileStore {
// we have to process the opts to find if they told us to change defaults
var wOpts WriterOpts
wOpts := DefaultWriterOpts()
for _, opt := range opts {
if err := opt(&wOpts); err != nil {
continue
Expand Down
4 changes: 2 additions & 2 deletions pkg/content/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ func TestFileStoreNoName(t *testing.T) {
opts []content.WriterOpt
err error
}{
{nil, content.ErrNoName},
{[]content.WriterOpt{content.WithIgnoreNoName()}, nil},
{nil, nil},
{[]content.WriterOpt{content.WithErrorOnNoName()}, content.ErrNoName},
}
for _, tt := range tests {
rootPath, err := ioutil.TempDir("", "oras_filestore_test")
Expand Down
14 changes: 13 additions & 1 deletion pkg/content/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func DefaultWriterOpts() WriterOpts {
InputHash: nil,
OutputHash: nil,
Blocksize: DefaultBlocksize,
IgnoreNoName: false,
IgnoreNoName: true,
}
}

Expand Down Expand Up @@ -74,9 +74,21 @@ func WithMultiWriterIngester() WriterOpt {
}
}

// WithErrorOnNoName some ingesters, when creating a Writer, do not return an error if
// the descriptor does not have a valid name on the descriptor. Passing WithErrorOnNoName
// tells the writer to return an error instead of passing the data to a nil writer.
func WithErrorOnNoName() WriterOpt {
return func(w *WriterOpts) error {
w.IgnoreNoName = false
return nil
}
}

// WithIgnoreNoName some ingesters, when creating a Writer, return an error if
// the descriptor does not have a valid name on the descriptor. Passing WithIgnoreNoName
// tells the writer not to return an error, but rather to pass the data to a nil writer.
//
// Deprecated: Use WithErrorOnNoName
func WithIgnoreNoName() WriterOpt {
return func(w *WriterOpts) error {
w.IgnoreNoName = true
Expand Down
2 changes: 1 addition & 1 deletion pkg/oras/oras_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ func (suite *ORASTestSuite) Test_4_GHSA_g5v4_5x39_vwhx() {
suite.FailNow("error creating temp directory", err)
}
defer os.RemoveAll(tempDir)
store := orascontent.NewFileStore(tempDir, orascontent.WithIgnoreNoName())
store := orascontent.NewFileStore(tempDir)
defer store.Close()
ref = fmt.Sprintf("%s/evil:%s", suite.DockerRegistryHost, tag)
_, _, err = Pull(newContext(), newResolver(), ref, store)
Expand Down
43 changes: 43 additions & 0 deletions scripts/acceptance.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/bin/bash -ex

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR/../

LOCAL_REGISTRY_HOSTNAME="${LOCAL_REGISTRY_HOSTNAME:-localhost}"

# Cleanup from previous runs
rm -f hello.txt
rm -f bin/oras-acceptance || true
docker rm -f oras-acceptance-registry || true

# Build the example into a binary
CGO_ENABLED=0 go build -v -o bin/oras-acceptance ./examples/

# Run a test registry and expose at localhost:5000
trap "docker rm -f oras-acceptance-registry" EXIT
docker run -d -p 5000:5000 \
--name oras-acceptance-registry \
index.docker.io/registry

# Wait for a connection to port 5000 (timeout after 1 minute)
WAIT_TIME=0
while true; do
if nc -w 1 -z "${LOCAL_REGISTRY_HOSTNAME}" 5000; then
echo "Able to connect to ${LOCAL_REGISTRY_HOSTNAME} port 5000"
break
else
if (( ${WAIT_TIME} >= 60 )); then
echo "Timed out waiting for connection to ${LOCAL_REGISTRY_HOSTNAME} on port 5000. Exiting."
exit 1
fi
echo "Waiting to connect to ${LOCAL_REGISTRY_HOSTNAME} on port 5000. Sleeping 5 seconds.."
sleep 5
WAIT_TIME=$((WAIT_TIME + 5))
fi
done

# Run the example binary
bin/oras-acceptance

# Ensure hello.txt exists and contains expected content
grep '^Hello World!$' hello.txt

0 comments on commit 2798290

Please sign in to comment.