Skip to content

Commit

Permalink
Use github.com/jonboulle/clockwork in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zregvart authored and tekton-robot committed Oct 5, 2023
1 parent 9dc58bf commit 1c52522
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 28 deletions.
19 changes: 9 additions & 10 deletions pkg/cmd/bundle/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package bundle

import (
"context"
"errors"
"fmt"
"io"
Expand All @@ -22,6 +23,7 @@ import (
"time"

"github.com/google/go-containerregistry/pkg/name"
"github.com/jonboulle/clockwork"
"github.com/spf13/cobra"
"github.com/tektoncd/cli/pkg/bundle"
"github.com/tektoncd/cli/pkg/cli"
Expand Down Expand Up @@ -92,7 +94,7 @@ Created time:
Err: cmd.OutOrStderr(),
}

return opts.Run(args)
return opts.Run(cmd.Context(), args)
},
}
c.Flags().StringSliceVarP(&opts.bundleContentPaths, "filenames", "f", []string{}, "List of fully-qualified file paths containing YAML or JSON defined Tekton objects to include in this bundle")
Expand All @@ -105,7 +107,7 @@ Created time:

// Reads the positional arguments and the `-f` flag to fill in the `bunldeContents` parameter with all of the raw Tekton
// contents.
func (p *pushOptions) parseArgsAndFlags(args []string) (err error) {
func (p *pushOptions) parseArgsAndFlags(ctx context.Context, args []string) (err error) {
p.ref, _ = name.ParseReference(args[0], name.StrictValidation, name.Insecure)

// If there are file paths specified, then read them and include their contents.
Expand Down Expand Up @@ -134,16 +136,16 @@ func (p *pushOptions) parseArgsAndFlags(args []string) (err error) {
return err
}

if p.ctime, err = determineCTime(p.ctimeParam); err != nil {
if p.ctime, err = determineCTime(p.ctimeParam, clockwork.FromContext(ctx)); err != nil {
return err
}

return nil
}

// Run performs the principal logic of reading and parsing the input, creating the bundle, and publishing it.
func (p *pushOptions) Run(args []string) error {
if err := p.parseArgsAndFlags(args); err != nil {
func (p *pushOptions) Run(ctx context.Context, args []string) error {
if err := p.parseArgsAndFlags(ctx, args); err != nil {
return err
}

Expand All @@ -160,10 +162,7 @@ func (p *pushOptions) Run(args []string) error {
return err
}

// to help with testing
var now = time.Now

func determineCTime(t string) (parsed time.Time, err error) {
func determineCTime(t string, clock clockwork.Clock) (parsed time.Time, err error) {
// if given the parameter don't lookup the SOURCE_DATE_EPOCH env var
if t == "" {
if sourceDateEpoch, found := os.LookupEnv(sourceDateEpochEnv); found && sourceDateEpoch != "" {
Expand All @@ -178,7 +177,7 @@ func determineCTime(t string) (parsed time.Time, err error) {
}

if t == "" {
return now(), nil
return clock.Now(), nil
}

parsed, err = time.Parse(time.DateOnly, t)
Expand Down
40 changes: 22 additions & 18 deletions pkg/cmd/bundle/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package bundle
import (
"archive/tar"
"bytes"
"context"
"fmt"
"io"
"net/http/httptest"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/google/go-containerregistry/pkg/registry"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/jonboulle/clockwork"
"github.com/tektoncd/cli/pkg/bundle"
"github.com/tektoncd/cli/pkg/cli"
tkremote "github.com/tektoncd/pipeline/pkg/remote/oci"
Expand Down Expand Up @@ -48,14 +50,9 @@ var (
apiVersion: "v1beta1",
raw: exampleTask,
}
)

func init() {
// fixed time to ease testing
now = func() time.Time {
return time.Date(2023, 9, 22, 1, 2, 3, 0, time.UTC)
}
}
fixedTime = time.Date(2023, 9, 22, 1, 2, 3, 0, time.UTC)
)

func TestPushCommand(t *testing.T) {
testcases := []struct {
Expand All @@ -74,7 +71,7 @@ func TestPushCommand(t *testing.T) {
"simple.yaml": exampleTask,
},
expectedContents: map[string]expected{exampleTaskExpected.name: exampleTaskExpected},
expectedCTime: now(),
expectedCTime: fixedTime,
},
{
name: "stdin-input",
Expand All @@ -83,7 +80,7 @@ func TestPushCommand(t *testing.T) {
},
stdin: exampleTask,
expectedContents: map[string]expected{exampleTaskExpected.name: exampleTaskExpected},
expectedCTime: now(),
expectedCTime: fixedTime,
},
{
name: "with-annotations",
Expand All @@ -96,16 +93,16 @@ func TestPushCommand(t *testing.T) {
"org.opencontainers.image.license": "Apache-2.0",
"org.opencontainers.image.url": "https://example.org",
},
expectedCTime: now(),
expectedCTime: fixedTime,
},
{
name: "with-ctime",
files: map[string]string{
"simple.yaml": exampleTask,
},
expectedContents: map[string]expected{exampleTaskExpected.name: exampleTaskExpected},
ctime: now().Format(time.RFC3339),
expectedCTime: now(),
ctime: fixedTime.Format(time.RFC3339),
expectedCTime: fixedTime,
},
}

Expand Down Expand Up @@ -155,7 +152,11 @@ func TestPushCommand(t *testing.T) {
remoteOptions: bundle.RemoteOptions{},
ctimeParam: tc.ctime,
}
if err := opts.Run([]string{ref}); err != nil {

ctx := context.Background()
ctx = clockwork.AddToContext(ctx, clockwork.NewFakeClockAt(fixedTime))

if err := opts.Run(ctx, []string{ref}); err != nil {
t.Errorf("Unexpected failure calling run: %v", err)
}

Expand Down Expand Up @@ -253,7 +254,7 @@ func TestParseTime(t *testing.T) {
err string
expected time.Time
}{
{name: "now", expected: now()},
{name: "now", expected: fixedTime},
{name: "date", given: "2023-09-22", expected: time.Date(2023, 9, 22, 0, 0, 0, 0, time.UTC)},
{name: "date and time", given: "2023-09-22T01:02:03", expected: time.Date(2023, 9, 22, 1, 2, 3, 0, time.UTC)},
{name: "utc with fraction", given: "2023-09-22T01:02:03.45Z", expected: time.Date(2023, 9, 22, 1, 2, 3, 45, time.UTC)},
Expand All @@ -265,7 +266,7 @@ func TestParseTime(t *testing.T) {
// remove SOURCE_DATE_EPOCH if set externally
t.Setenv("SOURCE_DATE_EPOCH", "")

got, err := determineCTime(c.given)
got, err := determineCTime(c.given, clockwork.NewFakeClockAt(fixedTime))

if err != nil {
if err.Error() != c.err {
Expand Down Expand Up @@ -341,7 +342,7 @@ func TestParseArgsAndFlags(t *testing.T) {
annotationsParams: []string{"a=b", "c=d"},
expectedRef: "registry.io/repository:tag",
expectedAnnotations: map[string]string{"a": "b", "c": "d"},
expectedCTime: now(),
expectedCTime: fixedTime,
},
{
name: "ctime param",
Expand All @@ -362,7 +363,7 @@ func TestParseArgsAndFlags(t *testing.T) {
refArg: "registry.io/repository:tag",
bundleContent: map[string]string{"-": ""},
expectedRef: "registry.io/repository:tag",
expectedCTime: now(),
expectedCTime: fixedTime,
err: "failed to read bundle contents from stdin: empty input",
},
}
Expand Down Expand Up @@ -404,7 +405,10 @@ func TestParseArgsAndFlags(t *testing.T) {
expectedContent = append(expectedContent, c)
}

if err := opts.parseArgsAndFlags([]string{c.refArg}); err != nil {
ctx := context.Background()
ctx = clockwork.AddToContext(ctx, clockwork.NewFakeClockAt(fixedTime))

if err := opts.parseArgsAndFlags(ctx, []string{c.refArg}); err != nil {
if err.Error() != c.err {
t.Errorf("unexpected error, expecting %q, got: %q", c.err, err)
}
Expand Down

0 comments on commit 1c52522

Please sign in to comment.