github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/test/integration/camget_test.go (about)

     1  /*
     2  Copyright 2013 Google Inc.
     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  	"io/ioutil"
    21  	"os"
    22  	"path/filepath"
    23  	"strings"
    24  	"testing"
    25  
    26  	"camlistore.org/pkg/test"
    27  	"camlistore.org/pkg/test/asserts"
    28  )
    29  
    30  // Test that `camget -o' can restore a symlink correctly.
    31  func TestCamgetSymlink(t *testing.T) {
    32  	w := test.GetWorld(t)
    33  
    34  	srcDir, err := ioutil.TempDir("", "camget-test-")
    35  	if err != nil {
    36  		t.Fatalf("ioutil.TempDir(): %v", err)
    37  	}
    38  	defer os.RemoveAll(srcDir)
    39  
    40  	targetBase := "a"
    41  	target := filepath.Join(srcDir, targetBase)
    42  	targetFD, err := os.Create(target)
    43  	if err != nil {
    44  		t.Fatalf("os.Create(): %v", err)
    45  	}
    46  	targetFD.Close()
    47  
    48  	subdirBase := "child"
    49  	subdirName := filepath.Join(srcDir, subdirBase)
    50  	linkBase := "b"
    51  	linkName := filepath.Join(subdirName, linkBase)
    52  	err = os.Mkdir(subdirName, 0777)
    53  	if err != nil {
    54  		t.Fatalf("os.Mkdir(): %v", err)
    55  	}
    56  
    57  	err = os.Symlink("../"+targetBase, linkName)
    58  	if err != nil {
    59  		t.Fatalf("os.Symlink(): %v", err)
    60  	}
    61  
    62  	out := test.MustRunCmd(t, w.Cmd("camput", "file", srcDir))
    63  	asserts.ExpectBool(t, true, out != "", "camput")
    64  	br := strings.Split(out, "\n")[0]
    65  	dstDir, err := ioutil.TempDir("", "camget-test-")
    66  	if err != nil {
    67  		t.Fatalf("ioutil.TempDir(): %v", err)
    68  	}
    69  	defer os.RemoveAll(dstDir)
    70  
    71  	// Now restore the symlink
    72  	_ = test.MustRunCmd(t, w.Cmd("camget", "-o", dstDir, br))
    73  
    74  	symlink := filepath.Join(dstDir, filepath.Base(srcDir), subdirBase,
    75  		linkBase)
    76  	link, err := os.Readlink(symlink)
    77  	if err != nil {
    78  		t.Fatalf("os.Readlink(): %v", err)
    79  	}
    80  	expected := "../a"
    81  	if expected != link {
    82  		t.Fatalf("os.Readlink(): Expected: %s, got %s", expected,
    83  			link)
    84  	}
    85  
    86  	// Ensure that the link is not broken
    87  	_, err = os.Stat(symlink)
    88  	if err != nil {
    89  		t.Fatalf("os.Stat(): %v", err)
    90  	}
    91  }