Skip to content

Commit

Permalink
Include path when calculating checksum
Browse files Browse the repository at this point in the history
* Introduce bucket controller Ginkgo test suite
* Include path in checksum calculation
* Remove usage of ioutil
  • Loading branch information
sata committed Jul 26, 2021
1 parent 40a4767 commit adc7a6f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 4 deletions.
7 changes: 3 additions & 4 deletions controllers/bucket_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
"context"
"crypto/sha1"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -184,7 +183,7 @@ func (r *BucketReconciler) reconcile(ctx context.Context, bucket sourcev1.Bucket
}

// create tmp dir
tempDir, err := ioutil.TempDir("", bucket.Name)
tempDir, err := os.MkdirTemp("", bucket.Name)
if err != nil {
err = fmt.Errorf("tmp dir error: %w", err)
return sourcev1.BucketNotReady(bucket, sourcev1.StorageOperationFailedReason, err.Error()), err
Expand Down Expand Up @@ -365,11 +364,11 @@ func (r *BucketReconciler) checksum(root string) (string, error) {
if !info.Mode().IsRegular() {
return nil
}
data, err := ioutil.ReadFile(path)
data, err := os.ReadFile(path)
if err != nil {
return err
}
checksum += fmt.Sprintf("%x", sha1.Sum(data))
checksum += fmt.Sprintf("%x", sha1.Sum(append([]byte(path), data...)))
return nil
})
if err != nil {
Expand Down
121 changes: 121 additions & 0 deletions controllers/bucket_controller_test.go
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, "")
}

0 comments on commit adc7a6f

Please sign in to comment.