forked from gambol99/go-marathon
-
Notifications
You must be signed in to change notification settings - Fork 0
/
application_marshalling_test.go
100 lines (84 loc) · 3.01 KB
/
application_marshalling_test.go
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2017 The go-marathon Authors All rights reserved.
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
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 marathon
import (
"encoding/json"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestEnvironmentVariableUnmarshal(t *testing.T) {
defaultConfig := NewDefaultConfig()
configs := &configContainer{
client: &defaultConfig,
server: &serverConfig{
scope: "environment-variables",
},
}
endpoint := newFakeMarathonEndpoint(t, configs)
defer endpoint.Close()
application, err := endpoint.Client.Application(fakeAppName)
require.NoError(t, err)
env := application.Env
secrets := application.Secrets
require.NotNil(t, env)
assert.Equal(t, "bar", (*env)["FOO"])
assert.Equal(t, "TOP", (*secrets)["secret"].EnvVar)
assert.Equal(t, "/path/to/secret", (*secrets)["secret"].Source)
}
func TestMalformedPayloadUnmarshal(t *testing.T) {
var tests = []struct {
expected string
given []byte
description string
}{
{
expected: "unexpected secret field",
given: []byte(`{"env": {"FOO": "bar", "SECRET": {"not_secret": "secret1"}}, "secrets": {"secret1": {"source": "/path/to/secret"}}}`),
description: "Field in environment secret not equal to secret.",
},
{
expected: "unexpected secret field",
given: []byte(`{"env": {"FOO": "bar", "SECRET": {"secret": 1}}, "secrets": {"secret1": {"source": "/path/to/secret"}}}`),
description: "Invalid value in environment secret.",
},
{
expected: "unexpected environment variable type",
given: []byte(`{"env": {"FOO": 1, "SECRET": {"secret": "secret1"}}, "secrets": {"secret1": {"source": "/path/to/secret"}}}`),
description: "Invalid environment variable type.",
},
{
expected: "malformed application definition",
given: []byte(`{"env": "value"}`),
description: "Bad application definition.",
},
}
for _, test := range tests {
tmpApp := new(Application)
err := json.Unmarshal(test.given, &tmpApp)
if assert.Error(t, err, test.description) {
assert.True(t, strings.HasPrefix(err.Error(), test.expected), test.description)
}
}
}
func TestEnvironmentVariableMarshal(t *testing.T) {
testApp := new(Application)
targetString := []byte(`{"ports":null,"dependencies":null,"env":{"FOO":"bar","TOP":{"secret":"secret1"}},"secrets":{"secret1":{"source":"/path/to/secret"}}}`)
testApp.AddEnv("FOO", "bar")
testApp.AddSecret("TOP", "secret1", "/path/to/secret")
app, err := json.Marshal(testApp)
if assert.NoError(t, err) {
assert.Equal(t, targetString, app)
}
}