-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
teststep apply, assert and errors can now take urls, files or folder (#…
…123) Signed-off-by: Ken Sipe <[email protected]>
- Loading branch information
Showing
7 changed files
with
150 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package http | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"io" | ||
coreHTTP "net/http" | ||
"net/url" | ||
|
||
"k8s.io/apimachinery/pkg/runtime" | ||
|
||
testutils "github.com/kudobuilder/kuttl/pkg/test/utils" | ||
) | ||
|
||
// IsURL returns true if string is an URL | ||
func IsURL(str string) bool { | ||
u, err := url.Parse(str) | ||
return err == nil && u.Scheme != "" && u.Host != "" | ||
} | ||
|
||
// ToRuntimeObjects takes a url, pulls the file and returns []runtime.Object | ||
// url must be a full path to a manifest file. that file can have multiple runtime objects. | ||
func ToRuntimeObjects(urlPath string) ([]runtime.Object, error) { | ||
apply := []runtime.Object{} | ||
|
||
buf, err := Read(urlPath) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
objs, err := testutils.LoadYAML(urlPath, buf) | ||
if err != nil { | ||
return nil, fmt.Errorf("url %q load yaml error", urlPath) | ||
} | ||
apply = append(apply, objs...) | ||
|
||
return apply, nil | ||
} | ||
|
||
// Read returns a buffer for the file at the url | ||
func Read(urlPath string) (*bytes.Buffer, error) { | ||
|
||
response, err := coreHTTP.Get(urlPath) // nolint:gosec | ||
if err != nil { | ||
return nil, err | ||
} | ||
if response != nil { | ||
defer response.Body.Close() | ||
} | ||
var buf bytes.Buffer | ||
_, err = io.Copy(&buf, response.Body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return &buf, nil | ||
} |
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,43 @@ | ||
package http | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestIsURL(t *testing.T) { | ||
|
||
tests := []struct { | ||
name string | ||
path string | ||
want bool | ||
}{ | ||
{ | ||
"path to folder", | ||
"/opt/foo", | ||
false, | ||
}, | ||
{ | ||
"path to file", | ||
"/opt/foo.txt", | ||
false, | ||
}, | ||
{ | ||
"http to file", | ||
"http://kuttl.dev/foo.txt", | ||
true, | ||
}, | ||
{ | ||
"https to file", | ||
"https://kuttl.dev/foo.txt", | ||
true, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
tt := tt | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := IsURL(tt.path); got != tt.want { | ||
t.Errorf("IsURL() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
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