Skip to content

Commit

Permalink
142: Update generated key with latest timestamp when index is changed
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Mar 12, 2024
1 parent 8003012 commit 8bf2cbf
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

- Update generated timestamp field in index.yaml when the index is modified.

## [0.16.0] - 2023-12-07

### Added
Expand Down
5 changes: 4 additions & 1 deletion cmd/helm-s3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,10 @@ 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()

Check failure on line 216 in cmd/helm-s3/push.go

View workflow job for this annotation

GitHub Actions / Build

idx.TimeStamp undefined (type helmutil.Index has no field or method TimeStamp)

idxReader, err := idx.Reader()
if err != nil {
return errors.WithMessage(err, "get index reader")
Expand Down
177 changes: 177 additions & 0 deletions go.sum

Large diffs are not rendered by default.

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()

// Updated Last indexation TimeStamp.
UpdateGeneratedTime()

// 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) UpdateGeneratedTime() {
idx.index.Generated = time.Now()
}

func (idx *IndexV2) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
13 changes: 13 additions & 0 deletions internal/helmutil/index_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ func TestIndexV2_AddOrReplace(t *testing.T) {
assert.Equal(t, "sha256:222", i.index.Entries["foo"][0].Digest)
})
}

func TestIndexV2_Timestamp(t *testing.T) {
idx := IndexV2{
index: &repo.IndexFile{
APIVersion: "foo",
Generated: time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC),
},
}
generatedOld := idx.index.Generated
idx.UpdateGeneratedTime()
generatedNew := idx.index.Generated
assert.True(t, generatedNew.After(generatedOld), "Expected %s greater than %s", generatedNew.String(), generatedOld.String())
}
4 changes: 4 additions & 0 deletions internal/helmutil/index_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func (idx *IndexV3) SortEntries() {
idx.index.SortEntries()
}

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

func (idx *IndexV3) MarshalBinary() (data []byte, err error) {
return yaml.Marshal(idx.index)
}
Expand Down
13 changes: 13 additions & 0 deletions internal/helmutil/index_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,3 +122,16 @@ func TestIndexV3_AddOrReplace(t *testing.T) {
assert.Equal(t, "sha256:222", i.index.Entries["foo"][0].Digest)
})
}

func TestIndexV3_Timestamp(t *testing.T) {
idx := IndexV3{
index: &repo.IndexFile{
APIVersion: "foo",
Generated: time.Date(2018, 01, 01, 0, 0, 0, 0, time.UTC),
},
}
generatedOld := idx.index.Generated
idx.UpdateGeneratedTime()
generatedNew := idx.index.Generated
assert.True(t, generatedNew.After(generatedOld), "Expected %s greater than %s", generatedNew.String(), generatedOld.String())
}
55 changes: 55 additions & 0 deletions tests/e2e/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,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 8bf2cbf

Please sign in to comment.