github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/build/json_test.go (about)

     1  /*
     2  Copyright 2020 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 build
    18  
    19  import (
    20  	"bytes"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/jib"
    24  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    25  	"github.com/GoogleContainerTools/skaffold/testutil"
    26  )
    27  
    28  func TestPrintAnalyzeJSON(t *testing.T) {
    29  	tests := []struct {
    30  		description   string
    31  		artifactInfos []ArtifactInfo
    32  		builders      []InitBuilder
    33  		images        []string
    34  		skipBuild     bool
    35  		shouldErr     bool
    36  		expected      string
    37  	}{
    38  		{
    39  			description:   "builders and images with pairs",
    40  			artifactInfos: []ArtifactInfo{{Builder: jib.ArtifactConfig{BuilderName: jib.PluginName(jib.JibGradle), Image: "image1", File: "build.gradle", Project: "project"}, ImageName: "image1"}},
    41  			builders:      []InitBuilder{docker.ArtifactConfig{File: "Dockerfile"}},
    42  			images:        []string{"image2"},
    43  			expected:      `{"builders":[{"name":"Jib Gradle Plugin","payload":{"image":"image1","path":"build.gradle","project":"project"}},{"name":"Docker","payload":{"path":"Dockerfile"}}],"images":[{"name":"image1","foundMatch":true},{"name":"image2","foundMatch":false}]}` + "\n",
    44  		},
    45  		{
    46  			description: "builders and images with no pairs",
    47  			builders:    []InitBuilder{jib.ArtifactConfig{BuilderName: jib.PluginName(jib.JibGradle), File: "build.gradle", Project: "project"}, docker.ArtifactConfig{File: "Dockerfile"}},
    48  			images:      []string{"image1", "image2"},
    49  			expected:    `{"builders":[{"name":"Jib Gradle Plugin","payload":{"path":"build.gradle","project":"project"}},{"name":"Docker","payload":{"path":"Dockerfile"}}],"images":[{"name":"image1","foundMatch":false},{"name":"image2","foundMatch":false}]}` + "\n",
    50  		},
    51  		{
    52  			description: "no dockerfile, skip build",
    53  			images:      []string{"image1", "image2"},
    54  			skipBuild:   true,
    55  			expected:    `{"images":[{"name":"image1","foundMatch":false},{"name":"image2","foundMatch":false}]}` + "\n",
    56  		},
    57  		{
    58  			description: "no dockerfile",
    59  			images:      []string{"image1", "image2"},
    60  			shouldErr:   true,
    61  		},
    62  		{
    63  			description: "no dockerfiles or images",
    64  			shouldErr:   true,
    65  		},
    66  	}
    67  	for _, test := range tests {
    68  		testutil.Run(t, test.description, func(t *testutil.T) {
    69  			var out bytes.Buffer
    70  
    71  			err := PrintAnalyzeJSON(&out, test.skipBuild, test.artifactInfos, test.builders, test.images)
    72  
    73  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, out.String())
    74  		})
    75  	}
    76  }
    77  
    78  func TestPrintAnalyzeJSONNoJib(t *testing.T) {
    79  	tests := []struct {
    80  		description   string
    81  		artifactInfos []ArtifactInfo
    82  		builders      []InitBuilder
    83  		images        []string
    84  		skipBuild     bool
    85  		shouldErr     bool
    86  		expected      string
    87  	}{
    88  		{
    89  			description: "builders and images (backwards compatibility)",
    90  			builders:    []InitBuilder{docker.ArtifactConfig{File: "Dockerfile1"}, docker.ArtifactConfig{File: "Dockerfile2"}},
    91  			images:      []string{"image1", "image2"},
    92  			expected:    `{"dockerfiles":["Dockerfile1","Dockerfile2"],"images":["image1","image2"]}` + "\n",
    93  		},
    94  		{
    95  			description: "no dockerfile, skip build (backwards compatibility)",
    96  			images:      []string{"image1", "image2"},
    97  			skipBuild:   true,
    98  			expected:    `{"images":["image1","image2"]}` + "\n",
    99  		},
   100  		{
   101  			description: "no dockerfile",
   102  			images:      []string{"image1", "image2"},
   103  			shouldErr:   true,
   104  		},
   105  		{
   106  			description: "no dockerfiles or images",
   107  			shouldErr:   true,
   108  		},
   109  	}
   110  	for _, test := range tests {
   111  		testutil.Run(t, test.description, func(t *testutil.T) {
   112  			var out bytes.Buffer
   113  
   114  			err := PrintAnalyzeOldFormat(&out, test.skipBuild, test.artifactInfos, test.builders, test.images)
   115  
   116  			t.CheckErrorAndDeepEqual(test.shouldErr, err, test.expected, out.String())
   117  		})
   118  	}
   119  }