|
| 1 | +/* |
| 2 | +Copyright 2020 The Flux authors |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +package controllers |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "io/ioutil" |
| 22 | + "strings" |
| 23 | + "testing" |
| 24 | + |
| 25 | + "github.com/fluxcd/source-controller/internal/helm" |
| 26 | + helmchart "helm.sh/helm/v3/pkg/chart" |
| 27 | + "helm.sh/helm/v3/pkg/getter" |
| 28 | + "helm.sh/helm/v3/pkg/repo" |
| 29 | +) |
| 30 | + |
| 31 | +var ( |
| 32 | + helmPackageFile = "testdata/charts/helmchart-0.1.0.tgz" |
| 33 | + |
| 34 | + localDepFixture helmchart.Dependency = helmchart.Dependency{ |
| 35 | + Name: "helmchart", |
| 36 | + Version: "0.1.0", |
| 37 | + Repository: "file://../helmchart", |
| 38 | + } |
| 39 | + remoteDepFixture helmchart.Dependency = helmchart.Dependency{ |
| 40 | + Name: "helmchart", |
| 41 | + Version: "0.1.0", |
| 42 | + Repository: "https://example.com/charts", |
| 43 | + } |
| 44 | + chartFixture helmchart.Chart = helmchart.Chart{ |
| 45 | + Metadata: &helmchart.Metadata{ |
| 46 | + Name: "test", |
| 47 | + }, |
| 48 | + } |
| 49 | +) |
| 50 | + |
| 51 | +func TestBuild_WithEmptyDependencies(t *testing.T) { |
| 52 | + dm := DependencyManager{ |
| 53 | + Dependencies: nil, |
| 54 | + } |
| 55 | + if err := dm.Build(); err != nil { |
| 56 | + t.Errorf("Build() should return nil") |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func TestBuild_WithLocalChart(t *testing.T) { |
| 61 | + loc := localDepFixture |
| 62 | + chart := chartFixture |
| 63 | + dm := DependencyManager{ |
| 64 | + Chart: &chart, |
| 65 | + ChartPath: "testdata/charts/helmchart", |
| 66 | + Dependencies: []*DependencyWithRepository{ |
| 67 | + { |
| 68 | + Dependency: &loc, |
| 69 | + Repo: nil, |
| 70 | + }, |
| 71 | + }, |
| 72 | + } |
| 73 | + |
| 74 | + if err := dm.Build(); err != nil { |
| 75 | + t.Errorf("Build() expected to not return error: %s", err) |
| 76 | + } |
| 77 | + |
| 78 | + deps := dm.Chart.Dependencies() |
| 79 | + if len(deps) != 1 { |
| 80 | + t.Fatalf("chart expected to have one dependency registered") |
| 81 | + } |
| 82 | + if deps[0].Metadata.Name != localDepFixture.Name { |
| 83 | + t.Errorf("chart dependency has incorrect name, expected: %s, got: %s", localDepFixture.Name, deps[0].Metadata.Name) |
| 84 | + } |
| 85 | + if deps[0].Metadata.Version != localDepFixture.Version { |
| 86 | + t.Errorf("chart dependency has incorrect version, expected: %s, got: %s", localDepFixture.Version, deps[0].Metadata.Version) |
| 87 | + } |
| 88 | + |
| 89 | + tests := []struct { |
| 90 | + name string |
| 91 | + dep helmchart.Dependency |
| 92 | + expectError string |
| 93 | + }{ |
| 94 | + { |
| 95 | + name: "invalid path", |
| 96 | + dep: helmchart.Dependency{ |
| 97 | + Name: "helmchart", |
| 98 | + Version: "0.1.0", |
| 99 | + Repository: "file://../invalid", |
| 100 | + }, |
| 101 | + expectError: "no such file or directory", |
| 102 | + }, |
| 103 | + { |
| 104 | + name: "invalid version constraint format", |
| 105 | + dep: helmchart.Dependency{ |
| 106 | + Name: "helmchart", |
| 107 | + Version: "!2.0", |
| 108 | + Repository: "file://../helmchart", |
| 109 | + }, |
| 110 | + expectError: "has an invalid version/constraint format", |
| 111 | + }, |
| 112 | + { |
| 113 | + name: "invalid version", |
| 114 | + dep: helmchart.Dependency{ |
| 115 | + Name: "helmchart", |
| 116 | + Version: "1.0.0", |
| 117 | + Repository: "file://../helmchart", |
| 118 | + }, |
| 119 | + expectError: "can't get a valid version for dependency", |
| 120 | + }, |
| 121 | + } |
| 122 | + |
| 123 | + for _, tt := range tests { |
| 124 | + t.Run(tt.name, func(t *testing.T) { |
| 125 | + c := chartFixture |
| 126 | + dm = DependencyManager{ |
| 127 | + Chart: &c, |
| 128 | + ChartPath: "testdata/charts/helmchart", |
| 129 | + Dependencies: []*DependencyWithRepository{ |
| 130 | + { |
| 131 | + Dependency: &tt.dep, |
| 132 | + Repo: nil, |
| 133 | + }, |
| 134 | + }, |
| 135 | + } |
| 136 | + |
| 137 | + if err := dm.Build(); err == nil { |
| 138 | + t.Errorf("Build() expected to return error") |
| 139 | + } else if !strings.Contains(err.Error(), tt.expectError) { |
| 140 | + t.Errorf("Build() expected to return error: %s, got: %s", tt.expectError, err) |
| 141 | + } |
| 142 | + if len(dm.Chart.Dependencies()) > 0 { |
| 143 | + t.Fatalf("chart expected to have no dependencies registered") |
| 144 | + } |
| 145 | + }) |
| 146 | + } |
| 147 | +} |
| 148 | + |
| 149 | +func TestBuild_WithRemoteChart(t *testing.T) { |
| 150 | + chart := chartFixture |
| 151 | + b, err := ioutil.ReadFile(helmPackageFile) |
| 152 | + if err != nil { |
| 153 | + t.Fatal(err) |
| 154 | + } |
| 155 | + i := repo.NewIndexFile() |
| 156 | + i.Add(&helmchart.Metadata{Name: "helmchart", Version: "0.1.0"}, "helmchart-0.1.0.tgz", "http://example.com/charts", "sha256:1234567890") |
| 157 | + mg := mockGetter{response: b} |
| 158 | + cr := &helm.ChartRepository{ |
| 159 | + URL: remoteDepFixture.Repository, |
| 160 | + Index: i, |
| 161 | + Client: &mg, |
| 162 | + } |
| 163 | + dm := DependencyManager{ |
| 164 | + Chart: &chart, |
| 165 | + Dependencies: []*DependencyWithRepository{ |
| 166 | + { |
| 167 | + Dependency: &remoteDepFixture, |
| 168 | + Repo: cr, |
| 169 | + }, |
| 170 | + }, |
| 171 | + } |
| 172 | + |
| 173 | + if err := dm.Build(); err != nil { |
| 174 | + t.Errorf("Build() expected to not return error: %s", err) |
| 175 | + } |
| 176 | + |
| 177 | + deps := dm.Chart.Dependencies() |
| 178 | + if len(deps) != 1 { |
| 179 | + t.Fatalf("chart expected to have one dependency registered") |
| 180 | + } |
| 181 | + if deps[0].Metadata.Name != remoteDepFixture.Name { |
| 182 | + t.Errorf("chart dependency has incorrect name, expected: %s, got: %s", remoteDepFixture.Name, deps[0].Metadata.Name) |
| 183 | + } |
| 184 | + if deps[0].Metadata.Version != remoteDepFixture.Version { |
| 185 | + t.Errorf("chart dependency has incorrect version, expected: %s, got: %s", remoteDepFixture.Version, deps[0].Metadata.Version) |
| 186 | + } |
| 187 | + |
| 188 | + // When repo is not set |
| 189 | + dm.Dependencies[0].Repo = nil |
| 190 | + if err := dm.Build(); err == nil { |
| 191 | + t.Errorf("Build() expected to return error") |
| 192 | + } else if !strings.Contains(err.Error(), "chartrepo should not be nil") { |
| 193 | + t.Errorf("Build() expected to return different error, got: %s", err) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +type mockGetter struct { |
| 198 | + response []byte |
| 199 | +} |
| 200 | + |
| 201 | +func (g *mockGetter) Get(url string, options ...getter.Option) (*bytes.Buffer, error) { |
| 202 | + return bytes.NewBuffer(g.response), nil |
| 203 | +} |
0 commit comments