-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error message when the
skaffold.yaml
is not found (#1947)
* Fix the error message when loading the default `skaffold.yaml` * Don't check for updates if the error is a missing config file * Unit test `newRunner()` Signed-off-by: David Gageot <[email protected]>
- Loading branch information
Showing
8 changed files
with
231 additions
and
97 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
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 cmd | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config" | ||
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest" | ||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
"github.com/pkg/errors" | ||
) | ||
|
||
func writeSkaffoldConfig(t *testing.T, content string) (string, func()) { | ||
yaml := fmt.Sprintf("apiVersion: %s\nkind: Config\n%s", latest.Version, content) | ||
return testutil.TempFile(t, "skaffold.yaml", []byte(yaml)) | ||
} | ||
|
||
func TestNewRunner(t *testing.T) { | ||
cfg, delete := writeSkaffoldConfig(t, "") | ||
defer delete() | ||
|
||
_, _, err := newRunner(&config.SkaffoldOptions{ | ||
ConfigurationFile: cfg, | ||
Trigger: "polling", | ||
}) | ||
|
||
testutil.CheckError(t, false, err) | ||
} | ||
|
||
func TestNewRunnerMissingConfig(t *testing.T) { | ||
_, _, err := newRunner(&config.SkaffoldOptions{ | ||
ConfigurationFile: "missing-skaffold.yaml", | ||
}) | ||
|
||
testutil.CheckError(t, true, err) | ||
if !os.IsNotExist(errors.Cause(err)) { | ||
t.Error("error should say that file is missing") | ||
} | ||
} | ||
|
||
func TestNewRunnerInvalidConfig(t *testing.T) { | ||
cfg, delete := writeSkaffoldConfig(t, "invalid") | ||
defer delete() | ||
|
||
_, _, err := newRunner(&config.SkaffoldOptions{ | ||
ConfigurationFile: cfg, | ||
}) | ||
|
||
testutil.CheckErrorContains(t, "parsing skaffold config", err) | ||
} | ||
|
||
func TestNewRunnerUnknownProfile(t *testing.T) { | ||
cfg, delete := writeSkaffoldConfig(t, "") | ||
defer delete() | ||
|
||
_, _, err := newRunner(&config.SkaffoldOptions{ | ||
ConfigurationFile: cfg, | ||
Profiles: []string{"unknown-profile"}, | ||
}) | ||
|
||
testutil.CheckErrorContains(t, "applying profiles", err) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
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 util | ||
|
||
import ( | ||
"io/ioutil" | ||
"os" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
// ReadConfiguration reads a `skaffold.yaml` configuration and | ||
// returns its content. | ||
func ReadConfiguration(filename string) ([]byte, error) { | ||
switch { | ||
case filename == "": | ||
return nil, errors.New("filename not specified") | ||
case filename == "-": | ||
return ioutil.ReadAll(os.Stdin) | ||
case IsURL(filename): | ||
return Download(filename) | ||
default: | ||
contents, err := ioutil.ReadFile(filename) | ||
if err != nil { | ||
// If the config file is the default `skaffold.yaml`, | ||
// then we also try to read `skaffold.yml`. | ||
if filename == "skaffold.yaml" { | ||
logrus.Infof("Could not open skaffold.yaml: \"%s\"", err) | ||
logrus.Infof("Trying to read from skaffold.yml instead") | ||
contents, errIgnored := ioutil.ReadFile("skaffold.yml") | ||
if errIgnored != nil { | ||
// Return original error because it's the one that matters | ||
return nil, err | ||
} | ||
|
||
return contents, nil | ||
} | ||
} | ||
|
||
return contents, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
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 util | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/GoogleContainerTools/skaffold/testutil" | ||
) | ||
|
||
func TestReadConfiguration(t *testing.T) { | ||
localFile, delete := testutil.TempFile(t, "skaffold.yaml", []byte("some yaml")) | ||
defer delete() | ||
|
||
content, err := ReadConfiguration(localFile) | ||
|
||
testutil.CheckErrorAndDeepEqual(t, false, err, []byte("some yaml"), content) | ||
} | ||
|
||
func TestReadConfigurationFallback(t *testing.T) { | ||
tmpDir, cleanup := testutil.NewTempDir(t) | ||
defer cleanup() | ||
|
||
reset := testutil.Chdir(t, tmpDir.Root()) | ||
defer reset() | ||
|
||
// skaffold.yaml doesn't exist but .yml does | ||
tmpDir.Write("skaffold.yml", "some yaml") | ||
|
||
content, err := ReadConfiguration("skaffold.yaml") | ||
|
||
testutil.CheckErrorAndDeepEqual(t, false, err, []byte("some yaml"), content) | ||
} | ||
|
||
func TestReadConfigurationNotFound(t *testing.T) { | ||
_, err := ReadConfiguration("unknown-config-file.yaml") | ||
|
||
testutil.CheckError(t, true, err) | ||
if !os.IsNotExist(err) { | ||
t.Error("error should say that file doesn't exist") | ||
} | ||
} | ||
|
||
func TestReadConfigurationRemote(t *testing.T) { | ||
remoteFile, teardown := testutil.ServeFile(t, []byte("remote file")) | ||
defer teardown() | ||
|
||
content, err := ReadConfiguration(remoteFile) | ||
|
||
testutil.CheckErrorAndDeepEqual(t, false, err, []byte("remote file"), content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters