github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/diagnose/diagnose_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 diagnose
    18  
    19  import (
    20  	"context"
    21  	"io/ioutil"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    26  	"github.com/GoogleContainerTools/skaffold/testutil"
    27  )
    28  
    29  func TestSizeOfDockerContext(t *testing.T) {
    30  	tests := []struct {
    31  		description        string
    32  		artifactName       string
    33  		DockerfileContents string
    34  		files              map[string]string
    35  		expected           int64
    36  		shouldErr          bool
    37  	}{
    38  		{
    39  			description:        "test size",
    40  			artifactName:       "empty",
    41  			DockerfileContents: "From Scratch",
    42  			expected:           2048,
    43  		},
    44  		{
    45  			description:        "test size for a image with file",
    46  			artifactName:       "image",
    47  			DockerfileContents: "From Scratch \n Copy foo /",
    48  			files:              map[string]string{"foo": "foo"},
    49  			expected:           3072,
    50  		},
    51  		{
    52  			description:        "incorrect docker file",
    53  			artifactName:       "error-artifact",
    54  			DockerfileContents: "From Scratch \n Copy doesNotExists /",
    55  			shouldErr:          true,
    56  		},
    57  	}
    58  	for _, test := range tests {
    59  		testutil.Run(t, test.description, func(t *testutil.T) {
    60  			tmpDir := t.NewTempDir().
    61  				Write("Dockerfile", test.DockerfileContents).
    62  				WriteFiles(test.files)
    63  
    64  			dummyArtifact := &latest.Artifact{
    65  				Workspace: tmpDir.Root(),
    66  				ImageName: test.artifactName,
    67  				ArtifactType: latest.ArtifactType{
    68  					DockerArtifact: &latest.DockerArtifact{
    69  						DockerfilePath: "Dockerfile",
    70  					},
    71  				},
    72  			}
    73  
    74  			actual, err := sizeOfDockerContext(context.TODO(), dummyArtifact, nil)
    75  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, actual)
    76  		})
    77  	}
    78  }
    79  
    80  func TestCheckArtifacts(t *testing.T) {
    81  	testutil.Run(t, "", func(t *testutil.T) {
    82  		tmpDir := t.NewTempDir().Write("Dockerfile", "FROM busybox")
    83  
    84  		err := CheckArtifacts(context.Background(), &mockConfig{
    85  			artifacts: []*latest.Artifact{{
    86  				Workspace: tmpDir.Root(),
    87  				ArtifactType: latest.ArtifactType{
    88  					DockerArtifact: &latest.DockerArtifact{
    89  						DockerfilePath: "Dockerfile",
    90  					},
    91  				},
    92  			}},
    93  		}, ioutil.Discard)
    94  
    95  		t.CheckNoError(err)
    96  	})
    97  }
    98  
    99  type mockConfig struct {
   100  	runcontext.RunContext // Embedded to provide the default values.
   101  	artifacts             []*latest.Artifact
   102  }
   103  
   104  func (c *mockConfig) PipelineForImage() latest.Pipeline {
   105  	var pipeline latest.Pipeline
   106  	pipeline.Build.Artifacts = c.artifacts
   107  	return pipeline
   108  }
   109  
   110  func (c *mockConfig) GetPipelines() []latest.Pipeline {
   111  	var pipelines []latest.Pipeline
   112  	pipelines = append(pipelines, c.PipelineForImage())
   113  	return pipelines
   114  }
   115  
   116  func (c *mockConfig) Artifacts() []*latest.Artifact {
   117  	return c.artifacts
   118  }