@ -17,15 +17,214 @@ limitations under the License.
package rules
import (
"k8s.io/helm/pkg/lint/support"
"errors"
"os"
"path/filepath"
"strings"
"testing"
"k8s.io/helm/pkg/chartutil"
"k8s.io/helm/pkg/lint/support"
"k8s.io/helm/pkg/proto/hapi/chart"
)
const badchartfile = "testdata/badchartfile"
const badChartDir = "testdata/badchartfile"
const goodChartDir = "testdata/goodone"
var badChartFilePath string = filepath . Join ( badChartDir , "Chart.yaml" )
var goodChartFilePath string = filepath . Join ( goodChartDir , "Chart.yaml" )
var nonExistingChartFilePath string = filepath . Join ( os . TempDir ( ) , "Chart.yaml" )
var badChart , chatLoadRrr = chartutil . LoadChartfile ( badChartFilePath )
var goodChart , _ = chartutil . LoadChartfile ( goodChartFilePath )
// Validation functions Test
func TestValidateChartYamlFileExistence ( t * testing . T ) {
err := validateChartYamlFileExistence ( nonExistingChartFilePath )
if err == nil {
t . Errorf ( "validateChartYamlFileExistence to return a linter error, got no error" )
}
err = validateChartYamlFileExistence ( badChartFilePath )
if err != nil {
t . Errorf ( "validateChartYamlFileExistence to return no error, got a linter error" )
}
}
func TestValidateChartYamlNotDirectory ( t * testing . T ) {
_ = os . Mkdir ( nonExistingChartFilePath , os . ModePerm )
defer os . Remove ( nonExistingChartFilePath )
err := validateChartYamlNotDirectory ( nonExistingChartFilePath )
if err == nil {
t . Errorf ( "validateChartYamlNotDirectory to return a linter error, got no error" )
}
}
func TestValidateChartYamlFormat ( t * testing . T ) {
err := validateChartYamlFormat ( errors . New ( "Read error" ) )
if err == nil {
t . Errorf ( "validateChartYamlFormat to return a linter error, got no error" )
}
err = validateChartYamlFormat ( nil )
if err != nil {
t . Errorf ( "validateChartYamlFormat to return no error, got a linter error" )
}
}
func TestValidateChartName ( t * testing . T ) {
err := validateChartName ( badChart )
if err == nil {
t . Errorf ( "validateChartName to return a linter error, got no error" )
}
}
func TestValidateChartNameDirMatch ( t * testing . T ) {
err := validateChartNameDirMatch ( goodChartDir , goodChart )
if err != nil {
t . Errorf ( "validateChartNameDirMatch to return no error, gor a linter error" )
}
// It has not name
err = validateChartNameDirMatch ( badChartDir , badChart )
if err == nil {
t . Errorf ( "validatechartnamedirmatch to return a linter error, got no error" )
}
// Wrong path
err = validateChartNameDirMatch ( badChartDir , goodChart )
if err == nil {
t . Errorf ( "validatechartnamedirmatch to return a linter error, got no error" )
}
}
func TestValidateChartVersion ( t * testing . T ) {
var failTest = [ ] struct {
Version string
ErrorMsg string
} {
{ "" , "'version' value is required" } ,
{ "0" , "0 is less than or equal to 0" } ,
{ "waps" , "is not a valid SemVer" } ,
{ "-3" , "is not a valid SemVer" } ,
}
var successTest = [ ] string { "0.0.1" , "0.0.1+build" , "0.0.1-beta" }
for _ , test := range failTest {
badChart . Version = test . Version
err := validateChartVersion ( badChart )
if err == nil || ! strings . Contains ( err . Error ( ) , test . ErrorMsg ) {
t . Errorf ( "validateChartVersion(%s) to return \"%s\", got no error" , test . Version , test . ErrorMsg )
}
}
for _ , version := range successTest {
badChart . Version = version
err := validateChartVersion ( badChart )
if err != nil {
t . Errorf ( "validateChartVersion(%s) to return no error, got a linter error" , version )
}
}
}
func TestValidateChartEngine ( t * testing . T ) {
var successTest = [ ] string { "" , "gotpl" }
for _ , engine := range successTest {
badChart . Engine = engine
err := validateChartEngine ( badChart )
if err != nil {
t . Errorf ( "validateChartEngine(%s) to return no error, got a linter error %s" , engine , err . Error ( ) )
}
}
badChart . Engine = "foobar"
err := validateChartEngine ( badChart )
if err == nil || ! strings . Contains ( err . Error ( ) , "not valid. Valid options are [gotpl" ) {
t . Errorf ( "validateChartEngine(%s) to return an error, got no error" , badChart . Engine )
}
}
func TestValidateChartMaintainer ( t * testing . T ) {
var failTest = [ ] struct {
Name string
Email string
ErrorMsg string
} {
{ "" , "" , "maintainer requires a name" } ,
{ "" , "test@test.com" , "maintainer requires a name" } ,
{ "John Snow" , "wrongFormatEmail.com" , "maintainer invalid email" } ,
}
var successTest = [ ] struct {
Name string
Email string
} {
{ "John Snow" , "" } ,
{ "John Snow" , "john@winterfell.com" } ,
}
for _ , test := range failTest {
badChart . Maintainers = [ ] * chart . Maintainer { { Name : test . Name , Email : test . Email } }
err := validateChartMaintainer ( badChart )
if err == nil || ! strings . Contains ( err . Error ( ) , test . ErrorMsg ) {
t . Errorf ( "validateChartMaintainer(%s, %s) to return \"%s\", got no error" , test . Name , test . Email , test . ErrorMsg )
}
}
for _ , test := range successTest {
badChart . Maintainers = [ ] * chart . Maintainer { { Name : test . Name , Email : test . Email } }
err := validateChartMaintainer ( badChart )
if err != nil {
t . Errorf ( "validateChartMaintainer(%s, %s) to return no error, got %s" , test . Name , test . Email , err . Error ( ) )
}
}
}
func TestValidateChartSources ( t * testing . T ) {
var failTest = [ ] string { "" , "RiverRun" , "john@winterfell" , "riverrun.io" }
var successTest = [ ] string { "http://riverrun.io" , "https://riverrun.io" , "https://riverrun.io/blackfish" }
for _ , test := range failTest {
badChart . Sources = [ ] string { test }
err := validateChartSources ( badChart )
if err == nil || ! strings . Contains ( err . Error ( ) , "invalid URL" ) {
t . Errorf ( "validateChartSources(%s) to return \"invalid URL\", got no error" , test )
}
}
for _ , test := range successTest {
badChart . Sources = [ ] string { test }
err := validateChartSources ( badChart )
if err != nil {
t . Errorf ( "validateChartSources(%s) to return no error, got %s" , test , err . Error ( ) )
}
}
}
func TestValidateChartHome ( t * testing . T ) {
var failTest = [ ] string { "RiverRun" , "john@winterfell" , "riverrun.io" }
var successTest = [ ] string { "" , "http://riverrun.io" , "https://riverrun.io" , "https://riverrun.io/blackfish" }
for _ , test := range failTest {
badChart . Home = test
err := validateChartHome ( badChart )
if err == nil || ! strings . Contains ( err . Error ( ) , "invalid URL" ) {
t . Errorf ( "validateChartHome(%s) to return \"invalid URL\", got no error" , test )
}
}
for _ , test := range successTest {
badChart . Home = test
err := validateChartHome ( badChart )
if err != nil {
t . Errorf ( "validateChartHome(%s) to return no error, got %s" , test , err . Error ( ) )
}
}
}
func TestChartfile ( t * testing . T ) {
linter := support . Linter { ChartDir : badchartfile }
linter := support . Linter { ChartDir : bad ChartDir }
Chartfile ( & linter )
msgs := linter . Messages