github.com/GoogleContainerTools/skaffold/v2@v2.13.2/integration/generate_pipeline_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 integration
    18  
    19  import (
    20  	"bytes"
    21  	"os"
    22  	"testing"
    23  
    24  	"github.com/GoogleContainerTools/skaffold/v2/integration/skaffold"
    25  )
    26  
    27  type configContents struct {
    28  	path string
    29  	data []byte
    30  }
    31  
    32  func TestGeneratePipeline(t *testing.T) {
    33  	// TODO: This test shall pass once render v2 is completed.
    34  	t.SkipNow()
    35  
    36  	MarkIntegrationTest(t, CanRunWithoutGcp)
    37  
    38  	tests := []struct {
    39  		description string
    40  		dir         string
    41  		input       []byte
    42  		args        []string
    43  		configFiles []string
    44  		skipCheck   bool
    45  	}{
    46  		{
    47  			description: "no profiles",
    48  			dir:         "testdata/generate_pipeline/no_profiles",
    49  			input:       []byte("y\n"),
    50  		},
    51  		{
    52  			description: "existing oncluster profile",
    53  			dir:         "testdata/generate_pipeline/existing_oncluster",
    54  			input:       []byte(""),
    55  		},
    56  		{
    57  			description: "existing other profile",
    58  			dir:         "testdata/generate_pipeline/existing_other",
    59  			input:       []byte("y\n"),
    60  		},
    61  		{
    62  			description: "multiple skaffold.yamls to create pipeline from",
    63  			dir:         "testdata/generate_pipeline/multiple_configs",
    64  			input:       []byte{'y', '\n', 'y', '\n'},
    65  			configFiles: []string{"sub-app/skaffold.yaml"},
    66  		},
    67  		{
    68  			description: "user example",
    69  			dir:         "examples/generate-pipeline",
    70  			input:       []byte("y\n"),
    71  			skipCheck:   true,
    72  		},
    73  	}
    74  	for _, test := range tests {
    75  		t.Run(test.description, func(t *testing.T) {
    76  			args, contents, err := getOriginalContents(test.args, test.dir, test.configFiles)
    77  			failNowIfError(t, err)
    78  			defer writeOriginalContents(contents)
    79  
    80  			originalConfig, err := os.ReadFile(test.dir + "/skaffold.yaml")
    81  			failNowIfError(t, err)
    82  			defer os.WriteFile(test.dir+"/skaffold.yaml", originalConfig, 0755)
    83  			defer os.Remove(test.dir + "/pipeline.yaml")
    84  
    85  			skaffoldEnv := []string{
    86  				"PIPELINE_GIT_URL=this-is-a-test",
    87  				"PIPELINE_SKAFFOLD_VERSION=latest",
    88  			}
    89  			skaffold.GeneratePipeline(args...).WithStdin(test.input).WithEnv(skaffoldEnv).InDir(test.dir).RunOrFail(t)
    90  
    91  			if !test.skipCheck {
    92  				checkFileContents(t, test.dir+"/expectedSkaffold.yaml", test.dir+"/skaffold.yaml")
    93  			}
    94  			checkFileContents(t, test.dir+"/expectedPipeline.yaml", test.dir+"/pipeline.yaml")
    95  		})
    96  	}
    97  }
    98  
    99  func getOriginalContents(testArgs []string, testDir string, configFiles []string) ([]string, []configContents, error) {
   100  	var originalConfigs []configContents
   101  
   102  	for _, configFile := range configFiles {
   103  		testArgs = append(testArgs, []string{"--config-files", configFile}...)
   104  
   105  		path := testDir + "/" + configFile
   106  		contents, err := os.ReadFile(path)
   107  		if err != nil {
   108  			return nil, nil, err
   109  		}
   110  		originalConfigs = append(originalConfigs, configContents{path, contents})
   111  	}
   112  
   113  	return testArgs, originalConfigs, nil
   114  }
   115  
   116  func writeOriginalContents(contents []configContents) {
   117  	for _, content := range contents {
   118  		os.WriteFile(content.path, content.data, 0755)
   119  	}
   120  }
   121  
   122  func checkFileContents(t *testing.T, wantFile, gotFile string) {
   123  	wantContents, err := os.ReadFile(wantFile)
   124  	failNowIfError(t, err)
   125  
   126  	gotContents, err := os.ReadFile(gotFile)
   127  	failNowIfError(t, err)
   128  
   129  	if !bytes.Equal(wantContents, gotContents) {
   130  		t.Errorf("Contents of %s did not match those of %s\ngot:%s\nwant:%s", gotFile, wantFile, string(gotContents), string(wantContents))
   131  	}
   132  }