github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/test/structure/structure_test.go (about)

     1  /*
     2  Copyright 2021 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 structure
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"io/ioutil"
    23  	"testing"
    24  
    25  	"github.com/blang/semver"
    26  
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/cluster"
    28  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
    29  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    30  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
    31  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    32  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
    33  	"github.com/GoogleContainerTools/skaffold/testutil"
    34  	testEvent "github.com/GoogleContainerTools/skaffold/testutil/event"
    35  )
    36  
    37  func TestNewRunner(t *testing.T) {
    38  	testutil.Run(t, "", func(t *testutil.T) {
    39  		tmpDir := t.NewTempDir().Touch("test.yaml")
    40  		t.Override(&cluster.FindMinikubeBinary, func(context.Context) (string, semver.Version, error) {
    41  			return "", semver.Version{}, errors.New("not found")
    42  		})
    43  
    44  		t.Override(&util.DefaultExecCommand, testutil.CmdRun("container-structure-test test -v warn --image image:tag --config "+tmpDir.Path("test.yaml")))
    45  
    46  		testCase := &latest.TestCase{
    47  			ImageName:      "image",
    48  			Workspace:      tmpDir.Root(),
    49  			StructureTests: []string{"test.yaml"},
    50  		}
    51  		cfg := &mockConfig{tests: []*latest.TestCase{testCase}}
    52  
    53  		testEvent.InitializeState([]latest.Pipeline{{}})
    54  
    55  		testRunner, err := New(context.Background(), cfg, testCase, true)
    56  		t.CheckNoError(err)
    57  		err = testRunner.Test(context.Background(), ioutil.Discard, "image:tag")
    58  		t.CheckNoError(err)
    59  	})
    60  }
    61  
    62  func TestIgnoreDockerNotFound(t *testing.T) {
    63  	testutil.Run(t, "", func(t *testutil.T) {
    64  		tmpDir := t.NewTempDir().Touch("test.yaml")
    65  		t.Override(&docker.NewAPIClient, func(context.Context, docker.Config) (docker.LocalDaemon, error) {
    66  			return nil, errors.New("not found")
    67  		})
    68  
    69  		testCase := &latest.TestCase{
    70  			ImageName:      "image",
    71  			Workspace:      tmpDir.Root(),
    72  			StructureTests: []string{"test.yaml"},
    73  		}
    74  		cfg := &mockConfig{tests: []*latest.TestCase{testCase}}
    75  
    76  		testRunner, err := New(context.Background(), cfg, testCase, true)
    77  		t.CheckError(true, err)
    78  		t.CheckNil(testRunner)
    79  	})
    80  }
    81  
    82  func TestCustomParams(t *testing.T) {
    83  	testCases := []struct {
    84  		structureTestArgs []string
    85  		expectedExtras    string
    86  	}{
    87  		{
    88  			structureTestArgs: []string{"--driver=tar", "--force", "-q", "--save"},
    89  			expectedExtras:    "--driver=tar --force -q --save",
    90  		},
    91  		{
    92  			structureTestArgs: []string{},
    93  			expectedExtras:    "",
    94  		},
    95  		{
    96  			structureTestArgs: nil,
    97  			expectedExtras:    "",
    98  		},
    99  	}
   100  
   101  	for _, tc := range testCases {
   102  		testutil.Run(t, "", func(t *testutil.T) {
   103  			tmpDir := t.NewTempDir().Touch("test.yaml")
   104  			t.Override(&cluster.FindMinikubeBinary, func(context.Context) (string, semver.Version, error) {
   105  				return "", semver.Version{}, errors.New("not found")
   106  			})
   107  
   108  			expected := "container-structure-test test -v warn --image image:tag --config " + tmpDir.Path("test.yaml")
   109  			if len(tc.expectedExtras) > 0 {
   110  				expected += " " + tc.expectedExtras
   111  			}
   112  			t.Override(&util.DefaultExecCommand, testutil.CmdRun(expected))
   113  
   114  			testCase := &latest.TestCase{
   115  				ImageName:         "image",
   116  				Workspace:         tmpDir.Root(),
   117  				StructureTests:    []string{"test.yaml"},
   118  				StructureTestArgs: tc.structureTestArgs,
   119  			}
   120  			cfg := &mockConfig{tests: []*latest.TestCase{testCase}}
   121  
   122  			testEvent.InitializeState([]latest.Pipeline{{}})
   123  
   124  			testRunner, err := New(context.Background(), cfg, testCase, true)
   125  			t.CheckNoError(err)
   126  			err = testRunner.Test(context.Background(), ioutil.Discard, "image:tag")
   127  			t.CheckNoError(err)
   128  		})
   129  	}
   130  }
   131  
   132  type mockConfig struct {
   133  	runcontext.RunContext // Embedded to provide the default values.
   134  	tests                 []*latest.TestCase
   135  	muted                 config.Muted
   136  }
   137  
   138  func (c *mockConfig) Muted() config.Muted { return c.muted }
   139  
   140  func (c *mockConfig) TestCases() []*latest.TestCase { return c.tests }