Skip to content

Commit

Permalink
hypnoglow#142: Update generated key with latest timestamp when index …
Browse files Browse the repository at this point in the history
…is changed
  • Loading branch information
Prem Joseph committed Jun 17, 2021
1 parent e208d4e commit 1145e0c
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 2 deletions.
3 changes: 2 additions & 1 deletion cmd/helms3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ func (act pushAction) Run(ctx context.Context) error {
return errors.WithMessage(err, "add/replace chart in the index")
}
idx.SortEntries()

// #142: update timestamp of the generated field
idx.TimeStamp()
idxReader, err := idx.Reader()
if err != nil {
return errors.WithMessage(err, "get index reader")
Expand Down
3 changes: 3 additions & 0 deletions internal/helmutil/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ type Index interface {
// SortEntries sorts the entries by version in descending order.
SortEntries()

// Add Last Generated TimeStamp.
TimeStamp()

// MarshalBinary encodes index to a binary form.
MarshalBinary() (data []byte, err error)

Expand Down
4 changes: 4 additions & 0 deletions internal/helmutil/index_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (idx *IndexV2) SortEntries() {
idx.index.SortEntries()
}

func (idx *IndexV2) TimeStamp() {
idx.index.Generated = time.Now()
}

func (idx *IndexV2) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
4 changes: 4 additions & 0 deletions internal/helmutil/index_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ func (idx *IndexV3) SortEntries() {
idx.index.SortEntries()
}

func (idx *IndexV3) TimeStamp() {
idx.index.Generated = time.Now()
}

func (idx *IndexV3) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
2 changes: 1 addition & 1 deletion plugin.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: "s3"
version: "0.10.0"
version: "0.10.1"
usage: "Manage chart repositories on Amazon S3"
description: |-
The plugin allows to use s3 protocol to upload, fetch charts and to work with repositories.
Expand Down
55 changes: 55 additions & 0 deletions tests/e2e/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,61 @@ func TestPushRelative(t *testing.T) {
assert.FileExists(t, filepath.Join(tmpdir, chartFilename))
}

func TestGeneratedTimeStamp(t *testing.T) {
t.Log("Test generated timestamp is updated on push")

const (
repoName = "test-push-index-timestamp"
repoDir = "charts"
chartName = "foo"
chartVersion = "1.2.3"
chartFilename = "foo-1.2.3.tgz"
chartFilepath = "testdata/" + chartFilename
)

setupRepo(t, repoName, repoDir)
defer teardownRepo(t, repoName)

cmd, stdout, stderr := command(fmt.Sprintf("helm s3 push %s %s", chartFilepath, repoName))
err := cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, stdout, stderr)

tmpdir, err := ioutil.TempDir("", t.Name())
require.NoError(t, err)
defer os.RemoveAll(tmpdir)

// Fetch the repo index and register the generated timestamp
indexFile := filepath.Join(tmpdir, "index.yaml")

err = mc.FGetObject(repoName, repoDir+"/index.yaml", indexFile, minio.GetObjectOptions{})
require.NoError(t, err)

idx, err := repo.LoadIndexFile(indexFile)
require.NoError(t, err)

generatedOld := idx.Generated

time.Sleep(time.Second)

// Force push the chart and register the generated timestamp
cmd, stdout, stderr = command(fmt.Sprintf("helm s3 push %s %s --force", chartFilepath, repoName))
err = cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, stdout, stderr)

err = mc.FGetObject(repoName, repoDir+"/index.yaml", indexFile, minio.GetObjectOptions{})
require.NoError(t, err)

idx, err = repo.LoadIndexFile(indexFile)
require.NoError(t, err)

generatedNew := idx.Generated
// t.Logf("\ngeneratedOld:%s\ngeneratedNew:%s", generatedOld.String(), generatedNew.String())
// Assert generatedNew is greater than generatedOld
assert.True(t, generatedNew.After(generatedOld), "Expected %s greater than %s", generatedNew.String(), generatedOld.String())
}

func assertEmptyOutput(t *testing.T, stdout, stderr *bytes.Buffer) {
t.Helper()

Expand Down

0 comments on commit 1145e0c

Please sign in to comment.