fix: service command not honoring --wait arg

The minikube service <service> CLI command has a --wait argument documented that enabled the user to wait the given seconds for a service to be available in the cluster, but this was being ignored.

The logic for waiting was already implemented, but was being overriden by an unneeded early return. I removed that, and added a test case that verifies this feature is working as intended, since it was untested.
pull/15735/head
Elizabeth Martin Campos 2023-01-29 13:30:15 +01:00
parent 78e258a73c
commit 3f9c7f3eb4
2 changed files with 41 additions and 7 deletions

View File

@ -261,11 +261,6 @@ func WaitForService(api libmachine.API, cname string, namespace string, service
interval = 1
}
err := CheckService(cname, namespace, service)
if err != nil {
return nil, &SVCNotFoundError{err}
}
chkSVC := func() error { return CheckService(cname, namespace, service) }
if err := retry.Expo(chkSVC, time.Duration(interval)*time.Second, time.Duration(wait)*time.Second); err != nil {

View File

@ -25,6 +25,7 @@ import (
"strings"
"testing"
"text/template"
"time"
"github.com/docker/machine/libmachine"
"github.com/docker/machine/libmachine/host"
@ -261,7 +262,12 @@ func (s MockServiceInterface) Get(ctx context.Context, name string, _ meta.GetOp
}
}
return nil, nil
return nil, errors.New("Service not found")
}
func (s MockServiceInterface) Create(ctx context.Context, service *core.Service, _ meta.CreateOptions) (*core.Service, error) {
s.ServiceList.Items = append(s.ServiceList.Items, *service)
return service, nil
}
func TestGetServiceListFromServicesByLabel(t *testing.T) {
@ -896,6 +902,14 @@ func TestWaitAndMaybeOpenService(t *testing.T) {
expected: []string{},
err: true,
},
{
description: "correctly return serviceURLs for a delayed service",
namespace: "default",
service: "mock-dashboard-delayed",
api: defaultAPI,
https: true,
expected: []string{"http://127.0.0.1:1111"},
},
}
defer revertK8sClient(K8s)
for _, test := range tests {
@ -905,8 +919,33 @@ func TestWaitAndMaybeOpenService(t *testing.T) {
endpointsMap: endpointNamespaces,
}
go func() {
// wait for the delayed mock service to be created
time.Sleep(2 * time.Second)
_, _ = serviceNamespaces[test.namespace].Create(context.Background(), &core.Service{
ObjectMeta: meta.ObjectMeta{
Name: "mock-dashboard-delayed",
Namespace: "default",
Labels: map[string]string{"mock": "mock"},
},
Spec: core.ServiceSpec{
Ports: []core.ServicePort{
{
Name: "port1",
NodePort: int32(1111),
Port: int32(11111),
TargetPort: intstr.IntOrString{
IntVal: int32(11111),
},
},
},
},
}, meta.CreateOptions{})
}()
var urlList []string
urlList, err := WaitForService(test.api, "minikube", test.namespace, test.service, defaultTemplate, test.urlMode, test.https, 1, 0)
urlList, err := WaitForService(test.api, "minikube", test.namespace, test.service, defaultTemplate, test.urlMode, test.https, 5, 0)
if test.err && err == nil {
t.Fatalf("WaitForService expected to fail for test: %v", test)
}