github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/build/init_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  	"io"
    21  	"testing"
    22  
    23  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/prompt"
    24  	"github.com/GoogleContainerTools/skaffold/testutil"
    25  )
    26  
    27  func TestDefaultInitializerGenerateManifests(t *testing.T) {
    28  	tests := []struct {
    29  		description       string
    30  		generatedInfos    []GeneratedArtifactInfo
    31  		mockPorts         []int
    32  		expectedManifests []string
    33  		force             bool
    34  		shouldErr         bool
    35  	}{
    36  		{
    37  			description: "one manifest, force",
    38  			generatedInfos: []GeneratedArtifactInfo{
    39  				{
    40  					ArtifactInfo{
    41  						ImageName: "image1",
    42  					},
    43  					"path/to/manifest",
    44  				},
    45  			},
    46  			expectedManifests: []string{
    47  				`apiVersion: v1
    48  kind: Service
    49  metadata:
    50    name: image1
    51    labels:
    52      app: image1
    53  spec:
    54    ports:
    55    - port: 8080
    56      protocol: TCP
    57    clusterIP: None
    58    selector:
    59      app: image1
    60  ---
    61  apiVersion: apps/v1
    62  kind: Deployment
    63  metadata:
    64    name: image1
    65    labels:
    66      app: image1
    67  spec:
    68    replicas: 1
    69    selector:
    70      matchLabels:
    71        app: image1
    72    template:
    73      metadata:
    74        labels:
    75          app: image1
    76      spec:
    77        containers:
    78        - name: image1
    79          image: image1
    80  `,
    81  			},
    82  			force: true,
    83  		},
    84  		{
    85  			description: "2 manifests, 1 port 0, interactive",
    86  			generatedInfos: []GeneratedArtifactInfo{
    87  				{
    88  					ArtifactInfo{
    89  						ImageName: "image1",
    90  					},
    91  					"path/to/manifest1",
    92  				},
    93  				{
    94  					ArtifactInfo{
    95  						ImageName: "image2",
    96  					},
    97  					"path/to/manifest2",
    98  				},
    99  			},
   100  			mockPorts: []int{8080, 0},
   101  			expectedManifests: []string{
   102  				`apiVersion: v1
   103  kind: Service
   104  metadata:
   105    name: image1
   106    labels:
   107      app: image1
   108  spec:
   109    ports:
   110    - port: 8080
   111      protocol: TCP
   112    clusterIP: None
   113    selector:
   114      app: image1
   115  ---
   116  apiVersion: apps/v1
   117  kind: Deployment
   118  metadata:
   119    name: image1
   120    labels:
   121      app: image1
   122  spec:
   123    replicas: 1
   124    selector:
   125      matchLabels:
   126        app: image1
   127    template:
   128      metadata:
   129        labels:
   130          app: image1
   131      spec:
   132        containers:
   133        - name: image1
   134          image: image1
   135  `,
   136  				`apiVersion: v1
   137  kind: Service
   138  metadata:
   139    name: image2
   140    labels:
   141      app: image2
   142  spec:
   143    clusterIP: None
   144    selector:
   145      app: image2
   146  ---
   147  apiVersion: apps/v1
   148  kind: Deployment
   149  metadata:
   150    name: image2
   151    labels:
   152      app: image2
   153  spec:
   154    replicas: 1
   155    selector:
   156      matchLabels:
   157        app: image2
   158    template:
   159      metadata:
   160        labels:
   161          app: image2
   162      spec:
   163        containers:
   164        - name: image2
   165          image: image2
   166  `,
   167  			},
   168  		},
   169  	}
   170  
   171  	for _, test := range tests {
   172  		testutil.Run(t, test.description, func(t *testutil.T) {
   173  			mockPortIdx := 0
   174  			t.Override(&prompt.PortForwardResourceFunc, func(_ io.Writer, imageName string) (int, error) {
   175  				port := test.mockPorts[mockPortIdx]
   176  				mockPortIdx++
   177  
   178  				return port, nil
   179  			})
   180  
   181  			d := defaultBuildInitializer{
   182  				generatedArtifactInfos: test.generatedInfos,
   183  			}
   184  
   185  			manifests, err := d.GenerateManifests(nil, test.force, true)
   186  
   187  			expected := make(map[GeneratedArtifactInfo][]byte)
   188  			for i, info := range test.generatedInfos {
   189  				expected[info] = []byte(test.expectedManifests[i])
   190  			}
   191  
   192  			t.CheckErrorAndDeepEqual(test.shouldErr, err, expected, manifests)
   193  		})
   194  	}
   195  }