Skip to content

Commit bb4bc30

Browse files
committed
Remove leaking logs from SDK
This commit removes leaking logs from the SDK (proxy package) of the CLI by removing custom print statements within proxy package. It updates method signature for DeployFunction which now returns deployOutput text along with status code. Fixes: #853 Signed-off-by: Vivek Singh <[email protected]>
1 parent 6b5e7a1 commit bb4bc30

File tree

6 files changed

+42
-52
lines changed

6 files changed

+42
-52
lines changed

commands/deploy.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,9 @@ Error: %s`, fprocessErr.Error())
276276
if msg := checkTLSInsecure(services.Provider.GatewayURL, deploySpec.TLSInsecure); len(msg) > 0 {
277277
fmt.Println(msg)
278278
}
279-
statusCode := proxyClient.DeployFunction(ctx, deploySpec)
279+
statusCode, deployOutputStr := proxyClient.DeployFunction(ctx, deploySpec)
280+
fmt.Println()
281+
fmt.Println(deployOutputStr)
280282
if badStatusCode(statusCode) {
281283
failedStatusCodes[k] = statusCode
282284
}
@@ -375,8 +377,9 @@ func deployImage(
375377
fmt.Println(msg)
376378
}
377379

378-
statusCode = client.DeployFunction(ctx, deploySpec)
379-
380+
statusCode, deployOutputStr := client.DeployFunction(ctx, deploySpec)
381+
fmt.Println()
382+
fmt.Println(deployOutputStr)
380383
return statusCode, nil
381384
}
382385

proxy/delete.go

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,12 @@ func (c *Client) DeleteFunction(ctx context.Context, functionName string, namesp
3030

3131
req, err := c.newRequest(http.MethodDelete, deleteEndpoint, reader)
3232
if err != nil {
33-
fmt.Println(err)
3433
return err
3534
}
3635
delRes, delErr := c.doRequest(ctx, req)
3736

3837
if delErr != nil {
39-
fmt.Printf("Error removing existing function: %s, gateway=%s, functionName=%s\n", delErr.Error(), c.GatewayURL.String(), functionName)
40-
return delErr
38+
return fmt.Errorf("error removing existing function: %s, gateway=%s, functionName=%s", delErr.Error(), c.GatewayURL.String(), functionName)
4139
}
4240

4341
if delRes.Body != nil {
@@ -46,7 +44,6 @@ func (c *Client) DeleteFunction(ctx context.Context, functionName string, namesp
4644

4745
switch delRes.StatusCode {
4846
case http.StatusOK, http.StatusCreated, http.StatusAccepted:
49-
fmt.Println("Removing old function.")
5047
case http.StatusNotFound:
5148
err = fmt.Errorf("No existing function to remove")
5249
case http.StatusUnauthorized:

proxy/delete_test.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,10 @@ func Test_DeleteFunction(t *testing.T) {
2121
cliAuth := NewTestAuth(nil)
2222
proxyClient, _ := NewClient(cliAuth, s.URL, nil, &defaultCommandTimeout)
2323

24-
stdout := test.CaptureStdout(func() {
25-
proxyClient.DeleteFunction(context.Background(), "function-to-delete", "")
26-
})
24+
err := proxyClient.DeleteFunction(context.Background(), "function-to-delete", "")
2725

28-
r := regexp.MustCompile(`(?m:Removing old function.)`)
29-
if !r.MatchString(stdout) {
30-
t.Fatalf("Want: %s, got: %s", "Removing old function", stdout)
26+
if err != nil {
27+
t.Fatalf("Got error: %s", err.Error())
3128
}
3229
}
3330

proxy/deploy.go

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,15 @@ func generateFuncStr(spec *DeployFunctionSpec) string {
5959

6060
// DeployFunction first tries to deploy a function and if it exists will then attempt
6161
// a rolling update. Warnings are suppressed for the second API call (if required.)
62-
func (c *Client) DeployFunction(context context.Context, spec *DeployFunctionSpec) int {
62+
func (c *Client) DeployFunction(context context.Context, spec *DeployFunctionSpec) (int, string) {
6363

64-
rollingUpdateInfo := fmt.Sprintf("Function %s already exists, attempting rolling-update.", spec.FunctionName)
6564
statusCode, deployOutput := c.deploy(context, spec, spec.Update)
6665

6766
if spec.Update == true && statusCode == http.StatusNotFound {
6867
// Re-run the function with update=false
69-
7068
statusCode, deployOutput = c.deploy(context, spec, false)
71-
} else if statusCode == http.StatusOK {
72-
fmt.Println(rollingUpdateInfo)
7369
}
74-
fmt.Println()
75-
fmt.Println(deployOutput)
76-
return statusCode
70+
return statusCode, deployOutput
7771
}
7872

7973
// deploy a function to an OpenFaaS gateway over REST

proxy/deploy_test.go

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@ package proxy
66
import (
77
"context"
88
"net/http"
9+
"regexp"
910

1011
"testing"
1112

12-
"regexp"
13-
1413
"github.com/openfaas/faas-cli/test"
1514
)
1615

@@ -22,6 +21,7 @@ type deployProxyTest struct {
2221
replace bool
2322
update bool
2423
expectedOutput string
24+
expectedStatus int
2525
}
2626

2727
func runDeployProxyTest(t *testing.T, deployTest deployProxyTest) {
@@ -34,32 +34,34 @@ func runDeployProxyTest(t *testing.T, deployTest deployProxyTest) {
3434
cliAuth := NewTestAuth(nil)
3535
proxyClient, _ := NewClient(cliAuth, s.URL, nil, &defaultCommandTimeout)
3636

37-
stdout := test.CaptureStdout(func() {
38-
proxyClient.DeployFunction(context.TODO(), &DeployFunctionSpec{
39-
"fprocess",
40-
"function",
41-
"image",
42-
"dXNlcjpwYXNzd29yZA==",
43-
"language",
44-
deployTest.replace,
45-
nil,
46-
"network",
47-
[]string{},
48-
deployTest.update,
49-
[]string{},
50-
map[string]string{},
51-
map[string]string{},
52-
FunctionResourceRequest{},
53-
false,
54-
tlsNoVerify,
55-
"",
56-
"",
57-
})
37+
statusCode, deployOutputStr := proxyClient.DeployFunction(context.TODO(), &DeployFunctionSpec{
38+
"fprocess",
39+
"function",
40+
"image",
41+
"dXNlcjpwYXNzd29yZA==",
42+
"language",
43+
deployTest.replace,
44+
nil,
45+
"network",
46+
[]string{},
47+
deployTest.update,
48+
[]string{},
49+
map[string]string{},
50+
map[string]string{},
51+
FunctionResourceRequest{},
52+
false,
53+
tlsNoVerify,
54+
"",
55+
"",
5856
})
5957

58+
if statusCode != deployTest.expectedStatus {
59+
t.Fatalf("Got: %d, expected: %d", statusCode, deployTest.expectedStatus)
60+
}
61+
6062
r := regexp.MustCompile(deployTest.expectedOutput)
61-
if !r.MatchString(stdout) {
62-
t.Fatalf("Output not matched: %s", stdout)
63+
if !r.MatchString(deployOutputStr) {
64+
t.Fatalf("Output not matched: %s", deployOutputStr)
6365
}
6466
}
6567

@@ -71,20 +73,23 @@ func Test_RunDeployProxyTests(t *testing.T) {
7173
replace: true,
7274
update: false,
7375
expectedOutput: `(?m:Deployed)`,
76+
expectedStatus: http.StatusOK,
7477
},
7578
{
7679
title: "404_Deploy",
7780
mockServerResponses: []int{http.StatusOK, http.StatusNotFound},
7881
replace: true,
7982
update: false,
8083
expectedOutput: `(?m:Unexpected status: 404)`,
84+
expectedStatus: http.StatusNotFound,
8185
},
8286
{
8387
title: "UpdateFailedDeployed",
8488
mockServerResponses: []int{http.StatusNotFound, http.StatusOK},
8589
replace: false,
8690
update: true,
8791
expectedOutput: `(?m:Deployed)`,
92+
expectedStatus: http.StatusOK,
8893
},
8994
}
9095
for _, tst := range deployProxyTests {

proxy/invoke.go

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package proxy
55

66
import (
77
"bytes"
8-
"os"
98

109
"fmt"
1110
"io/ioutil"
@@ -53,8 +52,6 @@ func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType st
5352

5453
req, err := http.NewRequest(httpMethod, gatewayURL, reader)
5554
if err != nil {
56-
fmt.Println()
57-
fmt.Println(err)
5855
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway)
5956
}
6057

@@ -71,8 +68,6 @@ func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType st
7168
res, err := client.Do(req)
7269

7370
if err != nil {
74-
fmt.Println()
75-
fmt.Println(err)
7671
return nil, fmt.Errorf("cannot connect to OpenFaaS on URL: %s", gateway)
7772
}
7873

@@ -82,7 +77,6 @@ func InvokeFunction(gateway string, name string, bytesIn *[]byte, contentType st
8277

8378
switch res.StatusCode {
8479
case http.StatusAccepted:
85-
fmt.Fprintf(os.Stderr, "Function submitted asynchronously.\n")
8680
case http.StatusOK:
8781
var readErr error
8882
resBytes, readErr = ioutil.ReadAll(res.Body)

0 commit comments

Comments
 (0)