Expand test coverage in master, kubectl/cmd/util, pkg/registry/resourcequota, and api/rest.

This commit is contained in:
Brendan Burns
2015-03-07 11:00:45 +01:00
parent 53ec66caf4
commit 7c654a3d1b
13 changed files with 900 additions and 234 deletions

View File

@@ -19,6 +19,7 @@ package util
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
@@ -59,11 +60,11 @@ func GetFlagBool(cmd *cobra.Command, flag string) bool {
if f == nil {
glog.Fatalf("Flag accessed but not defined for command %s: %s", cmd.Name(), flag)
}
// Caseless compare.
if strings.ToLower(f.Value.String()) == "true" {
return true
result, err := strconv.ParseBool(f.Value.String())
if err != nil {
glog.Fatalf("Invalid value for a boolean flag: %s", f.Value.String())
}
return false
return result
}
// Assumes the flag has a default value.
@@ -89,6 +90,19 @@ func GetFlagDuration(cmd *cobra.Command, flag string) time.Duration {
return v
}
func ReadConfigDataFromReader(reader io.Reader, source string) ([]byte, error) {
data, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf(`Read from %s but no data found`, source)
}
return data, nil
}
// ReadConfigData reads the bytes from the specified filesytem or network
// location or from stdin if location == "-".
// TODO: replace with resource.Builder
@@ -99,16 +113,7 @@ func ReadConfigData(location string) ([]byte, error) {
if location == "-" {
// Read from stdin.
data, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return nil, err
}
if len(data) == 0 {
return nil, fmt.Errorf(`Read from stdin specified ("-") but no data found`)
}
return data, nil
return ReadConfigDataFromReader(os.Stdin, "stdin ('-')")
}
// Use the location as a file path or URL.
@@ -127,17 +132,13 @@ func ReadConfigDataFromLocation(location string) ([]byte, error) {
if resp.StatusCode != 200 {
return nil, fmt.Errorf("unable to read URL, server reported %d %s", resp.StatusCode, resp.Status)
}
data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read URL %s: %v\n", location, err)
}
return data, nil
return ReadConfigDataFromReader(resp.Body, location)
} else {
data, err := ioutil.ReadFile(location)
file, err := os.Open(location)
if err != nil {
return nil, fmt.Errorf("unable to read %s: %v\n", location, err)
}
return data, nil
return ReadConfigDataFromReader(file, location)
}
}

View File

@@ -17,7 +17,11 @@ limitations under the License.
package util
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"reflect"
"syscall"
"testing"
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
@@ -187,5 +191,75 @@ func TestMerge(t *testing.T) {
t.Errorf("testcase[%d], unexpected non-error", i)
}
}
}
type fileHandler struct {
data []byte
}
func (f *fileHandler) ServeHTTP(res http.ResponseWriter, req *http.Request) {
if req.URL.Path == "/error" {
res.WriteHeader(http.StatusNotFound)
return
}
res.WriteHeader(http.StatusOK)
res.Write(f.data)
}
func TestReadConfigData(t *testing.T) {
httpData := []byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
server := httptest.NewServer(&fileHandler{data: httpData})
fileData := []byte{11, 12, 13, 14, 15, 16, 17, 18, 19}
f, err := ioutil.TempFile("", "config")
if err != nil {
t.Errorf("unexpected error setting up config file")
t.Fail()
}
defer syscall.Unlink(f.Name())
ioutil.WriteFile(f.Name(), fileData, 0644)
// TODO: test TLS here, requires making it possible to inject the HTTP client.
tests := []struct {
config string
data []byte
expectErr bool
}{
{
config: server.URL,
data: httpData,
},
{
config: server.URL + "/error",
expectErr: true,
},
{
config: "http://some.non.existent.url",
expectErr: true,
},
{
config: f.Name(),
data: fileData,
},
{
config: "some-non-existent-file",
expectErr: true,
},
{
config: "",
expectErr: true,
},
}
for _, test := range tests {
dataOut, err := ReadConfigData(test.config)
if err != nil && !test.expectErr {
t.Errorf("unexpected err: %v for %s", err, test.config)
}
if err == nil && test.expectErr {
t.Errorf("unexpected non-error for %s", test.config)
}
if !test.expectErr && !reflect.DeepEqual(test.data, dataOut) {
t.Errorf("unexpected data: %v, expected %v", dataOut, test.data)
}
}
}