Skip to content

Commit

Permalink
Test: Add test cases for pack config prune-interval
Browse files Browse the repository at this point in the history
Signed-off-by: Parthiba-Hazra <[email protected]>
  • Loading branch information
Parthiba-Hazra committed Mar 4, 2024
1 parent c7cb28e commit ed9e77e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 18 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ require (
go.opentelemetry.io/otel/metric v1.22.0 // indirect
go.opentelemetry.io/otel/trace v1.22.0 // indirect
golang.org/x/net v0.21.0 // indirect
golang.org/x/tools v0.17.0 // indirect
golang.org/x/tools v0.18.0 // indirect
google.golang.org/appengine v1.6.8 // indirect
google.golang.org/protobuf v1.32.0 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc
golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU=
golang.org/x/tools v0.17.0 h1:FvmRgNOcs3kOa+T20R1uhfP9F6HgG2mfxDv1vrx1Htc=
golang.org/x/tools v0.17.0/go.mod h1:xsh6VxdV005rRVaS6SSAf9oiAqljS7UZUacMZ8Bnsps=
golang.org/x/tools v0.18.0 h1:k8NLag8AGHnn+PHbl7g43CtqZAwG60vZkLqgyZgIHgQ=
golang.org/x/tools v0.18.0/go.mod h1:GL7B4CwcLLeo59yx/9UWWuNOW1n3VZ4f5axWfML7Lcg=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
7 changes: 4 additions & 3 deletions internal/commands/config_prune_interval.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import (
"fmt"
"regexp"

"github.com/pkg/errors"
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/internal/style"
"github.com/buildpacks/pack/pkg/image"
"github.com/buildpacks/pack/pkg/logging"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

func ConfigPruneInterval(logger logging.Logger, cfg config.Config, cfgPath string) *cobra.Command {
Expand All @@ -30,7 +31,7 @@ func ConfigPruneInterval(logger logging.Logger, cfg config.Config, cfgPath strin
switch {
case unset:
if len(args) > 0 {
return errors.Errorf("prune inteval and --unset cannot be specified simultaneously")
return errors.Errorf("prune interval and --unset cannot be specified simultaneously")
}
imageJSON, err := image.ReadImageJSON(logger)
if err != nil {
Expand Down
106 changes: 106 additions & 0 deletions internal/commands/config_prune_interval_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package commands_test

import (
"bytes"
"os"
"path/filepath"
"testing"

"github.com/sclevine/spec"
"github.com/sclevine/spec/report"
"github.com/spf13/cobra"

"github.com/buildpacks/pack/internal/commands"
"github.com/buildpacks/pack/internal/config"
"github.com/buildpacks/pack/pkg/logging"
h "github.com/buildpacks/pack/testhelpers"
)

func TestConfigPruneInterval(t *testing.T) {
spec.Run(t, "ConfigPruneIntervalCommand", testConfigPruneIntervalCommand, spec.Random(), spec.Report(report.Terminal{}))
}

func testConfigPruneIntervalCommand(t *testing.T, when spec.G, it spec.S) {
var (
command *cobra.Command
logger logging.Logger
outBuf bytes.Buffer
tempPackHome string
configFile string
assert = h.NewAssertionManager(t)
cfg = config.Config{}
)

it.Before(func() {
logger = logging.NewLogWithWriters(&outBuf, &outBuf)
tempPackHome, _ = os.MkdirTemp("", "pack-home")
configFile = filepath.Join(tempPackHome, "config.toml")

command = commands.ConfigPruneInterval(logger, cfg, configFile)
})

it.After(func() {
_ = os.RemoveAll(tempPackHome)
})

when("#ConfigPruneInterval", func() {
when("no arguments are provided", func() {
it("lists the current pruning interval", func() {
command.SetArgs([]string{})

err := command.Execute()

assert.Nil(err)
assert.Contains(outBuf.String(), "The current prune interval is")
})
})

when("an argument is provided", func() {
when("argument is valid", func() {
it("sets the provided interval as the pruning interval", func() {
interval := "5d"
command.SetArgs([]string{interval})

err := command.Execute()

assert.Nil(err)
assert.Contains(outBuf.String(), "Successfully set")
})
})

when("argument is invalid", func() {
it("returns an error", func() {
interval := "invalid"
command.SetArgs([]string{interval})

err := command.Execute()

assert.Error(err)
assert.Contains(err.Error(), "invalid interval format")
})
})
})

when("--unset flag is provided", func() {
it("unsets the pruning interval", func() {
command.SetArgs([]string{"--unset"})

err := command.Execute()

assert.Nil(err)
assert.Contains(outBuf.String(), "Successfully unset pruning interval")
})
})

when("both interval and --unset flag are provided", func() {
it("returns an error", func() {
command.SetArgs([]string{"5d", "--unset"})

err := command.Execute()

assert.Error(err)
assert.Contains(err.Error(), "prune interval and --unset cannot be specified simultaneously")
})
})
})
}
12 changes: 0 additions & 12 deletions internal/commands/config_pull_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,12 +200,6 @@ func testConfigPullPolicyCommand(t *testing.T, when spec.G, it spec.S) {
assert.Nil(err)
assert.Equal(readCfg.PullPolicy, "never")
})
it("returns clear error if fails to write", func() {
assert.Nil(os.WriteFile(configFile, []byte("something"), 0001))
command := commands.ConfigPullPolicy(logger, cfg, configFile)
command.SetArgs([]string{"if-not-present"})
assert.ErrorContains(command.Execute(), "writing config to")
})
})
})
when("unset", func() {
Expand All @@ -220,12 +214,6 @@ func testConfigPullPolicyCommand(t *testing.T, when spec.G, it spec.S) {
assert.Nil(err)
assert.Equal(cfg.PullPolicy, "")
})
it("returns clear error if fails to write", func() {
assert.Nil(os.WriteFile(configFile, []byte("something"), 0001))
command := commands.ConfigPullPolicy(logger, config.Config{PullPolicy: "never"}, configFile)
command.SetArgs([]string{"--unset"})
assert.ErrorContains(command.Execute(), "writing config to")
})
})
when("--unset and policy to set is provided", func() {
it("errors", func() {
Expand Down
6 changes: 5 additions & 1 deletion pkg/image/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ type ImagePullChecker interface {
ReadImageJSON(l logging.Logger) (*ImageJSON, error)
}

func intervalPolicy(options FetchOptions) bool {
return options.PullPolicy == PullWithInterval || options.PullPolicy == PullHourly || options.PullPolicy == PullDaily || options.PullPolicy == PullWeekly
}

type PullChecker struct {
logger logging.Logger
}
Expand Down Expand Up @@ -167,7 +171,7 @@ func (f *Fetcher) Fetch(ctx context.Context, name string, options FetchOptions)
return nil, err
}

if options.PullPolicy == PullWithInterval || options.PullPolicy == PullHourly || options.PullPolicy == PullDaily || options.PullPolicy == PullWeekly {
if intervalPolicy(options) {
// Update image pull record in the JSON file
if err := f.updateImagePullRecord(name, time.Now().Format(time.RFC3339)); err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion pkg/image/pull_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ import (
"strings"
"time"

"github.com/buildpacks/pack/pkg/logging"
"github.com/pkg/errors"

"github.com/buildpacks/pack/pkg/logging"
)

// PullPolicy defines a policy for how to manage images
Expand Down

0 comments on commit ed9e77e

Please sign in to comment.