From a9939d5067f447cb9f0f3a0355e33196e9a3f9ba Mon Sep 17 00:00:00 2001 From: Andrei Pohilko Date: Wed, 8 Feb 2023 10:11:18 +0000 Subject: [PATCH] Improve tests for repositories Relates to #210 --- pkg/dashboard/objects/repos_test.go | 140 ++++++++---------- .../objects/testdata/repositories.yaml | 3 + .../objects/testdata/testing-index.yaml | 66 +++++++++ 3 files changed, 129 insertions(+), 80 deletions(-) create mode 100644 pkg/dashboard/objects/testdata/testing-index.yaml diff --git a/pkg/dashboard/objects/repos_test.go b/pkg/dashboard/objects/repos_test.go index 8fe90a8..ceb517f 100644 --- a/pkg/dashboard/objects/repos_test.go +++ b/pkg/dashboard/objects/repos_test.go @@ -2,22 +2,47 @@ package objects import ( "helm.sh/helm/v3/pkg/action" + "io/ioutil" + "os" + "path" "testing" "gotest.tools/v3/assert" "helm.sh/helm/v3/pkg/cli" - "helm.sh/helm/v3/pkg/repo" ) var filePath = "./testdata/repositories.yaml" -func initRepository(t *testing.T, filePath string) *Repositories { +func initRepository(t *testing.T) *Repositories { t.Helper() settings := cli.New() + fname, err := ioutil.TempFile("", "repo-*.yaml") + if err != nil { + t.Fatal(err) + } + + input, err := ioutil.ReadFile(filePath) + if err != nil { + t.Fatal(err) + } + + err = ioutil.WriteFile(fname.Name(), input, 0644) + if err != nil { + t.Fatal(err) + } + + t.Cleanup(func() { + err := os.Remove(fname.Name()) + if err != nil { + t.Fatal(err) + } + }) + // Sets the repository file path - settings.RepositoryConfig = filePath + settings.RepositoryConfig = fname.Name() + settings.RepositoryCache = path.Dir(filePath) testRepository := &Repositories{ Settings: settings, @@ -27,118 +52,55 @@ func initRepository(t *testing.T, filePath string) *Repositories { return testRepository } -func TestLoadRepo(t *testing.T) { - - res, err := repo.LoadFile(filePath) - if err != nil { - t.Fatal(err) - } - - testRepository := initRepository(t, filePath) - - file, err := testRepository.Load() - if err != nil { - t.Fatal(err) - } - - assert.Equal(t, file.Generated, res.Generated) -} - func TestList(t *testing.T) { - res, err := repo.LoadFile(filePath) - if err != nil { - t.Fatal(err) - } - - testRepository := initRepository(t, filePath) + testRepository := initRepository(t) repos, err := testRepository.List() - if err != nil { t.Fatal(err) } - assert.Equal(t, len(repos), len(res.Repositories)) + assert.Equal(t, len(repos), 4) } func TestAdd(t *testing.T) { testRepoName := "TEST" testRepoUrl := "https://helm.github.io/examples" - res, err := repo.LoadFile(filePath) - if err != nil { - t.Fatal(err) - } - - // Delete the repository if already exist - res.Remove(testRepoName) - - testRepository := initRepository(t, filePath) - - err = testRepository.Add(testRepoName, testRepoUrl) - + testRepository := initRepository(t) + err := testRepository.Add(testRepoName, testRepoUrl) if err != nil { t.Fatal(err, "Failed to add repo") } - // Reload the file - res, err = repo.LoadFile(filePath) + r, err := testRepository.Get(testRepoName) if err != nil { - t.Fatal(err) + t.Fatal(err, "Failed to add repo") } - assert.Equal(t, res.Has(testRepoName), true) - - // Removes test repository which is added for testing - t.Cleanup(func() { - removed := res.Remove(testRepoName) - if removed != true { - t.Log("Failed to clean the test repository file") - } - err = res.WriteFile(filePath, 0644) - if err != nil { - t.Log("Failed to write the file while cleaning test repo") - } - }) + assert.Equal(t, r.Orig.URL, testRepoUrl) } func TestDelete(t *testing.T) { - testRepoName := "TEST DELETE" - testRepoUrl := "https://helm.github.io/examples" + testRepository := initRepository(t) - res, err := repo.LoadFile(filePath) - if err != nil { - t.Fatal(err) - } - - // Add a test entry - res.Add(&repo.Entry{Name: testRepoName, URL: testRepoUrl}) - err = res.WriteFile(filePath, 0644) - if err != nil { - t.Fatal("Failed to write the file while creating test repo") - } - - testRepository := initRepository(t, filePath) - - err = testRepository.Delete(testRepoName) + testRepoName := "charts" // don't ever delete 'testing'! + err := testRepository.Delete(testRepoName) if err != nil { t.Fatal(err, "Failed to delete the repo") } - // Reload the file - res, err = repo.LoadFile(filePath) - if err != nil { - t.Fatal(err) + _, err = testRepository.Get(testRepoName) + if err == nil { + t.Fatal("Failed to delete repo") } - - assert.Equal(t, res.Has(testRepoName), false) } func TestGet(t *testing.T) { // Initial repositiry name in test file repoName := "charts" - testRepository := initRepository(t, filePath) + testRepository := initRepository(t) repo, err := testRepository.Get(repoName) if err != nil { @@ -147,3 +109,21 @@ func TestGet(t *testing.T) { assert.Equal(t, repo.Orig.Name, repoName) } + +func TestCharts(t *testing.T) { + testRepository := initRepository(t) + + r, err := testRepository.Get("testing") + if err != nil { + t.Fatal(err) + } + + charts, err := r.Charts() + if err != nil { + t.Fatal(err) + } + + if len(charts) != 2 { + t.Fatalf("Wrong charts len: %d", len(charts)) + } +} diff --git a/pkg/dashboard/objects/testdata/repositories.yaml b/pkg/dashboard/objects/testdata/repositories.yaml index 4b40604..601c7fc 100644 --- a/pkg/dashboard/objects/testdata/repositories.yaml +++ b/pkg/dashboard/objects/testdata/repositories.yaml @@ -28,3 +28,6 @@ repositories: password: "" url: http://secondexample.com username: "" +- cache: testing-index.yaml + name: testing + url: http://example.com/charts diff --git a/pkg/dashboard/objects/testdata/testing-index.yaml b/pkg/dashboard/objects/testdata/testing-index.yaml new file mode 100644 index 0000000..91e4d46 --- /dev/null +++ b/pkg/dashboard/objects/testdata/testing-index.yaml @@ -0,0 +1,66 @@ +apiVersion: v1 +entries: + alpine: + - name: alpine + url: https://charts.helm.sh/stable/alpine-0.1.0.tgz + checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d + created: "2018-06-27T10:00:18.230700509Z" + deprecated: true + home: https://helm.sh/helm + sources: + - https://github.com/helm/helm + version: 0.1.0 + appVersion: 1.2.3 + description: Deploy a basic Alpine Linux pod + keywords: [] + maintainers: [] + icon: "" + apiVersion: v2 + - name: alpine + url: https://charts.helm.sh/stable/alpine-0.2.0.tgz + checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d + created: "2018-07-09T11:34:37.797864902Z" + home: https://helm.sh/helm + sources: + - https://github.com/helm/helm + version: 0.2.0 + appVersion: 2.3.4 + description: Deploy a basic Alpine Linux pod + keywords: [] + maintainers: [] + icon: "" + apiVersion: v2 + - name: alpine + url: https://charts.helm.sh/stable/alpine-0.3.0-rc.1.tgz + checksum: 0e6661f193211d7a5206918d42f5c2a9470b737d + created: "2020-11-12T08:44:58.872726222Z" + home: https://helm.sh/helm + sources: + - https://github.com/helm/helm + version: 0.3.0-rc.1 + appVersion: 3.0.0 + description: Deploy a basic Alpine Linux pod + keywords: [] + maintainers: [] + icon: "" + apiVersion: v2 + mariadb: + - name: mariadb + url: https://charts.helm.sh/stable/mariadb-0.3.0.tgz + checksum: 65229f6de44a2be9f215d11dbff311673fc8ba56 + created: "2018-04-23T08:20:27.160959131Z" + home: https://mariadb.org + sources: + - https://github.com/bitnami/bitnami-docker-mariadb + version: 0.3.0 + description: Chart for MariaDB + keywords: + - mariadb + - mysql + - database + - sql + maintainers: + - name: Bitnami + email: containers@bitnami.com + icon: "" + apiVersion: v2