Skip to content

Commit

Permalink
Add test coverage for docker compose platform type (#252)
Browse files Browse the repository at this point in the history
Signed-off-by: Tim Callaghan <[email protected]>
Co-authored-by: Yaron Schneider <[email protected]>
  • Loading branch information
timcallaghan and yaron2 authored Jul 22, 2023
1 parent 4c87fc7 commit 298a16a
Show file tree
Hide file tree
Showing 10 changed files with 329 additions and 5 deletions.
1 change: 1 addition & 0 deletions pkg/components/components.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func (c *components) getDockerComposeComponents(scope string) []Component {
err = yaml.Unmarshal(content, &comp)
if err != nil {
log.Printf("Failure unmarshalling %s into Component: %s\n", path, err.Error())
return nil
}

newComponent := Component{
Expand Down
69 changes: 68 additions & 1 deletion pkg/components/components_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,78 @@ Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package components

import (
"fmt"
"testing"

"github.com/dapr/dashboard/pkg/platforms"
"github.com/stretchr/testify/assert"
)

func TestSupported(t *testing.T) {
var scenarios = []struct {
platform platforms.Platform
want bool
}{
{platforms.Kubernetes, true},
{platforms.Standalone, true},
{platforms.DockerCompose, true},
}

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Platform %s should be supported", scenario.platform), func(t *testing.T) {
target := NewComponents(scenario.platform, nil, "")
isSupported := target.Supported()
assert.Equal(t, scenario.want, isSupported)
})
}
}

func TestDockerComposeGetComponents(t *testing.T) {
target := NewComponents(platforms.DockerCompose, nil, "testdata")
components := target.GetComponents("")
assert.Equal(t, 2, len(components), "Should load test data components")
for _, v := range components {
assert.Equal(t, v.Kind, "Component", "Should only load component files")
}
}

func TestDockerComposeGetComponent(t *testing.T) {
var scenarios = []struct {
name string
want bool
}{
{"cronjob", true},
{"messagebus", true},
{"does_not_exist", false},
}

target := NewComponents(platforms.DockerCompose, nil, "testdata")

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Should load valid component - %t", scenario.want), func(t *testing.T) {
component := target.GetComponent("", scenario.name)
assert.NotNil(t, component, "Should always return something")

if scenario.want {
assert.Equal(t, "Component", component.Kind, "Should only return components")
assert.Equal(t, scenario.name, component.Name, "Name should be set")
assert.NotEmpty(t, component.Type, "When component valid, type is set")
} else {
assert.Empty(t, component.Kind, "When component not valid, kind is not set")
assert.Empty(t, component.Type, "When component not valid, type is not set")
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/components/testdata/cron_job.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: cronjob
spec:
type: bindings.cron
version: v1
metadata:
- name: schedule
value: "@every 15m" # valid cron schedule
18 changes: 18 additions & 0 deletions pkg/components/testdata/message_bus.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: messagebus
spec:
type: pubsub.mqtt3
version: v1
metadata:
- name: consumerID
value: "{uuid}"
- name: url
value: "tcp://admin:public@localhost:1883"
- name: qos
value: 1
- name: retain
value: "false"
- name: cleanSession
value: "false"
67 changes: 66 additions & 1 deletion pkg/configurations/configurations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,76 @@ Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package configurations

import (
"fmt"
"testing"

"github.com/dapr/dashboard/pkg/platforms"
"github.com/stretchr/testify/assert"
)

func TestSupported(t *testing.T) {
var scenarios = []struct {
platform platforms.Platform
want bool
}{
{platforms.Kubernetes, true},
{platforms.Standalone, true},
{platforms.DockerCompose, true},
}

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Platform %s should be supported", scenario.platform), func(t *testing.T) {
target := NewConfigurations(scenario.platform, nil, "")
isSupported := target.Supported()
assert.Equal(t, scenario.want, isSupported)
})
}
}

func TestDockerComposeGetConfigurations(t *testing.T) {
target := NewConfigurations(platforms.DockerCompose, nil, "testdata")
configurations := target.GetConfigurations("")
assert.Equal(t, 1, len(configurations), "Should load test data configurations")
for _, v := range configurations {
assert.Equal(t, v.Kind, "Configuration", "Should only load configuration files")
}
}

func TestDockerComposeGetConfiguration(t *testing.T) {
var scenarios = []struct {
name string
want bool
}{
{"tracing", true},
{"does_not_exist", false},
}

target := NewConfigurations(platforms.DockerCompose, nil, "testdata")

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Should load valid configuration - %t", scenario.want), func(t *testing.T) {
config := target.GetConfiguration("", scenario.name)
assert.NotNil(t, config, "Should always return something")

if scenario.want {
assert.Equal(t, "Configuration", config.Kind, "Should only return configurations")
assert.Equal(t, scenario.name, config.Name, "When configuration valid, name is set")
} else {
assert.Empty(t, config.Kind, "When configuration not valid, kind is not set")
assert.Empty(t, config.Name, "When configuration not valid, name is not set")
}
})
}
}
10 changes: 10 additions & 0 deletions pkg/configurations/testdata/tracing.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: tracing
spec:
tracing:
samplingRate: "1"
zipkin:
endpointAddress: "http://otel-collector:9411/api/v2/spans"
6 changes: 4 additions & 2 deletions pkg/instances/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -818,7 +818,7 @@ func (i *instances) getDockerComposeInstances(scope string) []Instance {
configDetails := types.ConfigDetails{
WorkingDir: workingDir,
ConfigFiles: configFiles,
Environment: nil,
Environment: map[string]string{},
}

// Can't get creation time and age of daprd services so approximate from dashboard process
Expand All @@ -837,7 +837,9 @@ func (i *instances) getDockerComposeInstances(scope string) []Instance {

createTime := time.Unix(createUnixTimeMilliseconds/1000, 0)

project, err := loader.Load(configDetails)
project, err := loader.Load(configDetails, func(options *loader.Options) {
options.SetProjectName("dashboard", true)
})

if err != nil {
log.Println(err)
Expand Down
61 changes: 60 additions & 1 deletion pkg/instances/instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ Copyright 2021 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Expand All @@ -13,9 +15,11 @@ limitations under the License.
package instances

import (
"fmt"
"testing"
"time"

"github.com/dapr/dashboard/pkg/platforms"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -98,3 +102,58 @@ func TestControlPlaneServices(t *testing.T) {
status := k8s.GetControlPlaneStatus()
assert.Equal(t, 12, len(status), "Expected status list length to match")
}

func TestSupported(t *testing.T) {
var scenarios = []struct {
platform platforms.Platform
want bool
}{
{platforms.Kubernetes, true},
{platforms.Standalone, true},
{platforms.DockerCompose, true},
}

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Platform %s should be supported", scenario.platform), func(t *testing.T) {
target := NewInstances(scenario.platform, nil, "")
isSupported := target.Supported()
assert.Equal(t, scenario.want, isSupported)
})
}
}

func TestDockerComposeGetInstances(t *testing.T) {
target := NewInstances(platforms.DockerCompose, nil, "testdata/docker-compose.yml")
instances := target.GetInstances("")
assert.Equal(t, 1, len(instances), "Should parse docker compose file and detect one instance")
}

func TestDockerComposeGetInstance(t *testing.T) {
var scenarios = []struct {
id string
want bool
}{
{"MyApplication.DaprSidecar", true},
{"does_not_exist", false},
}

target := NewInstances(platforms.DockerCompose, nil, "testdata/docker-compose.yml")

for _, scenario := range scenarios {
t.Run(fmt.Sprintf("Should load valid instance data - %t", scenario.want), func(t *testing.T) {
instance := target.GetInstance("", scenario.id)
assert.NotNil(t, instance, "Should always return something")

if scenario.want {
assert.Equal(t, scenario.id, instance.AppID, "Should return the correct instance")
assert.Equal(t, 3500, instance.HTTPPort, "Port should be set")
assert.Equal(t, false, instance.SupportsLogs, "Logs are not supported")
assert.Equal(t, false, instance.SupportsDeletion, "Delegation is not supported")
assert.Equal(t, "MyApplication.DaprSidecar:3500", instance.Address, "Address should be set")
assert.Equal(t, 80, instance.AppPort, "AppPort should be set")
} else {
assert.Empty(t, instance.AppID, "When instance not valid, AppID is not set")
}
})
}
}
53 changes: 53 additions & 0 deletions pkg/instances/testdata/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
version: '3.8'
services:

my-application-webhost:
build:
context: .
dockerfile: src/My.Application.WebHost/Dockerfile
ports:
- "5002:80"
networks:
- my-network

my-application-webhost-dapr:
image: "daprio/daprd:1.8.0"
command: [ "./daprd",
"-app-id", "MyApplication.DaprSidecar",
"-app-port", "80",
"-placement-host-address", "dapr-placement:50000",
"-components-path", "/components",
"-config", "/configuration/config.yaml" ]
volumes:
- "./dockercompose/dapr/components/:/components"
- "./dockercompose/dapr/config/:/configuration"
depends_on:
- my-application-webhost
- dapr-placement
network_mode: "service:my-application-webhost"

dapr-placement:
image: "daprio/dapr:1.8.0"
command: [ "./placement", "-port", "50000" ]
ports:
- "50000:50000"
networks:
- my-network

dapr-dashboard:
image: "daprio/dashboard:latest"
command: [ "--docker-compose=true",
"--components-path=/home/nonroot/components",
"--config-path=/home/nonroot/configuration",
"--docker-compose-path=/home/nonroot/docker-compose.yml" ]
ports:
- "8080:8080"
volumes:
- "./dockercompose/dapr/components/:/home/nonroot/components"
- "./dockercompose/dapr/config/:/home/nonroot/configuration"
- ./docker-compose.yml:/home/nonroot/docker-compose.yml
networks:
- my-network

networks:
my-network:
Loading

0 comments on commit 298a16a

Please sign in to comment.