Commit ea61ace8 authored by Taylor Thomas's avatar Taylor Thomas Committed by GitHub

Merge pull request #2156 from sushilkm/version-tests

Added unit tests
parents a34f2328 cfd041ee
......@@ -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)
}
}
}
/*
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)
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment