Repository: qiniu/goc (go)

Issue:
When no profile fetched, it should return an error instead of  an unexpected response
Now, when get profile using "force" tag, it may get a strange response: "no profile". It should be an error or nothing back.

Pull request description:
Error returned when no profiles fetched
FIX #109 

Patch:
diff --git a/cmd/profile.go b/cmd/profile.go
index 503f49f..7dfe51e 100644
--- a/cmd/profile.go
+++ b/cmd/profile.go
@@ -59,7 +59,7 @@ goc profile --force
 		}
 		res, err := cover.NewWorker(center).Profile(p)
 		if err != nil {
-			log.Fatalf("call host %v failed, err: %v, response: %v", center, err, string(res))
+			log.Fatalf("Goc server %v return an error: %v", center, err)
 		}
 
 		if output == "" {
diff --git a/pkg/cover/server.go b/pkg/cover/server.go
index 93a50c1..b923d80 100644
--- a/pkg/cover/server.go
+++ b/pkg/cover/server.go
@@ -173,7 +173,7 @@ func profile(c *gin.Context) {
 	}
 
 	if len(mergedProfiles) == 0 {
-		c.JSON(http.StatusOK, gin.H{"message": "no profiles"})
+		c.JSON(http.StatusExpectationFailed, gin.H{"error": "no profiles"})
 		return
 	}
 


Test patch:
diff --git a/pkg/cover/client_test.go b/pkg/cover/client_test.go
index e5cc340..6d812cb 100644
--- a/pkg/cover/client_test.go
+++ b/pkg/cover/client_test.go
@@ -75,46 +75,51 @@ func TestClientAction(t *testing.T) {
 			expectedErr: true,
 		},
 		{
-			name:     "valid test with no service flag provied",
+			name:     "valid test with no service flag provide",
 			service:  Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
 			param:    ProfileParam{},
 			expected: "mockService/main.go:30.13,48.33 13 1",
 		},
 		{
-			name:     "valid test with service flag provied",
+			name:     "valid test with service flag provide",
 			service:  Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
 			param:    ProfileParam{Service: []string{"serviceOK"}},
 			expected: "mockService/main.go:30.13,48.33 13 1",
 		},
 		{
-			name:     "valid test with address flag provied",
+			name:     "valid test with address flag provide",
 			service:  Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
 			param:    ProfileParam{Address: []string{profileSuccessMockSvr.URL}},
 			expected: "mockService/main.go:30.13,48.33 13 1",
 		},
 		{
+			name:        "invalid test with invalid service flag provide",
 			service:     Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
 			param:       ProfileParam{Service: []string{"unknown"}},
 			expected:    "service [unknown] not found",
 			expectedErr: true,
 		},
 		{
+			name:        "invalid test with invalid profile got by service",
 			service:     Service{Name: "serviceErr", Address: profileErrMockSvr.URL},
 			expected:    "bad mode line: error",
 			expectedErr: true,
 		},
 		{
+			name:        "invalid test with disconnected service",
 			service:     Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
 			expected:    "connection refused",
 			expectedErr: true,
 		},
 		{
-			service:  Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
-			param:    ProfileParam{Force: true},
-			expected: `{"message":"no profiles"}`,
+			name:        "invalid test with empty profile",
+			service:     Service{Name: "serviceNotExist", Address: "http://172.0.0.2:7777"},
+			param:       ProfileParam{Force: true},
+			expectedErr: true,
+			expected:    "no profiles",
 		},
 		{
-			name:     "valid test with coverfile flag provied",
+			name:     "valid test with coverfile flag provide",
 			service:  Service{Name: "serviceOK", Address: profileSuccessMockSvr.URL},
 			param:    ProfileParam{CoverFilePatterns: []string{"b.go$"}},
 			expected: "b/b.go",
diff --git a/tests/profile.bats b/tests/profile.bats
index d4bc91e..6a64bf2 100755
--- a/tests/profile.bats
+++ b/tests/profile.bats
@@ -94,6 +94,40 @@ setup() {
     [[ "$output" == *"b.go"* ]] # contains b.go file
     [[ "$output" != *"main.go"* ]] # not contains main.go file
 
+    wait $profile_pid
+    kill -9 $SAMPLE_PID
+}
+
+@test "test goc profile with service flag" {
+    ./simple-project 3>&- &
+    SAMPLE_PID=$!
+    sleep 2
+
+    wait_profile_backend "profile4" &
+    profile_pid=$!
+
+    run gocc profile --center=http://127.0.0.1:60001 --service="simple-project" --debug --debugcisyncfile ci-sync.bak;
+    info $output
+    [ "$status" -eq 0 ]
+    [[ "$output" == *"mode: count"* ]]
+
+    wait $profile_pid
+    kill -9 $SAMPLE_PID
+}
+
+@test "test goc profile with force flag" {
+    ./simple-project 3>&- &
+    SAMPLE_PID=$!
+    sleep 2
+
+    wait_profile_backend "profile5" &
+    profile_pid=$!
+
+    run gocc profile --center=http://127.0.0.1:60001 --service="simple-project,unknown" --force --debug --debugcisyncfile ci-sync.bak;
+    info $output
+    [ "$status" -eq 0 ]
+    [[ "$output" == *"mode: count"* ]]
+
     wait $profile_pid
     kill -9 $SAMPLE_PID
 }
\ No newline at end of file
diff --git a/tests/server.bats b/tests/server.bats
index 6395801..371cee5 100755
--- a/tests/server.bats
+++ b/tests/server.bats
@@ -41,7 +41,9 @@ teardown_file() {
     [ "$status" -eq 0 ]
     # connect to covered goc
     run goc profile --center=http://127.0.0.1:60001
-    [ "$status" -eq 0 ]
+    # no profiles
+    [ "$status" -eq 1 ]
+    [[ "$output" == *"no profiles"* ]]
 }
 
 @test "register a covered service" {
@@ -54,5 +56,5 @@ teardown_file() {
     sleep 1
     # connect to covered goc
     run goc profile --center=http://127.0.0.1:60001
-    [ "$status" -eq 0 ]    
+    [ "$status" -eq 0 ]
 }
\ No newline at end of file