| 1 |
/* |
| 2 |
Copyright The containerd Authors. |
| 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 |
/* |
| 18 |
Copyright 2019 The Go Authors. All rights reserved. |
| 19 |
Use of this source code is governed by a BSD-style |
| 20 |
license that can be found in the NOTICE.md file. |
| 21 |
*/ |
| 22 |
|
| 23 |
package fs |
| 24 |
|
| 25 |
import ( |
| 26 |
"context" |
| 27 |
"fmt" |
| 28 |
"testing" |
| 29 |
"time" |
| 30 |
|
| 31 |
"github.com/containerd/containerd/reference" |
| 32 |
"github.com/containerd/containerd/remotes/docker" |
| 33 |
"github.com/containerd/stargz-snapshotter/fs/layer" |
| 34 |
"github.com/containerd/stargz-snapshotter/fs/remote" |
| 35 |
"github.com/containerd/stargz-snapshotter/fs/source" |
| 36 |
"github.com/containerd/stargz-snapshotter/task" |
| 37 |
fusefs "github.com/hanwen/go-fuse/v2/fs" |
| 38 |
digest "github.com/opencontainers/go-digest" |
| 39 |
ocispec "github.com/opencontainers/image-spec/specs-go/v1" |
| 40 |
) |
| 41 |
|
| 42 |
func TestCheck(t *testing.T) { |
| 43 |
bl := &breakableLayer{} |
| 44 |
fs := &filesystem{ |
| 45 |
layer: map[string]layer.Layer{ |
| 46 |
"test": bl, |
| 47 |
}, |
| 48 |
backgroundTaskManager: task.NewBackgroundTaskManager(1, time.Millisecond), |
| 49 |
getSources: source.FromDefaultLabels(func(refspec reference.Spec) (hosts []docker.RegistryHost, _ error) { |
| 50 |
return docker.ConfigureDefaultRegistries(docker.WithPlainHTTP(docker.MatchLocalhost))(refspec.Hostname()) |
| 51 |
}), |
| 52 |
} |
| 53 |
bl.success = true |
| 54 |
if err := fs.Check(context.TODO(), "test", nil); err != nil { |
| 55 |
t.Errorf("connection failed; wanted to succeed: %v", err) |
| 56 |
} |
| 57 |
|
| 58 |
bl.success = false |
| 59 |
if err := fs.Check(context.TODO(), "test", nil); err == nil { |
| 60 |
t.Errorf("connection succeeded; wanted to fail") |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
type breakableLayer struct { |
| 65 |
success bool |
| 66 |
} |
| 67 |
|
| 68 |
func (l *breakableLayer) Info() layer.Info { return layer.Info{} } |
| 69 |
func (l *breakableLayer) RootNode(uint32) (fusefs.InodeEmbedder, error) { return nil, nil } |
| 70 |
func (l *breakableLayer) Verify(tocDigest digest.Digest) error { return nil } |
| 71 |
func (l *breakableLayer) SkipVerify() {} |
| 72 |
func (l *breakableLayer) Prefetch(prefetchSize int64) error { return fmt.Errorf("fail") } |
| 73 |
func (l *breakableLayer) ReadAt([]byte, int64, ...remote.Option) (int, error) { return 0, nil } |
| 74 |
func (l *breakableLayer) WaitForPrefetchCompletion() error { return fmt.Errorf("fail") } |
| 75 |
func (l *breakableLayer) BackgroundFetch() error { return fmt.Errorf("fail") } |
| 76 |
func (l *breakableLayer) Check() error { |
| 77 |
if !l.success { |
| 78 |
return fmt.Errorf("failed") |
| 79 |
} |
| 80 |
return nil |
| 81 |
} |
| 82 |
func (l *breakableLayer) Refresh(ctx context.Context, hosts source.RegistryHosts, refspec reference.Spec, desc ocispec.Descriptor) error { |
| 83 |
if !l.success { |
| 84 |
return fmt.Errorf("failed") |
| 85 |
} |
| 86 |
return nil |
| 87 |
} |
| 88 |
func (l *breakableLayer) Done() {} |