This repository has been archived by the owner on Nov 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.go
67 lines (59 loc) · 2.23 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// dbaas-controller
// Copyright (C) 2020 Percona LLC
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
package tests
import (
"context"
"fmt"
"testing"
"time"
controllerv1beta1 "github.com/percona-platform/dbaas-api/gen/controller"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
// AssertGRPCErrorRE checks that actual error has expected gRPC error code, and error messages
// matches expected regular expression.
func AssertGRPCErrorRE(tb testing.TB, expectedCode codes.Code, expectedMessageRE string, actual error) {
tb.Helper()
s, ok := status.FromError(actual)
if !assert.True(tb, ok, "expected gRPC Status, got %T:\n%s", actual, actual) {
return
}
assert.Equal(tb, int(expectedCode), int(s.Code()), "gRPC status codes are not equal") // int() to log in decimal, not hex
assert.Regexp(tb, expectedMessageRE, s.Message(), "gRPC status message does not match")
}
func WaitForClusterState(ctx context.Context, kubeconfig string, name string, state controllerv1beta1.DBClusterState) error {
for {
clusters, err := PXCClusterAPIClient.ListPXCClusters(Context, &controllerv1beta1.ListPXCClustersRequest{
KubeAuth: &controllerv1beta1.KubeAuth{
Kubeconfig: kubeconfig,
},
})
if err != nil {
return errors.Wrap(err, "cannot get clusters list")
}
if len(clusters.Clusters) > 0 && clusters.Clusters[0].State == state && clusters.Clusters[0].Name == name {
return nil
}
select {
case <-ctx.Done():
return fmt.Errorf("timeout waiting for the cluster to be ready")
case <-time.After(100 * time.Millisecond):
continue
}
}
}