Skip to content

Commit

Permalink
Fix minor issues related index to generated time
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Mar 12, 2024
1 parent 8bf2cbf commit 9e920a8
Show file tree
Hide file tree
Showing 11 changed files with 44 additions and 247 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ 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.
### Changed

- `generated` timestamp field in the index file is now updated with current time
on push, reindex and delete.

## [0.16.0] - 2023-12-07

Expand Down
1 change: 1 addition & 0 deletions cmd/helm-s3/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (act *deleteAction) run(ctx context.Context) error {
if err != nil {
return err
}
idx.UpdateGeneratedTime()

idxReader, err := idx.Reader()
if err != nil {
Expand Down
6 changes: 2 additions & 4 deletions cmd/helm-s3/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,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()

idx.UpdateGeneratedTime()

idxReader, err := idx.Reader()
if err != nil {
return errors.WithMessage(err, "get index reader")
Expand Down
1 change: 1 addition & 0 deletions cmd/helm-s3/reindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ func (act *reindexAction) run(ctx context.Context) error {
}
}
idx.SortEntries()
idx.UpdateGeneratedTime()

builtIndex <- idx
}()
Expand Down
177 changes: 0 additions & 177 deletions go.sum

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion internal/helmutil/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type Index interface {
// SortEntries sorts the entries by version in descending order.
SortEntries()

// Updated Last indexation TimeStamp.
// UpdateGeneratedTime updates time when the index was generated.
UpdateGeneratedTime()

// MarshalBinary encodes index to a binary form.
Expand Down
2 changes: 1 addition & 1 deletion internal/helmutil/index_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (idx *IndexV2) SortEntries() {
}

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

func (idx *IndexV2) MarshalBinary() (data []byte, err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/helmutil/index_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestIndexV2_AddOrReplace(t *testing.T) {
})
}

func TestIndexV2_Timestamp(t *testing.T) {
func TestIndexV2_UpdateGeneratedTime(t *testing.T) {
idx := IndexV2{
index: &repo.IndexFile{
APIVersion: "foo",
Expand Down
2 changes: 1 addition & 1 deletion internal/helmutil/index_v3.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (idx *IndexV3) SortEntries() {
}

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

func (idx *IndexV3) MarshalBinary() (data []byte, err error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/helmutil/index_v3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ func TestIndexV3_AddOrReplace(t *testing.T) {
})
}

func TestIndexV3_Timestamp(t *testing.T) {
func TestIndexV3_UpdateGeneratedTime(t *testing.T) {
idx := IndexV3{
index: &repo.IndexFile{
APIVersion: "foo",
Expand Down
91 changes: 31 additions & 60 deletions tests/e2e/push_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,28 @@ func TestPush(t *testing.T) {
setupRepo(t, repoName, repoDir)
defer teardownRepo(t, repoName)

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

// Fetch the repo index before push to get generated time.

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)

assert.NotEmpty(t, idx.Generated)

idxGeneratedTimeBeforePush := idx.Generated

// Push chart.

cmd, stdout, stderr := command(fmt.Sprintf("helm s3 push %s %s", chartFilepath, repoName))
err := cmd.Run()
err = cmd.Run()
assert.NoError(t, err)
assertEmptyOutput(t, nil, stderr)
assert.Contains(t, stdout.String(), "Successfully uploaded the chart to the repository.")
Expand All @@ -64,10 +84,6 @@ func TestPush(t *testing.T) {

// Check that pushed chart can be fetched.

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

cmd, stdout, stderr = command(fmt.Sprintf("helm fetch %s/%s --version %s --destination %s", repoName, chartName, chartVersion, tmpdir))
err = cmd.Run()
assert.NoError(t, err)
Expand All @@ -83,6 +99,16 @@ func TestPush(t *testing.T) {

expected = "The chart already exists in the repository and cannot be overwritten without an explicit intent."
assert.Contains(t, stderr.String(), expected)

// Fetch the repo index again and check that generated time was updated.

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

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

assert.Greater(t, idx.Generated, idxGeneratedTimeBeforePush)
}

func TestPushContentType(t *testing.T) {
Expand Down Expand Up @@ -308,61 +334,6 @@ 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 9e920a8

Please sign in to comment.