From fe988d246b998cf50d5348a8202297797faeee9c Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sun, 22 Oct 2023 12:59:57 +0800 Subject: [PATCH 1/2] feature(main): check endpoint /v2/ Signed-off-by: cuisongliu --- pkg/utils/http/url.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pkg/utils/http/url.go b/pkg/utils/http/url.go index e545529..d2dd674 100644 --- a/pkg/utils/http/url.go +++ b/pkg/utils/http/url.go @@ -19,6 +19,7 @@ package http import ( "context" "crypto/tls" + "errors" "github.com/labring/sreg/pkg/utils/logger" "io" "net" @@ -39,6 +40,7 @@ func WaitUntilEndpointAlive(ctx context.Context, endpoint string) error { if !strings.HasPrefix(endpoint, "http") { endpoint = "http://" + endpoint } + endpoint = endpoint + "/v2/" u, err := url.Parse(endpoint) if err != nil { return err @@ -52,6 +54,9 @@ func WaitUntilEndpointAlive(ctx context.Context, endpoint string) error { var resp *http.Response resp, err = DefaultClient.Get(u.String()) if err == nil { + if resp.StatusCode != http.StatusOK { + return errors.New("registry http status code not 200") + } _, _ = io.Copy(io.Discard, resp.Body) resp.Body.Close() logger.Debug("http endpoint %s is alive", u.String()) From a179813efc115fc459b2e9d2df8a2c88bab92b19 Mon Sep 17 00:00:00 2001 From: cuisongliu Date: Sun, 22 Oct 2023 14:20:19 +0800 Subject: [PATCH 2/2] feature(main): add check test Signed-off-by: cuisongliu --- pkg/utils/http/url_test.go | 51 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 pkg/utils/http/url_test.go diff --git a/pkg/utils/http/url_test.go b/pkg/utils/http/url_test.go new file mode 100644 index 0000000..4876691 --- /dev/null +++ b/pkg/utils/http/url_test.go @@ -0,0 +1,51 @@ +/* +Copyright 2023 cuisongliu@qq.com. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package http + +import ( + "context" + "github.com/labring/sreg/pkg/registry/sync" + "testing" +) + +func TestWaitUntilEndpointAlive(t *testing.T) { + type args struct { + ctx context.Context + endpoint string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "default", + args: args{ + ctx: context.Background(), + endpoint: sync.ParseRegistryAddress("localhost:5050"), + }, + wantErr: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := WaitUntilEndpointAlive(tt.args.ctx, tt.args.endpoint); (err != nil) != tt.wantErr { + t.Errorf("WaitUntilEndpointAlive() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}