summaryrefslogtreecommitdiff
path: root/test/utils
diff options
context:
space:
mode:
Diffstat (limited to 'test/utils')
-rw-r--r--test/utils/common_function_test.go12
-rw-r--r--test/utils/matchers.go38
-rw-r--r--test/utils/utils.go27
3 files changed, 49 insertions, 28 deletions
diff --git a/test/utils/common_function_test.go b/test/utils/common_function_test.go
index 003d490ce..c996a302c 100644
--- a/test/utils/common_function_test.go
+++ b/test/utils/common_function_test.go
@@ -90,24 +90,24 @@ var _ = Describe("Common functions test", func() {
Entry("Command exist", "Fakecmd", false),
)
- It("Test WriteJsonFile", func() {
- type testJson struct {
+ It("Test WriteJSONFile", func() {
+ type testJSON struct {
Item1 int
Item2 []string
}
- compareData := &testJson{}
+ compareData := &testJSON{}
- testData := &testJson{
+ testData := &testJSON{
Item1: 5,
Item2: []string{"test"},
}
testByte, _ := json.Marshal(testData)
- err := WriteJsonFile(testByte, "/tmp/testJson")
+ err := WriteJSONFile(testByte, "/tmp/testJSON")
Expect(err).To(BeNil(), "Failed to write JSON to file.")
- read, err := os.Open("/tmp/testJson")
+ read, err := os.Open("/tmp/testJSON")
defer read.Close()
Expect(err).To(BeNil(), "Can not find the JSON file after we write it.")
diff --git a/test/utils/matchers.go b/test/utils/matchers.go
index 17ff3ea75..288779b63 100644
--- a/test/utils/matchers.go
+++ b/test/utils/matchers.go
@@ -1,11 +1,12 @@
package utils
import (
+ "encoding/json"
"fmt"
"net/url"
"github.com/containers/common/pkg/config"
- . "github.com/onsi/gomega"
+ . "github.com/onsi/gomega" //nolint:golint,stylecheck
"github.com/onsi/gomega/format"
"github.com/onsi/gomega/gexec"
"github.com/onsi/gomega/matchers"
@@ -95,7 +96,7 @@ func (matcher *URLMatcher) Match(actual interface{}) (bool, error) {
if !ok {
return false, fmt.Errorf("VerifyURL requires string inputs %T is not supported", matcher.Expected)
}
- e_uri, err := url.Parse(e)
+ eURI, err := url.Parse(e)
if err != nil {
return false, err
}
@@ -104,12 +105,12 @@ func (matcher *URLMatcher) Match(actual interface{}) (bool, error) {
if !ok {
return false, fmt.Errorf("VerifyURL requires string inputs %T is not supported", actual)
}
- a_uri, err := url.Parse(a)
+ aURI, err := url.Parse(a)
if err != nil {
return false, err
}
- return (&matchers.EqualMatcher{Expected: e_uri}).Match(a_uri)
+ return (&matchers.EqualMatcher{Expected: eURI}).Match(aURI)
}
type ExitMatcher struct {
@@ -166,3 +167,32 @@ func (matcher *ExitMatcher) MatchMayChangeInTheFuture(actual interface{}) bool {
}
return true
}
+
+type ValidJSONMatcher struct {
+ types.GomegaMatcher
+}
+
+func BeValidJSON() *ValidJSONMatcher {
+ return &ValidJSONMatcher{}
+}
+
+func (matcher *ValidJSONMatcher) Match(actual interface{}) (success bool, err error) {
+ s, ok := actual.(string)
+ if !ok {
+ return false, fmt.Errorf("ValidJSONMatcher expects a string, not %q", actual)
+ }
+
+ var i interface{}
+ if err := json.Unmarshal([]byte(s), &i); err != nil {
+ return false, nil
+ }
+ return true, nil
+}
+
+func (matcher *ValidJSONMatcher) FailureMessage(actual interface{}) (message string) {
+ return format.Message(actual, "to be valid JSON")
+}
+
+func (matcher *ValidJSONMatcher) NegatedFailureMessage(actual interface{}) (message string) {
+ return format.Message(actual, "to _not_ be valid JSON")
+}
diff --git a/test/utils/utils.go b/test/utils/utils.go
index 4a57d9ce7..f41024072 100644
--- a/test/utils/utils.go
+++ b/test/utils/utils.go
@@ -13,9 +13,9 @@ import (
"time"
"github.com/containers/storage/pkg/parsers/kernel"
- . "github.com/onsi/ginkgo"
- . "github.com/onsi/gomega"
- . "github.com/onsi/gomega/gexec"
+ . "github.com/onsi/ginkgo" //nolint:golint,stylecheck
+ . "github.com/onsi/gomega" //nolint:golint,stylecheck
+ . "github.com/onsi/gomega/gexec" //nolint:golint,stylecheck
)
var (
@@ -36,7 +36,6 @@ type PodmanTestCommon interface {
type PodmanTest struct {
PodmanMakeOptions func(args []string, noEvents, noCache bool) []string
PodmanBinary string
- ArtifactPath string
TempDir string
RemoteTest bool
RemotePodmanBinary string
@@ -439,25 +438,21 @@ func IsKernelNewerThan(version string) (bool, error) {
return true, nil
}
return false, nil
-
}
// IsCommandAvailable check if command exist
func IsCommandAvailable(command string) bool {
check := exec.Command("bash", "-c", strings.Join([]string{"command -v", command}, " "))
err := check.Run()
- if err != nil {
- return false
- }
- return true
+ return err == nil
}
-// WriteJsonFile write json format data to a json file
-func WriteJsonFile(data []byte, filePath string) error {
+// WriteJSONFile write json format data to a json file
+func WriteJSONFile(data []byte, filePath string) error {
var jsonData map[string]interface{}
json.Unmarshal(data, &jsonData)
- formatJson, _ := json.MarshalIndent(jsonData, "", " ")
- return ioutil.WriteFile(filePath, formatJson, 0644)
+ formatJSON, _ := json.MarshalIndent(jsonData, "", " ")
+ return ioutil.WriteFile(filePath, formatJSON, 0644)
}
// Containerized check the podman command run inside container
@@ -471,10 +466,7 @@ func Containerized() bool {
// shrug, if we cannot read that file, return false
return false
}
- if strings.Index(string(b), "docker") > -1 {
- return true
- }
- return false
+ return strings.Contains(string(b), "docker")
}
func init() {
@@ -485,7 +477,6 @@ var randomLetters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
// RandomString returns a string of given length composed of random characters
func RandomString(n int) string {
-
b := make([]rune, n)
for i := range b {
b[i] = randomLetters[rand.Intn(len(randomLetters))]