Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(main): check endpoint /v2/ #4

Merged
merged 2 commits into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
})
}
}
Loading