Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
Signed-off-by: cezhang <c1zhang.dev@gmail.com>
  • Loading branch information
cezhang committed Jan 10, 2023
1 parent cc43d37 commit e7eec64
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 0 deletions.
41 changes: 41 additions & 0 deletions pkg/appfile/dryrun/dryrun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,45 @@ var _ = Describe("Test dry run with policies", func() {
Expect(buff.String()).Should(ContainSubstring("name: testing-dryrun"))
Expect(buff.String()).Should(ContainSubstring("kind: Deployment"))
})

It("Test dry run without custom policy", func() {

topo, err := os.ReadFile("./testdata/pd-mypolicy.yaml")
Expect(err).Should(BeNil())
var pd v1beta1.PolicyDefinition
Expect(yaml.Unmarshal([]byte(topo), &pd)).Should(BeNil())
Expect(k8sClient.Create(context.TODO(), &pd)).Should(BeNil())

appYAML := readDataFromFile("./testdata/testing-dry-run-4.yaml")
app := &v1beta1.Application{}
Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil())

var buff = bytes.Buffer{}
err = dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff)
Expect(err).Should(BeNil())
Expect(buff.String()).Should(ContainSubstring("# Application(default) -- Component(testing-dryrun)"))
Expect(buff.String()).Should(ContainSubstring("# Application(default) -- Policy(mypolicy)"))
Expect(buff.String()).Should(ContainSubstring("name: my-policy"))
})

It("Test dry run with trait", func() {

nocalhost, err := os.ReadFile("../../../charts/vela-core/templates/defwithtemplate/nocalhost.yaml")
Expect(err).Should(BeNil())
nocalhostYAML := strings.Replace(string(nocalhost), "{{ include \"systemDefinitionNamespace\" . }}", types.DefaultKubeVelaNS, 1)
var td v1beta1.TraitDefinition
Expect(yaml.Unmarshal([]byte(nocalhostYAML), &td)).Should(BeNil())
Expect(k8sClient.Create(context.TODO(), &td)).Should(BeNil())

appYAML := readDataFromFile("./testdata/testing-dry-run-5.yaml")
app := &v1beta1.Application{}
Expect(yaml.Unmarshal([]byte(appYAML), &app)).Should(BeNil())

var buff = bytes.Buffer{}
err = dryrunOpt.ExecuteDryRunWithPolicies(context.TODO(), app, &buff)
Expect(err).Should(BeNil())
Expect(buff.String()).Should(ContainSubstring("# Application(default) -- Component(testing-dryrun)"))
Expect(buff.String()).Should(ContainSubstring("## From the trait nocalhost"))
Expect(buff.String()).Should(ContainSubstring("trait.oam.dev/type: nocalhost"))
})
})
22 changes: 22 additions & 0 deletions pkg/appfile/dryrun/testdata/pd-mypolicy.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Code generated by KubeVela templates. DO NOT EDIT. Please edit the original cue file.
# Definition source cue file: vela-templates/definitions/internal/topology.cue
apiVersion: core.oam.dev/v1beta1
kind: PolicyDefinition
metadata:
name: mypolicy
namespace: vela-system
spec:
schematic:
cue:
template: |
output: {
apiVersion: "testing/v1"
kind: "Policy"
policy: {
name: parameter.name
}
}
parameter: {
name: string
}
16 changes: 16 additions & 0 deletions pkg/appfile/dryrun/testdata/testing-dry-run-4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: testing-app
spec:
components:
- name: testing-dryrun
type: webservice
properties:
image: oamdev/hello-world:v1
policies:
- name: mypolicy
type: mypolicy
properties:
name: "my-policy"

48 changes: 48 additions & 0 deletions pkg/appfile/dryrun/testdata/testing-dry-run-5.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
apiVersion: core.oam.dev/v1beta1
kind: Application
metadata:
name: testing-app
spec:
components:
- name: testing-dryrun
type: webservice
properties:
image: oamdev/hello-world:v1
traits:
- type: nocalhost
properties:
port: 9080
gitUrl: https://github.com/nocalhost/bookinfo-productpage.git
image: nocalhost-docker.pkg.coding.net/nocalhost/dev-images/python:3.7.7-slim-productpage-with-pydevd
shell: "bash"
workDir: "/opt/work"
resources:
limits:
memory: 1Gi
cpu: "1"
requests:
memory: 512Mi
cpu: "0.5"
debug:
remoteDebugPort: 9009
hotReload: true
sync:
type: send
filePattern:
- ./
ignoreFilePattern:
- .git
- .idea
command:
run:
- sh
- run.sh
debug:
- sh
- debug.sh
env:
- name: "foo"
value: "bar"
portForward:
- 39080:9080

0 comments on commit e7eec64

Please sign in to comment.