From a859ee24d2e164803b06bf27b748b4d896e9cdc3 Mon Sep 17 00:00:00 2001 From: Jack Francis Date: Wed, 28 Sep 2016 12:19:00 -0700 Subject: [PATCH] tests(data): added test coverage to data package --- src/api/data/charts_test.go | 35 +++++++++++++++++++++++++++++++++++ src/api/mocks/charts.go | 17 ++++++++++++----- 2 files changed, 47 insertions(+), 5 deletions(-) create mode 100644 src/api/data/charts_test.go diff --git a/src/api/data/charts_test.go b/src/api/data/charts_test.go new file mode 100644 index 000000000..e05b3b4da --- /dev/null +++ b/src/api/data/charts_test.go @@ -0,0 +1,35 @@ +package data + +import ( + "testing" + + "github.com/arschles/assert" +) + +const ( + repoName = "stable" + chartName = "apache" +) + +func TestGetChart(t *testing.T) { + chart, err := GetChart(repoName, chartName) + assert.NoErr(t, err) + assert.Equal(t, *chart.ID, repoName+"/"+chartName, "chart ID") + chart, err = GetChart("bogon", chartName) + assert.ExistsErr(t, err, "sent bogus repo name to GetChart") + assert.Nil(t, chart.ID, "zero value ID") +} + +func TestGetAllCharts(t *testing.T) { + _, err := GetAllCharts() + assert.NoErr(t, err) +} + +func TestGetChartsInRepo(t *testing.T) { + charts, err := GetChartsInRepo(repoName) + assert.NoErr(t, err) + assert.True(t, len(charts) > 0, "returned charts") + noCharts, err := GetChartsInRepo("bogon") + assert.ExistsErr(t, err, "sent bogus repo name to GetChartsInRepo") + assert.True(t, len(noCharts) == 0, "empty charts slice") +} diff --git a/src/api/mocks/charts.go b/src/api/mocks/charts.go index cdfd6d07c..2e6f57d35 100644 --- a/src/api/mocks/charts.go +++ b/src/api/mocks/charts.go @@ -12,9 +12,12 @@ import ( func GetChartFromMockRepo(repo, chartName string) (models.Resource, error) { var ret models.Resource y, err := getMockRepo(repo) + if err != nil { + return ret, err + } charts, err := helpers.ParseYAMLRepo(y) if err != nil { - log.Fatalf("couldn't parse mock repo!") + log.Printf("couldn't parse mock repo %s!\n", repo) return ret, err } chart, err := helpers.GetLatestChartVersion(charts, chartName) @@ -33,7 +36,7 @@ func GetAllChartsFromMockRepos() ([]*models.Resource, error) { y, err := getMockRepo(repo) charts, err := helpers.ParseYAMLRepo(y) if err != nil { - log.Fatalf("couldn't parse mock repo!") + log.Printf("couldn't parse mock repo %s!\n", repo) return ret, err } for _, chart := range charts { @@ -48,9 +51,12 @@ func GetAllChartsFromMockRepos() ([]*models.Resource, error) { func GetChartsFromMockRepo(repo string) ([]*models.Resource, error) { var ret []*models.Resource y, err := getMockRepo(repo) + if err != nil { + return ret, err + } charts, err := helpers.ParseYAMLRepo(y) if err != nil { - log.Fatalf("couldn't parse mock repo %s!\n", repo) + log.Printf("couldn't parse mock repo %s!\n", repo) return ret, err } for _, chart := range charts { @@ -62,9 +68,10 @@ func GetChartsFromMockRepo(repo string) ([]*models.Resource, error) { // getMockRepo is a convenience that loads a yaml repo from the filesystem func getMockRepo(repo string) ([]byte, error) { - y, err := getYAML(getMocksWd() + fmt.Sprintf("repo-%s.yaml", repo)) + path := getMocksWd() + fmt.Sprintf("repo-%s.yaml", repo) + y, err := getYAML(path) if err != nil { - log.Fatalf("couldn't load mock repo %s!\n", repo) + log.Printf("couldn't load mock repo %s!\n", path) return nil, err } return y, nil