internal/helm: add cached chart build tests
Cached chart build tests for both local and remote builder. Signed-off-by: Sunny <darkowlzz@protonmail.com>
This commit is contained in:
parent
753abed30c
commit
dd3afce3be
|
@ -103,7 +103,6 @@ func TestLocalBuilder_Build(t *testing.T) {
|
||||||
wantVersion: "0.1.0+foo",
|
wantVersion: "0.1.0+foo",
|
||||||
wantPackaged: true,
|
wantPackaged: true,
|
||||||
},
|
},
|
||||||
// TODO: Test setting BuildOptions CachedChart and Force.
|
|
||||||
{
|
{
|
||||||
name: "already packaged chart",
|
name: "already packaged chart",
|
||||||
reference: LocalReference{Path: "./../testdata/charts/helmchart-0.1.0.tgz"},
|
reference: LocalReference{Path: "./../testdata/charts/helmchart-0.1.0.tgz"},
|
||||||
|
@ -236,6 +235,44 @@ fullnameOverride: "full-foo-name-override"`),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLocalBuilder_Build_CachedChart(t *testing.T) {
|
||||||
|
g := NewWithT(t)
|
||||||
|
|
||||||
|
workDir, err := os.MkdirTemp("", "local-builder-")
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(workDir)
|
||||||
|
|
||||||
|
reference := LocalReference{Path: "./../testdata/charts/helmchart"}
|
||||||
|
|
||||||
|
dm := NewDependencyManager()
|
||||||
|
b := NewLocalBuilder(dm)
|
||||||
|
|
||||||
|
tmpDir, err := os.MkdirTemp("", "local-chart-")
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
|
// Build first time.
|
||||||
|
targetPath := filepath.Join(tmpDir, "chart1.tgz")
|
||||||
|
buildOpts := BuildOptions{}
|
||||||
|
cb, err := b.Build(context.TODO(), reference, targetPath, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
// Set the result as the CachedChart for second build.
|
||||||
|
buildOpts.CachedChart = cb.Path
|
||||||
|
|
||||||
|
targetPath2 := filepath.Join(tmpDir, "chart2.tgz")
|
||||||
|
defer os.RemoveAll(targetPath2)
|
||||||
|
cb, err = b.Build(context.TODO(), reference, targetPath2, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
g.Expect(cb.Path).To(Equal(targetPath))
|
||||||
|
|
||||||
|
// Rebuild with build option Force.
|
||||||
|
buildOpts.Force = true
|
||||||
|
cb, err = b.Build(context.TODO(), reference, targetPath2, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
g.Expect(cb.Path).To(Equal(targetPath2))
|
||||||
|
}
|
||||||
|
|
||||||
func Test_mergeFileValues(t *testing.T) {
|
func Test_mergeFileValues(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
|
@ -19,12 +19,10 @@ package chart
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"context"
|
"context"
|
||||||
"math/rand"
|
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
|
||||||
|
|
||||||
. "github.com/onsi/gomega"
|
. "github.com/onsi/gomega"
|
||||||
helmchart "helm.sh/helm/v3/pkg/chart"
|
helmchart "helm.sh/helm/v3/pkg/chart"
|
||||||
|
@ -35,20 +33,6 @@ import (
|
||||||
"github.com/fluxcd/source-controller/internal/helm/repository"
|
"github.com/fluxcd/source-controller/internal/helm/repository"
|
||||||
)
|
)
|
||||||
|
|
||||||
var letterRunes = []rune("abcdefghijklmnopqrstuvwxyz1234567890")
|
|
||||||
|
|
||||||
func randStringRunes(n int) string {
|
|
||||||
b := make([]rune, n)
|
|
||||||
for i := range b {
|
|
||||||
b[i] = letterRunes[rand.Intn(len(letterRunes))]
|
|
||||||
}
|
|
||||||
return string(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
rand.Seed(time.Now().UnixNano())
|
|
||||||
}
|
|
||||||
|
|
||||||
// mockIndexChartGetter returns specific response for index and chart queries.
|
// mockIndexChartGetter returns specific response for index and chart queries.
|
||||||
type mockIndexChartGetter struct {
|
type mockIndexChartGetter struct {
|
||||||
IndexResponse []byte
|
IndexResponse []byte
|
||||||
|
@ -146,7 +130,6 @@ entries:
|
||||||
wantVersion: "6.17.4+foo",
|
wantVersion: "6.17.4+foo",
|
||||||
wantPackaged: true,
|
wantPackaged: true,
|
||||||
},
|
},
|
||||||
// TODO: Test setting BuildOptions CachedChart and Force.
|
|
||||||
{
|
{
|
||||||
name: "default values",
|
name: "default values",
|
||||||
reference: RemoteReference{Name: "grafana"},
|
reference: RemoteReference{Name: "grafana"},
|
||||||
|
@ -175,8 +158,10 @@ entries:
|
||||||
t.Run(tt.name, func(t *testing.T) {
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
g := NewWithT(t)
|
g := NewWithT(t)
|
||||||
|
|
||||||
targetPath := "/tmp/remote-chart-builder-" + randStringRunes(5) + ".tgz"
|
tmpDir, err := os.MkdirTemp("", "remote-chart-builder-")
|
||||||
defer os.RemoveAll(targetPath)
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
targetPath := filepath.Join(tmpDir, "chart.tgz")
|
||||||
|
|
||||||
if tt.repository != nil {
|
if tt.repository != nil {
|
||||||
_, err := tt.repository.CacheIndex()
|
_, err := tt.repository.CacheIndex()
|
||||||
|
@ -211,6 +196,73 @@ entries:
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoteBuilder_Build_CachedChart(t *testing.T) {
|
||||||
|
g := NewWithT(t)
|
||||||
|
|
||||||
|
chartGrafana, err := os.ReadFile("./../testdata/charts/helmchart-0.1.0.tgz")
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
g.Expect(chartGrafana).ToNot(BeEmpty())
|
||||||
|
|
||||||
|
index := []byte(`
|
||||||
|
apiVersion: v1
|
||||||
|
entries:
|
||||||
|
grafana:
|
||||||
|
- urls:
|
||||||
|
- https://example.com/grafana.tgz
|
||||||
|
description: string
|
||||||
|
version: 0.1.0
|
||||||
|
`)
|
||||||
|
|
||||||
|
mockGetter := &mockIndexChartGetter{
|
||||||
|
IndexResponse: index,
|
||||||
|
ChartResponse: chartGrafana,
|
||||||
|
}
|
||||||
|
mockRepo := func() *repository.ChartRepository {
|
||||||
|
return &repository.ChartRepository{
|
||||||
|
URL: "https://grafana.github.io/helm-charts/",
|
||||||
|
Client: mockGetter,
|
||||||
|
RWMutex: &sync.RWMutex{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
reference := RemoteReference{Name: "grafana"}
|
||||||
|
repository := mockRepo()
|
||||||
|
|
||||||
|
_, err = repository.CacheIndex()
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
// Cleanup the cache index path.
|
||||||
|
defer os.Remove(repository.CachePath)
|
||||||
|
|
||||||
|
b := NewRemoteBuilder(repository)
|
||||||
|
|
||||||
|
tmpDir, err := os.MkdirTemp("", "remote-chart-")
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
defer os.RemoveAll(tmpDir)
|
||||||
|
|
||||||
|
// Build first time.
|
||||||
|
targetPath := filepath.Join(tmpDir, "chart1.tgz")
|
||||||
|
defer os.RemoveAll(targetPath)
|
||||||
|
buildOpts := BuildOptions{}
|
||||||
|
cb, err := b.Build(context.TODO(), reference, targetPath, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
|
||||||
|
// Set the result as the CachedChart for second build.
|
||||||
|
buildOpts.CachedChart = cb.Path
|
||||||
|
|
||||||
|
// Rebuild with a new path.
|
||||||
|
targetPath2 := filepath.Join(tmpDir, "chart2.tgz")
|
||||||
|
defer os.RemoveAll(targetPath2)
|
||||||
|
cb, err = b.Build(context.TODO(), reference, targetPath2, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
g.Expect(cb.Path).To(Equal(targetPath))
|
||||||
|
|
||||||
|
// Rebuild with build option Force.
|
||||||
|
buildOpts.Force = true
|
||||||
|
cb, err = b.Build(context.TODO(), reference, targetPath2, buildOpts)
|
||||||
|
g.Expect(err).ToNot(HaveOccurred())
|
||||||
|
g.Expect(cb.Path).To(Equal(targetPath2))
|
||||||
|
}
|
||||||
|
|
||||||
func Test_mergeChartValues(t *testing.T) {
|
func Test_mergeChartValues(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
|
|
Loading…
Reference in New Issue