github.com/umeshredd/helm@v3.0.0-alpha.1+incompatible/pkg/action/release_testing.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 action
    18  
    19  import (
    20  	"time"
    21  
    22  	"github.com/pkg/errors"
    23  
    24  	"helm.sh/helm/pkg/release"
    25  	reltesting "helm.sh/helm/pkg/releasetesting"
    26  )
    27  
    28  // ReleaseTesting is the action for testing a release.
    29  //
    30  // It provides the implementation of 'helm test'.
    31  type ReleaseTesting struct {
    32  	cfg *Configuration
    33  
    34  	Timeout time.Duration
    35  	Cleanup bool
    36  }
    37  
    38  // NewReleaseTesting creates a new ReleaseTesting object with the given configuration.
    39  func NewReleaseTesting(cfg *Configuration) *ReleaseTesting {
    40  	return &ReleaseTesting{
    41  		cfg: cfg,
    42  	}
    43  }
    44  
    45  // Run executes 'helm test' against the given release.
    46  func (r *ReleaseTesting) Run(name string) (<-chan *release.TestReleaseResponse, <-chan error) {
    47  	errc := make(chan error, 1)
    48  	if err := validateReleaseName(name); err != nil {
    49  		errc <- errors.Errorf("releaseTest: Release name is invalid: %s", name)
    50  		return nil, errc
    51  	}
    52  
    53  	// finds the non-deleted release with the given name
    54  	rel, err := r.cfg.Releases.Last(name)
    55  	if err != nil {
    56  		errc <- err
    57  		return nil, errc
    58  	}
    59  
    60  	ch := make(chan *release.TestReleaseResponse, 1)
    61  	testEnv := &reltesting.Environment{
    62  		Namespace:  rel.Namespace,
    63  		KubeClient: r.cfg.KubeClient,
    64  		Timeout:    r.Timeout,
    65  		Messages:   ch,
    66  	}
    67  	r.cfg.Log("running tests for release %s", rel.Name)
    68  	tSuite := reltesting.NewTestSuite(rel)
    69  
    70  	go func() {
    71  		defer close(errc)
    72  		defer close(ch)
    73  
    74  		if err := tSuite.Run(testEnv); err != nil {
    75  			errc <- errors.Wrapf(err, "error running test suite for %s", rel.Name)
    76  			return
    77  		}
    78  
    79  		rel.Info.LastTestSuiteRun = &release.TestSuite{
    80  			StartedAt:   tSuite.StartedAt,
    81  			CompletedAt: tSuite.CompletedAt,
    82  			Results:     tSuite.Results,
    83  		}
    84  
    85  		if r.Cleanup {
    86  			testEnv.DeleteTestPods(tSuite.TestManifests)
    87  		}
    88  
    89  		if err := r.cfg.Releases.Update(rel); err != nil {
    90  			r.cfg.Log("test: Failed to store updated release: %s", err)
    91  		}
    92  	}()
    93  	return ch, errc
    94  }