From bb270f114577dcc75cf4fa370c59a2366bd0ed3b Mon Sep 17 00:00:00 2001 From: Johnny Steenbergen Date: Wed, 10 Jun 2020 13:37:41 -0700 Subject: [PATCH] feat(pkger): extend SummaryLabel with its env references references: #18407 --- http/swagger.yml | 13 ++++-- pkger/models.go | 1 + pkger/parser_models.go | 1 + pkger/parser_test.go | 83 +++++++++++++++++++----------------- pkger/service_test.go | 69 +++++++----------------------- pkger/testdata/label_ref.yml | 10 +++++ 6 files changed, 80 insertions(+), 97 deletions(-) create mode 100644 pkger/testdata/label_ref.yml diff --git a/http/swagger.yml b/http/swagger.yml index 85f1a03bfb..460bf7b38e 100644 --- a/http/swagger.yml +++ b/http/swagger.yml @@ -8486,10 +8486,15 @@ components: type: string name: type: string - description: - type: string - retentionPeriod: - type: string + properties: + type: object + properties: + color: + type: string + description: + type: string + envReferences: + $ref: "#/components/schemas/PkgEnvReferences" PkgChart: type: object properties: diff --git a/pkger/models.go b/pkger/models.go index 939a9b40ef..0aca9711f5 100644 --- a/pkger/models.go +++ b/pkger/models.go @@ -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. diff --git a/pkger/parser_models.go b/pkger/parser_models.go index dd16c69a85..9baa0630f7 100644 --- a/pkger/parser_models.go +++ b/pkger/parser_models.go @@ -1134,6 +1134,7 @@ func (l *label) summarize() SummaryLabel { Color: l.Color, Description: l.Description, }, + EnvReferences: l.identity.summarizeReferences(), } } diff --git a/pkger/parser_test.go b/pkger/parser_test.go index 3fb220a2d6..0974604597 100644 --- a/pkger/parser_test.go +++ b/pkger/parser_test.go @@ -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 } diff --git a/pkger/service_test.go b/pkger/service_test.go index b22f0fb902..6bbbee54d6 100644 --- a/pkger/service_test.go +++ b/pkger/service_test.go @@ -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 }) diff --git a/pkger/testdata/label_ref.yml b/pkger/testdata/label_ref.yml new file mode 100644 index 0000000000..9696a24e16 --- /dev/null +++ b/pkger/testdata/label_ref.yml @@ -0,0 +1,10 @@ +apiVersion: influxdata.com/v2alpha1 +kind: Label +metadata: + name: + envRef: + key: meta-name +spec: + name: + envRef: + key: spec-name