-
Notifications
You must be signed in to change notification settings - Fork 950
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Junjun Li <junjunli666@gmail.com>
- Loading branch information
Showing
10 changed files
with
319 additions
and
0 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package client | ||
|
||
import ( | ||
"context" | ||
"net/url" | ||
) | ||
|
||
// NetworkPrune delete unused networks. | ||
func (client *APIClient) NetworkPrune(ctx context.Context) ([]string, error) { | ||
resp, err := client.post(ctx, "/networks/prune", url.Values{}, nil, nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
result := []string{} | ||
err = decodeBody(&result, resp.Body) | ||
ensureCloseReader(resp) | ||
|
||
return result, err | ||
} |
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,66 @@ | ||
package client | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNetworkPruneError(t *testing.T) { | ||
client := &APIClient{ | ||
HTTPCli: newMockClient(errorMockResponse(http.StatusInternalServerError, "Server error")), | ||
} | ||
|
||
_, err := client.NetworkPrune(context.Background()) | ||
|
||
if err == nil || err.Error() == "Server error" { | ||
t.Fatalf("expected a Server Error, got %v", err) | ||
} | ||
} | ||
|
||
func TestNetworkPruneList(t *testing.T) { | ||
expectedURL := "/networks/prune" | ||
|
||
httpClient := newMockClient(func(req *http.Request) (*http.Response, error) { | ||
if !strings.HasPrefix(req.URL.Path, expectedURL) { | ||
return nil, fmt.Errorf("expected URL '%s', got '%s'", expectedURL, req.URL) | ||
} | ||
|
||
networksPruneJSON := []string{ | ||
"network0", | ||
"network1", | ||
"network2", | ||
} | ||
|
||
b, err := json.Marshal(networksPruneJSON) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &http.Response{ | ||
StatusCode: http.StatusOK, | ||
Body: ioutil.NopCloser(bytes.NewReader([]byte(b))), | ||
}, nil | ||
}) | ||
|
||
client := &APIClient{ | ||
HTTPCli: httpClient, | ||
} | ||
networkPruneResp, err := client.NetworkPrune(context.Background()) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if len(networkPruneResp) != 3 { | ||
t.Fatalf("expected 3 networks, got %v", networkPruneResp) | ||
} | ||
assert.Equal(t, networkPruneResp[0], "network0") | ||
assert.Equal(t, networkPruneResp[1], "network1") | ||
assert.Equal(t, networkPruneResp[2], "network2") | ||
} |
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,42 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/alibaba/pouch/test/command" | ||
"github.com/alibaba/pouch/test/environment" | ||
"github.com/alibaba/pouch/test/request" | ||
|
||
"github.com/go-check/check" | ||
"github.com/gotestyourself/gotestyourself/icmd" | ||
) | ||
|
||
// APINetworkPruneSuite is the test suite for network prune API. | ||
type APINetworkPruneSuite struct{} | ||
|
||
func init() { | ||
check.Suite(&APINetworkPruneSuite{}) | ||
} | ||
|
||
// SetUpTest does common setup in the beginning of each test. | ||
func (suite *APINetworkPruneSuite) SetUpTest(c *check.C) { | ||
SkipIfFalse(c, environment.IsLinux) | ||
} | ||
|
||
// TestNetworkPruneOk test if prune network is ok | ||
func (suite *APINetworkPruneSuite) TestNetworkPruneOk(c *check.C) { | ||
network1 := "TestPruneNetwork1" | ||
command.PouchRun("network", "create", network1, "-d", "bridge", "--gateway", "192.168.1.1", "--subnet", "192.168.1.0/24").Assert(c, icmd.Success) | ||
|
||
network2 := "TestPruneNetwork2" | ||
command.PouchRun("network", "create", network2, "-d", "bridge", "--gateway", "192.168.2.1", "--subnet", "192.168.2.0/24").Assert(c, icmd.Success) | ||
|
||
// prune the network. | ||
path := "/networks/prune" | ||
resp, err := request.Post(path) | ||
c.Assert(err, check.IsNil) | ||
CheckRespStatus(c, resp, 200) | ||
|
||
var networkPruneResp []string | ||
err = request.DecodeBody(&networkPruneResp, resp.Body) | ||
c.Assert(err, check.IsNil) | ||
c.Assert(len(networkPruneResp), check.Equals, 2) | ||
} |
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