mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-12-07 18:06:21 +00:00
Expand test coverage in master, kubectl/cmd/util, pkg/registry/resourcequota, and api/rest.
This commit is contained in:
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user