Skip to content

Commit 18c4704

Browse files
committed
add e2e-test
1 parent c1cf66a commit 18c4704

File tree

7 files changed

+137
-17
lines changed

7 files changed

+137
-17
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ kind-init:
7070
--image=kindest/node:v1.31.2@sha256:18fbefc20a7113353c7b75b5c869d7145a6abd6269154825872dc59c1329912e \
7171
--name kind-ydb-operator; \
7272
docker pull k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.0; \
73-
kind load docker-image k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.0 --nodes kind-worker,kind-worker2,kind-worker3 --name kind-ydb-operator; \
74-
YDB_IMAGE=$(grep "anchor_for_fetching_image_from_workflow" ./tests/**/*.go | grep -o -E '"cr\.yandex.*"'); \
75-
YDB_IMAGE=${YDB_IMAGE:1:-1}; \
76-
docker pull ${YDB_IMAGE}; \
77-
kind load docker-image ${YDB_IMAGE} --nodes kind-worker,kind-worker2,kind-worker3 --name kind-ydb-operator;
73+
kind load docker-image k8s.gcr.io/ingress-nginx/kube-webhook-certgen:v1.0 --nodes kind-worker,kind-worker2,kind-worker3 --name kind-ydb-operator; \
74+
YDB_IMAGE=$(grep "anchor_for_fetching_image_from_workflow" ./tests/**/*.go | grep -o -E '"cr\.yandex.*"'); \
75+
YDB_IMAGE=${YDB_IMAGE:1:-1}; \
76+
docker pull ${YDB_IMAGE}; \
77+
kind load docker-image ${YDB_IMAGE} --nodes kind-worker,kind-worker2,kind-worker3 --name kind-ydb-operator;
7878

7979
kind-load:
8080
docker tag cr.yandex/yc/ydb-operator:latest kind/ydb-operator:current

internal/controllers/database/controller_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ var _ = Describe("Database controller medium tests", func() {
310310

311311
It("Check externalHost and externalPort GRPC Service field propagation", func() {
312312
By("Create test database")
313-
publicHost := fmt.Sprintf(v1alpha1.InterconnectServiceFQDNFormat, testobjects.DatabaseName, testobjects.YdbNamespace, v1alpha1.DefaultDomainName)
314313
databaseSample = *testobjects.DefaultDatabase()
315314
Expect(k8sClient.Create(ctx, &databaseSample)).Should(Succeed())
316315

@@ -350,14 +349,14 @@ var _ = Describe("Database controller medium tests", func() {
350349
}
351350

352351
By("Check that args `--grpc-public-host` and `--grpc-public-port` propagated to StatefulSet pods...")
352+
publicHost := fmt.Sprintf(v1alpha1.InterconnectServiceFQDNFormat, testobjects.DatabaseName, testobjects.YdbNamespace, v1alpha1.DefaultDomainName)
353353
Eventually(
354354
checkPublicArgs(fmt.Sprintf("%s.%s", "$(NODE_NAME)", publicHost), fmt.Sprintf("%d", v1alpha1.GRPCPort)),
355355
test.Timeout,
356356
test.Interval).ShouldNot(HaveOccurred())
357357

358358
externalHost := fmt.Sprintf("%s.%s.%s", testobjects.DatabaseName, testobjects.YdbNamespace, "example.com")
359359
externalPort := int32(31001)
360-
361360
By("Update externalHost and externalPort for Database GRPC Service...", func() {
362361
database := v1alpha1.Database{}
363362
Expect(k8sClient.Get(ctx, types.NamespacedName{

internal/resources/database.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55

66
corev1 "k8s.io/api/core/v1"
7+
"k8s.io/apimachinery/pkg/util/intstr"
78
"k8s.io/client-go/rest"
89

910
api "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
@@ -152,9 +153,10 @@ func (b *DatabaseBuilder) GetResourceBuilders(restConfig *rest.Config) []Resourc
152153
SelectorLabels: databaseLabels,
153154
Annotations: b.Spec.Service.GRPC.AdditionalAnnotations,
154155
Ports: []corev1.ServicePort{{
155-
Name: api.GRPCServicePortName,
156-
Port: api.GRPCPort,
157-
NodePort: b.Spec.Service.GRPC.ExternalPort,
156+
Name: api.GRPCServicePortName,
157+
Port: api.GRPCPort,
158+
TargetPort: intstr.IntOrString{Type: intstr.String, StrVal: "grpc"},
159+
NodePort: b.Spec.Service.GRPC.ExternalPort,
158160
}},
159161
IPFamilies: b.Spec.Service.GRPC.IPFamilies,
160162
IPFamilyPolicy: b.Spec.Service.GRPC.IPFamilyPolicy,

internal/resources/database_statefulset.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,8 +427,15 @@ func (b *DatabaseStatefulSetBuilder) buildContainer() corev1.Container {
427427
}
428428
}
429429

430+
var grpcPublicPort int32
431+
if b.Spec.Service.GRPC.ExternalPort != 0 {
432+
grpcPublicPort = b.Spec.Service.GRPC.ExternalPort
433+
} else {
434+
grpcPublicPort = api.GRPCPort
435+
}
436+
430437
ports := []corev1.ContainerPort{{
431-
Name: "grpc", ContainerPort: api.GRPCPort,
438+
Name: "grpc", ContainerPort: grpcPublicPort,
432439
}, {
433440
Name: "interconnect", ContainerPort: api.InterconnectPort,
434441
}, {

tests/cfg/kind-cluster-config.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,31 @@ nodes:
44
- role: control-plane
55

66
- role: worker
7+
extraPortMappings:
8+
- containerPort: 30001
9+
hostPort: 2135
10+
listenAddress: "127.0.0.1"
11+
protocol: tcp
712
labels:
813
topology.kubernetes.io/zone: az-1
914
worker: true
1015

1116
- role: worker
17+
extraPortMappings:
18+
- containerPort: 30001
19+
hostPort: 2135
20+
listenAddress: "127.0.0.1"
21+
protocol: tcp
1222
labels:
1323
topology.kubernetes.io/zone: az-2
1424
worker: true
1525

1626
- role: worker
27+
extraPortMappings:
28+
- containerPort: 30001
29+
hostPort: 2135
30+
listenAddress: "127.0.0.1"
31+
protocol: tcp
1732
labels:
1833
topology.kubernetes.io/zone: az-3
1934
worker: true

tests/e2e/smoke_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,34 @@ var _ = Describe("Operator smoke test", func() {
733733
ExecuteSimpleTableE2ETest(podName, testobjects.YdbNamespace, storageEndpoint, databasePath)
734734
})
735735

736+
It("Check externalPort for Database", func() {
737+
By("create storage...")
738+
Expect(k8sClient.Create(ctx, storageSample)).Should(Succeed())
739+
defer DeleteStorageSafely(ctx, k8sClient, storageSample)
740+
By("create database...")
741+
databaseSample.Spec.Service.GRPC.ExternalPort = 30001
742+
Expect(k8sClient.Create(ctx, databaseSample)).Should(Succeed())
743+
defer func() {
744+
Expect(k8sClient.Delete(ctx, databaseSample)).Should(Succeed())
745+
}()
746+
747+
By("waiting until Storage is ready...")
748+
WaitUntilStorageReady(ctx, k8sClient, storageSample.Name, testobjects.YdbNamespace)
749+
750+
By("checking that all the storage pods are running and ready...")
751+
CheckPodsRunningAndReady(ctx, k8sClient, "ydb-cluster", "kind-storage", storageSample.Spec.Nodes)
752+
753+
By("waiting until database is ready...")
754+
WaitUntilDatabaseReady(ctx, k8sClient, databaseSample.Name, testobjects.YdbNamespace)
755+
756+
By("checking that all the database pods are running and ready...")
757+
CheckPodsRunningAndReady(ctx, k8sClient, "ydb-cluster", "kind-database", databaseSample.Spec.Nodes)
758+
759+
By("execute simple query with ydb-go-sdk...")
760+
databasePath := DatabasePathWithDefaultDomain(databaseSample)
761+
ExecuteSimpleTableE2ETestWithSDK(databasePath)
762+
})
763+
736764
AfterEach(func() {
737765
UninstallOperatorWithHelm(testobjects.YdbNamespace)
738766
Expect(k8sClient.Delete(ctx, &namespace)).Should(Succeed())

tests/test-utils/test-utils.go

Lines changed: 75 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package testutils
33
import (
44
"bufio"
55
"context"
6+
"database/sql"
67
"fmt"
78
"io"
89
"os"
@@ -23,6 +24,8 @@ import (
2324
. "github.com/onsi/ginkgo/v2"
2425
. "github.com/onsi/gomega"
2526

27+
ydb "github.com/ydb-platform/ydb-go-sdk/v3"
28+
"github.com/ydb-platform/ydb-go-sdk/v3/retry"
2629
v1alpha1 "github.com/ydb-platform/ydb-kubernetes-operator/api/v1alpha1"
2730
. "github.com/ydb-platform/ydb-kubernetes-operator/internal/controllers/constants"
2831
testobjects "github.com/ydb-platform/ydb-kubernetes-operator/tests/test-k8s-objects"
@@ -34,11 +37,17 @@ const (
3437
Interval = time.Second * 2
3538
YdbOperatorRemoteChart = "ydb/ydb-operator"
3639
YdbOperatorReleaseName = "ydb-operator"
40+
TestTablePath = "testfolder/testtable"
3741
)
3842

3943
var (
4044
pathToHelmValuesInLocalInstall = filepath.Join("..", "cfg", "operator-local-values.yaml")
4145
pathToHelmValuesInRemoteInstall = filepath.Join("..", "cfg", "operator-values.yaml")
46+
47+
createTableQuery = fmt.Sprintf("CREATE TABLE `%s` (testColumnA Utf8, testColumnB Utf8, PRIMARY KEY (testColumnA));", TestTablePath)
48+
insertQuery = fmt.Sprintf("INSERT INTO `%s` (testColumnA, testColumnB) VALUES ('valueA', 'valueB');", TestTablePath)
49+
selectQuery = fmt.Sprintf("SELECT testColumnA, testColumnB FROM `%s`;", TestTablePath)
50+
dropTableQuery = fmt.Sprintf("DROP TABLE `%s`;", TestTablePath)
4251
)
4352

4453
func InstallLocalOperatorWithHelm(namespace string) {
@@ -203,8 +212,6 @@ func BringYdbCliToPod(podName, podNamespace string) {
203212
}
204213

205214
func ExecuteSimpleTableE2ETest(podName, podNamespace, storageEndpoint string, databasePath string) {
206-
tablePath := "testfolder/testtable"
207-
208215
tableCreatingInterval := time.Second * 10
209216

210217
Eventually(func(g Gomega) {
@@ -217,7 +224,7 @@ func ExecuteSimpleTableE2ETest(podName, podNamespace, storageEndpoint string, da
217224
"-e", storageEndpoint,
218225
"yql",
219226
"-s",
220-
fmt.Sprintf("CREATE TABLE `%s` (testColumnA Utf8, testColumnB Utf8, PRIMARY KEY (testColumnA));", tablePath),
227+
createTableQuery,
221228
}
222229
output, _ := exec.Command("kubectl", args...).CombinedOutput()
223230
fmt.Println(string(output))
@@ -232,7 +239,7 @@ func ExecuteSimpleTableE2ETest(podName, podNamespace, storageEndpoint string, da
232239
"-e", storageEndpoint,
233240
"yql",
234241
"-s",
235-
fmt.Sprintf("INSERT INTO `%s` (testColumnA, testColumnB) VALUES ('valueA', 'valueB');", tablePath),
242+
insertQuery,
236243
}
237244
output, err := exec.Command("kubectl", argsInsert...).CombinedOutput()
238245
Expect(err).ShouldNot(HaveOccurred(), string(output))
@@ -247,7 +254,7 @@ func ExecuteSimpleTableE2ETest(podName, podNamespace, storageEndpoint string, da
247254
"yql",
248255
"--format", "csv",
249256
"-s",
250-
fmt.Sprintf("SELECT * FROM `%s`;", tablePath),
257+
selectQuery,
251258
}
252259
output, err = exec.Command("kubectl", argsSelect...).CombinedOutput()
253260
Expect(err).ShouldNot(HaveOccurred(), string(output))
@@ -262,12 +269,74 @@ func ExecuteSimpleTableE2ETest(podName, podNamespace, storageEndpoint string, da
262269
"-e", storageEndpoint,
263270
"yql",
264271
"-s",
265-
fmt.Sprintf("DROP TABLE `%s`;", tablePath),
272+
dropTableQuery,
266273
}
267274
output, err = exec.Command("kubectl", argsDrop...).CombinedOutput()
268275
Expect(err).ShouldNot(HaveOccurred(), string(output))
269276
}
270277

278+
func ExecuteSimpleTableE2ETestWithSDK(databasePath string) {
279+
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
280+
defer cancel()
281+
282+
cc, err := ydb.Open(ctx, fmt.Sprintf("grpc://localhost:2135/%s", databasePath))
283+
Expect(err).ShouldNot(HaveOccurred(), string(err.Error()))
284+
defer func() { _ = cc.Close(ctx) }()
285+
286+
c, err := ydb.Connector(cc,
287+
ydb.WithAutoDeclare(),
288+
ydb.WithTablePathPrefix(TestTablePath),
289+
)
290+
Expect(err).ShouldNot(HaveOccurred())
291+
defer func() { _ = c.Close() }()
292+
293+
db := sql.OpenDB(c)
294+
defer func() { _ = db.Close() }()
295+
296+
err = retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) error {
297+
_, err = cc.ExecContext(ydb.WithQueryMode(ctx, ydb.SchemeQueryMode), createTableQuery)
298+
if err != nil {
299+
return err
300+
}
301+
return nil
302+
}, retry.WithIdempotent(true))
303+
Expect(err).ShouldNot(HaveOccurred())
304+
305+
err = retry.DoTx(ctx, db, func(ctx context.Context, tx *sql.Tx) error {
306+
if _, err = tx.ExecContext(ctx, insertQuery); err != nil {
307+
return err
308+
}
309+
return nil
310+
}, retry.WithIdempotent(true))
311+
Expect(err).ShouldNot(HaveOccurred())
312+
313+
var (
314+
testColumnA string
315+
testColumnB string
316+
)
317+
err = retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) (err error) {
318+
row := cc.QueryRowContext(ctx, selectQuery)
319+
if err = row.Scan(&testColumnA, &testColumnB); err != nil {
320+
return err
321+
}
322+
323+
return nil
324+
}, retry.WithIdempotent(true))
325+
Expect(err).ShouldNot(HaveOccurred())
326+
Expect(testColumnA).To(BeEquivalentTo("valueA"))
327+
Expect(testColumnB).To(BeEquivalentTo("valueB"))
328+
329+
err = retry.Do(ctx, db, func(ctx context.Context, cc *sql.Conn) error {
330+
_, err = cc.ExecContext(ydb.WithQueryMode(ctx, ydb.SchemeQueryMode), dropTableQuery)
331+
if err != nil {
332+
return err
333+
}
334+
335+
return nil
336+
}, retry.WithIdempotent(true))
337+
Expect(err).ShouldNot(HaveOccurred())
338+
}
339+
271340
func PortForward(
272341
ctx context.Context,
273342
svcName, svcNamespace, serverName string,

0 commit comments

Comments
 (0)