Skip to content

Commit

Permalink
assets: seed gateway index assets on 'ipfs init'
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Henry <cryptix@riseup.net>

t0080-repo.sh: added gateway assets to pinning tests

License: MIT
Signed-off-by: Henry <cryptix@riseup.net>
  • Loading branch information
cryptix committed Jul 3, 2015
1 parent 358d04c commit 1b16712
Show file tree
Hide file tree
Showing 8 changed files with 503 additions and 8 deletions.
29 changes: 22 additions & 7 deletions assets/assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,54 @@ var initDocPaths = []string{
"init-doc/quick-start",
}

// SeedInitDocs adds the list of embedded init documentation to the passed node, pins it and returns the root key
func SeedInitDocs(nd *core.IpfsNode) (*key.Key, error) {
return addAssetList(nd, initDocPaths)
}

var initGwAssets = []string{
"gw-assets/icons.css",
"gw-assets/bootstrap.min.css",
}

// SeedGatewayAssets adds the list of embedded gateway inidex assets to the passed node, pins it and returns the root key
func SeedGatewayAssets(nd *core.IpfsNode) (*key.Key, error) {
return addAssetList(nd, initGwAssets)
}

func addAssetList(nd *core.IpfsNode, l []string) (*key.Key, error) {
dirb := uio.NewDirectory(nd.DAG)

for _, p := range initDocPaths {
for _, p := range l {
d, err := Asset(p)
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could load Asset '%s': %s", p, err)
return nil, fmt.Errorf("assets: could load Asset '%s': %s", p, err)
}

s, err := coreunix.Add(nd, bytes.NewBuffer(d))
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could not Add '%s': %s", p, err)
return nil, fmt.Errorf("assets: could not Add '%s': %s", p, err)
}

fname := filepath.Base(p)
k := key.B58KeyDecode(s)
if err := dirb.AddChild(fname, k); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: could not add '%s' as a child: %s", fname, err)
return nil, fmt.Errorf("assets: could not add '%s' as a child: %s", fname, err)
}
}

dir := dirb.GetNode()
dkey, err := nd.DAG.Add(dir)
if err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: DAG.Add(dir) failed: %s", err)
return nil, fmt.Errorf("assets: DAG.Add(dir) failed: %s", err)
}

if err := nd.Pinning.Pin(nd.Context(), dir, true); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: Pinning on init-docu failed: %s", err)
return nil, fmt.Errorf("assets: Pinning on init-docu failed: %s", err)
}

if err := nd.Pinning.Flush(); err != nil {
return nil, fmt.Errorf("assets.AddDocuDir: Pinnig flush failed: %s", err)
return nil, fmt.Errorf("assets: Pinnig flush failed: %s", err)
}

return &dkey, nil
Expand Down
31 changes: 31 additions & 0 deletions assets/assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,34 @@ func TestEmbeddedDocs(t *testing.T) {
}
wg.Wait()
}

func TestGatewayAssets(t *testing.T) {
const wantCnt = 2
if len(initGwAssets) < wantCnt {
t.Fatalf("expected %d assets. got %d", wantCnt, len(initDocPaths))
}

for _, f := range initGwAssets {
// load data from filesystem (git)
vcsData, err := ioutil.ReadFile(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}

// load data from emdedded source
embdData, err := Asset(f)
if err != nil {
t.Errorf("asset %s: could not read vcs file: %s", f, err)
return
}

if !bytes.Equal(vcsData, embdData) {
t.Errorf("asset %s: vcs and embedded data isnt equal", f)
return
}

t.Logf("checked %s", f)
}

}
48 changes: 48 additions & 0 deletions assets/bindata.go

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions assets/gw-assets/bootstrap.min.css

Large diffs are not rendered by default.

384 changes: 384 additions & 0 deletions assets/gw-assets/icons.css

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,12 @@ func addDefaultAssets(out io.Writer, repoRoot string) error {
}
defer nd.Close()

gwAkey, err := assets.SeedGatewayAssets(nd)
if err != nil {
return fmt.Errorf("init: seeding init docs failed: %s", err)
}
log.Debugf("init: seeded gateway assets %s", gwAkey)

dkey, err := assets.SeedInitDocs(nd)
if err != nil {
return fmt.Errorf("init: seeding init docs failed: %s", err)
Expand Down
1 change: 1 addition & 0 deletions test/sharness/lib/test-lib-hashes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
# thus they can be defined + changed in one place

HASH_WELCOME_DOCS="QmVtU7ths96fMgZ8YSZAbKghyieq7AjxNdcqyVzxTt3qVe"
HASH_GATEWAY_ASSETS="QmXB7PLRWH6bCiwrGh2MrBBjNkLv3mY3JdYXCikYZSwLED"
HASH_HELP_PAGE="QmY5heUM5qgRubMDD1og9fhCPA6QdkMp3QCwd4s7gJsyE7"
HASH_EMPTY_DIR="QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
7 changes: 6 additions & 1 deletion test/sharness/t0080-repo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ test_expect_success "'ipfs pin rm' output looks good" '
'

test_expect_success "file no longer pinned" '
# we expect the welcome files to show up here
# we expect the welcome files and gw assets to show up here
echo "$HASH_WELCOME_DOCS" >expected2 &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>expected2 &&
echo "$HASH_GATEWAY_ASSETS" >>expected2 &&
ipfs refs -r "$HASH_GATEWAY_ASSETS" >>expected2 &&
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >> expected2 &&
ipfs pin ls --type=recursive --quiet >actual2 &&
test_sort_cmp expected2 actual2
Expand Down Expand Up @@ -105,6 +107,7 @@ test_expect_success "adding multiblock random file succeeds" '
test_expect_success "'ipfs pin ls --type=indirect' is correct" '
ipfs refs "$MBLOCKHASH" >refsout &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>refsout &&
ipfs refs -r "$HASH_GATEWAY_ASSETS" >>refsout &&
sed -i="" "s/\(.*\)/\1 indirect/g" refsout &&
ipfs pin ls --type=indirect >indirectpins &&
test_sort_cmp refsout indirectpins
Expand All @@ -131,8 +134,10 @@ test_expect_success "'ipfs pin ls --type=direct' is correct" '
test_expect_success "'ipfs pin ls --type=recursive' is correct" '
echo "$MBLOCKHASH" >rp_expected &&
echo "$HASH_WELCOME_DOCS" >>rp_expected &&
echo "$HASH_GATEWAY_ASSETS" >>rp_expected &&
echo QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn >>rp_expected &&
ipfs refs -r "$HASH_WELCOME_DOCS" >>rp_expected &&
ipfs refs -r "$HASH_GATEWAY_ASSETS" >>rp_expected &&
sed -i="" "s/\(.*\)/\1 recursive/g" rp_expected &&
ipfs pin ls --type=recursive >rp_actual &&
test_sort_cmp rp_expected rp_actual
Expand Down

0 comments on commit 1b16712

Please sign in to comment.