Skip to content

Commit

Permalink
feature(main): check endpoint /v2/ (#4)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
cuisongliu authored Oct 22, 2023
1 parent c621609 commit e87f993
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/utils/http/url.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package http
import (
"context"
"crypto/tls"
"errors"
"github.com/labring/sreg/pkg/utils/logger"
"io"
"net"
Expand All @@ -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
Expand All @@ -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())
Expand Down
51 changes: 51 additions & 0 deletions pkg/utils/http/url_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
Copyright 2023 [email protected].
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)
}
})
}
}

0 comments on commit e87f993

Please sign in to comment.