Skip to content

Commit

Permalink
Add WithTransport to gcrane (#1022)
Browse files Browse the repository at this point in the history
Also add some silly tests for coverage.
  • Loading branch information
jonjohnsonjr committed Jun 11, 2021
1 parent 9e56ddd commit a27f4a4
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions pkg/gcrane/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package gcrane

import (
"context"
"net/http"
"runtime"

"github.com/google/go-containerregistry/pkg/authn"
Expand Down Expand Up @@ -64,6 +65,16 @@ func WithJobs(jobs int) Option {
}
}

// WithTransport is a functional option for overriding the default transport
// for remote operations.
func WithTransport(t http.RoundTripper) Option {
return func(o *options) {
o.remote = append(o.remote, remote.WithTransport(t))
o.google = append(o.google, google.WithTransport(t))
o.crane = append(o.crane, crane.WithTransport(t))
}
}

// WithUserAgent adds the given string to the User-Agent header for any HTTP
// requests.
func WithUserAgent(ua string) Option {
Expand Down
58 changes: 58 additions & 0 deletions pkg/gcrane/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Copyright 2021 Google LLC All Rights Reserved.
//
// 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 gcrane

import (
"context"
"net/http"
"testing"

"github.com/google/go-containerregistry/pkg/authn"
)

func TestOptions(t *testing.T) {
o := makeOptions()
if len(o.remote) != 1 {
t.Errorf("remote should default to Keychain")
}
if len(o.crane) != 1 {
t.Errorf("crane should default to Keychain")
}
if len(o.google) != 1 {
t.Errorf("google should default to Keychain")
}

o = makeOptions(WithAuth(authn.Anonymous), WithKeychain(authn.DefaultKeychain))
if len(o.remote) != 1 {
t.Errorf("WithKeychain should replace remote[0]")
}
if len(o.crane) != 1 {
t.Errorf("WithKeychain should replace crane[0]")
}
if len(o.google) != 1 {
t.Errorf("WithKeychain should replace google[0]")
}

o = makeOptions(WithTransport(http.DefaultTransport), WithUserAgent("hi"), WithContext(context.TODO()))
if len(o.remote) != 4 {
t.Errorf("wrong number of options: %d", len(o.remote))
}
if len(o.crane) != 4 {
t.Errorf("wrong number of options: %d", len(o.crane))
}
if len(o.google) != 4 {
t.Errorf("wrong number of options: %d", len(o.google))
}
}

0 comments on commit a27f4a4

Please sign in to comment.