Skip to content

Commit e87f993

Browse files
authored
feature(main): check endpoint /v2/ (#4)
* feature(main): check endpoint /v2/ Signed-off-by: cuisongliu <[email protected]> * feature(main): add check test Signed-off-by: cuisongliu <[email protected]> --------- Signed-off-by: cuisongliu <[email protected]>
1 parent c621609 commit e87f993

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

pkg/utils/http/url.go

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package http
1919
import (
2020
"context"
2121
"crypto/tls"
22+
"errors"
2223
"github.com/labring/sreg/pkg/utils/logger"
2324
"io"
2425
"net"
@@ -39,6 +40,7 @@ func WaitUntilEndpointAlive(ctx context.Context, endpoint string) error {
3940
if !strings.HasPrefix(endpoint, "http") {
4041
endpoint = "http://" + endpoint
4142
}
43+
endpoint = endpoint + "/v2/"
4244
u, err := url.Parse(endpoint)
4345
if err != nil {
4446
return err
@@ -52,6 +54,9 @@ func WaitUntilEndpointAlive(ctx context.Context, endpoint string) error {
5254
var resp *http.Response
5355
resp, err = DefaultClient.Get(u.String())
5456
if err == nil {
57+
if resp.StatusCode != http.StatusOK {
58+
return errors.New("registry http status code not 200")
59+
}
5560
_, _ = io.Copy(io.Discard, resp.Body)
5661
resp.Body.Close()
5762
logger.Debug("http endpoint %s is alive", u.String())

pkg/utils/http/url_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
Copyright 2023 [email protected].
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package http
18+
19+
import (
20+
"context"
21+
"github.com/labring/sreg/pkg/registry/sync"
22+
"testing"
23+
)
24+
25+
func TestWaitUntilEndpointAlive(t *testing.T) {
26+
type args struct {
27+
ctx context.Context
28+
endpoint string
29+
}
30+
tests := []struct {
31+
name string
32+
args args
33+
wantErr bool
34+
}{
35+
{
36+
name: "default",
37+
args: args{
38+
ctx: context.Background(),
39+
endpoint: sync.ParseRegistryAddress("localhost:5050"),
40+
},
41+
wantErr: false,
42+
},
43+
}
44+
for _, tt := range tests {
45+
t.Run(tt.name, func(t *testing.T) {
46+
if err := WaitUntilEndpointAlive(tt.args.ctx, tt.args.endpoint); (err != nil) != tt.wantErr {
47+
t.Errorf("WaitUntilEndpointAlive() error = %v, wantErr %v", err, tt.wantErr)
48+
}
49+
})
50+
}
51+
}

0 commit comments

Comments
 (0)