github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/initializer/config_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 initializer
    18  
    19  import (
    20  	"errors"
    21  	"io"
    22  	"path/filepath"
    23  	"testing"
    24  
    25  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
    26  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/initializer/build"
    27  	"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
    28  	"github.com/GoogleContainerTools/skaffold/testutil"
    29  )
    30  
    31  type stubDeploymentInitializer struct {
    32  	config   latest.DeployConfig
    33  	profiles []latest.Profile
    34  }
    35  
    36  func (s stubDeploymentInitializer) DeployConfig() (latest.DeployConfig, []latest.Profile) {
    37  	return s.config, s.profiles
    38  }
    39  
    40  func (s stubDeploymentInitializer) GetImages() []string {
    41  	panic("implement me")
    42  }
    43  
    44  func (s stubDeploymentInitializer) Validate() error {
    45  	panic("no thanks")
    46  }
    47  
    48  func (s stubDeploymentInitializer) AddManifestForImage(string, string) {
    49  	panic("don't call me")
    50  }
    51  
    52  type stubBuildInitializer struct {
    53  	artifactInfos []build.ArtifactInfo
    54  }
    55  
    56  func (s stubBuildInitializer) ProcessImages([]string) error {
    57  	panic("no")
    58  }
    59  
    60  func (s stubBuildInitializer) PrintAnalysis(io.Writer) error {
    61  	panic("no sir")
    62  }
    63  
    64  func (s stubBuildInitializer) BuildConfig() (latest.BuildConfig, []*latest.PortForwardResource) {
    65  	return latest.BuildConfig{
    66  		Artifacts: build.Artifacts(s.artifactInfos),
    67  	}, nil
    68  }
    69  
    70  func (s stubBuildInitializer) GenerateManifests(io.Writer, bool, bool) (map[build.GeneratedArtifactInfo][]byte, error) {
    71  	panic("no thank you")
    72  }
    73  
    74  func TestGenerateSkaffoldConfig(t *testing.T) {
    75  	tests := []struct {
    76  		name                   string
    77  		expectedSkaffoldConfig *latest.SkaffoldConfig
    78  		deployConfig           latest.DeployConfig
    79  		profiles               []latest.Profile
    80  		builderConfigInfos     []build.ArtifactInfo
    81  		getWd                  func() (string, error)
    82  	}{
    83  		{
    84  			name:               "empty",
    85  			builderConfigInfos: []build.ArtifactInfo{},
    86  			deployConfig:       latest.DeployConfig{},
    87  			getWd: func() (s string, err error) {
    88  				return filepath.Join("rootDir", "testConfig"), nil
    89  			},
    90  			expectedSkaffoldConfig: &latest.SkaffoldConfig{
    91  				APIVersion: latest.Version,
    92  				Kind:       "Config",
    93  				Metadata:   latest.Metadata{Name: "testconfig"},
    94  				Pipeline: latest.Pipeline{
    95  					Deploy: latest.DeployConfig{},
    96  				},
    97  			},
    98  		},
    99  		{
   100  			name: "root dir + builder image pairs",
   101  			builderConfigInfos: []build.ArtifactInfo{
   102  				{
   103  					Builder: docker.ArtifactConfig{
   104  						File: "testDir/Dockerfile",
   105  					},
   106  					ImageName: "image1",
   107  				},
   108  			},
   109  			deployConfig: latest.DeployConfig{},
   110  			getWd: func() (s string, err error) {
   111  				return string(filepath.Separator), nil
   112  			},
   113  			expectedSkaffoldConfig: &latest.SkaffoldConfig{
   114  				APIVersion: latest.Version,
   115  				Kind:       "Config",
   116  				Metadata:   latest.Metadata{},
   117  				Pipeline: latest.Pipeline{
   118  					Build: latest.BuildConfig{
   119  						Artifacts: []*latest.Artifact{
   120  							{
   121  								ImageName: "image1",
   122  								Workspace: "testDir",
   123  								ArtifactType: latest.ArtifactType{
   124  									DockerArtifact: &latest.DockerArtifact{DockerfilePath: "Dockerfile"},
   125  								},
   126  							},
   127  						},
   128  					},
   129  					Deploy: latest.DeployConfig{},
   130  				},
   131  			},
   132  		},
   133  		{
   134  			name:               "error working dir",
   135  			builderConfigInfos: []build.ArtifactInfo{},
   136  			deployConfig:       latest.DeployConfig{},
   137  			getWd: func() (s string, err error) {
   138  				return "", errors.New("testError")
   139  			},
   140  			expectedSkaffoldConfig: &latest.SkaffoldConfig{
   141  				APIVersion: latest.Version,
   142  				Kind:       "Config",
   143  				Metadata:   latest.Metadata{},
   144  			},
   145  		},
   146  	}
   147  
   148  	for _, test := range tests {
   149  		testutil.Run(t, test.name, func(t *testutil.T) {
   150  			deploymentInitializer := stubDeploymentInitializer{
   151  				test.deployConfig,
   152  				test.profiles,
   153  			}
   154  			buildInitializer := stubBuildInitializer{
   155  				test.builderConfigInfos,
   156  			}
   157  			t.Override(&getWd, test.getWd)
   158  			config := generateSkaffoldConfig(buildInitializer, deploymentInitializer)
   159  			t.CheckDeepEqual(config, test.expectedSkaffoldConfig)
   160  		})
   161  	}
   162  }
   163  
   164  func Test_canonicalizeName(t *testing.T) {
   165  	const length253 = "aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa-aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa-aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaaaaaaaa.aaa"
   166  	tests := []struct {
   167  		in, out string
   168  	}{
   169  		{
   170  			in:  "abc def",
   171  			out: "abc-def",
   172  		},
   173  		{
   174  			in:  "abc    def",
   175  			out: "abc-def",
   176  		},
   177  		{
   178  			in:  "abc...def",
   179  			out: "abc...def",
   180  		},
   181  		{
   182  			in:  "abc---def",
   183  			out: "abc---def",
   184  		},
   185  		{
   186  			in:  "aBc DeF",
   187  			out: "abc-def",
   188  		},
   189  		{
   190  			in:  length253 + "XXXXXXX",
   191  			out: length253,
   192  		},
   193  	}
   194  
   195  	for _, test := range tests {
   196  		testutil.Run(t, test.in, func(t *testutil.T) {
   197  			actual := canonicalizeName(test.in)
   198  
   199  			t.CheckDeepEqual(test.out, actual)
   200  		})
   201  	}
   202  }