From d3a71d8a617b81e5a050957e14028d802fc393b5 Mon Sep 17 00:00:00 2001 From: Tejal Desai Date: Thu, 7 Mar 2019 14:56:41 -0800 Subject: [PATCH] Add more tests for IsSkaffoldConfig and IsKubernetesFormat --- cmd/skaffold/app/cmd/init.go | 4 +- pkg/skaffold/initializer/init.go | 11 +- pkg/skaffold/initializer/init_test.go | 61 +++++++++++ .../initializer/kubectl/kubectl_test.go | 19 +--- pkg/skaffold/initializer/util_test.go | 101 ++++++++++++++++++ testutil/util.go | 18 ++++ 6 files changed, 190 insertions(+), 24 deletions(-) create mode 100644 pkg/skaffold/initializer/util_test.go diff --git a/cmd/skaffold/app/cmd/init.go b/cmd/skaffold/app/cmd/init.go index 0861c550680..6fbe1e8fa79 100644 --- a/cmd/skaffold/app/cmd/init.go +++ b/cmd/skaffold/app/cmd/init.go @@ -43,8 +43,8 @@ func NewCmdInit(out io.Writer) *cobra.Command { CliArtifacts: cliArtifacts, SkipBuild: skipBuild, Force: force, - Analyze: analyze - SkaffoldOpts: opts, + Analyze: analyze, + Opts: opts, } return initializer.DoInit(out, c) }, diff --git a/pkg/skaffold/initializer/init.go b/pkg/skaffold/initializer/init.go index 21f627ecd45..decb789022d 100644 --- a/pkg/skaffold/initializer/init.go +++ b/pkg/skaffold/initializer/init.go @@ -60,7 +60,7 @@ type Config struct { CliArtifacts []string SkipBuild bool Force bool - Opts config.SkaffoldOptions + Opts *config.SkaffoldOptions Analyze bool } @@ -80,10 +80,11 @@ func DoInit(out io.Writer, c Config) error { var potentialConfigs, dockerfiles []string err := filepath.Walk(rootDir, func(path string, f os.FileInfo, e error) error { - if f.IsDir() { - return nil + if f.IsDir() && util.IsHiddenDir(f.Name()) { + logrus.Debugf("skip walking hidden dir %s", f.Name()) + return filepath.SkipDir } - if strings.HasPrefix(path, ".") { + if f.IsDir() || util.IsHiddenFile(f.Name()) { return nil } if IsSkaffoldConfig(path) { @@ -168,7 +169,7 @@ func DoInit(out io.Writer, c Config) error { } fmt.Fprintf(out, "Configuration %s was written\n", c.Opts.ConfigurationFile) - tips.PrintForInit(out, &c.Opts) + tips.PrintForInit(out, c.Opts) return nil } diff --git a/pkg/skaffold/initializer/init_test.go b/pkg/skaffold/initializer/init_test.go index e69de29bb2d..9e0c5440c7e 100644 --- a/pkg/skaffold/initializer/init_test.go +++ b/pkg/skaffold/initializer/init_test.go @@ -0,0 +1,61 @@ +/* +Copyright 2019 The Skaffold Authors + +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 initializer + +import ( + "bytes" + "testing" + + "github.com/GoogleContainerTools/skaffold/testutil" +) + +func TestPrintAnalyzeJSON(t *testing.T) { + tests := []struct { + name string + dockerfiles []string + images []string + expected string + }{ + { + name: "dockerfile and image", + dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, + images: []string{"image1", "image2"}, + expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"],\"images\":[\"image1\",\"image2\"]}", + }, + { + name: "no dockerfile", + images: []string{"image1", "image2"}, + expected: "{\"images\":[\"image1\",\"image2\"]}", + }, + { + name: "no images", + dockerfiles: []string{"Dockerfile", "Dockerfile_2"}, + expected: "{\"dockerfiles\":[\"Dockerfile\",\"Dockerfile_2\"]}", + }, + { + name: "no dockerfiles or images", + expected: "{}", + }, + } + 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()) + }) + } +} diff --git a/pkg/skaffold/initializer/kubectl/kubectl_test.go b/pkg/skaffold/initializer/kubectl/kubectl_test.go index 7bd9f1c907f..5fb7d693efd 100644 --- a/pkg/skaffold/initializer/kubectl/kubectl_test.go +++ b/pkg/skaffold/initializer/kubectl/kubectl_test.go @@ -35,7 +35,7 @@ spec: - name: getting-started image: gcr.io/k8s-skaffold/skaffold-example `) - filename := createTempFileWithContents(t, "", "deployment.yaml", content) + filename := testutil.CreateTempFileWithContents(t, "", "deployment.yaml", content) defer os.Remove(filename) // clean up expectedConfig := latest.DeployConfig{ @@ -90,24 +90,9 @@ kind: Pod`), defer os.Remove(tmpDir) // clean up for _, test := range tests { t.Run(test.name, func(t *testing.T) { - tmpFile := createTempFileWithContents(t, tmpDir, "deployment.yaml", test.contents) + tmpFile := testutil.CreateTempFileWithContents(t, tmpDir, "deployment.yaml", test.contents) images, err := parseImagesFromKubernetesYaml(tmpFile) testutil.CheckErrorAndDeepEqual(t, test.err, err, test.images, images) }) } } - -func createTempFileWithContents(t *testing.T, dir string, name string, content []byte) string { - t.Helper() - tmpfile, err := ioutil.TempFile(dir, name) - if err != nil { - t.Fatal(err) - } - if _, err := tmpfile.Write(content); err != nil { - t.Fatal(err) - } - if err := tmpfile.Close(); err != nil { - t.Fatal(err) - } - return tmpfile.Name() -} diff --git a/pkg/skaffold/initializer/util_test.go b/pkg/skaffold/initializer/util_test.go new file mode 100644 index 00000000000..750f7a4988d --- /dev/null +++ b/pkg/skaffold/initializer/util_test.go @@ -0,0 +1,101 @@ +/* +Copyright 2019 The Skaffold Authors + +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 initializer + +import ( + "os" + "testing" + + "github.com/GoogleContainerTools/skaffold/testutil" +) + +func TestIsSupportedKubernetesFormat(t *testing.T) { + tests := []struct { + name string + filename string + expected bool + }{ + { + name: "valid k8 yaml filename format", + filename: "test1.yaml", + expected: true, + }, + { + name: "valid k8 json filename format", + filename: "test1.json", + expected: true, + }, + { + name: "valid k8 yaml filename format", + filename: "test1.yml", + expected: true, + }, + { + name: "invalid file", + filename: "some.config", + expected: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + if IsSupportedKubernetesFormat(test.filename) != test.expected { + t.Errorf("expected to see %t for %s, but instead got %t", test.expected, + test.filename, !test.expected) + } + }) + } +} + +func TestIsSkaffoldConfig(t *testing.T) { + tests := []struct { + name string + contents []byte + expected bool + }{ + { + name: "valid skaffold config", + contents: []byte(`apiVersion: skaffold/v1beta6 +kind: Config +deploy: + kustomize: {}`), + expected: true, + }, + { + name: "not a valid format", + contents: []byte("test"), + expected: false, + }, + { + name: "invalid skaffold config version", + contents: []byte(`apiVersion: skaffold/v2beta1 +kind: Config +deploy: + kustomize: {}`), + expected: false, + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + filename := testutil.CreateTempFileWithContents(t, "", "skaffold.yaml", test.contents) + defer os.Remove(filename) // clean up + if IsSkaffoldConfig(filename) != test.expected { + t.Errorf("expected to see %t for\n%s. but instead got %t", test.expected, + test.contents, !test.expected) + } + }) + } +} diff --git a/testutil/util.go b/testutil/util.go index 2b52387ecd4..7ac4dbd1bc6 100644 --- a/testutil/util.go +++ b/testutil/util.go @@ -19,6 +19,7 @@ package testutil import ( "errors" "fmt" + "io/ioutil" "net/http" "net/http/httptest" "os" @@ -132,3 +133,20 @@ func ServeFile(t *testing.T, content []byte) (url string, tearDown func()) { return ts.URL, ts.Close } + +// CreateTempFileWithContents creates a temporary file in the dir specified or +// os.TempDir by default with contents mentioned. +func CreateTempFileWithContents(t *testing.T, dir string, name string, content []byte) string { + t.Helper() + tmpfile, err := ioutil.TempFile(dir, name) + if err != nil { + t.Fatal(err) + } + if _, err := tmpfile.Write(content); err != nil { + t.Fatal(err) + } + if err := tmpfile.Close(); err != nil { + t.Fatal(err) + } + return tmpfile.Name() +}