github.com/stefanmcshane/helm@v0.0.0-20221213002717-88a4a2c6e77d/pkg/release/mock.go (about) 1 /* 2 Copyright The Helm Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package release 18 19 import ( 20 "fmt" 21 "math/rand" 22 23 "github.com/stefanmcshane/helm/pkg/chart" 24 "github.com/stefanmcshane/helm/pkg/time" 25 ) 26 27 // MockHookTemplate is the hook template used for all mock release objects. 28 var MockHookTemplate = `apiVersion: v1 29 kind: Job 30 metadata: 31 annotations: 32 "helm.sh/hook": pre-install 33 ` 34 35 // MockManifest is the manifest used for all mock release objects. 36 var MockManifest = `apiVersion: v1 37 kind: Secret 38 metadata: 39 name: fixture 40 ` 41 42 // MockReleaseOptions allows for user-configurable options on mock release objects. 43 type MockReleaseOptions struct { 44 Name string 45 Version int 46 Chart *chart.Chart 47 Status Status 48 Namespace string 49 } 50 51 // Mock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing. 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 }