@ -32,10 +32,10 @@ import (
"helm.sh/helm/pkg/chart"
"helm.sh/helm/pkg/chart"
"helm.sh/helm/pkg/chart/loader"
"helm.sh/helm/pkg/chart/loader"
"helm.sh/helm/pkg/chartutil"
"helm.sh/helm/pkg/chartutil"
"helm.sh/helm/pkg/helmpath"
)
)
func TestPackage ( t * testing . T ) {
func TestPackage ( t * testing . T ) {
t . Skip ( "TODO" )
statExe := "stat"
statExe := "stat"
statFileMsg := "no such file or directory"
statFileMsg := "no such file or directory"
if runtime . GOOS == "windows" {
if runtime . GOOS == "windows" {
@ -43,8 +43,6 @@ func TestPackage(t *testing.T) {
statFileMsg = "The system cannot find the file specified."
statFileMsg = "The system cannot find the file specified."
}
}
defer resetEnv ( ) ( )
tests := [ ] struct {
tests := [ ] struct {
name string
name string
flags map [ string ] string
flags map [ string ] string
@ -139,18 +137,18 @@ func TestPackage(t *testing.T) {
t . Fatal ( err )
t . Fatal ( err )
}
}
ensure . HelmHome ( t )
cachePath := ensure . TempDir ( t )
defer ensure . CleanHomeDirs ( t )
t . Logf ( "Running tests in %s" , cachePath )
t . Logf ( "Running tests in %s" , helmpath . CachePath ( ) )
defer testChdir ( t , helmpath . CachePath ( ) ) ( )
if err := os . Mkdir ( "toot" , 0777 ) ; err != nil {
for _ , tt := range tests {
t . Run ( tt . name , func ( t * testing . T ) {
defer testChdir ( t , cachePath ) ( )
if err := os . MkdirAll ( "toot" , 0777 ) ; err != nil {
t . Fatal ( err )
t . Fatal ( err )
}
}
var buf bytes . Buffer
for _ , tt := range tests {
c := newPackageCmd ( & buf )
buf := bytes . NewBuffer ( nil )
c := newPackageCmd ( buf )
// This is an unfortunate byproduct of the tmpdir
// This is an unfortunate byproduct of the tmpdir
if v , ok := tt . flags [ "keyring" ] ; ok && len ( v ) > 0 {
if v , ok := tt . flags [ "keyring" ] ; ok && len ( v ) > 0 {
@ -168,10 +166,9 @@ func TestPackage(t *testing.T) {
err := c . RunE ( c , adjustedArgs )
err := c . RunE ( c , adjustedArgs )
if err != nil {
if err != nil {
if tt . err && re . MatchString ( err . Error ( ) ) {
if tt . err && re . MatchString ( err . Error ( ) ) {
continue
return
}
}
t . Errorf ( "%q: expected error %q, got %q" , tt . name , tt . expect , err )
t . Fatalf ( "%q: expected error %q, got %q" , tt . name , tt . expect , err )
continue
}
}
if ! re . Match ( buf . Bytes ( ) ) {
if ! re . Match ( buf . Bytes ( ) ) {
@ -193,21 +190,19 @@ func TestPackage(t *testing.T) {
t . Errorf ( "%q: provenance file is empty" , tt . name )
t . Errorf ( "%q: provenance file is empty" , tt . name )
}
}
}
}
} )
}
}
}
}
func TestSetAppVersion ( t * testing . T ) {
func TestSetAppVersion ( t * testing . T ) {
defer resetEnv ( ) ( )
var ch * chart . Chart
var ch * chart . Chart
expectedAppVersion := "app-version-foo"
expectedAppVersion := "app-version-foo"
ensure . HelmHome ( t )
dir := ensure . TempDir ( t )
defer ensure . CleanHomeDirs ( t )
c := newPackageCmd ( & bytes . Buffer { } )
c := newPackageCmd ( & bytes . Buffer { } )
flags := map [ string ] string {
flags := map [ string ] string {
"destination" : helmpath. CachePath ( ) ,
"destination" : dir ,
"app-version" : expectedAppVersion ,
"app-version" : expectedAppVersion ,
}
}
setFlags ( c , flags )
setFlags ( c , flags )
@ -215,7 +210,7 @@ func TestSetAppVersion(t *testing.T) {
t . Errorf ( "unexpected error %q" , err )
t . Errorf ( "unexpected error %q" , err )
}
}
chartPath := filepath . Join ( helmpath. CachePath ( ) , "alpine-0.1.0.tgz" )
chartPath := filepath . Join ( dir , "alpine-0.1.0.tgz" )
if fi , err := os . Stat ( chartPath ) ; err != nil {
if fi , err := os . Stat ( chartPath ) ; err != nil {
t . Errorf ( "expected file %q, got err %q" , chartPath , err )
t . Errorf ( "expected file %q, got err %q" , chartPath , err )
} else if fi . Size ( ) == 0 {
} else if fi . Size ( ) == 0 {
@ -223,7 +218,7 @@ func TestSetAppVersion(t *testing.T) {
}
}
ch , err := loader . Load ( chartPath )
ch , err := loader . Load ( chartPath )
if err != nil {
if err != nil {
t . Error f( "unexpected error loading packaged chart: %v" , err )
t . Fatal f( "unexpected error loading packaged chart: %v" , err )
}
}
if ch . Metadata . AppVersion != expectedAppVersion {
if ch . Metadata . AppVersion != expectedAppVersion {
t . Errorf ( "expected app-version %q, found %q" , expectedAppVersion , ch . Metadata . AppVersion )
t . Errorf ( "expected app-version %q, found %q" , expectedAppVersion , ch . Metadata . AppVersion )
@ -233,6 +228,8 @@ func TestSetAppVersion(t *testing.T) {
func TestPackageValues ( t * testing . T ) {
func TestPackageValues ( t * testing . T ) {
defer resetEnv ( ) ( )
defer resetEnv ( ) ( )
repoFile := "testdata/helmhome/helm/repositories.yaml"
testCases := [ ] struct {
testCases := [ ] struct {
desc string
desc string
args [ ] string
args [ ] string
@ -243,33 +240,32 @@ func TestPackageValues(t *testing.T) {
{
{
desc : "helm package, single values file" ,
desc : "helm package, single values file" ,
args : [ ] string { "testdata/testcharts/alpine" } ,
args : [ ] string { "testdata/testcharts/alpine" } ,
flags : map [ string ] string { "repository-config" : repoFile } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" } ,
expected : [ ] string { "Name: chart-name-foo" } ,
expected : [ ] string { "Name: chart-name-foo" } ,
} ,
} ,
{
{
desc : "helm package, multiple values files" ,
desc : "helm package, multiple values files" ,
args : [ ] string { "testdata/testcharts/alpine" } ,
args : [ ] string { "testdata/testcharts/alpine" } ,
flags : map [ string ] string { "repository-config" : repoFile } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" , "foo: bar" } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" , "foo: bar" } ,
expected : [ ] string { "Name: chart-name-foo" , "foo: bar" } ,
expected : [ ] string { "Name: chart-name-foo" , "foo: bar" } ,
} ,
} ,
{
{
desc : "helm package, with set option" ,
desc : "helm package, with set option" ,
args : [ ] string { "testdata/testcharts/alpine" } ,
args : [ ] string { "testdata/testcharts/alpine" } ,
flags : map [ string ] string { "set" : "Name=chart-name-foo" },
flags : map [ string ] string { "set" : "Name=chart-name-foo" , "repository-config" : repoFile },
expected : [ ] string { "Name: chart-name-foo" } ,
expected : [ ] string { "Name: chart-name-foo" } ,
} ,
} ,
{
{
desc : "helm package, set takes precedence over value file" ,
desc : "helm package, set takes precedence over value file" ,
args : [ ] string { "testdata/testcharts/alpine" } ,
args : [ ] string { "testdata/testcharts/alpine" } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" } ,
valuefilesContents : [ ] string { "Name: chart-name-foo" } ,
flags : map [ string ] string { "set" : "Name=chart-name-bar" },
flags : map [ string ] string { "set" : "Name=chart-name-bar" , "repository-config" : repoFile },
expected : [ ] string { "Name: chart-name-bar" } ,
expected : [ ] string { "Name: chart-name-bar" } ,
} ,
} ,
}
}
ensure . HelmHome ( t )
defer ensure . CleanHomeDirs ( t )
for _ , tc := range testCases {
for _ , tc := range testCases {
var files [ ] string
var files [ ] string
for _ , contents := range tc . valuefilesContents {
for _ , contents := range tc . valuefilesContents {
@ -283,27 +279,21 @@ func TestPackageValues(t *testing.T) {
t . Errorf ( "unexpected error parsing values: %q" , err )
t . Errorf ( "unexpected error parsing values: %q" , err )
}
}
runAndVerifyPackageCommandValues ( t , tc . args , tc . flags , valueFiles , expected )
}
}
func runAndVerifyPackageCommandValues ( t * testing . T , args [ ] string , flags map [ string ] string , valueFiles string , expected chartutil . Values ) {
t . Helper ( )
outputDir := ensure . TempDir ( t )
outputDir := ensure . TempDir ( t )
if len ( flags ) == 0 {
if len ( tc . flags ) == 0 {
flags = make ( map [ string ] string )
tc . flags = make ( map [ string ] string )
}
}
flags [ "destination" ] = outputDir
tc . flags [ "destination" ] = outputDir
if len ( valueFiles ) > 0 {
if len ( valueFiles ) > 0 {
flags [ "values" ] = valueFiles
tc . flags [ "values" ] = valueFiles
}
}
cmd := newPackageCmd ( & bytes . Buffer { } )
cmd := newPackageCmd ( & bytes . Buffer { } )
setFlags ( cmd , flags )
setFlags ( cmd , tc . flags )
if err := cmd . RunE ( cmd , args ) ; err != nil {
if err := cmd . RunE ( cmd , tc . args ) ; err != nil {
t . Error f( "unexpected error: %q" , err )
t . Fatal f( "unexpected error: %q" , err )
}
}
outputFile := filepath . Join ( outputDir , "alpine-0.1.0.tgz" )
outputFile := filepath . Join ( outputDir , "alpine-0.1.0.tgz" )
@ -311,30 +301,28 @@ func runAndVerifyPackageCommandValues(t *testing.T, args []string, flags map[str
actual , err := getChartValues ( outputFile )
actual , err := getChartValues ( outputFile )
if err != nil {
if err != nil {
t . Error f( "unexpected error extracting chart values: %q" , err )
t . Fatal f( "unexpected error extracting chart values: %q" , err )
}
}
verifyValues ( t , actual , expected )
verifyValues ( t , actual , expected )
}
}
}
func createValuesFile ( t * testing . T , data string ) string {
func createValuesFile ( t * testing . T , data string ) string {
outputDir := ensure . TempDir ( t )
outputDir := ensure . TempDir ( t )
outputFile := filepath . Join ( outputDir , "values.yaml" )
outputFile := filepath . Join ( outputDir , "values.yaml" )
if err := ioutil . WriteFile ( outputFile , [ ] byte ( data ) , 0 755 ) ; err != nil {
if err := ioutil . WriteFile ( outputFile , [ ] byte ( data ) , 0 644 ) ; err != nil {
t . Fatalf ( "err: %s" , err )
t . Fatalf ( "err: %s" , err )
}
}
return outputFile
return outputFile
}
}
func getChartValues ( chartPath string ) ( chartutil . Values , error ) {
func getChartValues ( chartPath string ) ( chartutil . Values , error ) {
chart , err := loader . Load ( chartPath )
chart , err := loader . Load ( chartPath )
if err != nil {
if err != nil {
return nil , err
return nil , err
}
}
return chart . Values , nil
return chart . Values , nil
}
}