From 716be14ad3d253d696a9219076437218b1cc981e Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Mon, 5 Jun 2017 14:20:41 -0700 Subject: [PATCH] Updated code to read requirements.yaml as per following structure ``` - name: alias: version: repository: ``` --- pkg/chartutil/requirements.go | 39 ++++++-------- pkg/chartutil/requirements_test.go | 52 ++++++++++--------- .../dependent-chart-alias/requirements.yaml | 8 +-- pkg/resolver/resolver_test.go | 2 +- 4 files changed, 49 insertions(+), 52 deletions(-) diff --git a/pkg/chartutil/requirements.go b/pkg/chartutil/requirements.go index a656c2ea2..c681b6509 100644 --- a/pkg/chartutil/requirements.go +++ b/pkg/chartutil/requirements.go @@ -66,7 +66,7 @@ type Dependency struct { // string or pair of child/parent sublist items. ImportValues []interface{} `json:"import-values"` // Alias usable alias to be used for the chart - Alias []string `json:"alias"` + Alias string `json:"alias"` } // ErrNoRequirementsFile to detect error condition @@ -218,7 +218,7 @@ func ProcessRequirementsTags(reqs *Requirements, cvals Values) { } -func updateChartDependencyAlias(charts []*chart.Chart, dependentChart, aliasChart string, firstAlias bool) *chart.Chart { +func getAliasDependency(charts []*chart.Chart, aliasChart *Dependency) *chart.Chart { var chartFound chart.Chart for _, existingChart := range charts { if existingChart == nil { @@ -227,17 +227,17 @@ func updateChartDependencyAlias(charts []*chart.Chart, dependentChart, aliasChar if existingChart.Metadata == nil { continue } - if existingChart.Metadata.Name != dependentChart { + if existingChart.Metadata.Name != aliasChart.Name { continue } - if firstAlias { - existingChart.Metadata.Name = aliasChart - return nil + if existingChart.Metadata.Version != aliasChart.Version { + continue } - chartFound = *existingChart newMetadata := *existingChart.Metadata - newMetadata.Name = aliasChart + if aliasChart.Alias != "" { + newMetadata.Name = aliasChart.Alias + } chartFound.Metadata = &newMetadata return &chartFound } @@ -257,25 +257,16 @@ func ProcessRequirementsEnabled(c *chart.Chart, v *chart.Config) error { return nil } + var chartDependencies []*chart.Chart for _, req := range reqs.Dependencies { - var firstAlias = true - var dependentChartName = req.Name - for _, alias := range req.Alias { - aliasDependency := updateChartDependencyAlias(c.Dependencies, dependentChartName, alias, firstAlias) - if firstAlias { - dependentChartName = alias - firstAlias = false - continue - } - if aliasDependency == nil { - break - } - c.Dependencies = append(c.Dependencies, aliasDependency) - req.Name = alias - reqs.Dependencies = append(reqs.Dependencies, req) - req.Name = dependentChartName + if chartDependency := getAliasDependency(c.Dependencies, req); chartDependency != nil { + chartDependencies = append(chartDependencies, chartDependency) + } + if req.Alias != "" { + req.Name = req.Alias } } + c.Dependencies = chartDependencies // set all to true for _, lr := range reqs.Dependencies { diff --git a/pkg/chartutil/requirements_test.go b/pkg/chartutil/requirements_test.go index 8e299a09b..b6502078d 100644 --- a/pkg/chartutil/requirements_test.go +++ b/pkg/chartutil/requirements_test.go @@ -321,33 +321,37 @@ func verifyRequirementsImportValues(t *testing.T, c *chart.Chart, v *chart.Confi } } -func TestUpdateChartDependencyAlias(t *testing.T) { +func TestGetAliasDependency(t *testing.T) { c, err := Load("testdata/frobnitz") if err != nil { t.Fatalf("Failed to load testdata: %s", err) } - - if aliasChart := updateChartDependencyAlias(c.Dependencies, "mariners", "another-mariner", false); aliasChart != nil { - t.Fatalf("expected no chart but got %s", aliasChart.Metadata.Name) + req, err := LoadRequirements(c) + if err != nil { + t.Fatalf("Failed to load requirement for testdata: %s", err) + } + if len(req.Dependencies) == 0 { + t.Fatalf("There are no requirements to test") } - aliasChart := updateChartDependencyAlias(c.Dependencies, "mariner", "another-mariner", false) + // Success case + aliasChart := getAliasDependency(c.Dependencies, req.Dependencies[0]) if aliasChart == nil { - t.Fatal("Failed to find dependent chart") - } - if aliasChart.Metadata.Name != "another-mariner" { - t.Fatal(`Failed to update chart-name for alias "dependent chart`) + t.Fatalf("Failed to get dependency chart for alias %s", req.Dependencies[0].Name) } - - //Testing single-alias update, first update and then try same with non-first alias, we should not be able to find chart - if aliasChart := updateChartDependencyAlias(c.Dependencies, "mariner", "another-mariner", true); aliasChart != nil { - t.Fatalf("expected no chart but got %s", aliasChart.Metadata.Name) + if req.Dependencies[0].Alias != "" { + if aliasChart.Metadata.Name != req.Dependencies[0].Alias { + t.Fatalf("Dependency chart name should be %s but got %s", req.Dependencies[0].Alias, aliasChart.Metadata.Name) + } + } else if aliasChart.Metadata.Name != req.Dependencies[0].Name { + t.Fatalf("Dependency chart name should be %s but got %s", req.Dependencies[0].Name, aliasChart.Metadata.Name) } - if aliasChart := updateChartDependencyAlias(c.Dependencies, "mariner", "another-mariner", false); aliasChart != nil { + // Failure case + req.Dependencies[0].Name = "something-else" + if aliasChart := getAliasDependency(c.Dependencies, req.Dependencies[0]); aliasChart != nil { t.Fatalf("expected no chart but got %s", aliasChart.Metadata.Name) } - } func TestDependentChartAliases(t *testing.T) { @@ -374,15 +378,15 @@ func TestDependentChartAliases(t *testing.T) { t.Fatalf("Cannot load requirements for test chart, %v", err) } - var expectedDependencyCharts int - for _, reqmt := range reqmts.Dependencies { - expectedDependencyCharts++ - if len(reqmt.Alias) >= 0 { - expectedDependencyCharts += len(reqmt.Alias) - } - } - if len(c.Dependencies) != expectedDependencyCharts-1 { - t.Fatalf("Expected number of chart dependencies %d, but got %d", expectedDependencyCharts-1, len(c.Dependencies)) + // var expectedDependencyCharts int + // for _, reqmt := range reqmts.Dependencies { + // expectedDependencyCharts++ + // if len(reqmt.Alias) >= 0 { + // expectedDependencyCharts += len(reqmt.Alias) + // } + // } + if len(c.Dependencies) != len(reqmts.Dependencies) { + t.Fatalf("Expected number of chart dependencies %d, but got %d", len(reqmts.Dependencies), len(c.Dependencies)) } } diff --git a/pkg/chartutil/testdata/dependent-chart-alias/requirements.yaml b/pkg/chartutil/testdata/dependent-chart-alias/requirements.yaml index 2e41105c7..aab6cddf7 100644 --- a/pkg/chartutil/testdata/dependent-chart-alias/requirements.yaml +++ b/pkg/chartutil/testdata/dependent-chart-alias/requirements.yaml @@ -5,6 +5,8 @@ dependencies: - name: mariner version: "4.3.2" repository: https://example.com/charts - alias: - - mariners1 - - mariners2 + alias: mariners2 + - name: mariner + version: "4.3.2" + repository: https://example.com/charts + alias: mariners1 diff --git a/pkg/resolver/resolver_test.go b/pkg/resolver/resolver_test.go index 9453a979f..b87022b93 100644 --- a/pkg/resolver/resolver_test.go +++ b/pkg/resolver/resolver_test.go @@ -146,7 +146,7 @@ func TestResolve(t *testing.T) { } func TestHashReq(t *testing.T) { - expect := "sha256:917e251ddba291096889f81eb7de713ab4e1afbbb07c576dfd7d66ba9300b12b" + expect := "sha256:45b06fcc4496c705bf3d634f8a2ff84e6a6f0bdcaf010614b8886572d1e52b99" req := &chartutil.Requirements{ Dependencies: []*chartutil.Dependency{ {Name: "alpine", Version: "0.1.0", Repository: "http://localhost:8879/charts"},