开发与测试
开发与测试
本指南面向希望在本地使用 Polaris Helm chart 的开发者,内容包括搭建本地 Kubernetes 集群、从源码构建以及运行测试。
使用 Minikube 进行本地开发
前置条件
启动 Minikube
启动本地 Minikube 集群:
minikube start从官方仓库安装
添加官方 Polaris Helm 仓库并安装该 chart:
helm repo add polaris https://downloads.apache.org/polaris/helm-chart
helm repo update
kubectl create namespace polaris
helm install polaris polaris/polaris --namespace polaris验证安装:
helm test polaris --namespace polaris从源码构建与安装
本节假定你已克隆 Polaris Git 仓库,并已配置好构建该项目所需的前置条件。详情请参阅安装依赖指南。
构建容器镜像
启动 Minikube,并将 Docker 配置为使用 Minikube 的 Docker 守护进程:
minikube start
eval $(minikube docker-env)构建容器镜像。Polaris 服务镜像是必需的,管理工具镜像是可选的(在需要时可用于初始化 realm,见下文):
./gradlew \
:polaris-server:assemble \
:polaris-server:quarkusAppPartsBuild --rerun \
:polaris-admin:assemble \
:polaris-admin:quarkusAppPartsBuild --rerun \
-Dquarkus.container-image.build=true或者,你可以使用 Make 启动 Minikube 并构建镜像:
make minikube-start-cluster
make build
make minikube-load-images::: tip 提示
不要在已经执行过 $(minikube docker-env) 的 shell 会话中使用 minikube-load-images 目标,否则该目标将无法把镜像加载到 Minikube 中。
:::
创建命名空间与测试数据
创建命名空间并部署测试数据:
kubectl create namespace polaris
kubectl apply --namespace polaris -f helm/polaris/ci/fixtures/这些 Fixture 会部署一个 PostgreSQL 实例和一个 MongoDB 实例,可用于测试。如果你计划使用持久化后端进行测试,请等待数据库 Pod 就绪:
kubectl wait --namespace polaris --for=condition=ready pod --selector=app.kubernetes.io/name=postgres --timeout=120s
kubectl wait --namespace polaris --for=condition=ready pod --selector=app.kubernetes.io/name=mongodb --timeout=120s或者,你可以使用 Make 来部署测试夹具:
make helm-fixtures⚠️ 警告
helm/polaris/ci/fixtures/ 中的测试数据仅供测试使用,不适用于生产环境。尤其是其中的 PostgreSQL 和 MongoDB 实例未配置任何加密或安全措施,绝不能原样部署到生产环境中!
手动安装 Chart
从本地源安装 Chart,可使用 values.yaml 文件,也可使用 helm/polaris/ci/ 目录下的任意 values 文件。示例:
# Non-persistent backend
helm upgrade --install --namespace polaris polaris helm/polaris
# Persistent backend
helm upgrade --install --namespace polaris \
--values helm/polaris/ci/persistence-values.yaml \
polaris helm/polaris为开发环境初始化 Realm
在使用持久化后端进行临时测试时,你可能需要使用管理工具来初始化所有 realm。有关更多信息,请参阅管理工具指南。
以 PostgreSQL 后端为例:
kubectl run polaris-bootstrap \
-n polaris \
--image=apache/polaris-admin-tool:latest \
--restart=Never \
--rm -it \
--env="polaris.persistence.type=relational-jdbc" \
--env="quarkus.datasource.username=$(kubectl get secret polaris-persistence -n polaris -o jsonpath='{.data.username}' | base64 --decode)" \
--env="quarkus.datasource.password=$(kubectl get secret polaris-persistence -n polaris -o jsonpath='{.data.password}' | base64 --decode)" \
--env="quarkus.datasource.jdbc.url=$(kubectl get secret polaris-persistence -n polaris -o jsonpath='{.data.jdbcUrl}' | base64 --decode)" \
-- \
bootstrap -r POLARIS -c POLARIS,root,s3cr3tNoSQL(MongoDB)后端示例:
kubectl run polaris-bootstrap \
-n polaris \
--image=apache/polaris-admin-tool:latest \
--restart=Never \
--rm -it \
--env="polaris.persistence.type=nosql" \
--env="polaris.persistence.nosql.backend=MongoDb" \
--env="quarkus.mongodb.database=polaris" \
--env="quarkus.mongodb.connection-string=$(kubectl get secret polaris-nosql-persistence -n polaris -o jsonpath='{.data.connectionString}' | base64 --decode)" \
-- \
bootstrap -r POLARIS -c POLARIS,root,s3cr3t以上两条命令都会初始化一个名为 POLARIS 的 realm,其根密码为:s3cr3t。
📝 注意
Helm 集成测试(见下文)不需要初始化 realm,因为它们(目前)并不调用 Polaris REST API。
运行 Chart 测试
前置条件
安装所需的工具:
brew install chart-testing
brew install yamllint
make helm-install-plugins以下工具将会被安装:
单元测试
Helm 单元测试不需要 Kubernetes 集群。请在 Polaris 仓库根目录下运行它们:
make helm-unittestLint 检查
使用 Chart Testing 工具对 chart 进行 lint 检查:
make helm-lint修改文档或 Schema
如果你修改了 Helm chart 的 values.yaml 文件,就需要重新生成文档和 schema。请在 Polaris 仓库根目录下运行:
make helm-schema-generate helm-doc-generate或者,你可以运行:
make helm这将运行所有与 Helm 相关的目标,包括单元测试、代码检查、Schema 生成和文档生成(但不会运行集成测试)。
集成测试
集成测试是使用 ct install 工具执行的测试。它们需要一个已部署测试夹具的 Kubernetes 集群,具体说明见上文的手动安装 Chart部分。
运行集成测试最简单的方式是使用 Make:
make helm-integration-test上述命令会构建镜像并将其加载到 Minikube 中,部署 fixtures,并运行所有 ct install 测试。
清理
卸载 chart,移除资源并删除命名空间:
helm uninstall --namespace polaris polaris
kubectl delete namespace polaris --wait=true --ignore-not-found如果需要,停止 Minikube:
minikube stop另外,你也可以使用 Make 来执行清理:
make helm-fixtures-cleanup
make minikube-stop-cluster
make minikube-cleanup # will delete the Minikube cluster评论
登录后参与评论
KnowForge