feat(pkger): extend SummaryLabel with its env references

references: #18407
pull/18448/head
Johnny Steenbergen 2020-06-10 13:37:41 -07:00 committed by Johnny Steenbergen
parent 82185dd434
commit bb270f1145
6 changed files with 80 additions and 97 deletions

View File

@ -8486,10 +8486,15 @@ components:
type: string
name:
type: string
properties:
type: object
properties:
color:
type: string
description:
type: string
retentionPeriod:
type: string
envReferences:
$ref: "#/components/schemas/PkgEnvReferences"
PkgChart:
type: object
properties:

View File

@ -597,6 +597,7 @@ type SummaryLabel struct {
Color string `json:"color"`
Description string `json:"description"`
} `json:"properties"`
EnvReferences []SummaryReference `json:"envReferences"`
}
// SummaryLabelMapping provides a summary of a label mapped with a single resource.

View File

@ -1134,6 +1134,7 @@ func (l *label) summarize() SummaryLabel {
Color: l.Color,
Description: l.Description,
},
EnvReferences: l.identity.summarizeReferences(),
}
}

View File

@ -189,46 +189,38 @@ spec:
labels := pkg.Summary().Labels
require.Len(t, labels, 3)
expectedLabel := SummaryLabel{
PkgName: "label-1",
Name: "label-1",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#FFFFFF",
Description: "label 1 description",
},
}
expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description")
assert.Equal(t, expectedLabel, labels[0])
expectedLabel = SummaryLabel{
PkgName: "label-2",
Name: "label-2",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#000000",
Description: "label 2 description",
},
}
expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description")
assert.Equal(t, expectedLabel, labels[1])
expectedLabel = SummaryLabel{
PkgName: "label-3",
Name: "display name",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Description: "label 3 description",
},
}
expectedLabel = sumLabelGen("label-3", "display name", "", "label 3 description")
assert.Equal(t, expectedLabel, labels[2])
})
})
t.Run("with env refs should be valid", func(t *testing.T) {
testfileRunner(t, "testdata/label_ref.yml", func(t *testing.T, pkg *Pkg) {
actual := pkg.Summary().Labels
require.Len(t, actual, 1)
expected := sumLabelGen("env-meta-name", "env-spec-name", "", "",
SummaryReference{
Field: "metadata.name",
EnvRefKey: "meta-name",
DefaultValue: "env-meta-name",
},
SummaryReference{
Field: "spec.name",
EnvRefKey: "spec-name",
DefaultValue: "env-spec-name",
},
)
assert.Contains(t, actual, expected)
})
})
t.Run("with missing label name should error", func(t *testing.T) {
tests := []testPkgResourceError{
{
@ -3659,14 +3651,7 @@ spec:
sum := pkg.Summary()
labels := []SummaryLabel{
{
PkgName: "label-1",
Name: "label-1",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{Color: "#eee888", Description: "desc_1"},
},
sumLabelGen("label-1", "label-1", "#eee888", "desc_1"),
}
assert.Equal(t, labels, sum.Labels)
@ -4142,6 +4127,24 @@ func testfileRunner(t *testing.T, path string, testFn func(t *testing.T, pkg *Pk
}
}
func sumLabelGen(pkgName, name, color, desc string, envRefs ...SummaryReference) SummaryLabel {
if envRefs == nil {
envRefs = make([]SummaryReference, 0)
}
return SummaryLabel{
PkgName: pkgName,
Name: name,
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: color,
Description: desc,
},
EnvReferences: envRefs,
}
}
func strPtr(s string) *string {
return &s
}

View File

@ -678,33 +678,15 @@ func TestService(t *testing.T) {
sum := impact.Summary
require.Len(t, sum.Labels, 3)
assert.Contains(t, sum.Labels, SummaryLabel{
ID: 1,
OrgID: SafeID(orgID),
PkgName: "label-1",
Name: "label-1",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#FFFFFF",
Description: "label 1 description",
},
})
expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description")
expectedLabel.ID = 1
expectedLabel.OrgID = SafeID(orgID)
assert.Contains(t, sum.Labels, expectedLabel)
assert.Contains(t, sum.Labels, SummaryLabel{
ID: 2,
OrgID: SafeID(orgID),
PkgName: "label-2",
Name: "label-2",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#000000",
Description: "label 2 description",
},
})
expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description")
expectedLabel.ID = 2
expectedLabel.OrgID = SafeID(orgID)
assert.Contains(t, sum.Labels, expectedLabel)
})
})
@ -736,7 +718,6 @@ func TestService(t *testing.T) {
stubExisting := func(name string, id influxdb.ID) *influxdb.Label {
pkgLabel := pkg.mLabels[name]
fmt.Println(name, pkgLabel)
return &influxdb.Label{
// makes all pkg changes same as they are on the existing
ID: id,
@ -785,33 +766,15 @@ func TestService(t *testing.T) {
sum := impact.Summary
require.Len(t, sum.Labels, 3)
assert.Contains(t, sum.Labels, SummaryLabel{
ID: 1,
OrgID: SafeID(orgID),
PkgName: "label-1",
Name: "label-1",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#FFFFFF",
Description: "label 1 description",
},
})
expectedLabel := sumLabelGen("label-1", "label-1", "#FFFFFF", "label 1 description")
expectedLabel.ID = 1
expectedLabel.OrgID = SafeID(orgID)
assert.Contains(t, sum.Labels, expectedLabel)
assert.Contains(t, sum.Labels, SummaryLabel{
ID: 2,
OrgID: SafeID(orgID),
PkgName: "label-2",
Name: "label-2",
Properties: struct {
Color string `json:"color"`
Description string `json:"description"`
}{
Color: "#000000",
Description: "label 2 description",
},
})
expectedLabel = sumLabelGen("label-2", "label-2", "#000000", "label 2 description")
expectedLabel.ID = 2
expectedLabel.OrgID = SafeID(orgID)
assert.Contains(t, sum.Labels, expectedLabel)
assert.Equal(t, 1, fakeLabelSVC.CreateLabelCalls.Count()) // only called for second label
})

10
pkger/testdata/label_ref.yml vendored Normal file
View File

@ -0,0 +1,10 @@
apiVersion: influxdata.com/v2alpha1
kind: Label
metadata:
name:
envRef:
key: meta-name
spec:
name:
envRef:
key: spec-name