forked from containers/toolbox
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Increase the validation coverage for the container & release options
Currently, the container name and release are only validated if they were specified as command line options. Neither the value of release in the configuration file nor the container name generated from an image are validated. There's also a lot of repeated code in the command front-ends to validate the container name and release. This opens the door for mistakes. Any adjustment to the code must be repeated elsewhere, and there are subtle interactions and overlaps between the validation code and the code to resolve container and image names. It's worth noting that the container and image name resolution happens for both the command line and configuration file options, and generates the container name from the image when necessary. Therefore, validating everything while resolving cleans up the command front-ends and increases the coverage of the validation. This introduces the use of sentinel error values and custom error implementations to identify the different errors that can occur while resolving the container and images, so that they can be appropriately shown to the user. containers#937
- Loading branch information
1 parent
b5474bf
commit 13371b5
Showing
10 changed files
with
148 additions
and
66 deletions.
There are no files selected for viewing
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
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
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,44 @@ | ||
/* | ||
* Copyright © 2022 Red Hat Inc. | ||
* | ||
* 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 utils | ||
|
||
import ( | ||
"fmt" | ||
) | ||
|
||
type ContainerError struct { | ||
Container string | ||
Image string | ||
Err error | ||
} | ||
|
||
type ParseReleaseError struct { | ||
Hint string | ||
} | ||
|
||
func (err *ContainerError) Error() string { | ||
errMsg := fmt.Sprintf("%s: %s", err.Container, err.Err) | ||
return errMsg | ||
} | ||
|
||
func (err *ContainerError) Unwrap() error { | ||
return err.Err | ||
} | ||
|
||
func (err *ParseReleaseError) Error() string { | ||
return err.Hint | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,6 +56,17 @@ teardown() { | |
assert_line --index 2 "Run 'toolbox --help' for usage." | ||
} | ||
|
||
@test "create: Try to create a container with invalid custom image ('ß[email protected]€')" { | ||
run $TOOLBOX create --image "ß[email protected]€" | ||
|
||
assert_failure | ||
assert_line --index 0 "Error: invalid argument for '--image'" | ||
assert_line --index 1 "Image gives an invalid container name." | ||
assert_line --index 2 "Container names must match '[a-zA-Z0-9][a-zA-Z0-9_.-]*'." | ||
assert_line --index 3 "Run 'toolbox --help' for usage." | ||
assert [ ${#lines[@]} -eq 4 ] | ||
} | ||
|
||
@test "create: Create a container with a distro and release options ('fedora'; f32)" { | ||
pull_distro_image fedora 32 | ||
|
||
|