-
-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(test): Add unit tests for golang/internal/mapper
- Loading branch information
Showing
1 changed file
with
348 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,348 @@ | ||
//go:build unit | ||
|
||
package mapper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/docker/docker/api/types/container" | ||
"github.com/dyrector-io/dyrectorio/golang/internal/config" | ||
"github.com/dyrector-io/dyrectorio/golang/internal/mapper" | ||
builder "github.com/dyrector-io/dyrectorio/golang/pkg/builder/container" | ||
"github.com/dyrector-io/dyrectorio/protobuf/go/agent" | ||
"github.com/dyrector-io/dyrectorio/protobuf/go/common" | ||
"github.com/stretchr/testify/assert" | ||
|
||
v1 "github.com/dyrector-io/dyrectorio/golang/api/v1" | ||
) | ||
|
||
func TestMapDeployImage(t *testing.T) { | ||
req := testDeployRequest() | ||
cfg := testAppConfig() | ||
|
||
res := mapper.MapDeployImage(req, cfg) | ||
expected := &v1.DeployImageRequest{ | ||
RequestID: "testID", | ||
RegistryAuth: &builder.RegistryAuth{ | ||
Name: "test-name", | ||
URL: "https://test-url.com", | ||
User: "test-user", | ||
Password: "test-pass", | ||
}, | ||
InstanceConfig: v1.InstanceConfig{ | ||
ContainerPreName: "test-prefix", | ||
MountPath: "/path/to/mount", | ||
Name: "", | ||
Environment: []string{"Env1", "Val1", "Env2", "Val2"}, | ||
Registry: "", | ||
RepositoryPreName: "repo-prefix", | ||
SharedEnvironment: []string(nil), | ||
UseSharedEnvs: false}, | ||
ContainerConfig: v1.ContainerConfig{ | ||
ContainerPreName: "test-prefix", | ||
Container: "test-container", | ||
Ports: []builder.PortBinding{{ExposedPort: 0x4d2, PortBinding: 0x1a85}}, | ||
PortRanges: []builder.PortRangeBinding{{Internal: builder.PortRange{From: 0x0, To: 0x18}, External: builder.PortRange{From: 0x40, To: 0x80}}}, | ||
Mounts: []string(nil), | ||
Volumes: []v1.Volume{{Name: "test-vol", Path: "/Path/to/volume", Size: "512GB", Type: "Persistent", Class: "test-storage-class"}}, | ||
Environment: []string{"ENV1", "VAL1", "ENV2", "VAL2"}, | ||
Secrets: map[string]string{"testKey-1": "testValue-1", "testKey-2": "testValue-2"}, | ||
RuntimeConfigType: "", | ||
Expose: true, | ||
ExposeTLS: true, | ||
IngressName: "test-ingress", | ||
IngressHost: "test-host", | ||
IngressUploadLimit: "5Mi", | ||
Shared: false, | ||
ConfigContainer: &v1.ConfigContainer{ | ||
Image: "test-image", | ||
Volume: "test-volume", | ||
Path: "/path/to/vol", | ||
KeepFiles: true, | ||
}, | ||
ImportContainer: nil, | ||
InitContainers: []v1.InitContainer{ | ||
{ | ||
Name: "test-init", | ||
Image: "test-image", | ||
Volumes: []v1.VolumeLink{{Name: "test-vol-link", Path: "/path/to/mount"}}, | ||
Command: []string{"mybin", "run"}, | ||
Args: []string{"--verbose"}, | ||
UseParent: true, | ||
Envs: map[string]string{"env1": "val1", "env2": "val2"}, | ||
}, | ||
}, | ||
User: req.GetContainerConfig().User, | ||
Command: []string{"make", "test"}, | ||
Args: []string{"--name", "test-arg"}, | ||
TTY: true, | ||
LogConfig: &container.LogConfig{ | ||
Type: "test-lg-driver", | ||
Config: map[string]string{"opt1": "v1", "opt2": "v2"}, | ||
}, | ||
RestartPolicy: "always", | ||
NetworkMode: "BRIDGE", | ||
DeploymentStrategy: "RECREATE", | ||
HealthCheckConfig: v1.HealthCheckConfig{Port: uint16(req.ContainerConfig.Crane.HealthCheckConfig.Port), | ||
LivenessProbe: &v1.Probe{Path: "test-liveness"}, | ||
ReadinessProbe: &v1.Probe{Path: "test-readiness"}, | ||
StartupProbe: &v1.Probe{Path: "test-startup"}, | ||
}, | ||
CustomHeaders: []string(nil), | ||
ResourceConfig: v1.ResourceConfig{ | ||
Limits: v1.Resources{CPU: "250m", Memory: "512Mi"}, | ||
Requests: v1.Resources{CPU: "100m", Memory: "64Mi"}, | ||
}, | ||
ProxyHeaders: true, Networks: []string{"n1", "n2"}, | ||
UseLoadBalancer: true, | ||
ExtraLBAnnotations: map[string]string{"annotation1": "value1"}, | ||
}, | ||
RuntimeConfig: v1.Base64JSONBytes{0x6b, 0x65, 0x79, 0x31, 0x3d, 0x76, 0x61, 0x6c, 0x31, 0x2c, 0x6b, 0x65, 0x79, 0x32, 0x3d, 0x76, 0x61, 0x6c, 0x32}, // encoded string: a2V5MT12YWwxLGtleTI9dmFsMg== | ||
Registry: req.Registry, | ||
ImageName: "test-image", | ||
Tag: "test-tag", | ||
Issuer: "", | ||
} | ||
|
||
assert.Equal(t, expected, res) | ||
} | ||
|
||
func TestMapPorts(t *testing.T) { | ||
ps := []*common.Port{ | ||
{ | ||
Internal: 1234, | ||
External: 5678, | ||
}, | ||
{ | ||
Internal: 9999, | ||
External: 1111, | ||
}, | ||
} | ||
|
||
expected := []builder.PortBinding{ | ||
{ | ||
ExposedPort: 1234, | ||
PortBinding: 5678, | ||
}, | ||
{ | ||
ExposedPort: 9999, | ||
PortBinding: 1111, | ||
}, | ||
} | ||
|
||
bindings := mapper.MapPorts(ps) | ||
assert.Equal(t, expected, bindings) | ||
} | ||
|
||
func TestMapSecrets(t *testing.T) { | ||
kvl := testKeyValueList() | ||
|
||
m := mapper.MapSecrets(kvl) | ||
expected := map[string]string{ | ||
"testKey-1": "testValue-1", | ||
"testKey-2": "testValue-2", | ||
} | ||
|
||
assert.Equal(t, expected, m) | ||
} | ||
|
||
func testDeployRequest() *agent.DeployRequest { | ||
registry := "https://my-registry.com" | ||
runtimeCfg := "key1=val1,key2=val2" | ||
var uid int64 = 777 | ||
upLimit := "5Mi" | ||
mntPath := "/path/to/mount" | ||
repoPrefix := "repo-prefix" | ||
b := true | ||
return &agent.DeployRequest{ | ||
Id: "testID", | ||
ContainerName: "test-container", | ||
ImageName: "test-image", | ||
Tag: "test-tag", | ||
Registry: ®istry, | ||
RuntimeConfig: &runtimeCfg, | ||
ContainerConfig: &common.ExplicitContainerConfig{ | ||
User: &uid, | ||
Command: []string{"make", "test"}, | ||
Args: []string{"--name", "test-arg"}, | ||
Environments: []string{"ENV1", "VAL1", "ENV2", "VAL2"}, | ||
Secrets: testKeyValueList(), | ||
TTY: &b, | ||
Ports: []*common.Port{ | ||
{ | ||
Internal: 1234, | ||
External: 6789, | ||
}, | ||
}, | ||
PortRanges: []*common.PortRangeBinding{ | ||
{ | ||
Internal: &common.PortRange{From: 0, To: 24}, | ||
External: &common.PortRange{From: 64, To: 128}, | ||
}, | ||
}, | ||
Volumes: []*common.Volume{testVolume()}, | ||
InitContainers: []*common.InitContainer{testInitContainer()}, | ||
Dagent: testDagentConfig(), | ||
Crane: testCraneConfig(), | ||
Expose: &common.Expose{ | ||
Public: true, | ||
Tls: true, | ||
}, | ||
Ingress: &common.Ingress{ | ||
Name: "test-ingress", | ||
Host: "test-host", | ||
UploadLimit: &upLimit, | ||
}, | ||
ConfigContainer: &common.ConfigContainer{ | ||
Image: "test-image", | ||
Volume: "test-volume", | ||
Path: "/path/to/vol", | ||
KeepFiles: true, | ||
}, | ||
ImportContainer: &common.ImportContainer{ | ||
Volume: "test-volume", | ||
Command: "rm -rf /", | ||
Environments: map[string]string{"env1": "val1"}, | ||
}, | ||
}, | ||
RegistryAuth: &agent.DeployRequest_RegistryAuth{ | ||
Name: "test-name", | ||
User: "test-user", | ||
Password: "test-pass", | ||
Url: "https://test-url.com", | ||
}, | ||
InstanceConfig: &agent.DeployRequest_InstanceConfig{ | ||
Prefix: "test-prefix", | ||
MountPath: &mntPath, | ||
RepositoryPrefix: &repoPrefix, | ||
Environment: &agent.DeployRequest_InstanceConfig_Environment{ | ||
Env: []string{"Env1", "Val1", "Env2", "Val2"}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func testVolume() *common.Volume { | ||
size := "512GB" | ||
voltype := "Persistent" | ||
class := "test-storage-class" | ||
return &common.Volume{ | ||
Name: "test-vol", | ||
Path: "/Path/to/volume", | ||
Size: &size, | ||
Type: &voltype, | ||
Class: &class, | ||
} | ||
} | ||
|
||
func testInitContainer() *common.InitContainer { | ||
b := true | ||
return &common.InitContainer{ | ||
Name: "test-init", | ||
Image: "test-image", | ||
Command: []string{"mybin", "run"}, | ||
Args: []string{"--verbose"}, | ||
Environments: map[string]string{"env1": "val1", "env2": "val2"}, | ||
UseParentConfig: &b, | ||
Volumes: []*common.VolumeLink{ | ||
{ | ||
Name: "test-vol-link", | ||
Path: "/path/to/mount", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func testKeyValueList() *common.KeyValueList { | ||
return &common.KeyValueList{ | ||
Data: []*common.UniqueKeyValue{ | ||
{ | ||
Id: "testID-1", | ||
Key: "testKey-1", | ||
Value: "testValue-1", | ||
}, | ||
{ | ||
Id: "testID-2", | ||
Key: "testKey-2", | ||
Value: "testValue-2", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func testDagentConfig() *common.DagentContainerConfig { | ||
return &common.DagentContainerConfig{ | ||
RestartPolicy: common.RestartPolicy_ALWAYS.Enum(), | ||
LogConfig: &common.LogConfig{ | ||
Driver: "test-lg-driver", | ||
Options: map[string]string{"opt1": "v1", "opt2": "v2"}, | ||
}, | ||
NetworkMode: common.NetworkMode_BRIDGE.Enum(), | ||
Networks: []string{"n1", "n2"}, | ||
} | ||
} | ||
|
||
func testCraneConfig() *common.CraneContainerConfig { | ||
b := true | ||
cpuReq := "100m" | ||
memReq := "64Mi" | ||
cpuLim := "250m" | ||
memLim := "512Mi" | ||
|
||
lProbe := "test-liveness" | ||
rProbe := "test-readiness" | ||
sProbe := "test-startup" | ||
return &common.CraneContainerConfig{ | ||
CustomHeaders: []string{"header1", "value1", "header2", "value2"}, | ||
ExtraLBAnnotations: map[string]string{"annotation1": "value1"}, | ||
ProxyHeaders: &b, | ||
UseLoadBalancer: &b, | ||
ResourceConfig: &common.ResourceConfig{ | ||
Requests: &common.Resource{ | ||
Cpu: &cpuReq, | ||
Memory: &memReq, | ||
}, | ||
Limits: &common.Resource{ | ||
Cpu: &cpuLim, | ||
Memory: &memLim, | ||
}, | ||
}, | ||
HealthCheckConfig: &common.HealthCheckConfig{ | ||
Port: 1234, | ||
LivenessProbe: &lProbe, | ||
ReadinessProbe: &rProbe, | ||
StartupProbe: &sProbe, | ||
}, | ||
DeploymentStatregy: common.DeploymentStrategy_RECREATE.Enum(), | ||
} | ||
} | ||
|
||
func testAppConfig() *config.CommonConfiguration { | ||
return &config.CommonConfiguration{ | ||
DefaultLimitsCPU: "", | ||
DefaultLimitsMemory: "", | ||
DefaultRequestsCPU: "", | ||
DefaultRequestMemory: "", | ||
DefaultVolumeSize: "", | ||
DefaultTag: "", | ||
DefaultTimeout: 30 * time.Second, | ||
GrpcKeepalive: 30 * time.Second, | ||
Debug: false, | ||
GrpcInsecure: true, | ||
ImportContainerImage: "", | ||
IngressRootDomain: "", | ||
NodeID: "", | ||
ReadHeaderTimeout: 30 * time.Second, | ||
Registry: "", | ||
RegistryPassword: "", | ||
RegistryUsername: "", | ||
SecretPrivateKey: "", | ||
GrpcToken: &config.ValidJWT{ | ||
Issuer: "test-issuer", | ||
Subject: "test-subject", | ||
StringifiedToken: "test-token", | ||
IssuedAt: time.Now(), | ||
}, | ||
} | ||
} |