File: fake_plugingetter.go

package info (click to toggle)
docker.io 26.1.5%2Bdfsg1-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 68,576 kB
  • sloc: sh: 5,748; makefile: 912; ansic: 664; asm: 228; python: 162
file content (72 lines) | stat: -rw-r--r-- 1,701 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package testutils

import (
	"fmt"
	"net"

	"github.com/moby/swarmkit/v2/node/plugin"
)

const DockerCSIPluginNodeCap = "csinode"
const DockerCSIPluginControllerCap = "csicontroller"

type FakePluginGetter struct {
	Plugins map[string]*FakePlugin
}

var _ plugin.Getter = &FakePluginGetter{}

func (f *FakePluginGetter) Get(name, capability string) (plugin.Plugin, error) {
	if capability != DockerCSIPluginNodeCap && capability != DockerCSIPluginControllerCap {
		return nil, fmt.Errorf(
			"requested plugin with %s cap, but should only ever request %s or %s",
			capability, DockerCSIPluginNodeCap, DockerCSIPluginControllerCap,
		)
	}

	if plug, ok := f.Plugins[name]; ok {
		return plug, nil
	}
	return nil, fmt.Errorf("plugin %s not found", name)
}

// GetAllManagedPluginsByCap returns all of the fake's plugins. If capability
// is anything other than DockerCSIPluginCap, it returns nothing.
func (f *FakePluginGetter) GetAllManagedPluginsByCap(capability string) []plugin.Plugin {
	if capability != DockerCSIPluginNodeCap && capability != DockerCSIPluginControllerCap {
		return nil
	}

	allPlugins := make([]plugin.Plugin, 0, len(f.Plugins))
	for _, plug := range f.Plugins {
		allPlugins = append(allPlugins, plug)
	}
	return allPlugins
}

type FakePlugin struct {
	PluginName string
	PluginAddr net.Addr
	Scope      string
}

var _ plugin.AddrPlugin = &FakePlugin{}

func (f *FakePlugin) Name() string {
	return f.PluginName
}

func (f *FakePlugin) ScopedPath(path string) string {
	if f.Scope != "" {
		return fmt.Sprintf("%s/%s", f.Scope, path)
	}
	return path
}

func (f *FakePlugin) Client() plugin.Client {
	return nil
}

func (f *FakePlugin) Addr() net.Addr {
	return f.PluginAddr
}