Skip to content

Commit 466be94

Browse files
committed
feat: add e2e example tests for cli
Signed-off-by: peefy <xpf6677@163.com>
1 parent 93d5c5e commit 466be94

File tree

244 files changed

+7238
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

244 files changed

+7238
-3
lines changed
File renamed without changes.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
name: example-e2e-test
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
push:
7+
branches:
8+
- main
9+
jobs:
10+
example-e2e-test:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: actions/setup-go@v1
15+
with:
16+
go-version: "1.19"
17+
18+
- name: kcl Installation
19+
run: go install ./cmd/kcl
20+
21+
- name: Example tests
22+
shell: bash -ieo pipefail {0}
23+
run: PATH=$PATH:$HOME/go/bin ./examples/test.sh
File renamed without changes.

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ dist
2424
go.work
2525
# Go coverage test
2626
coverage.out
27+
.terraform
28+
.terraform.lock.hcl
2729

2830
build/
2931
.vscode/

examples/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Examples
2+
3+
This folder contains a collection of KCL examples:

examples/abstraction/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
docker:
2+
kcl main.k docker_compose_render.k
3+
4+
k8s:
5+
kcl main.k kubernetes_render.k
6+
7+
test:
8+
make docker
9+
make k8s

examples/abstraction/app.k

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
schema App:
2+
"""The application model."""
3+
name: str
4+
replicas: int = 1
5+
labels?: {str:str} = {app = name}
6+
service?: Service
7+
containers?: {str: Container}
8+
9+
schema Service:
10+
"""The service model."""
11+
$type?: str
12+
ports: [Port]
13+
14+
schema Port:
15+
"""The port model."""
16+
port: int
17+
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
18+
targetPort?: int | str
19+
20+
schema Container:
21+
"""The container model."""
22+
image: str
23+
command?: [str]
24+
args?: [str]
25+
env?: [Env]
26+
volumes?: [Volume]
27+
resources?: Resource
28+
ports: [ContainerPort]
29+
30+
schema ContainerPort:
31+
"""The container port model."""
32+
name?: str
33+
protocol: "TCP" | "UDP" | "SCTP" = "TCP"
34+
containerPort: int
35+
36+
check:
37+
1 <= containerPort <= 65535, "containerPort must be between 1 and 65535, inclusive"
38+
39+
schema Env:
40+
name: str
41+
value: str
42+
43+
schema Volume:
44+
source: str
45+
path: str
46+
target: str
47+
readOnly?: bool = False
48+
49+
schema Resource:
50+
limits?: {str:}
51+
requests?: {str:}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import manifests
2+
3+
import .app
4+
5+
# Convert the `App` model into the docker compose config.
6+
dockerComposeRender = lambda a: app.App {
7+
# NOTE: Only one container per workload can be defined for compose.
8+
# All other containers will be ignored by this tool.
9+
c: app.Container = [c for _, c in a.containers][0] if a.containers else {}
10+
# Construct the docker compose config.
11+
[{
12+
services = {
13+
"${a.name}" = {
14+
image = c.image
15+
entrypoint = c.command
16+
environment = c.env
17+
volumes = c.volumes
18+
ports = [{
19+
published = p.port
20+
target = p.targetPort or p.port
21+
protocol = p.protocol
22+
} for p in a.service?.ports]
23+
}
24+
}
25+
}]
26+
}
27+
28+
# Process multiple app instances and output them as docker config
29+
manifests.yaml_stream(sum([dockerComposeRender(a) for a in app.App.instances()], []))

examples/abstraction/kcl.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+

examples/abstraction/kcl.mod.lock

Whitespace-only changes.
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import manifests
2+
3+
import .app
4+
5+
# Convert the `App` model into Kubernetes Deployment and Service Manifests
6+
kubernetesRender = lambda a: app.App {
7+
# Construct the deployment manifest.
8+
deployment = {
9+
apiVersion = "apps/v1"
10+
kind = "Deployment"
11+
metadata.name = a.name
12+
metadata.labels = a.labels
13+
spec = {
14+
replicas = a.replicas
15+
selector.matchLabels = a.labels
16+
template.metadata.labels = a.labels
17+
template.spec.containers = [
18+
{
19+
name = name
20+
image = c.image
21+
command = c.command
22+
args = c.args
23+
env = c.env
24+
volumeMounts = c.volumes
25+
resources: c.resources
26+
ports = c.ports
27+
} for name, c in a.containers
28+
]
29+
}
30+
}
31+
# Construct the service manifest.
32+
service = {
33+
apiVersion = "v1"
34+
kind = "Service"
35+
metadata.name = a.name
36+
metadata.labels = a.labels
37+
spec = {
38+
type = a.service?.$type
39+
selector = a.labels
40+
ports = a.service?.ports
41+
}
42+
}
43+
# Returns Kubernetes manifests
44+
[deployment, if a.service: service]
45+
}
46+
47+
manifests.yaml_stream(sum([kubernetesRender(a) for a in app.App.instances()], []))

examples/abstraction/main.k

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import .app
2+
3+
app.App {
4+
name = "app"
5+
containers.ngnix = {
6+
image = "ngnix"
7+
ports = [{containerPort = 80}]
8+
}
9+
service.ports = [{ port = 80 }]
10+
}

examples/automation/Makefile

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
update:
2+
kcl main.k -O app.name='new_app'
3+
4+
delete:
5+
kcl main.k -O app.labels.key-
6+
7+
test:
8+
make update
9+
make delete

examples/automation/main.k

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
schema App:
2+
"""The application model."""
3+
name: str
4+
replicas: int
5+
labels?: {str:str} = {app = name}
6+
7+
app: App {
8+
name = "app"
9+
replicas = 1
10+
labels.key = "value"
11+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# This is a basic workflow to help you get started with Actions
2+
3+
name: CI
4+
5+
# Controls when the workflow will run
6+
on:
7+
# Triggers the workflow on push or pull request events but only for the main branch
8+
push:
9+
branches: [ main ]
10+
pull_request:
11+
branches: [ main ]
12+
13+
# Allows you to run this workflow manually from the Actions tab
14+
workflow_dispatch:
15+
16+
# A workflow run is made up of one or more jobs that can run sequentially or in parallel
17+
jobs:
18+
# This workflow contains a single job called "build"
19+
build:
20+
# The type of runner that the job will run on
21+
runs-on: ubuntu-latest
22+
23+
# Steps represent a sequence of tasks that will be executed as part of the job
24+
steps:
25+
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
26+
- uses: actions/checkout@v2
27+
28+
- name: Docker Login
29+
uses: docker/login-action@v1.10.0
30+
with:
31+
username: ${{ secrets.DOCKER_USERNAME }}
32+
password: ${{ secrets.DOCKER_PASSWORD }}
33+
logout: true
34+
35+
# Runs a set of commands using the runners shell
36+
- name: build image
37+
run: |
38+
make image
39+
docker tag flask_demo:latest ${{ secrets.DOCKER_USERNAME }}/flask_demo:${{ github.sha }}
40+
docker push ${{ secrets.DOCKER_USERNAME }}/flask_demo:${{ github.sha }}
41+
42+
- name: Trigger CI
43+
uses: InformaticsMatters/trigger-ci-action@1.0.1
44+
with:
45+
ci-owner: kcl-lang
46+
ci-repository: flask-demo-kcl-manifests
47+
ci-ref: refs/heads/main
48+
ci-user: peefy
49+
ci-user-token: ${{ secrets.DEPLOY_ACCESS_TOKEN }}
50+
ci-name: CI
51+
ci-inputs: >-
52+
image=${{ secrets.DOCKER_USERNAME }}/flask_demo
53+
sha-tag=${{ github.sha }}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
name: CI
2+
on:
3+
push:
4+
branches: [ main ]
5+
pull_request:
6+
branches: [ main ]
7+
workflow_dispatch:
8+
inputs:
9+
image:
10+
required: true
11+
description: 'docker image name'
12+
sha-tag:
13+
required: true
14+
description: 'docker image tag'
15+
jobs:
16+
update-manifests:
17+
runs-on: ubuntu-latest
18+
steps:
19+
- uses: actions/checkout@v2
20+
if: ${{ github.event.inputs.image }}
21+
- name: KCL config edit
22+
if: ${{ github.event.inputs.image }}
23+
run: |
24+
wget -q https://kcl-lang.io/script/install.sh -O - | /bin/bash
25+
/usr/local/kclvm/bin/kcl -d -O config.containers.flask_demo.image="${{ github.event.inputs.image }}:${{ github.event.inputs.sha-tag }}"
26+
- name: Git Commit/Push Changes
27+
uses: EndBug/add-and-commit@v9
28+
if: ${{ github.event.inputs.image }}
29+
with:
30+
default_author: github_actions
31+
message: "kcl code set image to ${{ github.event.inputs.image }}:${{ github.event.inputs.sha-tag }}"

examples/codelab/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
test:
2+
make -C simple
3+
make -C schema
4+
make -C collaborative
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pkg
2+
3+
server: pkg.Server {
4+
# Set the image with the value "nginx:1.14.2"
5+
image = "nginx:1.14.2"
6+
# Add a label app into labels
7+
labels.app = "test_app"
8+
# Add a mainContainer config, and its ports are [{protocol = "HTTP", port = 80, targetPort = 1100}]
9+
mainContainer.ports = [{
10+
protocol = "HTTP"
11+
port = 80
12+
targetPort = 1100
13+
}]
14+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
server:
2+
replicas: 1
3+
image: nginx:1.14.2-dev
4+
resource:
5+
cpu: 1
6+
memory: 1073741824
7+
disk: 10737418240
8+
mainContainer:
9+
name: main
10+
ports:
11+
- protocol: HTTP
12+
port: 80
13+
targetPort: 1100
14+
- protocol: TCP
15+
port: 443
16+
targetPort: 1100
17+
labels:
18+
app: test_app
19+
env: dev
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kcl_cli_configs:
2+
files:
3+
- ../base/base.k
4+
- main.k
5+
output: ./ci-test/stdout.golden.yaml
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import pkg
2+
3+
server: pkg.Server {
4+
# Override the base image.
5+
image = "nginx:1.14.2-dev"
6+
# Union a new label env into base labels.
7+
labels.env = "dev"
8+
# Append a port into base ports.
9+
mainContainer.ports += [{
10+
protocol = "TCP"
11+
port = 443
12+
targetPort = 1100
13+
}]
14+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
server:
2+
replicas: 1
3+
image: nginx:1.14.2
4+
resource:
5+
cpu: 1
6+
memory: 1073741824
7+
disk: 10737418240
8+
mainContainer:
9+
name: main
10+
ports:
11+
- protocol: HTTP
12+
port: 80
13+
targetPort: 1100
14+
labels:
15+
app: test_app
16+
env: prod
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
kcl_cli_configs:
2+
files:
3+
- ../base/base.k
4+
- main.k
5+
output: ./ci-test/stdout.golden.yaml
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pkg
2+
3+
server: pkg.Server {
4+
# Union a new label env into base labels
5+
labels.env = "prod"
6+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[package]
2+

examples/codelab/collaborative/kcl.mod.lock

Whitespace-only changes.

0 commit comments

Comments
 (0)