1 Star 0 Fork 0

coolops/helmx

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
helmx_test.go 12.45 KB
一键复制 编辑 原始数据 按行查看 历史
pippo 提交于 2022-08-27 17:24 . add ingress tls
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
package helmx
import (
"bytes"
"fmt"
"os"
"strings"
"testing"
"github.com/go-courier/helmx/spec"
"github.com/onsi/gomega"
)
func init() {
os.Setenv(spec.EnvKeyImagePullSecret, "qcloud-registry://username:password@docker.io/pf-")
}
func Test(t *testing.T) {
hx := NewHelmX()
hx.AddTemplate("pullSecret", pullSecret)
hx.AddTemplate("serviceAccount", serviceAccount)
hx.AddTemplate("ingress", ingress)
hx.AddTemplate("service", service)
hx.AddTemplate("deployment", deployment)
hx.AddTemplate("job", job)
hx.AddTemplate("cronJob", cronJob)
if err := hx.FromYAML([]byte(
`
project:
name: helmx
feature: test
group: helmx
version: 0.0.0
description: helmx
service:
hostNetwork: true
imagePullSecret: qcloud-registry://username:password@docker.io/pf-
hostAliases:
- "127.0.0.1:test1.com,test2.com"
- "127.0.0.2:test2.com,test3.com"
mounts:
- "data:/usr/share/nginx:ro"
ports:
- "80:80"
- "!20000:80"
livenessProbe:
action: "http://:80"
lifecycle:
preStop: "nginx -s quit"
ingresses:
- "http://helmx:80/helmx"
tls:
- "secretName"
- "secretNameWithHost:helmx"
serviceAccountName: test
serviceAccountRoleRules:
- secrets#get,update
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
topologySpreadConstraints:
- maxSkew: 1
topologyKey: zone
whenUnsatisfiable: DoNotSchedule
labelSelector:
matchLabels:
foo: bar
affinity:
nodeAffinity:
requiredDuringSchedulingIgnoredDuringExecution:
nodeSelectorTerms:
- matchExpressions:
- key: zone
operator: NotIn
values:
- zoneC
initials:
- image: dockercloud/hello-world
mounts:
- "data:/usr/share/nginx"
command:
- mv
args:
- /www
- /usr/share/nginx/html
jobs:
doonce:
image: busybox
backoffLimit: 4
dosomecron:
image: busybox
cron:
schedule: "*/1 * * * *"
envs:
env: "test"
valueWithDot: "value.with.dot"
secretFalse: "####secretName.secretKey.false####"
secretTrue: "####secretName.secretKey.true####"
configMap: "####configMapName.configMapKey####"
resources:
cpu: 10/20m
memory: 0/20Mi
nvidia.com/gpu: 0/20
tolerations:
- env=test
- project
volumes:
data:
emptyDir:
medium: Memory
sizeLimit: "1Gi"
upstreams:
- redis
- mysql
labels:
testKey1: testValue1
testKey2: testValue2
`)); err != nil {
t.Fatal(err)
}
if err := hx.ExecuteAll(os.Stdout, &hx.Spec); err != nil {
panic(err)
}
}
func TestTemplates(t *testing.T) {
baseProject := `
project:
name: helmx
feature: test
group: helmx
version: 0.0.0
description: helmx
`
t.Run("service", func(t *testing.T) {
check(t, baseProject+`
service:
ports:
- "80:80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
type: ClusterIP
ports:
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("headlessService", func(t *testing.T) {
check(t, baseProject+`
service:
headless: true
ports:
- "80:80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
clusterIP: None
type: ClusterIP
ports:
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("service with nodePort", func(t *testing.T) {
check(t, baseProject+`
service:
ports:
- "!20000:80"
- "!80"
- "!25000:25000"
- "!40000:80"
- "80:8080"
- "80"
`,
service,
`
---
apiVersion: v1
kind: Service
metadata:
name: helmx--test
annotations:
helmx/project: >-
{"name":"helmx","feature":"test","version":"0.0.0","group":"helmx","description":"helmx"}
helmx/upstreams: ""
spec:
selector:
srv: helmx--test
type: NodePort
ports:
- name: np-http-20000
nodePort: 20000
port: 80
targetPort: 80
protocol: TCP
- name: np-http-80
port: 80
targetPort: 80
protocol: TCP
- name: np-http-25000
nodePort: 25000
port: 25000
targetPort: 25000
protocol: TCP
- name: np-http-40000
nodePort: 40000
port: 80
targetPort: 80
protocol: TCP
- name: http-80
port: 80
targetPort: 8080
protocol: TCP
- name: http-80
port: 80
targetPort: 80
protocol: TCP
`,
)
})
t.Run("ingress with tls", func(t *testing.T) {
check(t, baseProject+`
service:
ingresses:
- "http://helmx:80/helmx"
tls:
- "secretName:helmx"
`,
ingress,
`
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: helmx--test
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
rules:
- host: helmx
http:
paths:
- path: /helmx
backend:
serviceName: helmx--test
servicePort: 80
tls:
- host:
- helmx
secretName: secretName
`,
)
})
t.Run("deployment", func(t *testing.T) {
check(t, baseProject+`
service:
hostNetwork: true
hosts:
- "127.0.0.1:test1.com,test2.com"
- "127.0.0.2:test3.com,test4.com"
ports:
- "80:80"
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
envs:
env: "test"
valueWithDot: "value.with.dot"
secretFalse: "####secretName.secretKey.false####"
secretTrue: "####secretName.secretKey.true####"
configMap: "####configMapName.configMapKey####"
`,
deployment,
`
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: helmx--test
labels:
app: helmx--test
version: 0.0.0
annotations:
helmx: "{\"project\":{\"name\":\"helmx\",\"feature\":\"test\",\"version\":\"0.0.0\",\"group\":\"helmx\",\"description\":\"helmx\"},\"service\":{\"securityContext\":{\"runAsUser\":1024,\"runAsGroup\":1000,\"runAsNonRoot\":true,\"readOnlyRootFilesystem\":true,\"privileged\":true},\"hostNetwork\":true,\"hosts\":[\"127.0.0.1:test1.com,test2.com\",\"127.0.0.2:test3.com,test4.com\"],\"ports\":[\"80\"]},\"envs\":{\"configMap\":\"####configMapName.configMapKey####\",\"env\":\"test\",\"secretFalse\":\"####secretName.secretKey.false####\",\"secretTrue\":\"####secretName.secretKey.true####\",\"valueWithDot\":\"value.with.dot\"}}"
spec:
selector:
matchLabels:
srv: helmx--test
template:
metadata:
labels:
srv: helmx--test
spec:
containers:
- name: helmx--test
securityContext:
runAsUser: 1024
runAsGroup: 1000
runAsNonRoot: true
readOnlyRootFilesystem: true
privileged: true
image: docker.io/pf-helmx/helmx:0.0.0
ports:
- containerPort: 80
protocol: TCP
env:
- name: configMap
valueFrom:
configMapKeyRef:
key: configMapKey
name: configMapName
- name: env
value: test
- name: secretFalse
valueFrom:
secretKeyRef:
key: secretKey
name: secretName
optional: false
- name: secretTrue
valueFrom:
secretKeyRef:
key: secretKey
name: secretName
optional: true
- name: valueWithDot
value: value.with.dot
imagePullSecrets:
- name: qcloud-registry
hostNetwork: true
hostAliases:
- ip: 127.0.0.1
hostnames:
- test1.com
- test2.com
- ip: 127.0.0.2
hostnames:
- test3.com
- test4.com
`,
)
})
t.Run("job", func(t *testing.T) {
check(t, baseProject+`
jobs:
doonce:
image: busybox
restartPolicy: Never
backoffLimit: 4
docron:
image: busybox
restartPolicy: Never
cron:
schedule: "*/1 * * * *"
`,
job,
`
---
apiVersion: batch/v1
kind: Job
metadata:
name: helmx--test--doonce
spec:
backoffLimit: 4
template:
spec:
containers:
- name: helmx--test
image: busybox
imagePullSecrets:
- name: qcloud-registry
restartPolicy: Never
`,
)
})
t.Run("cronJob", func(t *testing.T) {
check(t, baseProject+`
jobs:
doonce:
image: busybox
restartPolicy: Never
backoffLimit: 4
docron:
image: busybox
restartPolicy: Never
cron:
schedule: "*/1 * * * *"
`,
cronJob,
`
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: helmx--test--docron
spec:
schedule: '*/1 * * * *'
jobTemplate:
spec:
template:
spec:
containers:
- name: helmx--test
image: busybox
imagePullSecrets:
- name: qcloud-registry
restartPolicy: Never
`,
)
})
}
var (
service = `
{{ if ( and ( exists .Service ) ( gt ( len .Service.Ports ) 0 ) ) }}
---
apiVersion: v1
kind: Service
metadata:
name: {{ ( .Project.FullName ) }}
annotations:
helmx/project: >-
{{ toJson .Project }}
helmx/upstreams: {{ join .Upstreams "," | quote }}
spec:
selector:
srv: {{ ( .Project.FullName ) }}
{{ spaces 2 | toYamlIndent ( toKubeServiceSpec . ) }}
{{ end }}
`
deployment = `
{{ if ( exists .Service ) }}
---
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ ( .Project.FullName ) }}
labels:
app: {{ ( .Project.FullName ) }}
version: {{ ( .Project.Version ) }}
annotations:
helmx: {{ toJson . | quote }}
spec:
selector:
matchLabels:
srv: {{ ( .Project.FullName ) }}
{{ spaces 2 | toYamlIndent ( toKubeDeploymentSpec . ) }}
{{ end }}
`
job = `
{{ $spec := .}}
{{ range $name, $job := .Jobs }}
{{ if (not (exists $job.Cron)) }}
---
apiVersion: batch/v1
kind: Job
metadata:
name: {{ ( $spec.Project.FullName ) }}--{{ $name }}
spec:
{{ spaces 2 | toYamlIndent ( toKubeJobSpec $spec $job ) }}
{{ end }}
{{ end }}
`
cronJob = `
{{ $spec := .}}
{{ range $name, $job := .Jobs }}
{{ if (exists $job.Cron) }}
---
apiVersion: batch/v1beta1
kind: CronJob
metadata:
name: {{ ( $spec.Project.FullName ) }}--{{ $name }}
spec:
{{ spaces 2 | toYamlIndent ( toKubeCronJobSpec $spec $job ) }}
{{ end }}
{{ end }}
`
ingress = `
{{ if ( len .Service.Ingresses ) }}
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: {{ ( .Project.FullName ) }}
annotations:
kubernetes.io/ingress.class: "nginx"
spec:
{{ spaces 2 | toYamlIndent ( toKubeIngressSpec . )}}
{{ end }}
`
serviceAccount = `
{{ if ( len .Service.ServiceAccountRoleRules ) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: Role
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
rules:
{{ spaces 2 | toYamlIndent ( toKubeRoleRules . )}}
---
apiVersion: v1
kind: ServiceAccount
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
---
apiVersion: rbac.authorization.k8s.io/v1
kind: RoleBinding
metadata:
name: {{ ( .Service.ServiceAccountName ) }}
subjects:
- kind: ServiceAccount
name: {{ ( .Service.ServiceAccountName ) }}
apiGroup: rbac.authorization.k8s.io
roleRef:
kind: Role
name: {{ ( .Service.ServiceAccountName ) }}
apiGroup: rbac.authorization.k8s.io
{{ end }}
`
pullSecret = `
{{ if ( exists .Service.ImagePullSecret ) }}
---
apiVersion: v1
kind: Secret
metadata:
name: {{ ( .Service.ImagePullSecret.Name ) }}
data:
.dockerconfigjson: {{ ( .Service.ImagePullSecret.Base64EncodedDockerConfigJSON ) }}
{{ end }}
`
)
func check(t *testing.T, helmx string, tmpl string, expect string) {
fmt.Println(helmx)
hx := NewHelmX()
err := hx.FromYAML([]byte(helmx))
gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
hx.AddTemplate("tmpl", tmpl)
buf := &bytes.Buffer{}
err = hx.ExecuteAll(buf, &hx.Spec)
gomega.NewWithT(t).Expect(err).To(gomega.BeNil())
fmt.Println("----------helmx----------------")
fmt.Println(buf.String())
fmt.Println("-------------------------------")
gomega.NewWithT(t).Expect(strings.TrimSpace(buf.String())).To(gomega.Equal(strings.TrimSpace(expect)))
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/coolops/helmx.git
git@gitee.com:coolops/helmx.git
coolops
helmx
helmx
master

搜索帮助