mirror of
https://github.com/opencontainers/runc.git
synced 2026-07-12 09:46:25 +08:00
HookState adhears to OCI
Signed-off-by: George Lestaris <glestaris@pivotal.io> Signed-off-by: Ed King <eking@pivotal.io>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
||||
@@ -84,3 +85,18 @@ func CleanPath(path string) string {
|
||||
// Clean the path again for good measure.
|
||||
return filepath.Clean(path)
|
||||
}
|
||||
|
||||
// SearchLabels searches a list of key-value pairs for the provided key and
|
||||
// returns the corresponding value. The pairs must be separated with '='.
|
||||
func SearchLabels(labels []string, query string) string {
|
||||
for _, l := range labels {
|
||||
parts := strings.SplitN(l, "=", 2)
|
||||
if len(parts) < 2 {
|
||||
continue
|
||||
}
|
||||
if parts[0] == query {
|
||||
return parts[1]
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -23,3 +23,24 @@ func TestGenerateName(t *testing.T) {
|
||||
t.Fatalf("expected name to be %d chars but received %d", expected, len(name))
|
||||
}
|
||||
}
|
||||
|
||||
var labelTest = []struct {
|
||||
labels []string
|
||||
query string
|
||||
expectedValue string
|
||||
}{
|
||||
{[]string{"bundle=/path/to/bundle"}, "bundle", "/path/to/bundle"},
|
||||
{[]string{"test=a", "test=b"}, "bundle", ""},
|
||||
{[]string{"bundle=a", "test=b", "bundle=c"}, "bundle", "a"},
|
||||
{[]string{"", "test=a", "bundle=b"}, "bundle", "b"},
|
||||
{[]string{"test", "bundle=a"}, "bundle", "a"},
|
||||
{[]string{"test=a", "bundle="}, "bundle", ""},
|
||||
}
|
||||
|
||||
func TestSearchLabels(t *testing.T) {
|
||||
for _, tt := range labelTest {
|
||||
if v := SearchLabels(tt.labels, tt.query); v != tt.expectedValue {
|
||||
t.Errorf("expected value '%s' for query '%s'; got '%s'", tt.expectedValue, tt.query, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user