diff --git a/docs/special-capabilities.adoc b/docs/special-capabilities.adoc index 4798a033..2d6c0341 100644 --- a/docs/special-capabilities.adoc +++ b/docs/special-capabilities.adoc @@ -139,9 +139,9 @@ Thus entries from capabilities override entries from configuration file if some In big clusters you may want to pass additional metadata to every browser session: environment, VCS revision, build number and so on. These labels can be then used to enrich session logs and send them to a centralized log storage. Later this metadata can be used for more efficient search through logs. -.Type: array, format: = +.Type: map, format: "": "" ---- -labels: ["environment=testing", "build-number=14353"] +labels: {"environment": "testing", "build-number": "14353"] ---- Labels from this capability override labels from browsers configuration file. When `name` capability is specified - it is automatically added as a label to container. diff --git a/service/docker.go b/service/docker.go index 46516e89..86053b0e 100644 --- a/service/docker.go +++ b/service/docker.go @@ -22,8 +22,6 @@ import ( ) const ( - comma = "," - equality = "=" sysAdmin = "SYS_ADMIN" overrideVideoOutputDir = "OVERRIDE_VIDEO_OUTPUT_DIR" vncPort = "5900" @@ -201,7 +199,11 @@ func getLogConfig(logConfig ctr.LogConfig, caps session.Caps) ctr.LogConfig { } _, ok = logConfig.Config[labels] if len(caps.Labels) > 0 && !ok { - logConfig.Config[labels] = strings.Join(caps.Labels, comma) + joinedLabels := []string{} + for k, v := range caps.Labels { + joinedLabels = append(joinedLabels, fmt.Sprintf("%s=%s", k, v)) + } + logConfig.Config[labels] = strings.Join(joinedLabels, ",") } } return logConfig @@ -262,15 +264,8 @@ func getLabels(service *config.Browser, caps session.Caps) map[string]string { labels[k] = v } if len(caps.Labels) > 0 { - for _, lbl := range caps.Labels { - kv := strings.SplitN(lbl, equality, 2) - if len(kv) == 2 { - key := kv[0] - value := kv[1] - labels[key] = value - } else { - labels[lbl] = "" - } + for k, v := range caps.Labels { + labels[k] = v } } return labels diff --git a/service_test.go b/service_test.go index a360b0f4..155fe5ab 100644 --- a/service_test.go +++ b/service_test.go @@ -1,13 +1,18 @@ package main import ( + "bytes" "fmt" . "github.com/aandryashin/matchers" "github.com/aerokube/selenoid/config" "github.com/aerokube/selenoid/service" "github.com/aerokube/selenoid/session" + "github.com/aerokube/util" "github.com/docker/docker/api/types/container" "github.com/docker/docker/client" + "golang.org/x/net/websocket" + "io" + "net" "net/http" "net/http/httptest" "net/url" @@ -15,11 +20,6 @@ import ( "sync" "testing" "time" - "golang.org/x/net/websocket" - "github.com/aerokube/util" - "bytes" - "net" - "io" ) var ( @@ -264,7 +264,7 @@ func createDockerStarter(t *testing.T, env *service.Environment, cfg *config.Con VideoFrameRate: 25, Env: []string{"LANG=ru_RU.UTF-8", "LANGUAGE=ru:en"}, HostsEntries: []string{"example.com:192.168.0.1", "test.com:192.168.0.2"}, - Labels: []string{"label1=some-value", "label2"}, + Labels: map[string]string{"label1": "some-value", "label2": ""}, ApplicationContainers: []string{"one", "two"}, TimeZone: "Europe/Moscow", ContainerHostname: "some-hostname", @@ -327,8 +327,8 @@ func TestGetVNC(t *testing.T) { srv := httptest.NewServer(handler()) defer srv.Close() - - testTcpServer := testTCPServer("test-data") + + testTcpServer := testTCPServer("test-data") sessions.Put("test-session", &session.Session{ VNC: testTcpServer.Addr().String(), }) @@ -354,7 +354,7 @@ func testTCPServer(data string) net.Listener { return l } -func readDataFromWebSocket(t * testing.T, wsURL string) string { +func readDataFromWebSocket(t *testing.T, wsURL string) string { ws, err := websocket.Dial(wsURL, "", "http://localhost") AssertThat(t, err, Is{nil}) @@ -369,10 +369,10 @@ func TestGetLogs(t *testing.T) { srv := httptest.NewServer(handler()) defer srv.Close() - + sessions.Put("test-session", &session.Session{ Container: &session.Container{ - ID: "e90e34656806", + ID: "e90e34656806", IPAddress: "127.0.0.1", }, }) diff --git a/session/session.go b/session/session.go index d53f9cf5..672a9340 100644 --- a/session/session.go +++ b/session/session.go @@ -23,7 +23,7 @@ type Caps struct { Env []string `json:"env"` ApplicationContainers []string `json:"applicationContainers"` HostsEntries []string `json:"hostsEntries"` - Labels []string `json:"labels"` + Labels map[string]string `json:"labels"` ExtensionCapabilities map[string]interface{} `json:"selenoid:options"` }