Skip to content

Commit 9463d5b

Browse files
committed
more tests
1 parent a21294d commit 9463d5b

File tree

3 files changed

+151
-65
lines changed

3 files changed

+151
-65
lines changed

api/ocm/extensions/blobhandler/handlers/generic/ocirepo/explicit_hint_test.go

+24-4
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@ import (
55
. "github.com/onsi/ginkgo/v2"
66
. "github.com/onsi/gomega"
77
. "ocm.software/ocm/api/helper/builder"
8-
. "ocm.software/ocm/api/oci/testhelper"
9-
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
10-
"ocm.software/ocm/api/ocm/refhints"
11-
128
"ocm.software/ocm/api/oci"
139
ctfoci "ocm.software/ocm/api/oci/extensions/repositories/ctf"
1410
"ocm.software/ocm/api/oci/grammar"
11+
. "ocm.software/ocm/api/oci/testhelper"
1512
v1 "ocm.software/ocm/api/ocm/compdesc/meta/v1"
1613
"ocm.software/ocm/api/ocm/extensions/accessmethods/localblob"
1714
"ocm.software/ocm/api/ocm/extensions/accessmethods/ociartifact"
1815
resourcetypes "ocm.software/ocm/api/ocm/extensions/artifacttypes"
1916
"ocm.software/ocm/api/ocm/extensions/attrs/ociuploadattr"
2017
"ocm.software/ocm/api/ocm/extensions/blobhandler"
2118
"ocm.software/ocm/api/ocm/extensions/repositories/comparch"
19+
"ocm.software/ocm/api/ocm/extensions/repositories/composition"
2220
ctfocm "ocm.software/ocm/api/ocm/extensions/repositories/ctf"
21+
"ocm.software/ocm/api/ocm/refhints"
2322
"ocm.software/ocm/api/ocm/tools/transfer"
2423
"ocm.software/ocm/api/ocm/tools/transfer/transferhandler/standard"
2524
"ocm.software/ocm/api/utils/accessio"
2625
"ocm.software/ocm/api/utils/accessobj"
26+
"ocm.software/ocm/api/utils/runtime"
2727
)
2828

2929
const LOCALNAMESPACE = "ocm/local"
@@ -100,6 +100,26 @@ var _ = Describe("explicit hint upload", func() {
100100
ra := Must(cv.GetResourceByIndex(0))
101101

102102
Expect(ra.GetReferenceHints().Serialize(true)).To(Equal("oci::" + LOCALNAMESPACE + ":" + LOCALVERS))
103+
Expect(ra.ReferenceHintForAccess().Serialize(true)).To(Equal(""))
104+
})
105+
106+
It("validated legacy hint", func() {
107+
ctx := env.OCMContext()
108+
109+
ctf := Must(ctfocm.Open(ctx, accessobj.ACC_WRITABLE, CTF, 0o700, env))
110+
defer Close(ctf, "ctf")
111+
112+
cv := Must(ctf.LookupComponentVersion(COMP, VERS))
113+
defer Close(cv, "component version")
114+
115+
a := cv.GetDescriptor().Resources[0].Access
116+
Expect(a.(*runtime.UnstructuredVersionedTypedObject).Object["referenceName"]).To(BeNil())
117+
a.(*runtime.UnstructuredVersionedTypedObject).Object["referenceName"] = OCINAMESPACE + ":" + OCIVERSION
118+
119+
ra := Must(cv.GetResourceByIndex(0))
120+
121+
Expect(ra.GetReferenceHints().Serialize(true)).To(Equal("oci::" + LOCALNAMESPACE + ":" + LOCALVERS + ";implicit=true,reference=" + OCINAMESPACE + ":" + OCIVERSION))
122+
Expect(ra.ReferenceHintForAccess().Serialize(true)).To(Equal("implicit=true,reference=" + OCINAMESPACE + ":" + OCIVERSION))
103123
})
104124

105125
It("validated original digest", func() {

api/ocm/refhints/hints.go

+21-2
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,29 @@ func ParseHints(v string, implicit ...bool) ReferenceHints {
293293
state = 0
294294
}
295295
fallthrough
296-
case 0: // type
296+
case 0: // type or plain value
297297
if c == ':' {
298298
state = 1
299299
}
300+
if c == '=' {
301+
hint = DefaultReferenceHint{}
302+
prop = v[start:i]
303+
start = i + 1
304+
state = 3
305+
}
306+
if c == ',' || c == ';' {
307+
hint = DefaultReferenceHint{}
308+
hint[HINT_REFERENCE] = v[start:i]
309+
start = i + 1
310+
if c == ',' {
311+
state = 7
312+
}
313+
if c == ';' {
314+
hints = append(hints, hint)
315+
hint = nil
316+
state = -1
317+
}
318+
}
300319
case 1: // colon
301320
if c == ':' {
302321
hint = newHint(impl).SetProperty(HINT_TYPE, v[start:i-1])
@@ -346,7 +365,7 @@ func ParseHints(v string, implicit ...bool) ReferenceHints {
346365
hint = nil
347366
state = -1
348367
} else {
349-
state = 2
368+
state = 7
350369
}
351370
}
352371
case 5: // escaped value

api/ocm/refhints/hints_test.go

+106-59
Original file line numberDiff line numberDiff line change
@@ -5,108 +5,155 @@ import (
55

66
. "github.com/onsi/ginkgo/v2"
77
. "github.com/onsi/gomega"
8-
v1 "ocm.software/ocm/api/ocm/refhints"
8+
9+
"ocm.software/ocm/api/ocm/refhints"
910
)
1011

1112
var _ = Describe("Hints Test Environment", func() {
1213
Context("hint", func() {
1314
It("single attr", func() {
14-
CheckHint("test", v1.New("", "test"))
15-
CheckHint(`te\st`, v1.New("", "te\\st"))
16-
CheckHint(`"test"`, v1.New("", "test"))
17-
CheckHint(`"te;st"`, v1.New("", "te;st"))
18-
CheckHint(`"te,st"`, v1.New("", "te,st"))
19-
CheckHint("typ::te\\st", v1.New("typ", "te\\st"))
20-
CheckHint("typ::test", v1.New("typ", "test"))
21-
CheckHint(`typ::"te\"st"`, v1.New("typ", `te"st`))
22-
CheckHint(`typ::te\st`, v1.New("typ", `te\st`))
23-
CheckHint(`typ::x="te\\;st"`, v1.DefaultReferenceHint{
24-
v1.HINT_TYPE: "typ",
25-
"x": `te\;st`,
15+
CheckHint("test", refhints.New("", "test"))
16+
CheckHint(`te\st`, refhints.New("", "te\\st"))
17+
CheckHint(`"test"`, refhints.New("", "test"))
18+
CheckHint(`"te;st"`, refhints.New("", "te;st"))
19+
CheckHint(`"te,st"`, refhints.New("", "te,st"))
20+
CheckHint("typ::te\\st", refhints.New("typ", "te\\st"))
21+
CheckHint("typ::test", refhints.New("typ", "test"))
22+
CheckHint(`typ::"te\"st"`, refhints.New("typ", `te"st`))
23+
CheckHint(`typ::te\st`, refhints.New("typ", `te\st`))
24+
CheckHint(`typ::x="te\\;st"`, refhints.DefaultReferenceHint{
25+
refhints.HINT_TYPE: "typ",
26+
"x": `te\;st`,
27+
})
28+
CheckHint(`typ::x=test`, refhints.DefaultReferenceHint{
29+
refhints.HINT_TYPE: "typ",
30+
"x": `test`,
31+
})
32+
CheckHint(`typ::xy=test`, refhints.DefaultReferenceHint{
33+
refhints.HINT_TYPE: "typ",
34+
"xy": `test`,
35+
})
36+
CheckHint(`typ::x,=test`, refhints.DefaultReferenceHint{
37+
refhints.HINT_TYPE: "typ",
38+
"x,": `test`,
2639
})
27-
CheckHint(`typ::x=test`, v1.DefaultReferenceHint{
28-
v1.HINT_TYPE: "typ",
29-
"x": `test`,
40+
CheckHint("ref=test,x=y", refhints.DefaultReferenceHint{
41+
"ref": "test",
42+
"x": "y",
3043
})
31-
CheckHint(`typ::xy=test`, v1.DefaultReferenceHint{
32-
v1.HINT_TYPE: "typ",
33-
"xy": `test`,
44+
CheckHint(`"test,x=y"`, refhints.DefaultReferenceHint{
45+
refhints.HINT_REFERENCE: "test,x=y",
3446
})
35-
CheckHint(`typ::x,=test`, v1.DefaultReferenceHint{
36-
v1.HINT_TYPE: "typ",
37-
"x,": `test`,
47+
CheckHint2("test,x=y", "reference=test,x=y", refhints.DefaultReferenceHint{
48+
refhints.HINT_REFERENCE: "test",
49+
"x": "y",
3850
})
3951
})
4052

4153
It("multi attr", func() {
42-
CheckHint(`typ::x=te\st,xy=test`, v1.DefaultReferenceHint{
43-
v1.HINT_TYPE: "typ",
44-
"x": `te\st`,
45-
"xy": "test",
54+
CheckHint(`typ::x=te\st,xy=test`, refhints.DefaultReferenceHint{
55+
refhints.HINT_TYPE: "typ",
56+
"x": `te\st`,
57+
"xy": "test",
4658
})
4759
})
4860
})
4961

5062
Context("hints", func() {
5163
It("regular", func() {
5264
CheckHint("x::y;test",
53-
v1.New("x", `y`),
54-
v1.New("", "test"))
65+
refhints.New("x", `y`),
66+
refhints.New("", "test"))
5567
CheckHint("x::y;typ::test",
56-
v1.New("x", `y`),
57-
v1.New("typ", "test"))
68+
refhints.New("x", `y`),
69+
refhints.New("typ", "test"))
5870
CheckHint(`x::y;typ::"te\"st"`,
59-
v1.New("x", `y`),
60-
v1.New("typ", `te"st`))
71+
refhints.New("x", `y`),
72+
refhints.New("typ", `te"st`))
6173
CheckHint(`x::y;typ::"te;st"`,
62-
v1.New("x", `y`),
63-
v1.New("typ", `te;st`))
74+
refhints.New("x", `y`),
75+
refhints.New("typ", `te;st`))
6476
CheckHint(`x::y;typ::"te,st"`,
65-
v1.New("x", `y`),
66-
v1.New("typ", `te,st`))
77+
refhints.New("x", `y`),
78+
refhints.New("typ", `te,st`))
6779
CheckHint(`x::y;typ::te\st`,
68-
v1.New("x", `y`),
69-
v1.New("typ", `te\st`))
80+
refhints.New("x", `y`),
81+
refhints.New("typ", `te\st`))
7082
CheckHint(`x::y;typ::x=te\st`,
71-
v1.New("x", `y`),
72-
v1.DefaultReferenceHint{
73-
v1.HINT_TYPE: "typ",
74-
"x": `te\st`,
83+
refhints.New("x", `y`),
84+
refhints.DefaultReferenceHint{
85+
refhints.HINT_TYPE: "typ",
86+
"x": `te\st`,
7587
})
7688
CheckHint(`x::y;typ::x=test`,
77-
v1.New("x", `y`),
78-
v1.DefaultReferenceHint{
79-
v1.HINT_TYPE: "typ",
80-
"x": `test`,
89+
refhints.New("x", `y`),
90+
refhints.DefaultReferenceHint{
91+
refhints.HINT_TYPE: "typ",
92+
"x": `test`,
8193
})
8294
CheckHint(`x::y;typ::xy=test`,
83-
v1.New("x", `y`),
84-
v1.DefaultReferenceHint{
85-
v1.HINT_TYPE: "typ",
86-
"xy": `test`,
95+
refhints.New("x", `y`),
96+
refhints.DefaultReferenceHint{
97+
refhints.HINT_TYPE: "typ",
98+
"xy": `test`,
8799
})
88100
CheckHint(`x::y;typ::x,=test`,
89-
v1.New("x", `y`),
90-
v1.DefaultReferenceHint{
91-
v1.HINT_TYPE: "typ",
92-
"x,": `test`,
101+
refhints.New("x", `y`),
102+
refhints.DefaultReferenceHint{
103+
refhints.HINT_TYPE: "typ",
104+
"x,": `test`,
105+
})
106+
CheckHint(`xy;typ::x,=test`,
107+
refhints.New("", `xy`),
108+
refhints.DefaultReferenceHint{
109+
refhints.HINT_TYPE: "typ",
110+
"x,": `test`,
111+
})
112+
CheckHint(`"x;y";typ::x,=test`,
113+
refhints.New("", `x;y`),
114+
refhints.DefaultReferenceHint{
115+
refhints.HINT_TYPE: "typ",
116+
"x,": `test`,
93117
})
94118
})
95119

96120
It("special", func() {
97121
CheckHint(`typ::x;=test`,
98-
v1.New("typ", "x"),
99-
v1.New("", "=test"),
122+
refhints.New("typ", "x"),
123+
refhints.DefaultReferenceHint{
124+
"": "test",
125+
},
100126
)
101127
})
102128
})
129+
130+
Context("implicit", func() {
131+
It("regular", func() {
132+
CheckHintImplicit("", "test", "implicit=true,reference=test")
133+
CheckHintImplicit("typ", "test", "typ::implicit=true,reference=test")
134+
})
135+
})
103136
})
104137

105-
func CheckHint(s string, h ...v1.ReferenceHint) {
106-
r := v1.ParseHints(s)
138+
func CheckHint(s string, h ...refhints.ReferenceHint) {
139+
r := refhints.ParseHints(s)
107140
if strings.HasPrefix(s, "\"") && !strings.ContainsAny(s, ",;") {
108141
s = s[1 : len(s)-1]
109142
}
110-
ExpectWithOffset(1, r).To(Equal(v1.ReferenceHints(h)))
143+
ExpectWithOffset(1, r).To(Equal(refhints.ReferenceHints(h)))
111144
ExpectWithOffset(1, r.Serialize()).To(Equal(s))
112145
}
146+
147+
func CheckHint2(s string, ser string, h ...refhints.ReferenceHint) {
148+
r := refhints.ParseHints(s)
149+
ExpectWithOffset(1, r).To(Equal(refhints.ReferenceHints(h)))
150+
ExpectWithOffset(1, r.Serialize()).To(Equal(ser))
151+
}
152+
153+
func CheckHintImplicit(typ, ref, ser string) {
154+
h := refhints.New(typ, ref, true)
155+
s := h.Serialize(true)
156+
ExpectWithOffset(1, h.Serialize(true)).To(Equal(ser))
157+
r := refhints.ParseHints(s)
158+
ExpectWithOffset(1, r).To(Equal(refhints.ReferenceHints{h}))
159+
}

0 commit comments

Comments
 (0)