2018-01-12 14:24
浏览 389


I don't know how to test the http response given in the code below.

func getVolDetails(volName string, obj interface{}) error {
    addr := os.Getenv("MAPI_ADDR")
    if addr == "" {
        err := errors.New("MAPI_ADDR environment variable not set")
        return err
    url := addr + "/path/to/somepage/" + volName
    client := &http.Client{
        Timeout: timeout,
    resp, err := client.Get(url)
    if resp != nil {
        if resp.StatusCode == 500 {
            fmt.Printf("VSM %s not found
", volName)
            return err
        } else if resp.StatusCode == 503 {
            fmt.Println("server not reachable")
            return err
    } else {
        fmt.Println("server not reachable")
        return err

    if err != nil {
        return err
    defer resp.Body.Close()

    return json.NewDecoder(resp.Body).Decode(obj)

With the help of some references i wrote unit test for this which is given below

func TestGetVolDetails(t *testing.T) {
    var (
        volume v1.Volume
        server *httptest.Server
    tests := map[string]struct {
        volumeName string
        err        error
        "TestOne": {"vol", nil},
    for name, tt := range tests {
        t.Run(name, func(t *testing.T) {
            response := `{"metadata":{"annotations":{"vsm.openebs.io/targetportals":"","vsm.openebs.io/cluster-i    ps":"","openebs.io/jiva-iqn":"iqn.2016-09.com.openebs.jiva:vol","deployment.kubernetes.io/revision":"1","openebs.io/storage-pool"    :"default","vsm.openebs.io/replica-count":"1","openebs.io/jiva-controller-status":"Running","openebs.io/volume-monitor":"false","openebs.io/r    eplica-container-status":"Running","openebs.io/jiva-controller-cluster-ip":"","openebs.io/jiva-replica-status":"Running","vsm.ope    nebs.io/iqn":"iqn.2016-09.com.openebs.jiva:vol","openebs.io/capacity":"2G","openebs.io/jiva-controller-ips":"","openebs.io/jiva-repl    ica-ips":"","vsm.openebs.io/replica-status":"Running","vsm.openebs.io/controller-status":"Running","openebs.io/controller-container-    status":"Running","vsm.openebs.io/replica-ips":"","openebs.io/jiva-target-portal":"","openebs.io/volume-type":"jiva    ","openebs.io/jiva-replica-count":"1","vsm.openebs.io/volume-size":"2G","vsm.openebs.io/controller-ips":""},"creationTimestamp":null    ,"labels":{},"name":"vol"},"status":{"Message":"","Phase":"Running","Reason":""}}`
            server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
                fmt.Fprintln(w, response)
            os.Setenv("MAPI_ADDR", "http://"+server.URL)
            if got := GetVolDetails(tt.volumeName, &volume); got != tt.err {
                t.Fatalf("GetVolDetails(%v) => got %v, want %v ", tt.volumeName, got, tt.err)
            defer server.Close()

Where response is the response i'm getting from the server. This gives me always different errors. got invalid character '<' looking for beginning of value, want <nil>

got Get http://www.HugeDomains.com: net/http: request canceled (Client.Timeout exceeded while awaiting headers), want <nil>

What am I doing wrong?


Updated the code with SOME_ADDR to MAPI_ADDR which was done while posting question. Please don't be confused with that, problem remains as it is.

1条回答 默认 最新

相关推荐 更多相似问题