github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/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  	"math/rand"
    21  	"time"
    22  
    23  	"helm.sh/helm/pkg/chart"
    24  )
    25  
    26  // MockHookTemplate is the hook template used for all mock release objects.
    27  var MockHookTemplate = `apiVersion: v1
    28  kind: Job
    29  metadata:
    30    annotations:
    31      "helm.sh/hook": pre-install
    32  `
    33  
    34  // MockManifest is the manifest used for all mock release objects.
    35  var MockManifest = `apiVersion: v1
    36  kind: Secret
    37  metadata:
    38    name: fixture
    39  `
    40  
    41  // MockReleaseOptions allows for user-configurable options on mock release objects.
    42  type MockReleaseOptions struct {
    43  	Name             string
    44  	Version          int
    45  	Chart            *chart.Chart
    46  	Status           Status
    47  	Namespace        string
    48  	TestSuiteResults []*TestRun
    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-" + string(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  	}
    95  
    96  	if len(opts.TestSuiteResults) > 0 {
    97  		info.LastTestSuiteRun = &TestSuite{
    98  			StartedAt:   date,
    99  			CompletedAt: date,
   100  			Results:     opts.TestSuiteResults,
   101  		}
   102  	}
   103  
   104  	return &Release{
   105  		Name:      name,
   106  		Info:      info,
   107  		Chart:     ch,
   108  		Config:    map[string]interface{}{"name": "value"},
   109  		Version:   version,
   110  		Namespace: namespace,
   111  		Hooks: []*Hook{
   112  			{
   113  				Name:     "pre-install-hook",
   114  				Kind:     "Job",
   115  				Path:     "pre-install-hook.yaml",
   116  				Manifest: MockHookTemplate,
   117  				LastRun:  date,
   118  				Events:   []HookEvent{HookPreInstall},
   119  			},
   120  		},
   121  		Manifest: MockManifest,
   122  	}
   123  }