github.com/openshift/source-to-image@v1.4.1-0.20240516041539-bf52fc02204e/pkg/build/strategies/dockerfile/dockerfile_test.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  
    10  	"github.com/openshift/source-to-image/pkg/api"
    11  	"github.com/openshift/source-to-image/pkg/api/constants"
    12  	"github.com/openshift/source-to-image/pkg/util/fs"
    13  )
    14  
    15  func TestGetImageScriptsDir(t *testing.T) {
    16  	type testCase struct {
    17  		Config        *api.Config
    18  		ExpectedDir   string
    19  		HasAllScripts bool
    20  	}
    21  
    22  	cases := []testCase{
    23  		{
    24  			Config:      &api.Config{},
    25  			ExpectedDir: defaultScriptsDir,
    26  		},
    27  		{
    28  			Config: &api.Config{
    29  				ScriptsURL: "image:///usr/some/dir",
    30  			},
    31  			ExpectedDir: "/usr/some/dir",
    32  		},
    33  		{
    34  			Config: &api.Config{
    35  				ScriptsURL: "https://github.com/openshift/source-to-image",
    36  			},
    37  			ExpectedDir: defaultScriptsDir,
    38  		},
    39  		{
    40  			Config: &api.Config{
    41  				ImageScriptsURL: "image:///usr/some/dir",
    42  			},
    43  			ExpectedDir: "/usr/some/dir",
    44  		},
    45  		{
    46  			Config: &api.Config{
    47  				ImageScriptsURL: "https://github.com/openshift/source-to-image",
    48  			},
    49  			ExpectedDir: defaultScriptsDir,
    50  		},
    51  		{
    52  			Config: &api.Config{
    53  				ScriptsURL:      "https://github.com/openshift/source-to-image",
    54  				ImageScriptsURL: "image:///usr/some/dir",
    55  			},
    56  			ExpectedDir: "/usr/some/dir",
    57  		},
    58  		{
    59  			Config: &api.Config{
    60  				ScriptsURL:      "image:///usr/some/dir",
    61  				ImageScriptsURL: "image:///usr/other/dir",
    62  			},
    63  			ExpectedDir: "/usr/some/dir",
    64  		},
    65  		{
    66  			Config: &api.Config{
    67  				BuilderImageLabels: map[string]string{
    68  					constants.ScriptsURLLabel: "image:///usr/some/dir",
    69  				},
    70  			},
    71  			ExpectedDir: "/usr/some/dir",
    72  		},
    73  		{
    74  			Config: &api.Config{
    75  				BuilderImageLabels: map[string]string{
    76  					constants.ScriptsURLLabel:           "image:///usr/some/dir",
    77  					constants.DeprecatedScriptsURLLabel: "image:///usr/other/dir",
    78  				},
    79  			},
    80  			ExpectedDir: "/usr/some/dir",
    81  		},
    82  		{
    83  			Config: &api.Config{
    84  				BuilderImageLabels: map[string]string{
    85  					constants.DeprecatedScriptsURLLabel: "image:///usr/some/dir",
    86  				},
    87  			},
    88  			ExpectedDir: "/usr/some/dir",
    89  		},
    90  	}
    91  	for _, tc := range cases {
    92  		output, _ := getImageScriptsDir(tc.Config, &(Dockerfile{}))
    93  		if output != tc.ExpectedDir {
    94  			t.Errorf("Expected image scripts dir %s to be %s", output, tc.ExpectedDir)
    95  		}
    96  	}
    97  }
    98  
    99  func TestInstallScripts(t *testing.T) {
   100  	allErrs := map[string]bool{
   101  		constants.Assemble:      true,
   102  		constants.Run:           true,
   103  		constants.SaveArtifacts: true,
   104  	}
   105  
   106  	tests := []struct {
   107  		name                string
   108  		url                 string
   109  		createAssemble      bool
   110  		createRun           bool
   111  		createSaveArtifacts bool
   112  		scriptErrs          map[string]bool
   113  	}{
   114  		{
   115  			name:       "empty",
   116  			scriptErrs: allErrs,
   117  		},
   118  		{
   119  			name:       "bad url",
   120  			url:        "https://foobadbar.com",
   121  			scriptErrs: allErrs,
   122  		},
   123  		{
   124  			// image:// URLs should always report success
   125  			name: "image url",
   126  			url:  "image://path/to/scripts",
   127  		},
   128  		{
   129  			name:           "assemble script",
   130  			createAssemble: true,
   131  			scriptErrs: map[string]bool{
   132  				constants.Assemble:      false,
   133  				constants.Run:           true,
   134  				constants.SaveArtifacts: true,
   135  			},
   136  		},
   137  		{
   138  			name:      "run script",
   139  			createRun: true,
   140  			scriptErrs: map[string]bool{
   141  				constants.Assemble:      true,
   142  				constants.Run:           false,
   143  				constants.SaveArtifacts: true,
   144  			},
   145  		},
   146  		{
   147  			name:                "save-artifacts script",
   148  			createSaveArtifacts: true,
   149  			scriptErrs: map[string]bool{
   150  				constants.Assemble:      true,
   151  				constants.Run:           true,
   152  				constants.SaveArtifacts: false,
   153  			},
   154  		},
   155  		{
   156  			name:                "all scripts",
   157  			createAssemble:      true,
   158  			createRun:           true,
   159  			createSaveArtifacts: true,
   160  		},
   161  	}
   162  	for _, tc := range tests {
   163  		t.Run(tc.name, func(t *testing.T) {
   164  			workDir, err := ioutil.TempDir("", "s2i-dockerfile-uploads")
   165  			if err != nil {
   166  				t.Fatalf("failed to create working dir: %v", err)
   167  			}
   168  			defer os.RemoveAll(workDir)
   169  			config := &api.Config{
   170  				WorkingDir: workDir,
   171  			}
   172  			fileSystem := fs.NewFileSystem()
   173  			for _, v := range workingDirs {
   174  				err = fileSystem.MkdirAllWithPermissions(filepath.Join(workDir, v), 0755)
   175  				if err != nil {
   176  					t.Fatalf("failed to create working dir: %v", err)
   177  				}
   178  			}
   179  
   180  			tempDir, err := ioutil.TempDir("", "s2i-dockerfile-scripts")
   181  			if err != nil {
   182  				t.Fatalf("could not create temp dir: %v", err)
   183  			}
   184  			defer os.RemoveAll(tempDir)
   185  			if tc.createAssemble {
   186  				err := createTestScript(tempDir, constants.Assemble)
   187  				if err != nil {
   188  					t.Fatalf("failed to write %s script: %v", constants.Assemble, err)
   189  				}
   190  				tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir))
   191  			}
   192  			if tc.createRun {
   193  				err := createTestScript(tempDir, constants.Run)
   194  				if err != nil {
   195  					t.Fatalf("failed to write %s script: %v", constants.Run, err)
   196  				}
   197  				tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir))
   198  			}
   199  			if tc.createSaveArtifacts {
   200  				err := createTestScript(tempDir, constants.SaveArtifacts)
   201  				if err != nil {
   202  					t.Fatalf("failed to write %s script: %v", constants.SaveArtifacts, err)
   203  				}
   204  				tc.url = fmt.Sprintf("file://%s", filepath.ToSlash(tempDir))
   205  			}
   206  			builder, _ := New(config, fileSystem)
   207  			results := builder.installScripts(tc.url, config)
   208  			for _, script := range results {
   209  				expectErr := tc.scriptErrs[script.Script]
   210  				if expectErr && script.Error == nil {
   211  					t.Errorf("expected error for %s, got nil", script.Script)
   212  				}
   213  				if script.Error != nil && !expectErr {
   214  					t.Errorf("received unexpected error: %v", script.Error)
   215  				}
   216  			}
   217  		})
   218  	}
   219  }
   220  
   221  func createTestScript(dir string, name string) error {
   222  	script := "echo \"test script\""
   223  	path := filepath.Join(dir, name)
   224  	err := ioutil.WriteFile(path, []byte(script), 0700)
   225  	return err
   226  }