From cfd041eed56002475841aac89116390447722bd3 Mon Sep 17 00:00:00 2001 From: Sushil Kumar Date: Tue, 21 Mar 2017 11:41:39 -0700 Subject: [PATCH] Added unit tests Added tests for: - [ ] pkg/version/versions.go - [ ] pkg/repo/repo.go --- pkg/repo/repo_test.go | 99 +++++++++++++++++++++++++++++++++++++ pkg/version/version_test.go | 47 ++++++++++++++++++ 2 files changed, 146 insertions(+) create mode 100644 pkg/version/version_test.go diff --git a/pkg/repo/repo_test.go b/pkg/repo/repo_test.go index c0b0e6da7..6aee41faf 100644 --- a/pkg/repo/repo_test.go +++ b/pkg/repo/repo_test.go @@ -17,6 +17,8 @@ limitations under the License. package repo import "testing" +import "io/ioutil" +import "os" const testRepositoriesFile = "testdata/repositories.yaml" @@ -116,3 +118,100 @@ func TestNewPreV1RepositoriesFile(t *testing.T) { t.Errorf("expected the best charts ever. Got %#v", r.Repositories) } } + +func TestRemoveRepository(t *testing.T) { + sampleRepository := NewRepoFile() + sampleRepository.Add( + &Entry{ + Name: "stable", + URL: "https://example.com/stable/charts", + Cache: "stable-index.yaml", + }, + &Entry{ + Name: "incubator", + URL: "https://example.com/incubator", + Cache: "incubator-index.yaml", + }, + ) + + removeRepository := "stable" + found := sampleRepository.Remove(removeRepository) + if !found { + t.Errorf("expected repository %s not found", removeRepository) + } + + found = sampleRepository.Has(removeRepository) + if found { + t.Errorf("repository %s not deleted", removeRepository) + } +} + +func TestUpdateRepository(t *testing.T) { + sampleRepository := NewRepoFile() + sampleRepository.Add( + &Entry{ + Name: "stable", + URL: "https://example.com/stable/charts", + Cache: "stable-index.yaml", + }, + &Entry{ + Name: "incubator", + URL: "https://example.com/incubator", + Cache: "incubator-index.yaml", + }, + ) + newRepoName := "sample" + sampleRepository.Update(&Entry{Name: newRepoName, + URL: "https://example.com/sample", + Cache: "sample-index.yaml", + }) + + if !sampleRepository.Has(newRepoName) { + t.Errorf("expected repository %s not found", newRepoName) + } + repoCount := len(sampleRepository.Repositories) + + sampleRepository.Update(&Entry{Name: newRepoName, + URL: "https://example.com/sample", + Cache: "sample-index.yaml", + }) + + if repoCount != len(sampleRepository.Repositories) { + t.Errorf("invalid number of repositories found %d, expected number of repositories %d", len(sampleRepository.Repositories), repoCount) + } +} + +func TestWriteFile(t *testing.T) { + sampleRepository := NewRepoFile() + sampleRepository.Add( + &Entry{ + Name: "stable", + URL: "https://example.com/stable/charts", + Cache: "stable-index.yaml", + }, + &Entry{ + Name: "incubator", + URL: "https://example.com/incubator", + Cache: "incubator-index.yaml", + }, + ) + + repoFile, err := ioutil.TempFile("", "helm-repo") + if err != nil { + t.Errorf("failed to create test-file (%v)", err) + } + defer os.Remove(repoFile.Name()) + if err := sampleRepository.WriteFile(repoFile.Name(), 744); err != nil { + t.Errorf("failed to write file (%v)", err) + } + + repos, err := LoadRepositoriesFile(repoFile.Name()) + if err != nil { + t.Errorf("failed to load file (%v)", err) + } + for _, repo := range sampleRepository.Repositories { + if !repos.Has(repo.Name) { + t.Errorf("expected repository %s not found", repo.Name) + } + } +} diff --git a/pkg/version/version_test.go b/pkg/version/version_test.go new file mode 100644 index 000000000..e0e4cac0f --- /dev/null +++ b/pkg/version/version_test.go @@ -0,0 +1,47 @@ +/* +Copyright 2016 The Kubernetes Authors All rights reserved. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +// Package version represents the current version of the project. +package version // import "k8s.io/helm/pkg/version" + +import "testing" +import "k8s.io/helm/pkg/proto/hapi/version" + +func TestGetVersionProto(t *testing.T) { + tests := []struct { + version string + buildMetadata string + gitCommit string + gitTreeState string + expected version.Version + }{ + {"", "", "", "", version.Version{SemVer: "", GitCommit: "", GitTreeState: ""}}, + {"v1.0.0", "", "", "", version.Version{SemVer: "v1.0.0", GitCommit: "", GitTreeState: ""}}, + {"v1.0.0", "79d5c5f7", "", "", version.Version{SemVer: "v1.0.0+79d5c5f7", GitCommit: "", GitTreeState: ""}}, + {"v1.0.0", "79d5c5f7", "0d399baec2acda578a217d1aec8d7d707c71e44d", "", version.Version{SemVer: "v1.0.0+79d5c5f7", GitCommit: "0d399baec2acda578a217d1aec8d7d707c71e44d", GitTreeState: ""}}, + {"v1.0.0", "79d5c5f7", "0d399baec2acda578a217d1aec8d7d707c71e44d", "clean", version.Version{SemVer: "v1.0.0+79d5c5f7", GitCommit: "0d399baec2acda578a217d1aec8d7d707c71e44d", GitTreeState: "clean"}}, + } + for _, tt := range tests { + Version = tt.version + BuildMetadata = tt.buildMetadata + GitCommit = tt.gitCommit + GitTreeState = tt.gitTreeState + if versionProto := GetVersionProto(); *versionProto != tt.expected { + t.Errorf("expected Semver(%s), GitCommit(%s) and GitTreeState(%s) to be %v", tt.expected, tt.gitCommit, tt.gitTreeState, *versionProto) + } + } + +}