...
1
16
17 package release
18
19 import (
20 "fmt"
21 "math/rand"
22
23 "helm.sh/helm/v3/pkg/chart"
24 "helm.sh/helm/v3/pkg/time"
25 )
26
27
28 var MockHookTemplate = `apiVersion: v1
29 kind: Job
30 metadata:
31 annotations:
32 "helm.sh/hook": pre-install
33 `
34
35
36 var MockManifest = `apiVersion: v1
37 kind: Secret
38 metadata:
39 name: fixture
40 `
41
42
43 type MockReleaseOptions struct {
44 Name string
45 Version int
46 Chart *chart.Chart
47 Status Status
48 Namespace string
49 }
50
51
52 func Mock(opts *MockReleaseOptions) *Release {
53 date := time.Unix(242085845, 0).UTC()
54
55 name := opts.Name
56 if name == "" {
57 name = "testrelease-" + fmt.Sprint(rand.Intn(100))
58 }
59
60 version := 1
61 if opts.Version != 0 {
62 version = opts.Version
63 }
64
65 namespace := opts.Namespace
66 if namespace == "" {
67 namespace = "default"
68 }
69
70 ch := opts.Chart
71 if opts.Chart == nil {
72 ch = &chart.Chart{
73 Metadata: &chart.Metadata{
74 Name: "foo",
75 Version: "0.1.0-beta.1",
76 AppVersion: "1.0",
77 },
78 Templates: []*chart.File{
79 {Name: "templates/foo.tpl", Data: []byte(MockManifest)},
80 },
81 }
82 }
83
84 scode := StatusDeployed
85 if len(opts.Status) > 0 {
86 scode = opts.Status
87 }
88
89 info := &Info{
90 FirstDeployed: date,
91 LastDeployed: date,
92 Status: scode,
93 Description: "Release mock",
94 Notes: "Some mock release notes!",
95 }
96
97 return &Release{
98 Name: name,
99 Info: info,
100 Chart: ch,
101 Config: map[string]interface{}{"name": "value"},
102 Version: version,
103 Namespace: namespace,
104 Hooks: []*Hook{
105 {
106 Name: "pre-install-hook",
107 Kind: "Job",
108 Path: "pre-install-hook.yaml",
109 Manifest: MockHookTemplate,
110 LastRun: HookExecution{},
111 Events: []HookEvent{HookPreInstall},
112 },
113 },
114 Manifest: MockManifest,
115 }
116 }
117
View as plain text