Skip to content

Commit

Permalink
Add test for file timestamps
Browse files Browse the repository at this point in the history
  • Loading branch information
amisevsk committed Apr 18, 2024
1 parent cb90550 commit b915252
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions testing/pack-unpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (
"os"
"path/filepath"
"testing"
"time"

"kitops/pkg/lib/constants"

"github.com/stretchr/testify/assert"
)

// TestPackUnpack tests kit functionality by generating a file tree, packing it,
Expand Down Expand Up @@ -65,3 +68,46 @@ func TestPackUnpack(t *testing.T) {
})
}
}

func TestPackReproducibility(t *testing.T) {
tmpDir, removeTmp := setupTempDir(t)
defer removeTmp()

modelKitPath, _, contextPath := setupTestDirs(t, tmpDir)
t.Setenv("KITOPS_HOME", contextPath)

testKitfile := `
manifestVersion: 1.0.0
package:
name: test-repack
model:
path: test-file.txt
dataset:
- path: test-dir/test-subfile.txt
`
kitfilePath := filepath.Join(modelKitPath, constants.DefaultKitfileName)
if err := os.WriteFile(kitfilePath, []byte(testKitfile), 0644); err != nil {
t.Fatal(err)
}
setupFiles(t, modelKitPath, []string{"test-file.txt", "test-dir/test-subfile.txt"})

packOut := runCommand(t, "pack", modelKitPath, "-t", "test:repack1", "-v")
digestOne := digestFromPack(t, packOut)

// Change timestamps on file to simulate an unpacked modelkit at a future time
futureTime := time.Now().Add(time.Hour)
if err := os.Chtimes(filepath.Join(modelKitPath, "test-file.txt"), futureTime, futureTime); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(filepath.Join(modelKitPath, "test-dir"), futureTime, futureTime); err != nil {
t.Fatal(err)
}
if err := os.Chtimes(filepath.Join(modelKitPath, "test-dir/test-subfile.txt"), futureTime, futureTime); err != nil {
t.Fatal(err)
}

packOut = runCommand(t, "pack", modelKitPath, "-t", "test:repack2", "-v")
digestTwo := digestFromPack(t, packOut)

assert.Equal(t, digestOne, digestTwo, "Digests should be the same")
}

0 comments on commit b915252

Please sign in to comment.