github.com/zhuohuang-hust/src-cbuild@v0.0.0-20230105071821-c7aab3e7c840/builder/dockerfile/internals_test.go (about)

     1  package dockerfile
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/docker/docker/api/types"
     9  	"github.com/docker/docker/builder"
    10  	"github.com/docker/docker/pkg/archive"
    11  )
    12  
    13  func TestEmptyDockerfile(t *testing.T) {
    14  	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
    15  	defer cleanup()
    16  
    17  	createTestTempFile(t, contextDir, builder.DefaultDockerfileName, "", 0777)
    18  
    19  	readAndCheckDockerfile(t, "emptyDockefile", contextDir, "", "The Dockerfile (Dockerfile) cannot be empty")
    20  }
    21  
    22  func TestSymlinkDockerfile(t *testing.T) {
    23  	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
    24  	defer cleanup()
    25  
    26  	createTestSymlink(t, contextDir, builder.DefaultDockerfileName, "/etc/passwd")
    27  
    28  	// The reason the error is "Cannot locate specified Dockerfile" is because
    29  	// in the builder, the symlink is resolved within the context, therefore
    30  	// Dockerfile -> /etc/passwd becomes etc/passwd from the context which is
    31  	// a nonexistent file.
    32  	expectedError := fmt.Sprintf("Cannot locate specified Dockerfile: %s", builder.DefaultDockerfileName)
    33  
    34  	readAndCheckDockerfile(t, "symlinkDockerfile", contextDir, builder.DefaultDockerfileName, expectedError)
    35  }
    36  
    37  func TestDockerfileOutsideTheBuildContext(t *testing.T) {
    38  	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
    39  	defer cleanup()
    40  
    41  	expectedError := "Forbidden path outside the build context"
    42  
    43  	readAndCheckDockerfile(t, "DockerfileOutsideTheBuildContext", contextDir, "../../Dockerfile", expectedError)
    44  }
    45  
    46  func TestNonExistingDockerfile(t *testing.T) {
    47  	contextDir, cleanup := createTestTempDir(t, "", "builder-dockerfile-test")
    48  	defer cleanup()
    49  
    50  	expectedError := "Cannot locate specified Dockerfile: Dockerfile"
    51  
    52  	readAndCheckDockerfile(t, "NonExistingDockerfile", contextDir, "Dockerfile", expectedError)
    53  }
    54  
    55  func readAndCheckDockerfile(t *testing.T, testName, contextDir, dockerfilePath, expectedError string) {
    56  	tarStream, err := archive.Tar(contextDir, archive.Uncompressed)
    57  
    58  	if err != nil {
    59  		t.Fatalf("Error when creating tar stream: %s", err)
    60  	}
    61  
    62  	defer func() {
    63  		if err = tarStream.Close(); err != nil {
    64  			t.Fatalf("Error when closing tar stream: %s", err)
    65  		}
    66  	}()
    67  
    68  	context, err := builder.MakeTarSumContext(tarStream)
    69  
    70  	if err != nil {
    71  		t.Fatalf("Error when creating tar context: %s", err)
    72  	}
    73  
    74  	defer func() {
    75  		if err = context.Close(); err != nil {
    76  			t.Fatalf("Error when closing tar context: %s", err)
    77  		}
    78  	}()
    79  
    80  	options := &types.ImageBuildOptions{
    81  		Dockerfile: dockerfilePath,
    82  	}
    83  
    84  	b := &Builder{options: options, context: context}
    85  
    86  	err = b.readDockerfile()
    87  
    88  	if err == nil {
    89  		t.Fatalf("No error when executing test: %s", testName)
    90  	}
    91  
    92  	if !strings.Contains(err.Error(), expectedError) {
    93  		t.Fatalf("Wrong error message. Should be \"%s\". Got \"%s\"", expectedError, err.Error())
    94  	}
    95  }