Skip to content

Commit 60722c0

Browse files
committed
🐛 Fix 500 on get bundle with target without a ruleSet. (#1010)
closes #1009 This only affects the API user. The UI won't create a custom target without a ruleSet. <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Bug Fixes** * Avoided a crash when processing profiles that include targets without associated rule sets by skipping those targets. * **Tests** * Added end-to-end tests for analysis profiles: seeding files, creating profiles with mixed targets, and verifying profile bundle creation and retrieval. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Signed-off-by: Jeff Ortel <jortel@redhat.com>
1 parent 82d8d66 commit 60722c0

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

internal/api/profile.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,9 @@ func (b *ApBundle) addTargets(m *model.AnalysisProfile) (err error) {
406406
if target.Builtin() {
407407
continue
408408
}
409+
if target.RuleSet == nil {
410+
continue
411+
}
409412
err = b.addRuleSet(target.RuleSet)
410413
if err != nil {
411414
return

test/binding/profile_test.go

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package binding
22

33
import (
44
"errors"
5+
"os"
56
"testing"
67

78
"github.com/konveyor/tackle2-hub/shared/api"
@@ -12,17 +13,57 @@ import (
1213
func TestAnalysisProfile(t *testing.T) {
1314
g := NewGomegaWithT(t)
1415

16+
// Grab seeded files.
17+
files, err := client.File.List()
18+
g.Expect(err).To(BeNil())
19+
g.Expect(len(files)).To(BeNumerically(">", 0))
20+
1521
// Create an identity for the profile to reference
1622
identity := &api.Identity{
1723
Name: "test-identity",
1824
Kind: "Test",
1925
}
20-
err := client.Identity.Create(identity)
26+
err = client.Identity.Create(identity)
2127
g.Expect(err).To(BeNil())
2228
t.Cleanup(func() {
2329
_ = client.Identity.Delete(identity.ID)
2430
})
2531

32+
// Create a custom target with ruleSet.
33+
targetA := &api.Target{
34+
Name: "test-target-a",
35+
Image: api.Ref{ID: files[0].ID},
36+
RuleSet: &api.RuleSet{
37+
Rules: []api.Rule{
38+
{
39+
Name: "test-rule-1",
40+
File: &api.Ref{ID: files[0].ID},
41+
},
42+
{
43+
Name: "test-rule-2",
44+
},
45+
},
46+
},
47+
}
48+
err = client.Target.Create(targetA)
49+
g.Expect(err).To(BeNil())
50+
g.Expect(targetA.ID).NotTo(BeZero())
51+
t.Cleanup(func() {
52+
_ = client.Target.Delete(targetA.ID)
53+
})
54+
55+
// Create a custom target without ruleSet.
56+
targetB := &api.Target{
57+
Name: "test-target-no-ruleset",
58+
Image: api.Ref{ID: files[0].ID},
59+
}
60+
err = client.Target.Create(targetB)
61+
g.Expect(err).To(BeNil())
62+
g.Expect(targetB.ID).NotTo(BeZero())
63+
t.Cleanup(func() {
64+
_ = client.Target.Delete(targetB.ID)
65+
})
66+
2667
// Define the profile to create
2768
profile := &api.AnalysisProfile{
2869
Name: "Test Profile",
@@ -48,11 +89,13 @@ func TestAnalysisProfile(t *testing.T) {
4889
},
4990
Repository: &api.Repository{
5091
URL: "https://github.com/konveyor/rulesets.git",
51-
Path: "default/generated/camel3",
92+
Path: "stable/java/camel3",
5293
},
5394
Targets: []api.ApTargetRef{
5495
{ID: 2, Name: "Containerization"},
5596
{ID: 6, Name: "OpenJDK", Selection: "konveyor.io/target=openjdk17"},
97+
{ID: targetA.ID, Name: targetA.Name},
98+
{ID: targetB.ID, Name: targetB.Name},
5699
},
57100
},
58101
}
@@ -100,6 +143,19 @@ func TestAnalysisProfile(t *testing.T) {
100143
eq, report = cmp.Eq(profile, updated, "UpdateUser")
101144
g.Expect(eq).To(BeTrue(), report)
102145

146+
// GET: bundle.
147+
f, err := os.CreateTemp("", "bundle-*.tar")
148+
g.Expect(err).To(BeNil())
149+
defer func() {
150+
_ = os.Remove(f.Name())
151+
}()
152+
_ = f.Close()
153+
err = client.AnalysisProfile.GetBundle(profile.ID, f.Name())
154+
g.Expect(err).To(BeNil())
155+
st, err := os.Stat(f.Name())
156+
g.Expect(err).To(BeNil())
157+
g.Expect(st.Size()).NotTo(BeZero())
158+
103159
// DELETE: Remove the profile
104160
err = client.AnalysisProfile.Delete(profile.ID)
105161
g.Expect(err).To(BeNil())

0 commit comments

Comments
 (0)