gopkg.in/docker/libcompose.v0@v0.4.0/lookup/file_test.go (about)

     1  package lookup
     2  
     3  import (
     4  	"fmt"
     5  	"io/ioutil"
     6  	"os"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  type input struct {
    12  	file       string
    13  	relativeTo string
    14  }
    15  
    16  func TestLookupError(t *testing.T) {
    17  	abs, err := filepath.Abs(".")
    18  	if err != nil {
    19  		t.Fatalf("Failed to get absolute directory: %s", err)
    20  	}
    21  	invalids := map[input]string{
    22  		input{"", ""}:                             fmt.Sprintf("read %s: is a directory", abs),
    23  		input{"", "/tmp/"}:                        "read /tmp: is a directory",
    24  		input{"file", "/does/not/exists/"}:        "open /does/not/exists/file: no such file or directory",
    25  		input{"file", "/does/not/something"}:      "open /does/not/file: no such file or directory",
    26  		input{"file", "/does/not/exists/another"}: "open /does/not/exists/file: no such file or directory",
    27  		input{"/does/not/exists/file", "/tmp/"}:   "open /does/not/exists/file: no such file or directory",
    28  		input{"does/not/exists/file", "/tmp/"}:    "open /tmp/does/not/exists/file: no such file or directory",
    29  	}
    30  
    31  	fileConfigLookup := FileResourceLookup{}
    32  
    33  	for invalid, expectedError := range invalids {
    34  		_, _, err := fileConfigLookup.Lookup(invalid.file, invalid.relativeTo)
    35  		if err == nil || err.Error() != expectedError {
    36  			t.Fatalf("Expected error with '%s', got '%v'", expectedError, err)
    37  		}
    38  	}
    39  }
    40  
    41  func TestLookupOK(t *testing.T) {
    42  	tmpFolder, err := ioutil.TempDir("", "lookup-tests")
    43  	if err != nil {
    44  		t.Fatal(err)
    45  	}
    46  	tmpFile1 := filepath.Join(tmpFolder, "file1")
    47  	tmpFile2 := filepath.Join(tmpFolder, "file2")
    48  	if err = ioutil.WriteFile(tmpFile1, []byte("content1"), 0755); err != nil {
    49  		t.Fatal(err)
    50  	}
    51  	if err = ioutil.WriteFile(tmpFile2, []byte("content2"), 0755); err != nil {
    52  		t.Fatal(err)
    53  	}
    54  
    55  	fileConfigLookup := FileResourceLookup{}
    56  
    57  	valids := map[input]string{
    58  		input{"file1", tmpFolder + "/"}:     "content1",
    59  		input{"file2", tmpFolder + "/"}:     "content2",
    60  		input{tmpFile1, tmpFolder}:          "content1",
    61  		input{tmpFile1, "/does/not/exists"}: "content1",
    62  		input{"file2", tmpFile1}:            "content2",
    63  	}
    64  
    65  	for valid, expectedContent := range valids {
    66  		out, _, err := fileConfigLookup.Lookup(valid.file, valid.relativeTo)
    67  		if err != nil || string(out) != expectedContent {
    68  			t.Fatalf("Expected %s to contains '%s', got %s, %v.", valid.file, expectedContent, out, err)
    69  		}
    70  	}
    71  }
    72  
    73  func TestResolvePath(t *testing.T) {
    74  	cwd, err := os.Getwd()
    75  	if err != nil {
    76  		t.Fatal(err)
    77  	}
    78  
    79  	cases := []struct {
    80  		path       string
    81  		relativeTo string
    82  		expected   string
    83  	}{
    84  		{"../path:something", "./docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
    85  		{"../path:something", "docker-compose.yml", filepath.Join(cwd, "../path") + ":something"},
    86  		{"../path:something", "/tmp/docker-compose.yml", "/path:something"},
    87  		{"path:something", "/tmp/docker-compose.yml", "/tmp/path:something"},
    88  		{"/path:something", "/tmp/docker-compose.yml", "/path:something"},
    89  		{"path:something", "-", filepath.Join(cwd, "path") + ":something"},
    90  		{"path/:something", "-", filepath.Join(cwd, "path") + ":something"},
    91  		{"../path:something", "-", filepath.Join(cwd, "../path") + ":something"},
    92  	}
    93  
    94  	fileConfigLookup := FileResourceLookup{}
    95  
    96  	for index, c := range cases {
    97  		actual := fileConfigLookup.ResolvePath(c.path, c.relativeTo)
    98  		if actual != c.expected {
    99  			t.Errorf("Expected %s, got %s for case %d", c.expected, actual, index)
   100  		}
   101  	}
   102  }