Skip to content

Commit

Permalink
Merge pull request #1810 from priyawadhwa/validation
Browse files Browse the repository at this point in the history
Error if no Dockerfiles are found for skaffold init --analyze
  • Loading branch information
tejal29 authored Mar 18, 2019
2 parents be9b932 + 9d652dd commit 5b54d97
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
9 changes: 6 additions & 3 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,13 @@ func DoInit(out io.Writer, c Config) error {
}
images := k.GetImages()
if c.Analyze {
return printAnalyzeJSON(out, dockerfiles, images)
return printAnalyzeJSON(out, c.SkipBuild, dockerfiles, images)
}
var pairs []dockerfilePair
// conditionally generate build artifacts
if !c.SkipBuild {
if len(dockerfiles) == 0 {
return errors.New("one or more valid Dockerfiles must be present to run skaffold; please provide at least one Dockerfile and try again")
return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`")
}

if c.CliArtifacts != nil {
Expand Down Expand Up @@ -289,7 +289,10 @@ func generateSkaffoldPipeline(k Initializer, dockerfilePairs []dockerfilePair) (
return pipelineStr, nil
}

func printAnalyzeJSON(out io.Writer, dockerfiles, images []string) error {
func printAnalyzeJSON(out io.Writer, skipBuild bool, dockerfiles, images []string) error {
if !skipBuild && len(dockerfiles) == 0 {
return errors.New("one or more valid Dockerfiles must be present to build images with skaffold; please provide at least one Dockerfile and try again or run `skaffold init --skip-build`")
}
a := struct {
Dockerfiles []string `json:"dockerfiles,omitempty"`
Images []string `json:"images,omitempty"`
Expand Down
24 changes: 13 additions & 11 deletions pkg/skaffold/initializer/init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ func TestPrintAnalyzeJSON(t *testing.T) {
name string
dockerfiles []string
images []string
skipBuild bool
shouldErr bool
expected string
}{
{
Expand All @@ -37,25 +39,25 @@ func TestPrintAnalyzeJSON(t *testing.T) {
expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}",
},
{
name: "no dockerfile",
images: []string{"image1", "image2"},
expected: "{\"images\":[\"image1\",\"image2\"]}",
},
name: "no dockerfile, skip build",
images: []string{"image1", "image2"},
skipBuild: true,
expected: "{\"images\":[\"image1\",\"image2\"]}"},
{
name: "no images",
dockerfiles: []string{"Dockerfile", "Dockerfile_2"},
expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"]}",
name: "no dockerfile",
images: []string{"image1", "image2"},
shouldErr: true,
},
{
name: "no dockerfiles or images",
expected: "{}",
name: "no dockerfiles or images",
shouldErr: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
out := bytes.NewBuffer([]byte{})
err := printAnalyzeJSON(out, test.dockerfiles, test.images)
testutil.CheckErrorAndDeepEqual(t, false, err, test.expected, out.String())
err := printAnalyzeJSON(out, test.skipBuild, test.dockerfiles, test.images)
testutil.CheckErrorAndDeepEqual(t, test.shouldErr, err, test.expected, out.String())
})
}
}

0 comments on commit 5b54d97

Please sign in to comment.