mirror of
https://github.com/yannh/kubeconform.git
synced 2026-02-11 14:09:21 +00:00
add tests to DownloadSchema
This commit is contained in:
parent
b524ace87f
commit
43b5c5c152
2 changed files with 106 additions and 1 deletions
|
|
@ -8,9 +8,13 @@ import (
|
|||
"time"
|
||||
)
|
||||
|
||||
type httpGetter interface {
|
||||
Get(url string) (resp *http.Response, err error)
|
||||
}
|
||||
|
||||
// SchemaRegistry is a file repository (local or remote) that contains JSON schemas for Kubernetes resources
|
||||
type SchemaRegistry struct {
|
||||
c *http.Client
|
||||
c httpGetter
|
||||
schemaPathTemplate string
|
||||
strict bool
|
||||
}
|
||||
|
|
|
|||
101
pkg/registry/http_test.go
Normal file
101
pkg/registry/http_test.go
Normal file
|
|
@ -0,0 +1,101 @@
|
|||
package registry
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type mockHTTPGetter struct {
|
||||
httpGet func(string) (*http.Response, error)
|
||||
}
|
||||
|
||||
func newMockHTTPGetter(f func(string) (*http.Response, error)) *mockHTTPGetter {
|
||||
return &mockHTTPGetter{
|
||||
httpGet: f,
|
||||
}
|
||||
}
|
||||
func (m mockHTTPGetter) Get(url string) (resp *http.Response, err error) {
|
||||
return m.httpGet(url)
|
||||
}
|
||||
|
||||
func TestDownloadSchema(t *testing.T) {
|
||||
for _, testCase := range []struct {
|
||||
name string
|
||||
c httpGetter
|
||||
schemaPathTemplate string
|
||||
strict bool
|
||||
resourceKind, resourceAPIVersion, k8sversion string
|
||||
expect []byte
|
||||
expectErr error
|
||||
}{
|
||||
{
|
||||
"error when downloading",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
return nil, fmt.Errorf("failed downloading from registry")
|
||||
}),
|
||||
"http://kubernetesjson.dev",
|
||||
true,
|
||||
"Deployment",
|
||||
"v1",
|
||||
"1.18.0",
|
||||
nil,
|
||||
fmt.Errorf("failed downloading schema at http://kubernetesjson.dev: failed downloading from registry"),
|
||||
},
|
||||
{
|
||||
"getting 404",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusNotFound,
|
||||
Body: ioutil.NopCloser(strings.NewReader("http response mock body")),
|
||||
}, nil
|
||||
}),
|
||||
"http://kubernetesjson.dev",
|
||||
true,
|
||||
"Deployment",
|
||||
"v1",
|
||||
"1.18.0",
|
||||
nil,
|
||||
fmt.Errorf("no schema found"),
|
||||
},
|
||||
{
|
||||
"200",
|
||||
newMockHTTPGetter(func(url string) (resp *http.Response, err error) {
|
||||
return &http.Response{
|
||||
StatusCode: http.StatusOK,
|
||||
Body: ioutil.NopCloser(strings.NewReader("http response mock body")),
|
||||
}, nil
|
||||
}),
|
||||
"http://kubernetesjson.dev",
|
||||
true,
|
||||
"Deployment",
|
||||
"v1",
|
||||
"1.18.0",
|
||||
[]byte("http response mock body"),
|
||||
nil,
|
||||
},
|
||||
} {
|
||||
reg := SchemaRegistry{
|
||||
c: testCase.c,
|
||||
schemaPathTemplate: testCase.schemaPathTemplate,
|
||||
strict: testCase.strict,
|
||||
}
|
||||
|
||||
res, err := reg.DownloadSchema(testCase.resourceKind, testCase.resourceAPIVersion, testCase.k8sversion)
|
||||
if err == nil || testCase.expectErr == nil {
|
||||
if err != testCase.expectErr {
|
||||
t.Errorf("during test '%s': expected error, got:\n%s\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
}
|
||||
} else if err.Error() != testCase.expectErr.Error() {
|
||||
t.Errorf("during test '%s': expected error, got:\n%s\n%s\n", testCase.name, testCase.expectErr, err)
|
||||
}
|
||||
|
||||
if bytes.Compare(res, testCase.expect) != 0 {
|
||||
t.Errorf("during test '%s': expected %s, got %s", testCase.name, testCase.expect, res)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in a new issue