forked from fluxcd/source-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Include path when calculating checksum
* Introduce bucket controller Ginkgo test suite * Include path in checksum calculation * Remove usage of ioutil
- Loading branch information
Showing
2 changed files
with
124 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package controllers | ||
|
||
import ( | ||
"io/fs" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
. "github.com/onsi/ginkgo" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
const ( | ||
chartTestPath = "testdata/charts/helmchart" | ||
) | ||
|
||
var _ = Describe("BucketReconciler", func() { | ||
Describe("Calculating checksum", func() { | ||
var ( | ||
tmpDir string | ||
bucket *BucketReconciler | ||
|
||
checksum string | ||
) | ||
|
||
BeforeEach(func() { | ||
tmpDir = setupTestCharts() | ||
bucket = &BucketReconciler{} | ||
|
||
var err error | ||
checksum, err = bucket.checksum(tmpDir) | ||
Expect(err).Should(Succeed()) | ||
}) | ||
|
||
AfterEach(func() { | ||
// err := os.RemoveAll(tmpDir) | ||
// Expect(err).Should(Succeed()) | ||
log.Printf("Path: %s", tmpDir) | ||
}) | ||
|
||
Context("When no changes to Helm Manifests have been performed", func() { | ||
It("should generate the same checksum", func() { | ||
actual, err := bucket.checksum(tmpDir) | ||
Expect(err).Should(Succeed()) | ||
Expect(actual).Should(Equal(checksum)) | ||
}) | ||
}) | ||
|
||
Context("When introducing a change in a Helm manifest", func() { | ||
BeforeEach(func() { | ||
err := os.WriteFile(filepath.Join(tmpDir, "some-file.yaml"), []byte("some-content"), 0644) | ||
Expect(err).Should(Succeed()) | ||
}) | ||
|
||
It("should generate a different checksum", func() { | ||
actual, err := bucket.checksum(tmpDir) | ||
Expect(err).Should(Succeed()) | ||
Expect(actual).ShouldNot(Equal(checksum)) | ||
}) | ||
}) | ||
|
||
Context("When moving Helm manifests to a subdirectory", func() { | ||
BeforeEach(func() { | ||
subdir := "duplicate" | ||
err := os.Mkdir(filepath.Join(tmpDir, subdir), 0755) | ||
Expect(err).Should(Succeed()) | ||
|
||
filename := "duplicate.yaml" | ||
srcFile := filepath.Join(tmpDir, filename) | ||
content, err := os.ReadFile(srcFile) | ||
Expect(err).Should(Succeed()) | ||
|
||
err = os.Remove(srcFile) | ||
Expect(err).Should(Succeed()) | ||
|
||
err = os.WriteFile(filepath.Join(tmpDir, subdir, filename), content, 0644) | ||
Expect(err).Should(Succeed()) | ||
}) | ||
|
||
It("should generate a different checksum", func() { | ||
actual, err := bucket.checksum(tmpDir) | ||
Expect(err).Should(Succeed()) | ||
Expect(actual).ShouldNot(Equal(checksum)) | ||
}) | ||
}) | ||
}) | ||
}) | ||
|
||
func setupTestCharts() string { | ||
tmpDir, err := os.MkdirTemp(os.TempDir(), "bucket-controller-tests*") | ||
Expect(err).Should(Succeed()) | ||
|
||
err = filepath.Walk(chartTestPath, func(path string, info fs.FileInfo, err error) error { | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if info.IsDir() { | ||
err := os.MkdirAll(filepath.Join(tmpDir, withoutTestPath(path)), 0755) | ||
Expect(err).To(Succeed()) | ||
return nil | ||
} | ||
|
||
content, err := os.ReadFile(path) | ||
Expect(err).To(Succeed()) | ||
|
||
err = os.WriteFile(filepath.Join(tmpDir, withoutTestPath(path)), content, 0644) | ||
Expect(err).To(Succeed()) | ||
|
||
return nil | ||
}) | ||
|
||
Expect(err).Should(Succeed()) | ||
|
||
return tmpDir | ||
} | ||
|
||
func withoutTestPath(path string) string { | ||
return strings.ReplaceAll(path, chartTestPath, "") | ||
} |