github.com/qsis/helm@v3.0.0-beta.3+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  }
    49  
    50  // Mock creates a mock release object based on options set by MockReleaseOptions. This function should typically not be used outside of testing.
    51  func Mock(opts *MockReleaseOptions) *Release {
    52  	date := time.Unix(242085845, 0).UTC()
    53  
    54  	name := opts.Name
    55  	if name == "" {
    56  		name = "testrelease-" + string(rand.Intn(100))
    57  	}
    58  
    59  	version := 1
    60  	if opts.Version != 0 {
    61  		version = opts.Version
    62  	}
    63  
    64  	namespace := opts.Namespace
    65  	if namespace == "" {
    66  		namespace = "default"
    67  	}
    68  
    69  	ch := opts.Chart
    70  	if opts.Chart == nil {
    71  		ch = &chart.Chart{
    72  			Metadata: &chart.Metadata{
    73  				Name:       "foo",
    74  				Version:    "0.1.0-beta.1",
    75  				AppVersion: "1.0",
    76  			},
    77  			Templates: []*chart.File{
    78  				{Name: "templates/foo.tpl", Data: []byte(MockManifest)},
    79  			},
    80  		}
    81  	}
    82  
    83  	scode := StatusDeployed
    84  	if len(opts.Status) > 0 {
    85  		scode = opts.Status
    86  	}
    87  
    88  	info := &Info{
    89  		FirstDeployed: date,
    90  		LastDeployed:  date,
    91  		Status:        scode,
    92  		Description:   "Release mock",
    93  	}
    94  
    95  	return &Release{
    96  		Name:      name,
    97  		Info:      info,
    98  		Chart:     ch,
    99  		Config:    map[string]interface{}{"name": "value"},
   100  		Version:   version,
   101  		Namespace: namespace,
   102  		Hooks: []*Hook{
   103  			{
   104  				Name:     "pre-install-hook",
   105  				Kind:     "Job",
   106  				Path:     "pre-install-hook.yaml",
   107  				Manifest: MockHookTemplate,
   108  				LastRun:  HookExecution{},
   109  				Events:   []HookEvent{HookPreInstall},
   110  			},
   111  		},
   112  		Manifest: MockManifest,
   113  	}
   114  }