github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/runner/v1/deploy_test.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 v1
    18  
    19  import (
    20  	"bytes"
    21  	"context"
    22  	"errors"
    23  	"io/ioutil"
    24  	"testing"
    25  
    26  	"k8s.io/client-go/tools/clientcmd/api"
    27  
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
    30  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/client"
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner"
    32  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
    33  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    34  	"github.com/GoogleContainerTools/skaffold/testutil"
    35  )
    36  
    37  func TestDeploy(t *testing.T) {
    38  	tests := []struct {
    39  		description string
    40  		testBench   *TestBench
    41  		shouldErr   bool
    42  	}{
    43  		{
    44  			description: "deploy succeeds",
    45  			testBench:   &TestBench{},
    46  		},
    47  		{
    48  			description: "deploy fails",
    49  			testBench:   &TestBench{deployErrors: []error{errors.New("deploy error")}},
    50  			shouldErr:   true,
    51  		},
    52  	}
    53  
    54  	for _, test := range tests {
    55  		testutil.Run(t, test.description, func(t *testutil.T) {
    56  			t.SetupFakeKubernetesContext(api.Config{CurrentContext: "cluster1"})
    57  			t.Override(&client.Client, mockK8sClient)
    58  
    59  			r := createRunner(t, test.testBench, nil, []*latest.Artifact{{ImageName: "img1"}, {ImageName: "img2"}}, nil)
    60  			out := new(bytes.Buffer)
    61  
    62  			err := r.Deploy(context.Background(), out, []graph.Artifact{
    63  				{ImageName: "img1", Tag: "img1:tag1"},
    64  				{ImageName: "img2", Tag: "img2:tag2"},
    65  			})
    66  			t.CheckError(test.shouldErr, err)
    67  		})
    68  	}
    69  }
    70  
    71  func TestSkaffoldDeployRenderOnly(t *testing.T) {
    72  	testutil.Run(t, "does not make kubectl calls", func(t *testutil.T) {
    73  		runCtx := &runcontext.RunContext{
    74  			Opts: config.SkaffoldOptions{
    75  				Namespace:  "testNamespace",
    76  				RenderOnly: true,
    77  			},
    78  			KubeContext: "does-not-exist",
    79  		}
    80  
    81  		deployer, err := runner.GetDeployer(context.Background(), runCtx, nil)
    82  		t.RequireNoError(err)
    83  		r := SkaffoldRunner{
    84  			runCtx:   runCtx,
    85  			deployer: deployer,
    86  		}
    87  		var builds []graph.Artifact
    88  
    89  		err = r.Deploy(context.Background(), ioutil.Discard, builds)
    90  
    91  		t.CheckNoError(err)
    92  	})
    93  }