提交 1b16712b 作者: Henry

assets: seed gateway index assets on 'ipfs init'

License: MIT
Signed-off-by: 's avatarHenry <cryptix@riseup.net>

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

License: MIT
Signed-off-by: 's avatarHenry <cryptix@riseup.net>
上级 358d04c6
......@@ -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
......
......@@ -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)
}
}
This source diff could not be displayed because it is too large. You can view the blob instead.
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -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)
......
......@@ -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"
......@@ -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
......@@ -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
......@@ -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
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论