Skip to content

Commit

Permalink
fix: TestHelmReconciler flake (istio#46458)
Browse files Browse the repository at this point in the history
* fix racing workspace

* fix lint
  • Loading branch information
w12379564 authored Aug 11, 2023
1 parent dcd92ae commit c6150b3
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
38 changes: 32 additions & 6 deletions operator/cmd/mesh/manifest-generate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"encoding/json"
"fmt"
"io"
"io/fs"
"os"
"path"
"path/filepath"
Expand All @@ -41,6 +42,7 @@ import (
"istio.io/istio/operator/pkg/util"
"istio.io/istio/operator/pkg/util/clog"
tutil "istio.io/istio/pilot/test/util"
"istio.io/istio/pkg/file"
"istio.io/istio/pkg/test"
"istio.io/istio/pkg/test/env"
"istio.io/istio/pkg/version"
Expand Down Expand Up @@ -148,6 +150,27 @@ func extract(gzipStream io.Reader, destination string) error {
return nil
}

func copyDir(src string, dest string) error {
return filepath.Walk(src, func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}

outpath := filepath.Join(dest, strings.TrimPrefix(path, src))

if info.IsDir() {
os.MkdirAll(outpath, info.Mode())
return nil
}
cpErr := file.AtomicCopy(path, filepath.Dir(outpath), filepath.Base(outpath))
if cpErr != nil {
return cpErr
}

return nil
})
}

func TestMain(m *testing.M) {
code := m.Run()
// Cleanup uncompress snapshot charts
Expand Down Expand Up @@ -275,24 +298,27 @@ func TestManifestGenerateWithDuplicateMutatingWebhookConfig(t *testing.T) {

recreateSimpleTestEnv()

rs, err := readFile(filepath.Join(testDataDir, "input-extra-resources", testResourceFile+".yaml"))
tmpDir := t.TempDir()
tmpCharts := chartSourceType(filepath.Join(tmpDir, operatorSubdirFilePath))
err := copyDir(string(liveCharts), string(tmpCharts))
if err != nil {
t.Fatal(err)
}

err = writeFile(filepath.Join(env.IstioSrc, operatorSubdirFilePath+"/"+testIstioDiscoveryChartPath+"/"+testResourceFile+".yaml"), []byte(rs))
rs, err := readFile(filepath.Join(testDataDir, "input-extra-resources", testResourceFile+".yaml"))
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
removeFile(filepath.Join(env.IstioSrc, operatorSubdirFilePath+"/"+testIstioDiscoveryChartPath+"/"+testResourceFile+".yaml"))
})
err = writeFile(filepath.Join(tmpDir, operatorSubdirFilePath+"/"+testIstioDiscoveryChartPath+"/"+testResourceFile+".yaml"), []byte(rs))
if err != nil {
t.Fatal(err)
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
g := NewWithT(t)
objs, err := fakeControllerReconcile(testResourceFile, liveCharts, &helmreconciler.Options{Force: tc.force, SkipPrune: true})
objs, err := fakeControllerReconcile(testResourceFile, tmpCharts, &helmreconciler.Options{Force: tc.force, SkipPrune: true})
tc.assertFunc(g, objs, err)
})
}
Expand Down
5 changes: 0 additions & 5 deletions operator/cmd/mesh/manifest_shared_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,6 @@ func writeFile(path string, data []byte) error {
return os.WriteFile(path, data, 0o644)
}

// removeFile removes given file from provided path.
func removeFile(path string) error {
return os.Remove(path)
}

// inFileAbsolutePath returns the absolute path for an input file like "gateways".
func inFileAbsolutePath(inFile string) string {
return filepath.Join(testDataDir, "input", inFile+".yaml")
Expand Down

0 comments on commit c6150b3

Please sign in to comment.