Skip to content

Commit

Permalink
Add more tests for IsSkaffoldConfig and IsKubernetesFormat
Browse files Browse the repository at this point in the history
  • Loading branch information
tejal29 committed Mar 8, 2019
1 parent 4557fa8 commit d3a71d8
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 24 deletions.
4 changes: 2 additions & 2 deletions cmd/skaffold/app/cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand Down
11 changes: 6 additions & 5 deletions pkg/skaffold/initializer/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type Config struct {
CliArtifacts []string
SkipBuild bool
Force bool
Opts config.SkaffoldOptions
Opts *config.SkaffoldOptions
Analyze bool
}

Expand All @@ -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) {
Expand Down Expand Up @@ -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
}
Expand Down
61 changes: 61 additions & 0 deletions pkg/skaffold/initializer/init_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
}
}
19 changes: 2 additions & 17 deletions pkg/skaffold/initializer/kubectl/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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()
}
101 changes: 101 additions & 0 deletions pkg/skaffold/initializer/util_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}
18 changes: 18 additions & 0 deletions testutil/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package testutil
import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
Expand Down Expand Up @@ -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()
}

0 comments on commit d3a71d8

Please sign in to comment.