Skip to content

Commit 6d2b87c

Browse files
author
Eric Stroczynski
authored
internal/generate/crd: fix flaky unit test (#2410)
strings containing numbers starting with 0 that contain non-octal digits, ex. "012345678". This commit fixes a flakey unit test that occasionally triggers this bug. A more thorough fix will be implemented in the near future. * internal/generate/crd: fix controller-gen octal string bug
1 parent 0deb782 commit 6d2b87c

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

internal/generate/crd/crd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ func (g crdGenerator) generateGo() (map[string][]byte, error) {
154154
// Generate files in the generator's cache so we can modify the file name
155155
// and annotations.
156156
defName := "output:crd:cache"
157-
cacheOutputDir := string(filepath.Separator) + filepath.Clean(g.OutputDir)
157+
cacheOutputDir := filepath.Clean(g.OutputDir)
158158
rawOpts := []string{
159159
"crd",
160160
fmt.Sprintf("paths=%s/...", fileutil.DotPath(g.Inputs[APIsDirKey])),

internal/generate/crd/crd_test.go

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,14 @@
1515
package crd
1616

1717
import (
18-
"io/ioutil"
18+
"encoding/base32"
1919
"math/rand"
2020
"os"
2121
"path"
2222
"path/filepath"
2323
"strconv"
2424
"testing"
25+
"time"
2526

2627
gen "github.com/operator-framework/operator-sdk/internal/generate/gen"
2728
"github.com/operator-framework/operator-sdk/internal/scaffold"
@@ -41,13 +42,24 @@ var (
4142
testAPIVersion = path.Join(testGroup, testVersion)
4243
)
4344

45+
func init() {
46+
rand.Seed(time.Now().UnixNano())
47+
}
48+
49+
// randomString returns a base32-encoded random string, reasonably sized for
50+
// directory creation.
51+
func randomString() string {
52+
rb := []byte(strconv.Itoa(rand.Int() % (2 << 20)))
53+
return base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString(rb)
54+
}
55+
4456
func TestGenerate(t *testing.T) {
45-
tmp, err := ioutil.TempDir("", "")
46-
if err != nil {
57+
tmp := filepath.Join(os.TempDir(), randomString())
58+
if err := os.MkdirAll(tmp, 0755); err != nil {
4759
t.Fatal(err)
4860
}
4961
defer func() {
50-
if err = os.RemoveAll(tmp); err != nil {
62+
if err := os.RemoveAll(tmp); err != nil {
5163
t.Fatal(err)
5264
}
5365
}()
@@ -66,7 +78,7 @@ func TestGenerate(t *testing.T) {
6678
Inputs: map[string]string{
6779
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
6880
},
69-
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
81+
OutputDir: filepath.Join(tmp, randomString()),
7082
}),
7183
},
7284
{
@@ -75,7 +87,7 @@ func TestGenerate(t *testing.T) {
7587
Inputs: map[string]string{
7688
APIsDirKey: filepath.Join(testGoDataDir, scaffold.ApisDir),
7789
},
78-
OutputDir: filepath.Join(tmp, strconv.Itoa(rand.Int())),
90+
OutputDir: filepath.Join(tmp, randomString()),
7991
}, *r),
8092
},
8193
}

0 commit comments

Comments
 (0)