Repository: go-kratos/kratos
Pull request #2349 (2022-09-02 03:51:19)

test(config): add etcd config method test
Added test cases for the about etcd method
- WithPath(path string)
- WithPrefix(prefix bool)

Update #2135

codecov-commenter: # [Codecov](https://codecov.io/gh/go-kratos/kratos/pull/2349?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) Report
> Merging [#2349](https://codecov.io/gh/go-kratos/kratos/pull/2349?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (d833960) into [main](https://codecov.io/gh/go-kratos/kratos/commit/60b00c8ade032c18b1bba9c9a96a84db4a574bae?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos) (60b00c8) will **not change** coverage.
> The diff coverage is `n/a`.

```diff
@@           Coverage Diff           @@
##             main    #2349   +/-   ##
=======================================
  Coverage   85.27%   85.27%           
=======================================
  Files          88       88           
  Lines        3775     3775           
=======================================
  Hits         3219     3219           
  Misses        391      391           
  Partials      165      165           
```



Help us with your feedback. Take ten seconds to tell us [how you rate us](https://about.codecov.io/nps?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos). Have a feature suggestion? [Share it here.](https://app.codecov.io/gh/feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=go-kratos)


Patch:
diff --git a/contrib/opensergo/opensergo.go b/contrib/opensergo/opensergo.go
index 6da1f596..112c0bab 100644
--- a/contrib/opensergo/opensergo.go
+++ b/contrib/opensergo/opensergo.go
@@ -2,7 +2,6 @@ package opensergo
 
 import (
 	"encoding/json"
-	"io/ioutil"
 	"net"
 	"net/url"
 	"os"
@@ -52,7 +51,7 @@ func New(opts ...Option) (*OpenSergo, error) {
 		}
 	}
 	if v := os.Getenv("OPENSERGO_BOOTSTRAP_CONFIG"); v != "" {
-		b, err := ioutil.ReadFile(v)
+		b, err := os.ReadFile(v)
 		if err != nil {
 			return nil, err
 		}
@@ -89,7 +88,7 @@ func (s *OpenSergo) ReportMetadata(ctx context.Context, app kratos.AppInfo) erro
 	}
 
 	for _, endpoint := range app.Endpoint() {
-		u, err := url.Parse(endpoint) //nolint
+		u, err := url.Parse(endpoint) // nolint
 		if err != nil {
 			return err
 		}
diff --git a/contrib/registry/eureka/client.go b/contrib/registry/eureka/client.go
index 64cdddec..35dd1472 100644
--- a/contrib/registry/eureka/client.go
+++ b/contrib/registry/eureka/client.go
@@ -6,7 +6,6 @@ import (
 	"encoding/json"
 	"fmt"
 	"io"
-	"io/ioutil"
 	"math/rand"
 	"net/http"
 	"strings"
@@ -323,12 +322,12 @@ func (e *Client) do(ctx context.Context, method string, params []string, input i
 			continue
 		}
 		defer func() {
-			_, _ = io.Copy(ioutil.Discard, resp.Body)
+			_, _ = io.Copy(io.Discard, resp.Body)
 			resp.Body.Close()
 		}()
 
 		if output != nil && resp.StatusCode/100 == 2 {
-			data, err := ioutil.ReadAll(resp.Body)
+			data, err := io.ReadAll(resp.Body)
 			if err != nil {
 				return err
 			}


Test patch:
diff --git a/contrib/config/etcd/config_test.go b/contrib/config/etcd/config_test.go
index c106c057..821167ec 100644
--- a/contrib/config/etcd/config_test.go
+++ b/contrib/config/etcd/config_test.go
@@ -15,7 +15,8 @@ const testKey = "/kratos/test/config"
 func TestConfig(t *testing.T) {
 	client, err := clientv3.New(clientv3.Config{
 		Endpoints:   []string{"127.0.0.1:2379"},
-		DialTimeout: time.Second, DialOptions: []grpc.DialOption{grpc.WithBlock()},
+		DialTimeout: time.Second,
+		DialOptions: []grpc.DialOption{grpc.WithBlock()},
 	})
 	if err != nil {
 		t.Fatal(err)
@@ -108,3 +109,61 @@ func TestExtToFormat(t *testing.T) {
 		t.Errorf("kvs[0].Format is %s", kvs[0].Format)
 	}
 }
+
+func TestEtcdWithPath(t *testing.T) {
+	tests := []struct {
+		name   string
+		fields string
+		want   string
+	}{
+		{
+			name:   "default",
+			fields: testKey,
+			want:   testKey,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			options := &options{
+				ctx: context.Background(),
+			}
+
+			got := WithPath(tt.fields)
+			got(options)
+
+			if options.path != tt.want {
+				t.Errorf("WithPath(tt.fields) = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
+
+func TestEtcdWithPrefix(t *testing.T) {
+	tests := []struct {
+		name   string
+		fields bool
+		want   bool
+	}{
+		{
+			name:   "default",
+			fields: false,
+			want:   false,
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			options := &options{
+				ctx: context.Background(),
+			}
+
+			got := WithPrefix(tt.fields)
+			got(options)
+
+			if options.prefix != tt.want {
+				t.Errorf("WithPrefix(tt.fields) = %v, want %v", got, tt.want)
+			}
+		})
+	}
+}
diff --git a/contrib/config/polaris/config_test.go b/contrib/config/polaris/config_test.go
index fc864526..bf8b6fd6 100644
--- a/contrib/config/polaris/config_test.go
+++ b/contrib/config/polaris/config_test.go
@@ -4,7 +4,7 @@ import (
 	"encoding/json"
 	"errors"
 	"fmt"
-	"io/ioutil"
+	"io"
 	"net/http"
 	"reflect"
 	"strings"
@@ -38,7 +38,7 @@ func makeJSONRequest(uri string, data string, method string, headers map[string]
 		return nil, err
 	}
 	defer res.Body.Close()
-	return ioutil.ReadAll(res.Body)
+	return io.ReadAll(res.Body)
 }
 
 type commonRes struct {
diff --git a/contrib/opensergo/opensergo_test.go b/contrib/opensergo/opensergo_test.go
index 5c752d1c..9f49bbee 100644
--- a/contrib/opensergo/opensergo_test.go
+++ b/contrib/opensergo/opensergo_test.go
@@ -1,7 +1,6 @@
 package opensergo
 
 import (
-	"io/ioutil"
 	"net"
 	"os"
 	"path/filepath"
@@ -337,7 +336,7 @@ func TestOpenSergo(t *testing.T) {
 			},
 			preFunc: func(t *testing.T) {
 				fileContent := `{"endpoint": "127.0.0.1:9090"}`
-				err := ioutil.WriteFile("test.json", []byte(fileContent), 0o644)
+				err := os.WriteFile("test.json", []byte(fileContent), 0o644)
 				if err != nil {
 					t.Fatalf("ioutil.WriteFile error:%s", err)
 				}