Skip to content

Commit 0934d42

Browse files
Merge pull request #150 from timflannagan/fix-opm-index-prune-panic
Bug 1943284: Fix nil function in buildContext return values
2 parents b5600cf + bb05a86 commit 0934d42

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed

staging/operator-registry/pkg/lib/indexer/indexer.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ func copyDatabaseTo(databaseFile, targetDir string) (string, error) {
396396
}
397397

398398
func buildContext(generate bool, requestedDockerfile string) (buildDir, outDockerfile string, cleanup func(), err error) {
399+
// set cleanup to a no-op until explicitly set
400+
cleanup = func() {}
401+
399402
if generate {
400403
buildDir = "./"
401404
if len(requestedDockerfile) == 0 {

staging/operator-registry/pkg/lib/indexer/indexer_test.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,3 +83,61 @@ func TestGeneratePackageYaml(t *testing.T) {
8383

8484
_ = os.RemoveAll("./package.yaml")
8585
}
86+
87+
func TestBuildContext(t *testing.T) {
88+
// TODO(): Test does not currently have a clean way
89+
// of testing the generated returned values such as
90+
// outDockerfile and buildDir.
91+
92+
defaultBuildDirOnGenerate := "./"
93+
fooDockerfile := "foo.Dockerfile"
94+
defaultDockerfile := defaultDockerfileName
95+
96+
cases := []struct {
97+
generate bool
98+
requestedDockerfile string
99+
expectedBuildDir *string // return values not checked if nil
100+
expectedOutDockerfile *string // return values not checked if nil
101+
}{
102+
{
103+
generate: true,
104+
requestedDockerfile: "",
105+
expectedOutDockerfile: &defaultDockerfile,
106+
expectedBuildDir: &defaultBuildDirOnGenerate,
107+
},
108+
{
109+
generate: false,
110+
requestedDockerfile: "foo.Dockerfile",
111+
expectedOutDockerfile: &fooDockerfile,
112+
expectedBuildDir: nil,
113+
},
114+
{
115+
generate: false,
116+
requestedDockerfile: "",
117+
expectedOutDockerfile: nil,
118+
expectedBuildDir: nil,
119+
},
120+
}
121+
122+
for _, testCase := range cases {
123+
actualBuildDir, actualOutDockerfile, actualCleanup, _ := buildContext(
124+
testCase.generate, testCase.requestedDockerfile)
125+
126+
if actualCleanup == nil {
127+
// prevent regression - cleanup should never be nil
128+
t.Fatal("buildContext returned nil cleanup function")
129+
}
130+
131+
if testCase.expectedOutDockerfile != nil && actualOutDockerfile != *testCase.expectedOutDockerfile {
132+
t.Fatalf("comparing outDockerfile: expected %v actual %v",
133+
*testCase.expectedOutDockerfile,
134+
actualOutDockerfile)
135+
}
136+
137+
if testCase.expectedBuildDir != nil && actualBuildDir != *testCase.expectedBuildDir {
138+
t.Fatalf("comparing buildDir: expected %v actual %v",
139+
*testCase.expectedBuildDir,
140+
actualBuildDir)
141+
}
142+
}
143+
}

vendor/github.com/operator-framework/operator-registry/pkg/lib/indexer/indexer.go

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)