github.com/keltia/go-ipfs@v0.3.8-0.20150909044612-210793031c63/test/sharness/t0081-repo-pinning.sh (about)

     1  #!/bin/sh
     2  #
     3  # Copyright (c) 2014 Jeromy Johnson
     4  # MIT Licensed; see the LICENSE file in this repository.
     5  #
     6  
     7  test_description="Test ipfs repo pinning"
     8  
     9  . lib/test-lib.sh
    10  
    11  
    12  
    13  test_pin_flag() {
    14  	object=$1
    15  	ptype=$2
    16  	expect=$3
    17  
    18  	echo "test_pin_flag" $@
    19  
    20  	ipfs-pin-stat "$object" | grep "$ptype"
    21  	actual=$?
    22  
    23  	if [ "$expect" = "true" ]; then
    24  		if [ "$actual" != "0" ]; then
    25  			echo "$object should be pinned $ptype ($actual)"
    26  			return 1
    27  		fi
    28  	else
    29  		if [ "$actual" != "1" ]; then
    30  			echo "$object should NOT be pinned $ptype ($actual)"
    31  			return 1
    32  		fi
    33  	fi
    34  	return 0
    35  }
    36  
    37  test_pin() {
    38  	object=$1
    39  	shift
    40  
    41  	test_str_contains "recursive" $@
    42  	[ "$?" = "0" ] && r="true" || r="false"
    43  
    44  	test_str_contains "indirect" $@
    45  	[ "$?" = "0" ] && i="true" || i="false"
    46  
    47  	test_str_contains "direct" $@
    48  	[ "$?" = "0" ] && d="true" || d="false"
    49  
    50  	test_pin_flag "$object" "recursive" $r || return 1
    51  	test_pin_flag "$object" "indirect"  $i || return 1
    52  	test_pin_flag "$object" "direct"    $d || return 1
    53  	return 0
    54  }
    55  
    56  
    57  test_init_ipfs
    58  
    59  # test runs much faster without daemon.
    60  # TODO: turn this back on after:
    61  # https://github.com/ipfs/go-ipfs/issues/1075
    62  # test_launch_ipfs_daemon
    63  
    64  HASH_FILE6="QmRsBC3Y2G6VRPYGAVpZczx1W7Xw54MtM1NcLKTkn6rx3U"
    65  HASH_FILE5="QmaN3PtyP8DcVGHi3Q2Fcp7CfAFVcVXKddWbHoNvaA41zf"
    66  HASH_FILE4="QmV1aiVgpDknKQugrK59uBUbMrPnsQM1F9FXbFcfgEvUvH"
    67  HASH_FILE3="QmZrr4Pzqp3NnMzMfbMhNe7LghfoUFHVx7c9Po9GZrhKZ7"
    68  HASH_FILE2="QmSkjTornLY72QhmK9NvAz26815pTaoAL42rF8Qi3w2WBP"
    69  HASH_FILE1="QmbgX4aXhSSY88GHmPQ4roizD8wFwPX8jzTLjc8VAp89x4"
    70  HASH_DIR4="QmW98gV71Ns4bX7QbgWAqLiGF3SDC1JpveZSgBh4ExaSAd"
    71  HASH_DIR3="QmRsCaNBMkweZ9vHT5PJRd2TT9rtNKEKyuognCEVxZxF1H"
    72  HASH_DIR2="QmTUTQAgeVfughDSFukMZLbfGvetDJY7Ef5cDXkKK4abKC"
    73  HASH_DIR1="QmNyZVFbgvmzguS2jVMRb8PQMNcCMJrn9E3doDhBbcPNTY"
    74  
    75  DIR1="dir1"
    76  DIR2="dir1/dir2"
    77  DIR4="dir1/dir2/dir4"
    78  DIR3="dir1/dir3"
    79  FILE1="dir1/file1"
    80  FILE2="dir1/file2"
    81  FILE3="dir1/file3"
    82  FILE4="dir1/dir2/file4"
    83  FILE6="dir1/dir2/dir4/file6"
    84  FILE5="dir1/dir3/file5"
    85  
    86  test_expect_success "'ipfs add dir' succeeds" '
    87  	mkdir dir1 &&
    88  	mkdir dir1/dir2 &&
    89  	mkdir dir1/dir2/dir4 &&
    90  	mkdir dir1/dir3 &&
    91  	echo "some text 1" >dir1/file1 &&
    92  	echo "some text 2" >dir1/file2 &&
    93  	echo "some text 3" >dir1/file3 &&
    94  	echo "some text 1" >dir1/dir2/file1 &&
    95  	echo "some text 4" >dir1/dir2/file4 &&
    96  	echo "some text 1" >dir1/dir2/dir4/file1 &&
    97  	echo "some text 2" >dir1/dir2/dir4/file2 &&
    98  	echo "some text 6" >dir1/dir2/dir4/file6 &&
    99  	echo "some text 2" >dir1/dir3/file2 &&
   100  	echo "some text 5" >dir1/dir3/file5 &&
   101  	ipfs add -q -r dir1 | tail -n1 >actual1 &&
   102  	echo "$HASH_DIR1" >expected1 &&
   103  	ipfs repo gc && # remove the patch chaff
   104  	test_cmp actual1 expected1
   105  '
   106  
   107  test_expect_success "objects are there" '
   108  	ipfs cat "$HASH_FILE6" >FILE6_a &&
   109  	ipfs cat "$HASH_FILE5" >FILE5_a &&
   110  	ipfs cat "$HASH_FILE4" >FILE4_a &&
   111  	ipfs cat "$HASH_FILE3" >FILE3_a &&
   112  	ipfs cat "$HASH_FILE2" >FILE2_a &&
   113  	ipfs cat "$HASH_FILE1" >FILE1_a &&
   114  	ipfs ls "$HASH_DIR3"   >DIR3_a &&
   115  	ipfs ls "$HASH_DIR4"   >DIR4_a &&
   116  	ipfs ls "$HASH_DIR2"   >DIR2_a &&
   117  	ipfs ls "$HASH_DIR1"   >DIR1_a
   118  '
   119  
   120  # saving this output for later
   121  test_expect_success "ipfs object links $HASH_DIR1 works" '
   122  	ipfs object links $HASH_DIR1 > DIR1_objlink
   123  '
   124  
   125  
   126  test_expect_success "added dir was pinned recursively" '
   127  	test_pin_flag $HASH_DIR1 recursive true
   128  '
   129  
   130  test_expect_success "rest were pinned indirectly" '
   131  	test_pin_flag "$HASH_FILE6" indirect true
   132  	test_pin_flag "$HASH_FILE5" indirect true
   133  	test_pin_flag "$HASH_FILE4" indirect true
   134  	test_pin_flag "$HASH_FILE3" indirect true
   135  	test_pin_flag "$HASH_FILE2" indirect true
   136  	test_pin_flag "$HASH_FILE1" indirect true
   137  	test_pin_flag "$HASH_DIR3" indirect true
   138  	test_pin_flag "$HASH_DIR4" indirect true
   139  	test_pin_flag "$HASH_DIR2" indirect true
   140  '
   141  
   142  test_expect_success "added dir was NOT pinned indirectly" '
   143  	test_pin_flag "$HASH_DIR1" indirect false
   144  '
   145  
   146  test_expect_success "nothing is pinned directly" '
   147  	ipfs pin ls --type=direct >actual4 &&
   148  	test_must_be_empty actual4
   149  '
   150  
   151  test_expect_success "'ipfs repo gc' succeeds" '
   152  	ipfs repo gc >gc_out_actual &&
   153  	test_must_be_empty gc_out_actual
   154  '
   155  
   156  test_expect_success "objects are still there" '
   157  	cat FILE6_a FILE5_a FILE4_a FILE3_a FILE2_a FILE1_a >expected45 &&
   158  	cat DIR3_a DIR4_a DIR2_a DIR1_a >>expected45 &&
   159  	ipfs cat "$HASH_FILE6"  >actual45 &&
   160  	ipfs cat "$HASH_FILE5" >>actual45 &&
   161  	ipfs cat "$HASH_FILE4" >>actual45 &&
   162  	ipfs cat "$HASH_FILE3" >>actual45 &&
   163  	ipfs cat "$HASH_FILE2" >>actual45 &&
   164  	ipfs cat "$HASH_FILE1" >>actual45 &&
   165  	ipfs ls "$HASH_DIR3"   >>actual45 &&
   166  	ipfs ls "$HASH_DIR4"   >>actual45 &&
   167  	ipfs ls "$HASH_DIR2"   >>actual45 &&
   168  	ipfs ls "$HASH_DIR1"   >>actual45 &&
   169  	test_cmp expected45 actual45
   170  '
   171  
   172  test_expect_success "remove dir recursive pin succeeds" '
   173  	echo "unpinned $HASH_DIR1" >expected5 &&
   174  	ipfs pin rm -r "$HASH_DIR1" >actual5 &&
   175  	test_cmp expected5 actual5
   176  '
   177  
   178  test_expect_success "none are pinned any more" '
   179  	test_pin "$HASH_FILE6" &&
   180  	test_pin "$HASH_FILE5" &&
   181  	test_pin "$HASH_FILE4" &&
   182  	test_pin "$HASH_FILE3" &&
   183  	test_pin "$HASH_FILE2" &&
   184  	test_pin "$HASH_FILE1" &&
   185  	test_pin "$HASH_DIR3"  &&
   186  	test_pin "$HASH_DIR4"  &&
   187  	test_pin "$HASH_DIR2"  &&
   188  	test_pin "$HASH_DIR1"
   189  '
   190  
   191  test_expect_success "pin some directly and indirectly" '
   192  	ipfs pin add    "$HASH_DIR1"  >actual7 &&
   193  	ipfs pin add -r "$HASH_DIR2"  >>actual7 &&
   194  	ipfs pin add    "$HASH_FILE1" >>actual7 &&
   195  	echo "pinned $HASH_DIR1 directly"	   >expected7 &&
   196  	echo "pinned $HASH_DIR2 recursively" >>expected7 &&
   197  	echo "pinned $HASH_FILE1 directly"	 >>expected7 &&
   198  	test_cmp expected7 actual7
   199  '
   200  
   201  test_expect_success "pin lists look good" '
   202  	test_pin $HASH_DIR1  direct &&
   203  	test_pin $HASH_DIR2  recursive &&
   204  	test_pin $HASH_DIR3  &&
   205  	test_pin $HASH_DIR4  indirect &&
   206  	test_pin $HASH_FILE1 indirect direct &&
   207  	test_pin $HASH_FILE2 indirect &&
   208  	test_pin $HASH_FILE3 &&
   209  	test_pin $HASH_FILE4 indirect &&
   210  	test_pin $HASH_FILE5 &&
   211  	test_pin $HASH_FILE6 indirect
   212  '
   213  
   214  test_expect_success "'ipfs repo gc' succeeds" '
   215  	ipfs repo gc >gc_out_actual2 &&
   216  	echo "removed $HASH_FILE3" > gc_out_exp2 &&
   217  	echo "removed $HASH_FILE5" >> gc_out_exp2 &&
   218  	echo "removed $HASH_DIR3" >> gc_out_exp2 &&
   219  	test_sort_cmp gc_out_actual2 gc_out_exp2
   220  '
   221  
   222  # use object links for HASH_DIR1 here because its children
   223  # no longer exist
   224  test_expect_success "some objects are still there" '
   225  	cat FILE6_a FILE4_a FILE2_a FILE1_a >expected8 &&
   226  	cat DIR4_a DIR2_a DIR1_objlink >>expected8 &&
   227  	ipfs cat "$HASH_FILE6"  >actual8 &&
   228  	ipfs cat "$HASH_FILE4" >>actual8 &&
   229  	ipfs cat "$HASH_FILE2" >>actual8 &&
   230  	ipfs cat "$HASH_FILE1" >>actual8 &&
   231  	ipfs ls "$HASH_DIR4"   >>actual8 &&
   232  	ipfs ls "$HASH_DIR2"   >>actual8 &&
   233  	ipfs object links "$HASH_DIR1" >>actual8 &&
   234  	test_cmp actual8 expected8
   235  '
   236  
   237  # todo: make this faster somehow.
   238  test_expect_success "some are no longer there" '
   239  	test_must_fail ipfs cat "$HASH_FILE5" &&
   240  	test_must_fail ipfs cat "$HASH_FILE3" &&
   241  	test_must_fail ipfs ls "$HASH_DIR3"
   242  '
   243  
   244  test_expect_success "recursive pin fails without objects" '
   245  	ipfs pin rm "$HASH_DIR1" &&
   246  	test_must_fail ipfs pin add -r "$HASH_DIR1" --timeout=500ms 2>err_expected8 &&
   247  	grep "context deadline exceeded" err_expected8 ||
   248  	test_fsh cat err_expected8
   249  '
   250  
   251  # test_kill_ipfs_daemon
   252  
   253  test_done